blob: a39d3c6c422b631d4ef60fdbae29b80e7fc6b6ba [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:369 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "pc/peer_connection.h"
henrike@webrtc.org28e20752013-07-10 00:45:3612
deadbeefeb459812015-12-16 03:24:4313#include <algorithm>
Harald Alvestrand8ebba742018-05-31 12:00:3414#include <limits>
Mirko Bonadei317a1f02019-09-17 15:06:1815#include <memory>
Steve Antondcc3c022017-12-23 00:02:5416#include <queue>
Steve Anton75737c02017-11-06 18:37:1717#include <set>
kwiberg0eb15ed2015-12-17 11:04:1518#include <utility>
19#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:3620
Steve Anton64b626b2019-01-29 01:25:2621#include "absl/algorithm/container.h"
Fredrik Solenberg41f3a432018-12-17 20:02:2222#include "absl/strings/match.h"
Steve Anton10542f22019-01-11 17:11:0023#include "api/jsep_ice_candidate.h"
24#include "api/jsep_session_description.h"
25#include "api/media_stream_proxy.h"
26#include "api/media_stream_track_proxy.h"
Amit Hilbuchf4770402019-04-08 21:11:5727#include "api/rtc_error.h"
Danil Chapovalov83bbe912019-08-07 10:24:5328#include "api/rtc_event_log/rtc_event_log.h"
Niels Möllerd8b9ed72019-05-08 11:53:5129#include "api/rtc_event_log_output_file.h"
Amit Hilbuchf4770402019-04-08 21:11:5730#include "api/rtp_parameters.h"
Steve Anton10542f22019-01-11 17:11:0031#include "api/uma_metrics.h"
Jonas Orelanda3aa9bd2019-04-17 05:38:4032#include "api/video/builtin_video_bitrate_allocator_factory.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3133#include "call/call.h"
Steve Anton10542f22019-01-11 17:11:0034#include "logging/rtc_event_log/ice_logger.h"
Amit Hilbuchf4770402019-04-08 21:11:5735#include "media/base/rid_description.h"
Steve Anton10542f22019-01-11 17:11:0036#include "media/sctp/sctp_transport.h"
Ruslan Burakov501bfba2019-02-11 09:29:1937#include "pc/audio_rtp_receiver.h"
Steve Anton10542f22019-01-11 17:11:0038#include "pc/audio_track.h"
Steve Anton75737c02017-11-06 18:37:1739#include "pc/channel.h"
Steve Anton10542f22019-01-11 17:11:0040#include "pc/channel_manager.h"
41#include "pc/dtmf_sender.h"
42#include "pc/media_stream.h"
43#include "pc/media_stream_observer.h"
44#include "pc/remote_audio_source.h"
45#include "pc/rtp_media_utils.h"
46#include "pc/rtp_receiver.h"
47#include "pc/rtp_sender.h"
Harald Alvestrandc85328f2019-02-28 06:51:0048#include "pc/sctp_transport.h"
Steve Anton10542f22019-01-11 17:11:0049#include "pc/sctp_utils.h"
50#include "pc/sdp_utils.h"
51#include "pc/stream_collection.h"
Ruslan Burakov501bfba2019-02-11 09:29:1952#include "pc/video_rtp_receiver.h"
Steve Anton10542f22019-01-11 17:11:0053#include "pc/video_track.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3154#include "rtc_base/bind.h"
55#include "rtc_base/checks.h"
56#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0057#include "rtc_base/string_encode.h"
Jonas Olsson366a50c2018-09-06 11:41:3058#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3159#include "rtc_base/trace_event.h"
60#include "system_wrappers/include/clock.h"
61#include "system_wrappers/include/field_trial.h"
Qingsi Wang7fc821d2018-07-12 19:54:5362#include "system_wrappers/include/metrics.h"
henrike@webrtc.org28e20752013-07-10 00:45:3663
Steve Anton75737c02017-11-06 18:37:1764using cricket::ContentInfo;
65using cricket::ContentInfos;
66using cricket::MediaContentDescription;
Steve Anton5adfafd2017-12-21 00:34:0067using cricket::MediaProtocolType;
Amit Hilbuchbcd39d42019-01-26 01:13:5668using cricket::RidDescription;
69using cricket::RidDirection;
Amit Hilbuchc63ddb22019-01-02 18:13:5870using cricket::SessionDescription;
Amit Hilbuchbcd39d42019-01-26 01:13:5671using cricket::SimulcastDescription;
72using cricket::SimulcastLayer;
Amit Hilbuchc63ddb22019-01-02 18:13:5873using cricket::SimulcastLayerList;
Amit Hilbuchbcd39d42019-01-26 01:13:5674using cricket::StreamParams;
Steve Anton75737c02017-11-06 18:37:1775using cricket::TransportInfo;
76
77using cricket::LOCAL_PORT_TYPE;
Steve Anton75737c02017-11-06 18:37:1778using cricket::PRFLX_PORT_TYPE;
Jeroen de Borstaf242c82019-04-24 20:13:4879using cricket::RELAY_PORT_TYPE;
80using cricket::STUN_PORT_TYPE;
Steve Anton75737c02017-11-06 18:37:1781
Steve Antonba818672017-11-06 18:21:5782namespace webrtc {
83
Steve Anton75737c02017-11-06 18:37:1784// Error messages
85const char kBundleWithoutRtcpMux[] =
86 "rtcp-mux must be enabled when BUNDLE "
87 "is enabled.";
Steve Anton75737c02017-11-06 18:37:1788const char kInvalidCandidates[] = "Description contains invalid candidates.";
89const char kInvalidSdp[] = "Invalid session description.";
90const char kMlineMismatchInAnswer[] =
91 "The order of m-lines in answer doesn't match order in offer. Rejecting "
92 "answer.";
93const char kMlineMismatchInSubsequentOffer[] =
94 "The order of m-lines in subsequent offer doesn't match order from "
95 "previous offer/answer.";
Steve Anton75737c02017-11-06 18:37:1796const char kSdpWithoutDtlsFingerprint[] =
97 "Called with SDP without DTLS fingerprint.";
98const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto.";
99const char kSdpWithoutIceUfragPwd[] =
100 "Called with SDP without ice-ufrag and ice-pwd.";
101const char kSessionError[] = "Session error code: ";
102const char kSessionErrorDesc[] = "Session error description: ";
103const char kDtlsSrtpSetupFailureRtp[] =
104 "Couldn't set up DTLS-SRTP on RTP channel.";
105const char kDtlsSrtpSetupFailureRtcp[] =
106 "Couldn't set up DTLS-SRTP on RTCP channel.";
henrike@webrtc.org28e20752013-07-10 00:45:36107
Steve Anton75737c02017-11-06 18:37:17108namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36109
Amit Hilbuche2a284d2019-03-05 20:36:31110// UMA metric names.
111const char kSimulcastVersionApplyLocalDescription[] =
112 "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription";
113const char kSimulcastVersionApplyRemoteDescription[] =
114 "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription";
115const char kSimulcastNumberOfEncodings[] =
116 "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings";
117const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled";
118
Seth Hampson845e8782018-03-02 19:34:10119static const char kDefaultStreamId[] = "default";
Steve Anton4171afb2017-11-20 18:20:22120static const char kDefaultAudioSenderId[] = "defaulta0";
121static const char kDefaultVideoSenderId[] = "defaultv0";
deadbeefab9b2d12015-10-14 18:33:11122
zhihuang8f65cdf2016-05-07 01:40:30123// The length of RTCP CNAMEs.
124static const int kRtcpCnameLength = 16;
125
Steve Antonad182762018-09-05 20:22:40126enum {
127 MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0,
128 MSG_SET_SESSIONDESCRIPTION_FAILED,
129 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
130 MSG_GETSTATS,
Steve Antonad182762018-09-05 20:22:40131 MSG_REPORT_USAGE_PATTERN,
132};
133
Harald Alvestrand19793842018-06-25 10:03:50134static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000;
135
Steve Antonad182762018-09-05 20:22:40136struct SetSessionDescriptionMsg : public rtc::MessageData {
137 explicit SetSessionDescriptionMsg(
138 webrtc::SetSessionDescriptionObserver* observer)
139 : observer(observer) {}
140
141 rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer;
142 RTCError error;
143};
144
145struct CreateSessionDescriptionMsg : public rtc::MessageData {
146 explicit CreateSessionDescriptionMsg(
147 webrtc::CreateSessionDescriptionObserver* observer)
148 : observer(observer) {}
149
150 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
151 RTCError error;
152};
153
154struct GetStatsMsg : public rtc::MessageData {
155 GetStatsMsg(webrtc::StatsObserver* observer,
156 webrtc::MediaStreamTrackInterface* track)
157 : observer(observer), track(track) {}
158 rtc::scoped_refptr<webrtc::StatsObserver> observer;
159 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track;
160};
161
deadbeefab9b2d12015-10-14 18:33:11162// Check if we can send |new_stream| on a PeerConnection.
163bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams,
164 webrtc::MediaStreamInterface* new_stream) {
165 if (!new_stream || !current_streams) {
166 return false;
167 }
Seth Hampson13b8bad2018-03-13 23:05:28168 if (current_streams->find(new_stream->id()) != nullptr) {
169 RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id()
Mirko Bonadei675513b2017-11-09 10:09:25170 << " is already added.";
deadbeefab9b2d12015-10-14 18:33:11171 return false;
172 }
173 return true;
174}
175
deadbeef5e97fb52015-10-15 19:49:08176// If the direction is "recvonly" or "inactive", treat the description
177// as containing no streams.
178// See: https://code.google.com/p/webrtc/issues/detail?id=5054
179std::vector<cricket::StreamParams> GetActiveStreams(
180 const cricket::MediaContentDescription* desc) {
Steve Anton4e70a722017-11-28 22:57:10181 return RtpTransceiverDirectionHasSend(desc->direction())
deadbeef5e97fb52015-10-15 19:49:08182 ? desc->streams()
183 : std::vector<cricket::StreamParams>();
184}
185
deadbeefab9b2d12015-10-14 18:33:11186bool IsValidOfferToReceiveMedia(int value) {
187 typedef PeerConnectionInterface::RTCOfferAnswerOptions Options;
188 return (value >= Options::kUndefined) &&
189 (value <= Options::kMaxOfferToReceiveMedia);
190}
191
zhihuang1c378ed2017-08-17 21:10:50192// Add options to |[audio/video]_media_description_options| from |senders|.
Amit Hilbuchc63ddb22019-01-02 18:13:58193void AddPlanBRtpSenderOptions(
deadbeefa601f5c2016-06-06 21:27:39194 const std::vector<rtc::scoped_refptr<
195 RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders,
zhihuang1c378ed2017-08-17 21:10:50196 cricket::MediaDescriptionOptions* audio_media_description_options,
Jonas Orelandfc1acd22018-08-24 08:58:37197 cricket::MediaDescriptionOptions* video_media_description_options,
198 int num_sim_layers) {
olka3c747662017-08-17 13:50:32199 for (const auto& sender : senders) {
zhihuang1c378ed2017-08-17 21:10:50200 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
201 if (audio_media_description_options) {
202 audio_media_description_options->AddAudioSender(
Steve Anton8ffb9c32017-08-31 22:45:38203 sender->id(), sender->internal()->stream_ids());
zhihuang1c378ed2017-08-17 21:10:50204 }
205 } else {
206 RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO);
207 if (video_media_description_options) {
208 video_media_description_options->AddVideoSender(
Amit Hilbuchc63ddb22019-01-02 18:13:58209 sender->id(), sender->internal()->stream_ids(), {},
210 SimulcastLayerList(), num_sim_layers);
zhihuang1c378ed2017-08-17 21:10:50211 }
212 }
zhihuanga77e6bb2017-08-15 01:17:48213 }
zhihuang1c378ed2017-08-17 21:10:50214}
olka3c747662017-08-17 13:50:32215
zhihuang1c378ed2017-08-17 21:10:50216// Add options to |session_options| from |rtp_data_channels|.
217void AddRtpDataChannelOptions(
Taylor Brandstetter3a034e12020-07-09 22:32:34218 const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>&
zhihuang1c378ed2017-08-17 21:10:50219 rtp_data_channels,
220 cricket::MediaDescriptionOptions* data_media_description_options) {
221 if (!data_media_description_options) {
222 return;
223 }
deadbeefab9b2d12015-10-14 18:33:11224 // Check for data channels.
225 for (const auto& kv : rtp_data_channels) {
Taylor Brandstetter3a034e12020-07-09 22:32:34226 const RtpDataChannel* channel = kv.second;
227 if (channel->state() == RtpDataChannel::kConnecting ||
228 channel->state() == RtpDataChannel::kOpen) {
zhihuang1c378ed2017-08-17 21:10:50229 // Legacy RTP data channels are signaled with the track/stream ID set to
230 // the data channel's label.
231 data_media_description_options->AddRtpDataChannel(channel->label(),
232 channel->label());
deadbeefab9b2d12015-10-14 18:33:11233 }
234 }
235}
236
Taylor Brandstettera1c30352016-05-13 15:15:11237uint32_t ConvertIceTransportTypeToCandidateFilter(
238 PeerConnectionInterface::IceTransportsType type) {
239 switch (type) {
240 case PeerConnectionInterface::kNone:
241 return cricket::CF_NONE;
242 case PeerConnectionInterface::kRelay:
243 return cricket::CF_RELAY;
244 case PeerConnectionInterface::kNoHost:
245 return (cricket::CF_ALL & ~cricket::CF_HOST);
246 case PeerConnectionInterface::kAll:
247 return cricket::CF_ALL;
248 default:
nissec80e7412017-01-11 13:56:46249 RTC_NOTREACHED();
Taylor Brandstettera1c30352016-05-13 15:15:11250 }
251 return cricket::CF_NONE;
252}
253
Philipp Hanckec2cfd182020-06-30 11:56:40254// Map internal signaling state name to spec name:
255// https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum
Steve Antonba818672017-11-06 18:21:57256std::string GetSignalingStateString(
257 PeerConnectionInterface::SignalingState state) {
258 switch (state) {
259 case PeerConnectionInterface::kStable:
Philipp Hanckec2cfd182020-06-30 11:56:40260 return "stable";
Steve Antonba818672017-11-06 18:21:57261 case PeerConnectionInterface::kHaveLocalOffer:
Philipp Hanckec2cfd182020-06-30 11:56:40262 return "have-local-offer";
Steve Antonba818672017-11-06 18:21:57263 case PeerConnectionInterface::kHaveLocalPrAnswer:
Philipp Hanckec2cfd182020-06-30 11:56:40264 return "have-local-pranswer";
Steve Antonba818672017-11-06 18:21:57265 case PeerConnectionInterface::kHaveRemoteOffer:
Philipp Hanckec2cfd182020-06-30 11:56:40266 return "have-remote-offer";
Steve Antonba818672017-11-06 18:21:57267 case PeerConnectionInterface::kHaveRemotePrAnswer:
Philipp Hanckec2cfd182020-06-30 11:56:40268 return "have-remote-pranswer";
Steve Antonba818672017-11-06 18:21:57269 case PeerConnectionInterface::kClosed:
Philipp Hanckec2cfd182020-06-30 11:56:40270 return "closed";
Steve Antonba818672017-11-06 18:21:57271 }
272 RTC_NOTREACHED();
273 return "";
274}
deadbeef0a6c4ca2015-10-06 18:38:28275
Steve Anton75737c02017-11-06 18:37:17276IceCandidatePairType GetIceCandidatePairCounter(
277 const cricket::Candidate& local,
278 const cricket::Candidate& remote) {
279 const auto& l = local.type();
280 const auto& r = remote.type();
281 const auto& host = LOCAL_PORT_TYPE;
282 const auto& srflx = STUN_PORT_TYPE;
283 const auto& relay = RELAY_PORT_TYPE;
284 const auto& prflx = PRFLX_PORT_TYPE;
285 if (l == host && r == host) {
Jeroen de Borst833979f2018-12-13 16:25:54286 bool local_hostname =
287 !local.address().hostname().empty() && local.address().IsUnresolvedIP();
288 bool remote_hostname = !remote.address().hostname().empty() &&
289 remote.address().IsUnresolvedIP();
Steve Anton75737c02017-11-06 18:37:17290 bool local_private = IPIsPrivate(local.address().ipaddr());
291 bool remote_private = IPIsPrivate(remote.address().ipaddr());
Jeroen de Borst833979f2018-12-13 16:25:54292 if (local_hostname) {
293 if (remote_hostname) {
294 return kIceCandidatePairHostNameHostName;
295 } else if (remote_private) {
296 return kIceCandidatePairHostNameHostPrivate;
297 } else {
298 return kIceCandidatePairHostNameHostPublic;
299 }
300 } else if (local_private) {
301 if (remote_hostname) {
302 return kIceCandidatePairHostPrivateHostName;
303 } else if (remote_private) {
Steve Anton75737c02017-11-06 18:37:17304 return kIceCandidatePairHostPrivateHostPrivate;
305 } else {
306 return kIceCandidatePairHostPrivateHostPublic;
307 }
308 } else {
Jeroen de Borst833979f2018-12-13 16:25:54309 if (remote_hostname) {
310 return kIceCandidatePairHostPublicHostName;
311 } else if (remote_private) {
Steve Anton75737c02017-11-06 18:37:17312 return kIceCandidatePairHostPublicHostPrivate;
313 } else {
314 return kIceCandidatePairHostPublicHostPublic;
315 }
316 }
317 }
318 if (l == host && r == srflx)
319 return kIceCandidatePairHostSrflx;
320 if (l == host && r == relay)
321 return kIceCandidatePairHostRelay;
322 if (l == host && r == prflx)
323 return kIceCandidatePairHostPrflx;
324 if (l == srflx && r == host)
325 return kIceCandidatePairSrflxHost;
326 if (l == srflx && r == srflx)
327 return kIceCandidatePairSrflxSrflx;
328 if (l == srflx && r == relay)
329 return kIceCandidatePairSrflxRelay;
330 if (l == srflx && r == prflx)
331 return kIceCandidatePairSrflxPrflx;
332 if (l == relay && r == host)
333 return kIceCandidatePairRelayHost;
334 if (l == relay && r == srflx)
335 return kIceCandidatePairRelaySrflx;
336 if (l == relay && r == relay)
337 return kIceCandidatePairRelayRelay;
338 if (l == relay && r == prflx)
339 return kIceCandidatePairRelayPrflx;
340 if (l == prflx && r == host)
341 return kIceCandidatePairPrflxHost;
342 if (l == prflx && r == srflx)
343 return kIceCandidatePairPrflxSrflx;
344 if (l == prflx && r == relay)
345 return kIceCandidatePairPrflxRelay;
346 return kIceCandidatePairMax;
347}
348
Seth Hampsonae8a90a2018-02-13 23:33:48349// Logic to decide if an m= section can be recycled. This means that the new
350// m= section is not rejected, but the old local or remote m= section is
351// rejected. |old_content_one| and |old_content_two| refer to the m= section
352// of the old remote and old local descriptions in no particular order.
353// We need to check both the old local and remote because either
354// could be the most current from the latest negotation.
355bool IsMediaSectionBeingRecycled(SdpType type,
356 const ContentInfo& content,
357 const ContentInfo* old_content_one,
358 const ContentInfo* old_content_two) {
359 return type == SdpType::kOffer && !content.rejected &&
360 ((old_content_one && old_content_one->rejected) ||
361 (old_content_two && old_content_two->rejected));
362}
363
Steve Anton75737c02017-11-06 18:37:17364// Verify that the order of media sections in |new_desc| matches
Seth Hampsonae8a90a2018-02-13 23:33:48365// |current_desc|. The number of m= sections in |new_desc| should be no
366// less than |current_desc|. In the case of checking an answer's
367// |new_desc|, the |current_desc| is the last offer that was set as the
368// local or remote. In the case of checking an offer's |new_desc| we
369// check against the local and remote descriptions stored from the last
370// negotiation, because either of these could be the most up to date for
371// possible rejected m sections. These are the |current_desc| and
372// |secondary_current_desc|.
373bool MediaSectionsInSameOrder(const SessionDescription& current_desc,
374 const SessionDescription* secondary_current_desc,
375 const SessionDescription& new_desc,
376 const SdpType type) {
377 if (current_desc.contents().size() > new_desc.contents().size()) {
Steve Anton75737c02017-11-06 18:37:17378 return false;
379 }
380
Seth Hampsonae8a90a2018-02-13 23:33:48381 for (size_t i = 0; i < current_desc.contents().size(); ++i) {
382 const cricket::ContentInfo* secondary_content_info = nullptr;
383 if (secondary_current_desc &&
384 i < secondary_current_desc->contents().size()) {
385 secondary_content_info = &secondary_current_desc->contents()[i];
386 }
387 if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i],
388 &current_desc.contents()[i],
389 secondary_content_info)) {
390 // For new offer descriptions, if the media section can be recycled, it's
391 // valid for the MID and media type to change.
Steve Antondcc3c022017-12-23 00:02:54392 continue;
393 }
Seth Hampsonae8a90a2018-02-13 23:33:48394 if (new_desc.contents()[i].name != current_desc.contents()[i].name) {
Steve Anton75737c02017-11-06 18:37:17395 return false;
396 }
397 const MediaContentDescription* new_desc_mdesc =
Seth Hampsonae8a90a2018-02-13 23:33:48398 new_desc.contents()[i].media_description();
399 const MediaContentDescription* current_desc_mdesc =
400 current_desc.contents()[i].media_description();
401 if (new_desc_mdesc->type() != current_desc_mdesc->type()) {
Steve Anton75737c02017-11-06 18:37:17402 return false;
403 }
404 }
405 return true;
406}
407
Seth Hampsonae8a90a2018-02-13 23:33:48408bool MediaSectionsHaveSameCount(const SessionDescription& desc1,
409 const SessionDescription& desc2) {
410 return desc1.contents().size() == desc2.contents().size();
Steve Anton75737c02017-11-06 18:37:17411}
412
Qingsi Wang7fc821d2018-07-12 19:54:53413void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type,
414 cricket::MediaType media_type) {
Mirko Bonadeiab499822018-09-11 08:43:20415 // Array of structs needed to map {KeyExchangeProtocolType,
416 // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in
417 // order to avoid -Wglobal-constructors and -Wexit-time-destructors.
418 static constexpr struct {
419 KeyExchangeProtocolType protocol_type;
420 cricket::MediaType media_type;
421 KeyExchangeProtocolMedia protocol_media;
422 } kEnumCounterKeyProtocolMediaMap[] = {
423 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO,
424 kEnumCounterKeyProtocolMediaTypeDtlsAudio},
425 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO,
426 kEnumCounterKeyProtocolMediaTypeDtlsVideo},
427 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA,
428 kEnumCounterKeyProtocolMediaTypeDtlsData},
429 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO,
430 kEnumCounterKeyProtocolMediaTypeSdesAudio},
431 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO,
432 kEnumCounterKeyProtocolMediaTypeSdesVideo},
433 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA,
434 kEnumCounterKeyProtocolMediaTypeSdesData},
435 };
436
Qingsi Wang7fc821d2018-07-12 19:54:53437 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type,
438 kEnumCounterKeyProtocolMax);
Harald Alvestrandf9d0f1d2018-03-02 13:15:26439
Mirko Bonadeiab499822018-09-11 08:43:20440 for (const auto& i : kEnumCounterKeyProtocolMediaMap) {
441 if (i.protocol_type == protocol_type && i.media_type == media_type) {
442 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia",
443 i.protocol_media,
444 kEnumCounterKeyProtocolMediaTypeMax);
445 }
Harald Alvestrandf9d0f1d2018-03-02 13:15:26446 }
447}
448
Harald Alvestrand76829d72018-07-18 21:24:36449void NoteAddIceCandidateResult(int result) {
450 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result,
451 kAddIceCandidateMax);
452}
453
Steve Anton75737c02017-11-06 18:37:17454// Checks that each non-rejected content has SDES crypto keys or a DTLS
455// fingerprint, unless it's in a BUNDLE group, in which case only the
456// BUNDLE-tag section (first media section/description in the BUNDLE group)
457// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
458// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
459// by Channel's |srtp_required| check.
Qingsi Wang7fc821d2018-07-12 19:54:53460RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) {
Steve Anton75737c02017-11-06 18:37:17461 const cricket::ContentGroup* bundle =
462 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
Steve Anton8a006912017-12-04 23:25:56463 for (const cricket::ContentInfo& content_info : desc->contents()) {
464 if (content_info.rejected) {
Steve Anton75737c02017-11-06 18:37:17465 continue;
466 }
Harald Alvestrand2e180612018-03-07 09:56:14467 // Note what media is used with each crypto protocol, for all sections.
468 NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls
469 : webrtc::kEnumCounterKeyProtocolSdes,
Qingsi Wang7fc821d2018-07-12 19:54:53470 content_info.media_description()->type());
Steve Anton8a006912017-12-04 23:25:56471 const std::string& mid = content_info.name;
472 if (bundle && bundle->HasContentName(mid) &&
473 mid != *(bundle->FirstContentName())) {
Steve Anton75737c02017-11-06 18:37:17474 // This isn't the first media section in the BUNDLE group, so it's not
475 // required to have crypto attributes, since only the crypto attributes
476 // from the first section actually get used.
477 continue;
478 }
479
480 // If the content isn't rejected or bundled into another m= section, crypto
481 // must be present.
Steve Antonb1c1de12017-12-21 23:14:30482 const MediaContentDescription* media = content_info.media_description();
Steve Anton8a006912017-12-04 23:25:56483 const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
Steve Anton75737c02017-11-06 18:37:17484 if (!media || !tinfo) {
485 // Something is not right.
Steve Anton8a006912017-12-04 23:25:56486 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
Steve Anton75737c02017-11-06 18:37:17487 }
488 if (dtls_enabled) {
489 if (!tinfo->description.identity_fingerprint) {
Mirko Bonadei675513b2017-11-09 10:09:25490 RTC_LOG(LS_WARNING)
491 << "Session description must have DTLS fingerprint if "
492 "DTLS enabled.";
Steve Anton8a006912017-12-04 23:25:56493 return RTCError(RTCErrorType::INVALID_PARAMETER,
494 kSdpWithoutDtlsFingerprint);
Steve Anton75737c02017-11-06 18:37:17495 }
496 } else {
497 if (media->cryptos().empty()) {
Mirko Bonadei675513b2017-11-09 10:09:25498 RTC_LOG(LS_WARNING)
Steve Anton75737c02017-11-06 18:37:17499 << "Session description must have SDES when DTLS disabled.";
Steve Anton8a006912017-12-04 23:25:56500 return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto);
Steve Anton75737c02017-11-06 18:37:17501 }
502 }
503 }
Steve Anton8a006912017-12-04 23:25:56504 return RTCError::OK();
Steve Anton75737c02017-11-06 18:37:17505}
506
507// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
508// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
509// media section/description in the BUNDLE group) needs a ufrag and pwd.
510bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
511 const cricket::ContentGroup* bundle =
512 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
Steve Anton8a006912017-12-04 23:25:56513 for (const cricket::ContentInfo& content_info : desc->contents()) {
514 if (content_info.rejected) {
Steve Anton75737c02017-11-06 18:37:17515 continue;
516 }
Steve Anton8a006912017-12-04 23:25:56517 const std::string& mid = content_info.name;
518 if (bundle && bundle->HasContentName(mid) &&
519 mid != *(bundle->FirstContentName())) {
Steve Anton75737c02017-11-06 18:37:17520 // This isn't the first media section in the BUNDLE group, so it's not
521 // required to have ufrag/password, since only the ufrag/password from
522 // the first section actually get used.
523 continue;
524 }
525
526 // If the content isn't rejected or bundled into another m= section,
527 // ice-ufrag and ice-pwd must be present.
Steve Anton8a006912017-12-04 23:25:56528 const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
Steve Anton75737c02017-11-06 18:37:17529 if (!tinfo) {
530 // Something is not right.
Mirko Bonadei675513b2017-11-09 10:09:25531 RTC_LOG(LS_ERROR) << kInvalidSdp;
Steve Anton75737c02017-11-06 18:37:17532 return false;
533 }
534 if (tinfo->description.ice_ufrag.empty() ||
535 tinfo->description.ice_pwd.empty()) {
Mirko Bonadei675513b2017-11-09 10:09:25536 RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
Steve Anton75737c02017-11-06 18:37:17537 return false;
538 }
539 }
540 return true;
541}
542
Steve Anton75737c02017-11-06 18:37:17543// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
544bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
545 const SessionDescriptionInterface* new_desc,
546 const std::string& content_name) {
547 if (!old_desc) {
548 return false;
549 }
550 const SessionDescription* new_sd = new_desc->description();
551 const SessionDescription* old_sd = old_desc->description();
552 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
553 if (!cinfo || cinfo->rejected) {
554 return false;
555 }
556 // If the content isn't rejected, check if ufrag and password has changed.
557 const cricket::TransportDescription* new_transport_desc =
558 new_sd->GetTransportDescriptionByName(content_name);
559 const cricket::TransportDescription* old_transport_desc =
560 old_sd->GetTransportDescriptionByName(content_name);
561 if (!new_transport_desc || !old_transport_desc) {
562 // No transport description exists. This is not an ICE restart.
563 return false;
564 }
565 if (cricket::IceCredentialsChanged(
566 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
567 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
Mirko Bonadei675513b2017-11-09 10:09:25568 RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
569 << ".";
Steve Anton75737c02017-11-06 18:37:17570 return true;
571 }
572 return false;
573}
574
Steve Anton80dd7b52018-02-17 01:08:42575// Generates a string error message for SetLocalDescription/SetRemoteDescription
576// from an RTCError.
577std::string GetSetDescriptionErrorMessage(cricket::ContentSource source,
578 SdpType type,
579 const RTCError& error) {
Jonas Olsson366a50c2018-09-06 11:41:30580 rtc::StringBuilder oss;
Steve Anton80dd7b52018-02-17 01:08:42581 oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote")
582 << " " << SdpTypeToString(type) << " sdp: " << error.message();
Jonas Olsson84df1c72018-09-14 14:59:32583 return oss.Release();
Steve Anton80dd7b52018-02-17 01:08:42584}
585
Seth Hampson5b4f0752018-04-02 23:31:36586std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) {
587 std::string output = "streams=[";
588 const char* separator = "";
589 for (const auto& stream_id : stream_ids) {
590 output.append(separator).append(stream_id);
591 separator = ", ";
592 }
593 output.append("]");
594 return output;
595}
596
Danil Chapovalov66cadcc2018-06-19 14:47:43597absl::optional<int> RTCConfigurationToIceConfigOptionalInt(
Qingsi Wang866e08d2018-03-23 00:54:23598 int rtc_configuration_parameter) {
599 if (rtc_configuration_parameter ==
600 webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) {
Danil Chapovalov66cadcc2018-06-19 14:47:43601 return absl::nullopt;
Qingsi Wang866e08d2018-03-23 00:54:23602 }
603 return rtc_configuration_parameter;
604}
605
Amit Hilbuche2a284d2019-03-05 20:36:31606void ReportSimulcastApiVersion(const char* name,
607 const SessionDescription& session) {
608 bool has_legacy = false;
609 bool has_spec_compliant = false;
610 for (const ContentInfo& content : session.contents()) {
Harald Alvestrand1716d392019-06-03 18:35:45611 if (!content.media_description()) {
Amit Hilbuche2a284d2019-03-05 20:36:31612 continue;
613 }
Harald Alvestrand1716d392019-06-03 18:35:45614 has_spec_compliant |= content.media_description()->HasSimulcast();
615 for (const StreamParams& sp : content.media_description()->streams()) {
Amit Hilbuche2a284d2019-03-05 20:36:31616 has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics);
617 }
618 }
619
620 if (has_legacy) {
621 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy,
622 kSimulcastApiVersionMax);
623 }
624 if (has_spec_compliant) {
625 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant,
626 kSimulcastApiVersionMax);
627 }
628 if (!has_legacy && !has_spec_compliant) {
629 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone,
630 kSimulcastApiVersionMax);
631 }
632}
633
Guido Urdaneta70c2db12019-04-16 10:24:14634const ContentInfo* FindTransceiverMSection(
635 RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver,
636 const SessionDescriptionInterface* session_description) {
637 return transceiver->mid()
638 ? session_description->description()->GetContentByName(
639 *transceiver->mid())
640 : nullptr;
641}
642
Henrik Boströma3728d32019-10-28 11:09:49643// Wraps a CreateSessionDescriptionObserver and an OperationsChain operation
644// complete callback. When the observer is invoked, the wrapped observer is
645// invoked followed by invoking the completion callback.
646class CreateSessionDescriptionObserverOperationWrapper
647 : public CreateSessionDescriptionObserver {
648 public:
649 CreateSessionDescriptionObserverOperationWrapper(
650 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer,
651 std::function<void()> operation_complete_callback)
652 : observer_(std::move(observer)),
653 operation_complete_callback_(std::move(operation_complete_callback)) {
654 RTC_DCHECK(observer_);
655 }
656 ~CreateSessionDescriptionObserverOperationWrapper() override {
657 RTC_DCHECK(was_called_);
658 }
659
660 void OnSuccess(SessionDescriptionInterface* desc) override {
661 RTC_DCHECK(!was_called_);
662#ifdef RTC_DCHECK_IS_ON
663 was_called_ = true;
664#endif // RTC_DCHECK_IS_ON
665 // Completing the operation before invoking the observer allows the observer
666 // to execute SetLocalDescription() without delay.
667 operation_complete_callback_();
668 observer_->OnSuccess(desc);
669 }
670
671 void OnFailure(RTCError error) override {
672 RTC_DCHECK(!was_called_);
673#ifdef RTC_DCHECK_IS_ON
674 was_called_ = true;
675#endif // RTC_DCHECK_IS_ON
676 operation_complete_callback_();
677 observer_->OnFailure(std::move(error));
678 }
679
680 private:
681#ifdef RTC_DCHECK_IS_ON
682 bool was_called_ = false;
683#endif // RTC_DCHECK_IS_ON
684 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer_;
685 std::function<void()> operation_complete_callback_;
686};
687
Jonas Orelande3096512020-05-27 07:01:05688// Check if the changes of IceTransportsType motives an ice restart.
689bool NeedIceRestart(bool surface_ice_candidates_on_ice_transport_type_changed,
690 PeerConnectionInterface::IceTransportsType current,
691 PeerConnectionInterface::IceTransportsType modified) {
692 if (current == modified) {
693 return false;
694 }
695
696 if (!surface_ice_candidates_on_ice_transport_type_changed) {
697 return true;
698 }
699
700 auto current_filter = ConvertIceTransportTypeToCandidateFilter(current);
701 auto modified_filter = ConvertIceTransportTypeToCandidateFilter(modified);
702
703 // If surface_ice_candidates_on_ice_transport_type_changed is true and we
704 // extend the filter, then no ice restart is needed.
705 return (current_filter & modified_filter) != current_filter;
706}
707
Steve Anton75737c02017-11-06 18:37:17708} // namespace
709
Henrik Boström4e196702019-10-30 09:35:50710// Used by parameterless SetLocalDescription() to create an offer or answer.
711// Upon completion of creating the session description, SetLocalDescription() is
712// invoked with the result.
Henrik Boström4e196702019-10-30 09:35:50713class PeerConnection::ImplicitCreateSessionDescriptionObserver
714 : public CreateSessionDescriptionObserver {
715 public:
716 ImplicitCreateSessionDescriptionObserver(
717 rtc::WeakPtr<PeerConnection> pc,
Henrik Boström831ae4e2020-07-29 10:04:00718 rtc::scoped_refptr<SetLocalDescriptionObserverInterface>
Henrik Boström4e196702019-10-30 09:35:50719 set_local_description_observer)
720 : pc_(std::move(pc)),
721 set_local_description_observer_(
722 std::move(set_local_description_observer)) {}
723 ~ImplicitCreateSessionDescriptionObserver() override {
724 RTC_DCHECK(was_called_);
725 }
726
727 void SetOperationCompleteCallback(
728 std::function<void()> operation_complete_callback) {
729 operation_complete_callback_ = std::move(operation_complete_callback);
730 }
731
732 bool was_called() const { return was_called_; }
733
734 void OnSuccess(SessionDescriptionInterface* desc_ptr) override {
735 RTC_DCHECK(!was_called_);
736 std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr);
737 was_called_ = true;
738
739 // Abort early if |pc_| is no longer valid.
740 if (!pc_) {
741 operation_complete_callback_();
742 return;
743 }
Henrik Boström831ae4e2020-07-29 10:04:00744 // DoSetLocalDescription() is a synchronous operation that invokes
745 // |set_local_description_observer_| with the result.
Henrik Boström4e196702019-10-30 09:35:50746 pc_->DoSetLocalDescription(std::move(desc),
747 std::move(set_local_description_observer_));
Henrik Boström4e196702019-10-30 09:35:50748 operation_complete_callback_();
749 }
750
751 void OnFailure(RTCError error) override {
752 RTC_DCHECK(!was_called_);
753 was_called_ = true;
Henrik Boström831ae4e2020-07-29 10:04:00754 set_local_description_observer_->OnSetLocalDescriptionComplete(RTCError(
755 error.type(), std::string("SetLocalDescription failed to create "
756 "session description - ") +
757 error.message()));
Henrik Boström4e196702019-10-30 09:35:50758 operation_complete_callback_();
759 }
760
761 private:
762 bool was_called_ = false;
763 rtc::WeakPtr<PeerConnection> pc_;
Henrik Boström831ae4e2020-07-29 10:04:00764 rtc::scoped_refptr<SetLocalDescriptionObserverInterface>
Henrik Boström4e196702019-10-30 09:35:50765 set_local_description_observer_;
766 std::function<void()> operation_complete_callback_;
767};
768
Henrik Boström79b69802019-07-18 09:16:56769class PeerConnection::LocalIceCredentialsToReplace {
770 public:
771 // Sets the ICE credentials that need restarting to the ICE credentials of
772 // the current and pending descriptions.
773 void SetIceCredentialsFromLocalDescriptions(
774 const SessionDescriptionInterface* current_local_description,
775 const SessionDescriptionInterface* pending_local_description) {
776 ice_credentials_.clear();
777 if (current_local_description) {
778 AppendIceCredentialsFromSessionDescription(*current_local_description);
779 }
780 if (pending_local_description) {
781 AppendIceCredentialsFromSessionDescription(*pending_local_description);
782 }
783 }
784
785 void ClearIceCredentials() { ice_credentials_.clear(); }
786
787 // Returns true if we have ICE credentials that need restarting.
788 bool HasIceCredentials() const { return !ice_credentials_.empty(); }
789
790 // Returns true if |local_description| shares no ICE credentials with the
791 // ICE credentials that need restarting.
792 bool SatisfiesIceRestart(
793 const SessionDescriptionInterface& local_description) const {
794 for (const auto& transport_info :
795 local_description.description()->transport_infos()) {
796 if (ice_credentials_.find(std::make_pair(
797 transport_info.description.ice_ufrag,
798 transport_info.description.ice_pwd)) != ice_credentials_.end()) {
799 return false;
800 }
801 }
802 return true;
803 }
804
805 private:
806 void AppendIceCredentialsFromSessionDescription(
807 const SessionDescriptionInterface& desc) {
808 for (const auto& transport_info : desc.description()->transport_infos()) {
809 ice_credentials_.insert(
810 std::make_pair(transport_info.description.ice_ufrag,
811 transport_info.description.ice_pwd));
812 }
813 }
814
815 std::set<std::pair<std::string, std::string>> ice_credentials_;
816};
817
Henrik Boström831ae4e2020-07-29 10:04:00818// Wrapper for SetSessionDescriptionObserver that invokes the success or failure
819// callback in a posted message handled by the peer connection. This introduces
820// a delay that prevents recursive API calls by the observer, but this also
821// means that the PeerConnection can be modified before the observer sees the
822// result of the operation. This is ill-advised for synchronizing states.
823//
824// Implements both the SetLocalDescriptionObserverInterface and the
825// SetRemoteDescriptionObserverInterface.
826class PeerConnection::SetSessionDescriptionObserverAdapter
827 : public SetLocalDescriptionObserverInterface,
828 public SetRemoteDescriptionObserverInterface {
Henrik Boström31638672017-11-23 16:48:32829 public:
Henrik Boström831ae4e2020-07-29 10:04:00830 SetSessionDescriptionObserverAdapter(
831 rtc::WeakPtr<PeerConnection> pc,
832 rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer)
833 : pc_(std::move(pc)), inner_observer_(std::move(inner_observer)) {}
Henrik Boström31638672017-11-23 16:48:32834
Henrik Boström831ae4e2020-07-29 10:04:00835 // SetLocalDescriptionObserverInterface implementation.
836 void OnSetLocalDescriptionComplete(RTCError error) override {
837 OnSetDescriptionComplete(std::move(error));
838 }
Henrik Boström31638672017-11-23 16:48:32839 // SetRemoteDescriptionObserverInterface implementation.
840 void OnSetRemoteDescriptionComplete(RTCError error) override {
Henrik Boström831ae4e2020-07-29 10:04:00841 OnSetDescriptionComplete(std::move(error));
Henrik Boström31638672017-11-23 16:48:32842 }
843
844 private:
Henrik Boström831ae4e2020-07-29 10:04:00845 void OnSetDescriptionComplete(RTCError error) {
846 if (!pc_)
847 return;
848 if (error.ok()) {
849 pc_->PostSetSessionDescriptionSuccess(inner_observer_);
850 } else {
851 pc_->PostSetSessionDescriptionFailure(inner_observer_, std::move(error));
852 }
853 }
854
855 rtc::WeakPtr<PeerConnection> pc_;
856 rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer_;
Henrik Boström31638672017-11-23 16:48:32857};
858
deadbeef293e9262017-01-11 20:28:30859bool PeerConnectionInterface::RTCConfiguration::operator==(
860 const PeerConnectionInterface::RTCConfiguration& o) const {
861 // This static_assert prevents us from accidentally breaking operator==.
Steve Anton300bf8e2017-07-14 17:13:10862 // Note: Order matters! Fields must be ordered the same as RTCConfiguration.
deadbeef293e9262017-01-11 20:28:30863 struct stuff_being_tested_for_equality {
Magnus Jedvert3beb2072017-07-14 14:23:56864 IceServers servers;
Steve Anton300bf8e2017-07-14 17:13:10865 IceTransportsType type;
deadbeef293e9262017-01-11 20:28:30866 BundlePolicy bundle_policy;
867 RtcpMuxPolicy rtcp_mux_policy;
Steve Anton300bf8e2017-07-14 17:13:10868 std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates;
869 int ice_candidate_pool_size;
870 bool disable_ipv6;
871 bool disable_ipv6_on_wifi;
deadbeefd21eab3e2017-07-26 23:50:11872 int max_ipv6_networks;
Daniel Lazarenko2870b0a2018-01-25 09:30:22873 bool disable_link_local_networks;
Steve Anton300bf8e2017-07-14 17:13:10874 bool enable_rtp_data_channel;
Danil Chapovalov66cadcc2018-06-19 14:47:43875 absl::optional<int> screencast_min_bitrate;
876 absl::optional<bool> combined_audio_video_bwe;
877 absl::optional<bool> enable_dtls_srtp;
deadbeef293e9262017-01-11 20:28:30878 TcpCandidatePolicy tcp_candidate_policy;
879 CandidateNetworkPolicy candidate_network_policy;
880 int audio_jitter_buffer_max_packets;
881 bool audio_jitter_buffer_fast_accelerate;
Jakob Ivarsson10403ae2018-11-27 14:45:20882 int audio_jitter_buffer_min_delay_ms;
Jakob Ivarsson53eae872019-01-10 14:58:36883 bool audio_jitter_buffer_enable_rtx_handling;
deadbeef293e9262017-01-11 20:28:30884 int ice_connection_receiving_timeout;
885 int ice_backup_candidate_pair_ping_interval;
886 ContinualGatheringPolicy continual_gathering_policy;
deadbeef293e9262017-01-11 20:28:30887 bool prioritize_most_likely_ice_candidate_pairs;
888 struct cricket::MediaConfig media_config;
deadbeef293e9262017-01-11 20:28:30889 bool prune_turn_ports;
Honghai Zhangf8998cf2019-10-14 18:27:50890 PortPrunePolicy turn_port_prune_policy;
deadbeef293e9262017-01-11 20:28:30891 bool presume_writable_when_fully_relayed;
892 bool enable_ice_renomination;
893 bool redetermine_role_on_ice_restart;
Qingsi Wang1fe119f2019-05-31 23:55:33894 bool surface_ice_candidates_on_ice_transport_type_changed;
Danil Chapovalov66cadcc2018-06-19 14:47:43895 absl::optional<int> ice_check_interval_strong_connectivity;
896 absl::optional<int> ice_check_interval_weak_connectivity;
897 absl::optional<int> ice_check_min_interval;
898 absl::optional<int> ice_unwritable_timeout;
899 absl::optional<int> ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-07 07:30:17900 absl::optional<int> ice_inactive_timeout;
Danil Chapovalov66cadcc2018-06-19 14:47:43901 absl::optional<int> stun_candidate_keepalive_interval;
Jonas Orelandbdcee282017-10-10 12:01:40902 webrtc::TurnCustomizer* turn_customizer;
Steve Anton79e79602017-11-20 18:25:56903 SdpSemantics sdp_semantics;
Danil Chapovalov66cadcc2018-06-19 14:47:43904 absl::optional<rtc::AdapterType> network_preference;
Zhi Huangb57e1692018-06-12 18:41:11905 bool active_reset_srtp_params;
Benjamin Wright8c27cca2018-10-25 17:16:44906 absl::optional<CryptoOptions> crypto_options;
Johannes Kron89f874e2018-11-12 09:25:48907 bool offer_extmap_allow_mixed;
Jonas Oreland3c028422019-08-22 14:16:35908 std::string turn_logging_id;
Eldar Rello5ab79e62019-10-09 15:29:44909 bool enable_implicit_rollback;
philipel16cec3b2019-10-25 10:23:02910 absl::optional<bool> allow_codec_switching;
deadbeef293e9262017-01-11 20:28:30911 };
912 static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this),
913 "Did you add something to RTCConfiguration and forget to "
914 "update operator==?");
915 return type == o.type && servers == o.servers &&
916 bundle_policy == o.bundle_policy &&
917 rtcp_mux_policy == o.rtcp_mux_policy &&
918 tcp_candidate_policy == o.tcp_candidate_policy &&
919 candidate_network_policy == o.candidate_network_policy &&
920 audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets &&
921 audio_jitter_buffer_fast_accelerate ==
922 o.audio_jitter_buffer_fast_accelerate &&
Jakob Ivarsson10403ae2018-11-27 14:45:20923 audio_jitter_buffer_min_delay_ms ==
924 o.audio_jitter_buffer_min_delay_ms &&
Jakob Ivarsson53eae872019-01-10 14:58:36925 audio_jitter_buffer_enable_rtx_handling ==
926 o.audio_jitter_buffer_enable_rtx_handling &&
deadbeef293e9262017-01-11 20:28:30927 ice_connection_receiving_timeout ==
928 o.ice_connection_receiving_timeout &&
929 ice_backup_candidate_pair_ping_interval ==
930 o.ice_backup_candidate_pair_ping_interval &&
931 continual_gathering_policy == o.continual_gathering_policy &&
932 certificates == o.certificates &&
933 prioritize_most_likely_ice_candidate_pairs ==
934 o.prioritize_most_likely_ice_candidate_pairs &&
935 media_config == o.media_config && disable_ipv6 == o.disable_ipv6 &&
zhihuangb09b3f92017-03-07 22:40:51936 disable_ipv6_on_wifi == o.disable_ipv6_on_wifi &&
deadbeefd21eab3e2017-07-26 23:50:11937 max_ipv6_networks == o.max_ipv6_networks &&
Daniel Lazarenko2870b0a2018-01-25 09:30:22938 disable_link_local_networks == o.disable_link_local_networks &&
deadbeef293e9262017-01-11 20:28:30939 enable_rtp_data_channel == o.enable_rtp_data_channel &&
deadbeef293e9262017-01-11 20:28:30940 screencast_min_bitrate == o.screencast_min_bitrate &&
941 combined_audio_video_bwe == o.combined_audio_video_bwe &&
942 enable_dtls_srtp == o.enable_dtls_srtp &&
943 ice_candidate_pool_size == o.ice_candidate_pool_size &&
944 prune_turn_ports == o.prune_turn_ports &&
Honghai Zhangf8998cf2019-10-14 18:27:50945 turn_port_prune_policy == o.turn_port_prune_policy &&
deadbeef293e9262017-01-11 20:28:30946 presume_writable_when_fully_relayed ==
947 o.presume_writable_when_fully_relayed &&
948 enable_ice_renomination == o.enable_ice_renomination &&
skvlad51072462017-02-02 19:50:14949 redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart &&
Qingsi Wang1fe119f2019-05-31 23:55:33950 surface_ice_candidates_on_ice_transport_type_changed ==
951 o.surface_ice_candidates_on_ice_transport_type_changed &&
Qingsi Wange6826d22018-03-08 22:55:14952 ice_check_interval_strong_connectivity ==
953 o.ice_check_interval_strong_connectivity &&
954 ice_check_interval_weak_connectivity ==
955 o.ice_check_interval_weak_connectivity &&
Steve Anton300bf8e2017-07-14 17:13:10956 ice_check_min_interval == o.ice_check_min_interval &&
Qingsi Wang22e623a2018-03-13 17:53:57957 ice_unwritable_timeout == o.ice_unwritable_timeout &&
958 ice_unwritable_min_checks == o.ice_unwritable_min_checks &&
Jiawei Ou9d4fd5552018-12-07 07:30:17959 ice_inactive_timeout == o.ice_inactive_timeout &&
Qingsi Wangdb53f8e2018-02-20 22:45:49960 stun_candidate_keepalive_interval ==
961 o.stun_candidate_keepalive_interval &&
Steve Anton79e79602017-11-20 18:25:56962 turn_customizer == o.turn_customizer &&
Qingsi Wang9a5c6f82018-02-01 18:38:40963 sdp_semantics == o.sdp_semantics &&
Zhi Huangb57e1692018-06-12 18:41:11964 network_preference == o.network_preference &&
Piotr (Peter) Slatalae0c2e972018-10-08 16:43:21965 active_reset_srtp_params == o.active_reset_srtp_params &&
Johannes Kron89f874e2018-11-12 09:25:48966 crypto_options == o.crypto_options &&
Jonas Oreland3c028422019-08-22 14:16:35967 offer_extmap_allow_mixed == o.offer_extmap_allow_mixed &&
Eldar Rello5ab79e62019-10-09 15:29:44968 turn_logging_id == o.turn_logging_id &&
philipel16cec3b2019-10-25 10:23:02969 enable_implicit_rollback == o.enable_implicit_rollback &&
Eldar Rello9276e2c2020-06-10 14:53:39970 allow_codec_switching == o.allow_codec_switching;
deadbeef293e9262017-01-11 20:28:30971}
972
973bool PeerConnectionInterface::RTCConfiguration::operator!=(
974 const PeerConnectionInterface::RTCConfiguration& o) const {
975 return !(*this == o);
deadbeef3edec7c2016-12-10 19:44:26976}
977
Eldar Rello353a7182019-11-25 16:49:44978void PeerConnection::TransceiverStableState::set_newly_created() {
979 RTC_DCHECK(!has_m_section_);
980 newly_created_ = true;
981}
982
983void PeerConnection::TransceiverStableState::SetMSectionIfUnset(
984 absl::optional<std::string> mid,
985 absl::optional<size_t> mline_index) {
986 if (!has_m_section_) {
987 mid_ = mid;
988 mline_index_ = mline_index;
989 has_m_section_ = true;
990 }
991}
992
993void PeerConnection::TransceiverStableState::SetRemoteStreamIdsIfUnset(
994 const std::vector<std::string>& ids) {
995 if (!remote_stream_ids_.has_value()) {
996 remote_stream_ids_ = ids;
997 }
998}
999
zhihuang8f65cdf2016-05-07 01:40:301000// Generate a RTCP CNAME when a PeerConnection is created.
1001std::string GenerateRtcpCname() {
1002 std::string cname;
1003 if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) {
Mirko Bonadei675513b2017-11-09 10:09:251004 RTC_LOG(LS_ERROR) << "Failed to generate CNAME.";
nisseeb4ca4e2017-01-12 10:24:271005 RTC_NOTREACHED();
zhihuang8f65cdf2016-05-07 01:40:301006 }
1007 return cname;
1008}
1009
zhihuang1c378ed2017-08-17 21:10:501010bool ValidateOfferAnswerOptions(
1011 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) {
1012 return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) &&
1013 IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video);
olka3c747662017-08-17 13:50:321014}
1015
zhihuang1c378ed2017-08-17 21:10:501016// From |rtc_options|, fill parts of |session_options| shared by all generated
1017// m= sections (in other words, nothing that involves a map/array).
1018void ExtractSharedMediaSessionOptions(
1019 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
1020 cricket::MediaSessionOptions* session_options) {
1021 session_options->vad_enabled = rtc_options.voice_activity_detection;
1022 session_options->bundle_enabled = rtc_options.use_rtp_mux;
Mirta Dvornicic479a3c02019-06-04 13:38:501023 session_options->raw_packetization_for_video =
1024 rtc_options.raw_packetization_for_video;
zhihuang1c378ed2017-08-17 21:10:501025}
zhihuanga77e6bb2017-08-15 01:17:481026
zhihuang38ede132017-06-15 19:52:321027PeerConnection::PeerConnection(PeerConnectionFactory* factory,
1028 std::unique_ptr<RtcEventLog> event_log,
1029 std::unique_ptr<Call> call)
henrike@webrtc.org28e20752013-07-10 00:45:361030 : factory_(factory),
zhihuang38ede132017-06-15 19:52:321031 event_log_(std::move(event_log)),
Karl Wibergb03ab712019-02-14 10:59:571032 event_log_ptr_(event_log_.get()),
Henrik Boströma3728d32019-10-28 11:09:491033 operations_chain_(rtc::OperationsChain::Create()),
zhihuang8f65cdf2016-05-07 01:40:301034 rtcp_cname_(GenerateRtcpCname()),
deadbeefab9b2d12015-10-14 18:33:111035 local_streams_(StreamCollection::Create()),
zhihuang38ede132017-06-15 19:52:321036 remote_streams_(StreamCollection::Create()),
Karl Wiberg6cab5c82019-03-26 08:57:011037 call_(std::move(call)),
Henrik Boström79b69802019-07-18 09:16:561038 call_ptr_(call_.get()),
Henrik Boströma3728d32019-10-28 11:09:491039 local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()),
Harald Alvestrand00cf34c2019-12-02 08:56:021040 data_channel_controller_(this),
Henrik Boströme574a312020-08-25 08:20:111041 weak_ptr_factory_(this) {
1042 operations_chain_->SetOnChainEmptyCallback(
1043 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr()]() {
1044 if (!this_weak_ptr)
1045 return;
1046 this_weak_ptr->OnOperationsChainEmpty();
1047 });
1048}
henrike@webrtc.org28e20752013-07-10 00:45:361049
1050PeerConnection::~PeerConnection() {
Peter Boström1a9d6152015-12-08 21:15:171051 TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection");
Steve Anton4171afb2017-11-20 18:20:221052 RTC_DCHECK_RUN_ON(signaling_thread());
1053
Henrik Boströma3728d32019-10-28 11:09:491054 weak_ptr_factory_.InvalidateWeakPtrs();
1055
Steve Anton8af21862017-12-15 19:20:131056 // Need to stop transceivers before destroying the stats collector because
1057 // AudioRtpSender has a reference to the StatsCollector it will update when
1058 // stopping.
Mirko Bonadei739baf02019-01-27 16:29:421059 for (const auto& transceiver : transceivers_) {
Harald Alvestrand6060df52020-08-11 07:54:021060 transceiver->StopInternal();
Steve Anton8af21862017-12-15 19:20:131061 }
Steve Anton4171afb2017-11-20 18:20:221062
Taylor Brandstettera1c30352016-05-13 15:15:111063 stats_.reset(nullptr);
hbosb78306a2016-12-19 13:06:571064 if (stats_collector_) {
1065 stats_collector_->WaitForPendingRequest();
1066 stats_collector_ = nullptr;
1067 }
Steve Anton75737c02017-11-06 18:37:171068
Steve Anton8af21862017-12-15 19:20:131069 // Don't destroy BaseChannels until after stats has been cleaned up so that
1070 // the last stats request can still read from the channels.
1071 DestroyAllChannels();
1072
Mirko Bonadei675513b2017-11-09 10:09:251073 RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed.";
Steve Anton75737c02017-11-06 18:37:171074
1075 webrtc_session_desc_factory_.reset();
Steve Anton75737c02017-11-06 18:37:171076 transport_controller_.reset();
1077
deadbeef91dd5672016-05-18 23:55:301078 // port_allocator_ lives on the network thread and should be destroyed there.
Karl Wibergfb3be392019-03-22 13:13:221079 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1080 RTC_DCHECK_RUN_ON(network_thread());
1081 port_allocator_.reset();
1082 });
eladalon248fd4f2017-09-06 12:18:151083 // call_ and event_log_ must be destroyed on the worker thread.
Steve Anton978b8762017-09-29 19:15:021084 worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
Karl Wibergb03ab712019-02-14 10:59:571085 RTC_DCHECK_RUN_ON(worker_thread());
eladalon248fd4f2017-09-06 12:18:151086 call_.reset();
Qingsi Wang93a84392018-01-31 01:13:091087 // The event log must outlive call (and any other object that uses it).
eladalon248fd4f2017-09-06 12:18:151088 event_log_.reset();
1089 });
Henrik Boströma3728d32019-10-28 11:09:491090
1091 // Process all pending notifications in the message queue. If we don't do
1092 // this, requests will linger and not know they succeeded or failed.
1093 rtc::MessageList list;
1094 signaling_thread()->Clear(this, rtc::MQID_ANY, &list);
1095 for (auto& msg : list) {
1096 if (msg.message_id == MSG_CREATE_SESSIONDESCRIPTION_FAILED) {
1097 // Processing CreateOffer() and CreateAnswer() messages ensures their
1098 // observers are invoked even if the PeerConnection is destroyed early.
1099 OnMessage(&msg);
1100 } else {
1101 // TODO(hbos): Consider processing all pending messages. This would mean
1102 // that SetLocalDescription() and SetRemoteDescription() observers are
1103 // informed of successes and failures; this is currently NOT the case.
1104 delete msg.pdata;
1105 }
1106 }
henrike@webrtc.org28e20752013-07-10 00:45:361107}
1108
Steve Anton8af21862017-12-15 19:20:131109void PeerConnection::DestroyAllChannels() {
Steve Anton3fe1b152017-12-12 18:20:081110 // Destroy video channels first since they may have a pointer to a voice
1111 // channel.
Mirko Bonadei739baf02019-01-27 16:29:421112 for (const auto& transceiver : transceivers_) {
Steve Anton69470252018-02-09 19:43:081113 if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
Steve Anton3fe1b152017-12-12 18:20:081114 DestroyTransceiverChannel(transceiver);
1115 }
1116 }
Mirko Bonadei739baf02019-01-27 16:29:421117 for (const auto& transceiver : transceivers_) {
Steve Anton69470252018-02-09 19:43:081118 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton3fe1b152017-12-12 18:20:081119 DestroyTransceiverChannel(transceiver);
1120 }
1121 }
Harald Alvestrand408cb4b2019-11-16 11:09:081122 DestroyDataChannelTransport();
Steve Anton3fe1b152017-12-12 18:20:081123}
1124
henrike@webrtc.org28e20752013-07-10 00:45:361125bool PeerConnection::Initialize(
buildbot@webrtc.org41451d42014-05-03 05:39:451126 const PeerConnectionInterface::RTCConfiguration& configuration,
Benjamin Wrightcab588882018-05-02 22:12:471127 PeerConnectionDependencies dependencies) {
Karl Wiberg744310f2019-02-14 09:18:561128 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 21:15:171129 TRACE_EVENT0("webrtc", "PeerConnection::Initialize");
Steve Anton038834f2017-07-14 22:59:591130
1131 RTCError config_error = ValidateConfiguration(configuration);
1132 if (!config_error.ok()) {
Mirko Bonadei675513b2017-11-09 10:09:251133 RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message();
Steve Anton038834f2017-07-14 22:59:591134 return false;
1135 }
1136
Benjamin Wrightcab588882018-05-02 22:12:471137 if (!dependencies.allocator) {
Mirko Bonadei675513b2017-11-09 10:09:251138 RTC_LOG(LS_ERROR)
1139 << "PeerConnection initialized without a PortAllocator? "
Jonas Olsson45cc8902018-02-13 09:37:071140 "This shouldn't happen if using PeerConnectionFactory.";
deadbeef293e9262017-01-11 20:28:301141 return false;
1142 }
Jonas Orelandbdcee282017-10-10 12:01:401143
Benjamin Wrightcab588882018-05-02 22:12:471144 if (!dependencies.observer) {
deadbeef293e9262017-01-11 20:28:301145 // TODO(deadbeef): Why do we do this?
Mirko Bonadei675513b2017-11-09 10:09:251146 RTC_LOG(LS_ERROR) << "PeerConnection initialized without a "
Jonas Olsson45cc8902018-02-13 09:37:071147 "PeerConnectionObserver";
deadbeef653b8e02015-11-11 20:55:101148 return false;
1149 }
Benjamin Wrightd6f86e82018-05-08 20:12:251150
Benjamin Wrightcab588882018-05-02 22:12:471151 observer_ = dependencies.observer;
Zach Steine20867f2018-08-02 20:20:151152 async_resolver_factory_ = std::move(dependencies.async_resolver_factory);
Benjamin Wrightcab588882018-05-02 22:12:471153 port_allocator_ = std::move(dependencies.allocator);
Jorge E. Moreira00b46f72020-03-28 01:01:191154 packet_socket_factory_ = std::move(dependencies.packet_socket_factory);
Qingsi Wang25ec8882019-11-15 20:33:051155 ice_transport_factory_ = std::move(dependencies.ice_transport_factory);
Benjamin Wrightd6f86e82018-05-08 20:12:251156 tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier);
deadbeef653b8e02015-11-11 20:55:101157
Harald Alvestrandb2a74782018-06-28 11:54:071158 cricket::ServerAddresses stun_servers;
1159 std::vector<cricket::RelayServerConfig> turn_servers;
1160
1161 RTCErrorType parse_error =
1162 ParseIceServers(configuration.servers, &stun_servers, &turn_servers);
1163 if (parse_error != RTCErrorType::NONE) {
1164 return false;
1165 }
1166
Jonas Oreland3c028422019-08-22 14:16:351167 // Add the turn logging id to all turn servers
1168 for (cricket::RelayServerConfig& turn_server : turn_servers) {
1169 turn_server.turn_logging_id = configuration.turn_logging_id;
1170 }
1171
deadbeef91dd5672016-05-18 23:55:301172 // The port allocator lives on the network thread and should be initialized
Taylor Brandstettera1c30352016-05-13 15:15:111173 // there.
Karl Wibergfb3be392019-03-22 13:13:221174 const auto pa_result =
1175 network_thread()->Invoke<InitializePortAllocatorResult>(
Harald Alvestrandb2a74782018-06-28 11:54:071176 RTC_FROM_HERE,
1177 rtc::Bind(&PeerConnection::InitializePortAllocator_n, this,
Karl Wibergfb3be392019-03-22 13:13:221178 stun_servers, turn_servers, configuration));
1179
Harald Alvestrandb2a74782018-06-28 11:54:071180 // If initialization was successful, note if STUN or TURN servers
1181 // were supplied.
1182 if (!stun_servers.empty()) {
1183 NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED);
1184 }
1185 if (!turn_servers.empty()) {
1186 NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED);
1187 }
henrike@webrtc.org28e20752013-07-10 00:45:361188
Qingsi Wang7fc821d2018-07-12 19:54:531189 // Send information about IPv4/IPv6 status.
1190 PeerConnectionAddressFamilyCounter address_family;
Karl Wibergfb3be392019-03-22 13:13:221191 if (pa_result.enable_ipv6) {
Qingsi Wang7fc821d2018-07-12 19:54:531192 address_family = kPeerConnection_IPv6;
1193 } else {
1194 address_family = kPeerConnection_IPv4;
1195 }
1196 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family,
1197 kPeerConnectionAddressFamilyCounter_Max);
1198
Zhi Huange830e682018-03-30 17:48:351199 const PeerConnectionFactoryInterface::Options& options = factory_->options();
1200
Steve Anton75737c02017-11-06 18:37:171201 // RFC 3264: The numeric value of the session id and version in the
1202 // o line MUST be representable with a "64 bit signed integer".
1203 // Due to this constraint session id |session_id_| is max limited to
1204 // LLONG_MAX.
1205 session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX);
Zhi Huange830e682018-03-30 17:48:351206 JsepTransportController::Config config;
1207 config.redetermine_role_on_ice_restart =
1208 configuration.redetermine_role_on_ice_restart;
1209 config.ssl_max_version = factory_->options().ssl_max_version;
1210 config.disable_encryption = options.disable_encryption;
1211 config.bundle_policy = configuration.bundle_policy;
1212 config.rtcp_mux_policy = configuration.rtcp_mux_policy;
Benjamin Wright8c27cca2018-10-25 17:16:441213 // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this
1214 // stub.
1215 config.crypto_options = configuration.crypto_options.has_value()
1216 ? *configuration.crypto_options
1217 : options.crypto_options;
Zhi Huang365381f2018-04-13 23:44:341218 config.transport_observer = this;
Sebastian Jansson1b83a9e2019-09-18 16:22:121219 // It's safe to pass |this| and using |rtcp_invoker_| and the |call_| pointer
1220 // since the JsepTransportController instance is owned by this PeerConnection
1221 // instance and is destroyed before both |rtcp_invoker_| and the |call_|
1222 // pointer.
1223 config.rtcp_handler = [this](const rtc::CopyOnWriteBuffer& packet,
1224 int64_t packet_time_us) {
1225 RTC_DCHECK_RUN_ON(network_thread());
1226 rtcp_invoker_.AsyncInvoke<void>(
1227 RTC_FROM_HERE, worker_thread(), [this, packet, packet_time_us] {
1228 RTC_DCHECK_RUN_ON(worker_thread());
1229 // |call_| is reset on the worker thread in the PeerConnection
1230 // destructor, so we check that it's still valid before propagating
1231 // the packet.
1232 if (call_) {
1233 call_->Receiver()->DeliverPacket(MediaType::ANY, packet,
1234 packet_time_us);
1235 }
1236 });
1237 };
Karl Wibergb03ab712019-02-14 10:59:571238 config.event_log = event_log_ptr_;
Zhi Huange830e682018-03-30 17:48:351239#if defined(ENABLE_EXTERNAL_AUTH)
1240 config.enable_external_auth = true;
1241#endif
Zhi Huangb57e1692018-06-12 18:41:111242 config.active_reset_srtp_params = configuration.active_reset_srtp_params;
Anton Sukhanov98a462c2018-10-17 20:15:421243
Bjorn A Mellembc3eebc2019-09-23 21:53:541244 // Obtain a certificate from RTCConfiguration if any were provided (optional).
1245 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
1246 if (!configuration.certificates.empty()) {
1247 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
1248 // just picking the first one. The decision should be made based on the DTLS
1249 // handshake. The DTLS negotiations need to know about all certificates.
1250 certificate = configuration.certificates[0];
1251 }
1252
1253 if (options.disable_encryption) {
1254 dtls_enabled_ = false;
1255 } else {
1256 // Enable DTLS by default if we have an identity store or a certificate.
1257 dtls_enabled_ = (dependencies.cert_generator || certificate);
1258 // |configuration| can override the default |dtls_enabled_| value.
1259 if (configuration.enable_dtls_srtp) {
1260 dtls_enabled_ = *(configuration.enable_dtls_srtp);
1261 }
1262 }
1263
Niels Möller2a707032020-06-16 14:39:131264 if (configuration.enable_rtp_data_channel) {
Bjorn A Mellembc3eebc2019-09-23 21:53:541265 // Enable creation of RTP data channels if the kEnableRtpDataChannels is
1266 // set. It takes precendence over the disable_sctp_data_channels
1267 // PeerConnectionFactoryInterface::Options.
Harald Alvestrand05e4d082019-12-03 13:04:211268 data_channel_controller_.set_data_channel_type(cricket::DCT_RTP);
Bjorn A Mellembc3eebc2019-09-23 21:53:541269 } else {
1270 // DTLS has to be enabled to use SCTP.
1271 if (!options.disable_sctp_data_channels && dtls_enabled_) {
Harald Alvestrand05e4d082019-12-03 13:04:211272 data_channel_controller_.set_data_channel_type(cricket::DCT_SCTP);
Per Kjellander2bca0082020-08-28 07:15:151273 config.sctp_factory = factory_->sctp_transport_factory();
Bjorn A Mellembc3eebc2019-09-23 21:53:541274 }
1275 }
1276
Qingsi Wang25ec8882019-11-15 20:33:051277 config.ice_transport_factory = ice_transport_factory_.get();
1278
Zhi Huange830e682018-03-30 17:48:351279 transport_controller_.reset(new JsepTransportController(
Zach Steine20867f2018-08-02 20:20:151280 signaling_thread(), network_thread(), port_allocator_.get(),
1281 async_resolver_factory_.get(), config));
Zhi Huange830e682018-03-30 17:48:351282 transport_controller_->SignalIceConnectionState.connect(
Alex Loiko9289eda2018-11-23 16:18:591283 this, &PeerConnection::OnTransportControllerConnectionState);
1284 transport_controller_->SignalStandardizedIceConnectionState.connect(
1285 this, &PeerConnection::SetStandardizedIceConnectionState);
Jonas Olsson635474e2018-10-18 13:58:171286 transport_controller_->SignalConnectionState.connect(
1287 this, &PeerConnection::SetConnectionState);
Zhi Huange830e682018-03-30 17:48:351288 transport_controller_->SignalIceGatheringState.connect(
Steve Anton75737c02017-11-06 18:37:171289 this, &PeerConnection::OnTransportControllerGatheringState);
Zhi Huange830e682018-03-30 17:48:351290 transport_controller_->SignalIceCandidatesGathered.connect(
Steve Anton75737c02017-11-06 18:37:171291 this, &PeerConnection::OnTransportControllerCandidatesGathered);
Eldar Relloda13ea22019-06-01 09:23:431292 transport_controller_->SignalIceCandidateError.connect(
1293 this, &PeerConnection::OnTransportControllerCandidateError);
Zhi Huange830e682018-03-30 17:48:351294 transport_controller_->SignalIceCandidatesRemoved.connect(
Steve Anton75737c02017-11-06 18:37:171295 this, &PeerConnection::OnTransportControllerCandidatesRemoved);
1296 transport_controller_->SignalDtlsHandshakeError.connect(
1297 this, &PeerConnection::OnTransportControllerDtlsHandshakeError);
Alex Drake00c7ecf2019-08-06 17:54:471298 transport_controller_->SignalIceCandidatePairChanged.connect(
1299 this, &PeerConnection::OnTransportControllerCandidateChanged);
Steve Anton75737c02017-11-06 18:37:171300
deadbeefab9b2d12015-10-14 18:33:111301 stats_.reset(new StatsCollector(this));
hbos74e1a4f2016-09-16 06:33:011302 stats_collector_ = RTCStatsCollector::Create(this);
henrike@webrtc.org28e20752013-07-10 00:45:361303
Steve Antonba818672017-11-06 18:21:571304 configuration_ = configuration;
1305
Steve Antond25da372017-11-06 22:50:291306 transport_controller_->SetIceConfig(ParseIceConfig(configuration));
Steve Anton75737c02017-11-06 18:37:171307
Steve Anton75737c02017-11-06 18:37:171308 video_options_.screencast_min_bitrate_kbps =
1309 configuration.screencast_min_bitrate;
1310 audio_options_.combined_audio_video_bwe =
1311 configuration.combined_audio_video_bwe;
1312
1313 audio_options_.audio_jitter_buffer_max_packets =
Oskar Sundbom9b28a032017-11-16 09:53:301314 configuration.audio_jitter_buffer_max_packets;
Steve Anton75737c02017-11-06 18:37:171315
1316 audio_options_.audio_jitter_buffer_fast_accelerate =
Oskar Sundbom9b28a032017-11-16 09:53:301317 configuration.audio_jitter_buffer_fast_accelerate;
Steve Anton75737c02017-11-06 18:37:171318
Jakob Ivarsson10403ae2018-11-27 14:45:201319 audio_options_.audio_jitter_buffer_min_delay_ms =
1320 configuration.audio_jitter_buffer_min_delay_ms;
1321
Jakob Ivarsson53eae872019-01-10 14:58:361322 audio_options_.audio_jitter_buffer_enable_rtx_handling =
1323 configuration.audio_jitter_buffer_enable_rtx_handling;
1324
Steve Anton75737c02017-11-06 18:37:171325 // Whether the certificate generator/certificate is null or not determines
1326 // what PeerConnectionDescriptionFactory will do, so make sure that we give it
1327 // the right instructions by clearing the variables if needed.
1328 if (!dtls_enabled_) {
Benjamin Wrightcab588882018-05-02 22:12:471329 dependencies.cert_generator.reset();
Steve Anton75737c02017-11-06 18:37:171330 certificate = nullptr;
1331 } else if (certificate) {
1332 // Favor generated certificate over the certificate generator.
Benjamin Wrightcab588882018-05-02 22:12:471333 dependencies.cert_generator.reset();
Steve Anton75737c02017-11-06 18:37:171334 }
1335
1336 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
1337 signaling_thread(), channel_manager(), this, session_id(),
Amit Hilbuchbcd39d42019-01-26 01:13:561338 std::move(dependencies.cert_generator), certificate, &ssrc_generator_));
Steve Anton75737c02017-11-06 18:37:171339 webrtc_session_desc_factory_->SignalCertificateReady.connect(
1340 this, &PeerConnection::OnCertificateReady);
1341
1342 if (options.disable_encryption) {
1343 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
1344 }
1345
1346 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
Benjamin Wright8c27cca2018-10-25 17:16:441347 GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions);
Steve Anton8f66ddb2018-12-11 00:08:051348 webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan());
henrike@webrtc.org28e20752013-07-10 00:45:361349
Steve Anton4171afb2017-11-20 18:20:221350 // Add default audio/video transceivers for Plan B SDP.
1351 if (!IsUnifiedPlan()) {
1352 transceivers_.push_back(
1353 RtpTransceiverProxyWithInternal<RtpTransceiver>::Create(
1354 signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO)));
1355 transceivers_.push_back(
1356 RtpTransceiverProxyWithInternal<RtpTransceiver>::Create(
1357 signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO)));
1358 }
Harald Alvestrand183e09d2018-06-28 10:04:411359 int delay_ms =
1360 return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS;
Steve Antonad182762018-09-05 20:22:401361 signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this,
1362 MSG_REPORT_USAGE_PATTERN, nullptr);
Jonas Orelanda3aa9bd2019-04-17 05:38:401363
1364 if (dependencies.video_bitrate_allocator_factory) {
1365 video_bitrate_allocator_factory_ =
1366 std::move(dependencies.video_bitrate_allocator_factory);
1367 } else {
1368 video_bitrate_allocator_factory_ =
1369 CreateBuiltinVideoBitrateAllocatorFactory();
1370 }
henrike@webrtc.org28e20752013-07-10 00:45:361371 return true;
1372}
1373
Steve Anton038834f2017-07-14 22:59:591374RTCError PeerConnection::ValidateConfiguration(
1375 const RTCConfiguration& config) const {
Steve Antonf4172382020-01-27 23:45:021376 return cricket::P2PTransportChannel::ValidateIceConfig(
1377 ParseIceConfig(config));
Steve Anton038834f2017-07-14 22:59:591378}
1379
Yves Gerey665174f2018-06-19 13:03:051380rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() {
Karl Wiberg5966c502019-02-21 22:55:091381 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 21:48:581382 RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified "
1383 "Plan SdpSemantics. Please use GetSenders "
1384 "instead.";
deadbeefab9b2d12015-10-14 18:33:111385 return local_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:361386}
1387
Yves Gerey665174f2018-06-19 13:03:051388rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() {
Karl Wiberg5966c502019-02-21 22:55:091389 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 21:48:581390 RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified "
1391 "Plan SdpSemantics. Please use GetReceivers "
1392 "instead.";
deadbeefab9b2d12015-10-14 18:33:111393 return remote_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:361394}
1395
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:291396bool PeerConnection::AddStream(MediaStreamInterface* local_stream) {
Karl Wiberg744310f2019-02-14 09:18:561397 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 21:48:581398 RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan "
1399 "SdpSemantics. Please use AddTrack instead.";
Peter Boström1a9d6152015-12-08 21:15:171400 TRACE_EVENT0("webrtc", "PeerConnection::AddStream");
henrike@webrtc.org28e20752013-07-10 00:45:361401 if (IsClosed()) {
1402 return false;
1403 }
deadbeefab9b2d12015-10-14 18:33:111404 if (!CanAddLocalMediaStream(local_streams_, local_stream)) {
henrike@webrtc.org28e20752013-07-10 00:45:361405 return false;
1406 }
deadbeefab9b2d12015-10-14 18:33:111407
1408 local_streams_->AddStream(local_stream);
deadbeefeb459812015-12-16 03:24:431409 MediaStreamObserver* observer = new MediaStreamObserver(local_stream);
1410 observer->SignalAudioTrackAdded.connect(this,
1411 &PeerConnection::OnAudioTrackAdded);
1412 observer->SignalAudioTrackRemoved.connect(
1413 this, &PeerConnection::OnAudioTrackRemoved);
1414 observer->SignalVideoTrackAdded.connect(this,
1415 &PeerConnection::OnVideoTrackAdded);
1416 observer->SignalVideoTrackRemoved.connect(
1417 this, &PeerConnection::OnVideoTrackRemoved);
kwibergd1fe2812016-04-27 13:47:291418 stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer));
deadbeefab9b2d12015-10-14 18:33:111419
deadbeefab9b2d12015-10-14 18:33:111420 for (const auto& track : local_stream->GetAudioTracks()) {
korniltsev.anatolyec390b52017-07-25 00:00:251421 AddAudioTrack(track.get(), local_stream);
deadbeefab9b2d12015-10-14 18:33:111422 }
1423 for (const auto& track : local_stream->GetVideoTracks()) {
korniltsev.anatolyec390b52017-07-25 00:00:251424 AddVideoTrack(track.get(), local_stream);
deadbeefab9b2d12015-10-14 18:33:111425 }
1426
tommi@webrtc.org03505bc2014-07-14 20:15:261427 stats_->AddStream(local_stream);
Guido Urdaneta70c2db12019-04-16 10:24:141428 UpdateNegotiationNeeded();
henrike@webrtc.org28e20752013-07-10 00:45:361429 return true;
1430}
1431
1432void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) {
Karl Wiberg744310f2019-02-14 09:18:561433 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 21:48:581434 RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified "
1435 "Plan SdpSemantics. Please use RemoveTrack "
1436 "instead.";
Peter Boström1a9d6152015-12-08 21:15:171437 TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream");
korniltsev.anatolyec390b52017-07-25 00:00:251438 if (!IsClosed()) {
1439 for (const auto& track : local_stream->GetAudioTracks()) {
1440 RemoveAudioTrack(track.get(), local_stream);
1441 }
1442 for (const auto& track : local_stream->GetVideoTracks()) {
1443 RemoveVideoTrack(track.get(), local_stream);
1444 }
deadbeefab9b2d12015-10-14 18:33:111445 }
deadbeefab9b2d12015-10-14 18:33:111446 local_streams_->RemoveStream(local_stream);
deadbeefeb459812015-12-16 03:24:431447 stream_observers_.erase(
1448 std::remove_if(
1449 stream_observers_.begin(), stream_observers_.end(),
kwibergd1fe2812016-04-27 13:47:291450 [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) {
Seth Hampson13b8bad2018-03-13 23:05:281451 return observer->stream()->id().compare(local_stream->id()) == 0;
deadbeefeb459812015-12-16 03:24:431452 }),
1453 stream_observers_.end());
deadbeefab9b2d12015-10-14 18:33:111454
henrike@webrtc.org28e20752013-07-10 00:45:361455 if (IsClosed()) {
1456 return;
1457 }
Guido Urdaneta70c2db12019-04-16 10:24:141458 UpdateNegotiationNeeded();
henrike@webrtc.org28e20752013-07-10 00:45:361459}
1460
Steve Anton2d6c76a2018-01-06 01:10:521461RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack(
Steve Antonf9381f02017-12-14 18:23:571462 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 19:34:101463 const std::vector<std::string>& stream_ids) {
Karl Wiberg744310f2019-02-14 09:18:561464 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton2d6c76a2018-01-06 01:10:521465 TRACE_EVENT0("webrtc", "PeerConnection::AddTrack");
Steve Antonf9381f02017-12-14 18:23:571466 if (!track) {
1467 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null.");
1468 }
1469 if (!(track->kind() == MediaStreamTrackInterface::kAudioKind ||
1470 track->kind() == MediaStreamTrackInterface::kVideoKind)) {
1471 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1472 "Track has invalid kind: " + track->kind());
1473 }
Steve Antonf9381f02017-12-14 18:23:571474 if (IsClosed()) {
1475 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
1476 "PeerConnection is closed.");
deadbeefe1f9d832016-01-14 23:35:421477 }
Steve Anton4171afb2017-11-20 18:20:221478 if (FindSenderForTrack(track)) {
Steve Antonf9381f02017-12-14 18:23:571479 LOG_AND_RETURN_ERROR(
1480 RTCErrorType::INVALID_PARAMETER,
1481 "Sender already exists for track " + track->id() + ".");
deadbeefe1f9d832016-01-14 23:35:421482 }
Steve Antonf9381f02017-12-14 18:23:571483 auto sender_or_error =
Seth Hampson5b4f0752018-04-02 23:31:361484 (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids)
1485 : AddTrackPlanB(track, stream_ids));
Steve Antonf9381f02017-12-14 18:23:571486 if (sender_or_error.ok()) {
Guido Urdaneta70c2db12019-04-16 10:24:141487 UpdateNegotiationNeeded();
Steve Anton43a723a2018-01-04 23:48:171488 stats_->AddTrack(track);
Steve Antonf9381f02017-12-14 18:23:571489 }
1490 return sender_or_error;
1491}
deadbeefe1f9d832016-01-14 23:35:421492
Steve Antonf9381f02017-12-14 18:23:571493RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>>
1494PeerConnection::AddTrackPlanB(
1495 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 19:34:101496 const std::vector<std::string>& stream_ids) {
Seth Hampson5b4f0752018-04-02 23:31:361497 if (stream_ids.size() > 1u) {
1498 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION,
1499 "AddTrack with more than one stream is not "
1500 "supported with Plan B semantics.");
1501 }
1502 std::vector<std::string> adjusted_stream_ids = stream_ids;
1503 if (adjusted_stream_ids.empty()) {
1504 adjusted_stream_ids.push_back(rtc::CreateRandomUuid());
1505 }
Steve Anton02ee47c2018-01-11 00:26:061506 cricket::MediaType media_type =
1507 (track->kind() == MediaStreamTrackInterface::kAudioKind
1508 ? cricket::MEDIA_TYPE_AUDIO
1509 : cricket::MEDIA_TYPE_VIDEO);
Steve Anton111fdfd2018-06-25 20:03:361510 auto new_sender =
Florent Castelli892acf02018-10-01 20:47:201511 CreateSender(media_type, track->id(), track, adjusted_stream_ids, {});
deadbeefe1f9d832016-01-14 23:35:421512 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
Amit Hilbuchdd9390c2018-11-14 00:26:051513 new_sender->internal()->SetMediaChannel(voice_media_channel());
Steve Anton4171afb2017-11-20 18:20:221514 GetAudioTransceiver()->internal()->AddSender(new_sender);
Steve Anton4171afb2017-11-20 18:20:221515 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609eaa2018-03-27 21:57:181516 FindSenderInfo(local_audio_sender_infos_,
Seth Hampson5b4f0752018-04-02 23:31:361517 new_sender->internal()->stream_ids()[0], track->id());
Steve Anton4171afb2017-11-20 18:20:221518 if (sender_info) {
1519 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
deadbeefe1f9d832016-01-14 23:35:421520 }
Steve Antonf9381f02017-12-14 18:23:571521 } else {
1522 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
Amit Hilbuchdd9390c2018-11-14 00:26:051523 new_sender->internal()->SetMediaChannel(video_media_channel());
Steve Anton4171afb2017-11-20 18:20:221524 GetVideoTransceiver()->internal()->AddSender(new_sender);
Steve Anton4171afb2017-11-20 18:20:221525 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609eaa2018-03-27 21:57:181526 FindSenderInfo(local_video_sender_infos_,
Seth Hampson5b4f0752018-04-02 23:31:361527 new_sender->internal()->stream_ids()[0], track->id());
Steve Anton4171afb2017-11-20 18:20:221528 if (sender_info) {
1529 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
deadbeefe1f9d832016-01-14 23:35:421530 }
deadbeefe1f9d832016-01-14 23:35:421531 }
Steve Anton02ee47c2018-01-11 00:26:061532 return rtc::scoped_refptr<RtpSenderInterface>(new_sender);
Steve Antonf9381f02017-12-14 18:23:571533}
deadbeefe1f9d832016-01-14 23:35:421534
Steve Antonf9381f02017-12-14 18:23:571535RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>>
1536PeerConnection::AddTrackUnifiedPlan(
1537 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 19:34:101538 const std::vector<std::string>& stream_ids) {
Steve Antonf9381f02017-12-14 18:23:571539 auto transceiver = FindFirstTransceiverForAddedTrack(track);
1540 if (transceiver) {
Steve Anton3d954a62018-04-02 18:27:231541 RTC_LOG(LS_INFO) << "Reusing an existing "
1542 << cricket::MediaTypeToString(transceiver->media_type())
1543 << " transceiver for AddTrack.";
Harald Alvestrand6060df52020-08-11 07:54:021544 if (transceiver->stopping()) {
1545 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1546 "The existing transceiver is stopping.");
1547 }
1548
Steve Antonf9381f02017-12-14 18:23:571549 if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) {
Steve Anton52d86772018-02-20 23:48:121550 transceiver->internal()->set_direction(
1551 RtpTransceiverDirection::kSendRecv);
Steve Antonf9381f02017-12-14 18:23:571552 } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) {
Steve Anton52d86772018-02-20 23:48:121553 transceiver->internal()->set_direction(
1554 RtpTransceiverDirection::kSendOnly);
Steve Antonf9381f02017-12-14 18:23:571555 }
Steve Anton02ee47c2018-01-11 00:26:061556 transceiver->sender()->SetTrack(track);
Henrik Andreasssoncc189172019-05-20 09:01:381557 transceiver->internal()->sender_internal()->set_stream_ids(stream_ids);
Eldar Rello353a7182019-11-25 16:49:441558 transceiver->internal()->set_reused_for_addtrack(true);
Steve Antonf9381f02017-12-14 18:23:571559 } else {
1560 cricket::MediaType media_type =
1561 (track->kind() == MediaStreamTrackInterface::kAudioKind
1562 ? cricket::MEDIA_TYPE_AUDIO
1563 : cricket::MEDIA_TYPE_VIDEO);
Steve Anton3d954a62018-04-02 18:27:231564 RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type)
1565 << " transceiver in response to a call to AddTrack.";
Steve Anton07563732018-06-26 18:13:501566 std::string sender_id = track->id();
1567 // Avoid creating a sender with an existing ID by generating a random ID.
1568 // This can happen if this is the second time AddTrack has created a sender
1569 // for this track.
1570 if (FindSenderById(sender_id)) {
1571 sender_id = rtc::CreateRandomUuid();
1572 }
Florent Castelli892acf02018-10-01 20:47:201573 auto sender = CreateSender(media_type, sender_id, track, stream_ids, {});
Steve Anton02ee47c2018-01-11 00:26:061574 auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid());
1575 transceiver = CreateAndAddTransceiver(sender, receiver);
Steve Antonf9381f02017-12-14 18:23:571576 transceiver->internal()->set_created_by_addtrack(true);
Steve Anton52d86772018-02-20 23:48:121577 transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv);
Steve Antonf9381f02017-12-14 18:23:571578 }
Steve Antonf9381f02017-12-14 18:23:571579 return transceiver->sender();
1580}
1581
1582rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1583PeerConnection::FindFirstTransceiverForAddedTrack(
1584 rtc::scoped_refptr<MediaStreamTrackInterface> track) {
1585 RTC_DCHECK(track);
1586 for (auto transceiver : transceivers_) {
1587 if (!transceiver->sender()->track() &&
Steve Anton69470252018-02-09 19:43:081588 cricket::MediaTypeToString(transceiver->media_type()) ==
Steve Antonf9381f02017-12-14 18:23:571589 track->kind() &&
Seth Hampson2f0d7022018-02-20 19:54:421590 !transceiver->internal()->has_ever_been_used_to_send() &&
1591 !transceiver->stopped()) {
Steve Antonf9381f02017-12-14 18:23:571592 return transceiver;
1593 }
1594 }
1595 return nullptr;
deadbeefe1f9d832016-01-14 23:35:421596}
1597
1598bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) {
1599 TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack");
Steve Anton24db5732018-07-23 17:27:331600 return RemoveTrackNew(sender).ok();
Steve Antonf9381f02017-12-14 18:23:571601}
1602
Steve Anton24db5732018-07-23 17:27:331603RTCError PeerConnection::RemoveTrackNew(
Steve Antonf9381f02017-12-14 18:23:571604 rtc::scoped_refptr<RtpSenderInterface> sender) {
Karl Wiberg744310f2019-02-14 09:18:561605 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 18:23:571606 if (!sender) {
1607 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null.");
1608 }
deadbeefe1f9d832016-01-14 23:35:421609 if (IsClosed()) {
Steve Antonf9381f02017-12-14 18:23:571610 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
1611 "PeerConnection is closed.");
deadbeefe1f9d832016-01-14 23:35:421612 }
Steve Antonf9381f02017-12-14 18:23:571613 if (IsUnifiedPlan()) {
1614 auto transceiver = FindTransceiverBySender(sender);
1615 if (!transceiver || !sender->track()) {
1616 return RTCError::OK();
1617 }
1618 sender->SetTrack(nullptr);
1619 if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) {
Steve Anton52d86772018-02-20 23:48:121620 transceiver->internal()->set_direction(
1621 RtpTransceiverDirection::kRecvOnly);
Steve Antonf9381f02017-12-14 18:23:571622 } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) {
Steve Anton52d86772018-02-20 23:48:121623 transceiver->internal()->set_direction(
1624 RtpTransceiverDirection::kInactive);
Steve Antonf9381f02017-12-14 18:23:571625 }
Steve Anton4171afb2017-11-20 18:20:221626 } else {
Steve Antonf9381f02017-12-14 18:23:571627 bool removed;
1628 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
1629 removed = GetAudioTransceiver()->internal()->RemoveSender(sender);
1630 } else {
1631 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type());
1632 removed = GetVideoTransceiver()->internal()->RemoveSender(sender);
1633 }
1634 if (!removed) {
1635 LOG_AND_RETURN_ERROR(
1636 RTCErrorType::INVALID_PARAMETER,
1637 "Couldn't find sender " + sender->id() + " to remove.");
1638 }
Steve Anton4171afb2017-11-20 18:20:221639 }
Guido Urdaneta70c2db12019-04-16 10:24:141640 UpdateNegotiationNeeded();
Steve Antonf9381f02017-12-14 18:23:571641 return RTCError::OK();
1642}
1643
1644rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1645PeerConnection::FindTransceiverBySender(
1646 rtc::scoped_refptr<RtpSenderInterface> sender) {
1647 for (auto transceiver : transceivers_) {
1648 if (transceiver->sender() == sender) {
1649 return transceiver;
1650 }
1651 }
1652 return nullptr;
deadbeefe1f9d832016-01-14 23:35:421653}
1654
Steve Anton9158ef62017-11-27 21:01:521655RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1656PeerConnection::AddTransceiver(
1657 rtc::scoped_refptr<MediaStreamTrackInterface> track) {
1658 return AddTransceiver(track, RtpTransceiverInit());
1659}
1660
1661RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1662PeerConnection::AddTransceiver(
1663 rtc::scoped_refptr<MediaStreamTrackInterface> track,
1664 const RtpTransceiverInit& init) {
Karl Wiberg744310f2019-02-14 09:18:561665 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 21:48:581666 RTC_CHECK(IsUnifiedPlan())
1667 << "AddTransceiver is only available with Unified Plan SdpSemantics";
Steve Anton9158ef62017-11-27 21:01:521668 if (!track) {
1669 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null");
1670 }
1671 cricket::MediaType media_type;
1672 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
1673 media_type = cricket::MEDIA_TYPE_AUDIO;
1674 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
1675 media_type = cricket::MEDIA_TYPE_VIDEO;
1676 } else {
1677 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1678 "Track kind is not audio or video");
1679 }
1680 return AddTransceiver(media_type, track, init);
1681}
1682
1683RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1684PeerConnection::AddTransceiver(cricket::MediaType media_type) {
1685 return AddTransceiver(media_type, RtpTransceiverInit());
1686}
1687
1688RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1689PeerConnection::AddTransceiver(cricket::MediaType media_type,
1690 const RtpTransceiverInit& init) {
Karl Wiberg744310f2019-02-14 09:18:561691 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 21:48:581692 RTC_CHECK(IsUnifiedPlan())
1693 << "AddTransceiver is only available with Unified Plan SdpSemantics";
Steve Anton9158ef62017-11-27 21:01:521694 if (!(media_type == cricket::MEDIA_TYPE_AUDIO ||
1695 media_type == cricket::MEDIA_TYPE_VIDEO)) {
1696 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1697 "media type is not audio or video");
1698 }
1699 return AddTransceiver(media_type, nullptr, init);
1700}
1701
1702RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1703PeerConnection::AddTransceiver(
1704 cricket::MediaType media_type,
1705 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 21:58:071706 const RtpTransceiverInit& init,
Guido Urdaneta70c2db12019-04-16 10:24:141707 bool update_negotiation_needed) {
Steve Anton9158ef62017-11-27 21:01:521708 RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO ||
1709 media_type == cricket::MEDIA_TYPE_VIDEO));
1710 if (track) {
1711 RTC_DCHECK_EQ(media_type,
1712 (track->kind() == MediaStreamTrackInterface::kAudioKind
1713 ? cricket::MEDIA_TYPE_AUDIO
1714 : cricket::MEDIA_TYPE_VIDEO));
1715 }
1716
Amit Hilbuche2a284d2019-03-05 20:36:311717 RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings,
1718 init.send_encodings.size(), 0, 7, 8);
1719
Amit Hilbuchaa584152019-02-07 01:09:521720 size_t num_rids = absl::c_count_if(init.send_encodings,
1721 [](const RtpEncodingParameters& encoding) {
1722 return !encoding.rid.empty();
1723 });
1724 if (num_rids > 0 && num_rids != init.send_encodings.size()) {
Amit Hilbuchce470aa2019-02-07 01:09:521725 LOG_AND_RETURN_ERROR(
Amit Hilbuchaa584152019-02-07 01:09:521726 RTCErrorType::INVALID_PARAMETER,
1727 "RIDs must be provided for either all or none of the send encodings.");
Emircan Uysaler78323432019-02-08 20:41:391728 }
1729
Amit Hilbuchf4770402019-04-08 21:11:571730 if (num_rids > 0 && absl::c_any_of(init.send_encodings,
1731 [](const RtpEncodingParameters& encoding) {
1732 return !IsLegalRsidName(encoding.rid);
1733 })) {
1734 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1735 "Invalid RID value provided.");
1736 }
1737
Amit Hilbuchaa584152019-02-07 01:09:521738 if (absl::c_any_of(init.send_encodings,
1739 [](const RtpEncodingParameters& encoding) {
1740 return encoding.ssrc.has_value();
1741 })) {
1742 LOG_AND_RETURN_ERROR(
1743 RTCErrorType::UNSUPPORTED_PARAMETER,
1744 "Attempted to set an unimplemented parameter of RtpParameters.");
Florent Castelli892acf02018-10-01 20:47:201745 }
1746
1747 RtpParameters parameters;
1748 parameters.encodings = init.send_encodings;
Amit Hilbuchaa584152019-02-07 01:09:521749
1750 // Encodings are dropped from the tail if too many are provided.
1751 if (parameters.encodings.size() > kMaxSimulcastStreams) {
1752 parameters.encodings.erase(
1753 parameters.encodings.begin() + kMaxSimulcastStreams,
1754 parameters.encodings.end());
1755 }
1756
1757 // Single RID should be removed.
1758 if (parameters.encodings.size() == 1 &&
1759 !parameters.encodings[0].rid.empty()) {
1760 RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << ".";
1761 parameters.encodings[0].rid.clear();
1762 }
1763
1764 // If RIDs were not provided, they are generated for simulcast scenario.
1765 if (parameters.encodings.size() > 1 && num_rids == 0) {
1766 rtc::UniqueStringGenerator rid_generator;
1767 for (RtpEncodingParameters& encoding : parameters.encodings) {
1768 encoding.rid = rid_generator();
1769 }
1770 }
1771
Florent Castelli892acf02018-10-01 20:47:201772 if (UnimplementedRtpParameterHasValue(parameters)) {
1773 LOG_AND_RETURN_ERROR(
1774 RTCErrorType::UNSUPPORTED_PARAMETER,
1775 "Attempted to set an unimplemented parameter of RtpParameters.");
1776 }
Steve Anton9158ef62017-11-27 21:01:521777
Florent Castellic1a0bcb2019-01-29 13:26:481778 auto result = cricket::CheckRtpParametersValues(parameters);
1779 if (!result.ok()) {
1780 LOG_AND_RETURN_ERROR(result.type(), result.message());
1781 }
1782
Steve Anton3d954a62018-04-02 18:27:231783 RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type)
1784 << " transceiver in response to a call to AddTransceiver.";
Steve Anton07563732018-06-26 18:13:501785 // Set the sender ID equal to the track ID if the track is specified unless
1786 // that sender ID is already in use.
1787 std::string sender_id =
1788 (track && !FindSenderById(track->id()) ? track->id()
1789 : rtc::CreateRandomUuid());
Florent Castelli892acf02018-10-01 20:47:201790 auto sender = CreateSender(media_type, sender_id, track, init.stream_ids,
Amit Hilbuchaa584152019-02-07 01:09:521791 parameters.encodings);
Steve Anton02ee47c2018-01-11 00:26:061792 auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid());
1793 auto transceiver = CreateAndAddTransceiver(sender, receiver);
1794 transceiver->internal()->set_direction(init.direction);
1795
Guido Urdaneta70c2db12019-04-16 10:24:141796 if (update_negotiation_needed) {
1797 UpdateNegotiationNeeded();
Steve Anton22da89f2018-01-25 21:58:071798 }
Steve Antonf9381f02017-12-14 18:23:571799
1800 return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver);
1801}
1802
Steve Anton02ee47c2018-01-11 00:26:061803rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
1804PeerConnection::CreateSender(
1805 cricket::MediaType media_type,
Steve Anton111fdfd2018-06-25 20:03:361806 const std::string& id,
Steve Anton02ee47c2018-01-11 00:26:061807 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Florent Castelli892acf02018-10-01 20:47:201808 const std::vector<std::string>& stream_ids,
1809 const std::vector<RtpEncodingParameters>& send_encodings) {
Karl Wiberg6cab5c82019-03-26 08:57:011810 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton9158ef62017-11-27 21:01:521811 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender;
Steve Anton02ee47c2018-01-11 00:26:061812 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1813 RTC_DCHECK(!track ||
1814 (track->kind() == MediaStreamTrackInterface::kAudioKind));
1815 sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1816 signaling_thread(),
Guido Urdaneta1ff16c82019-05-20 17:31:531817 AudioRtpSender::Create(worker_thread(), id, stats_.get(), this));
Harald Alvestrand8ebba742018-05-31 12:00:341818 NoteUsageEvent(UsageEvent::AUDIO_ADDED);
Steve Anton02ee47c2018-01-11 00:26:061819 } else {
1820 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
1821 RTC_DCHECK(!track ||
1822 (track->kind() == MediaStreamTrackInterface::kVideoKind));
1823 sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Guido Urdaneta1ff16c82019-05-20 17:31:531824 signaling_thread(), VideoRtpSender::Create(worker_thread(), id, this));
Harald Alvestrand8ebba742018-05-31 12:00:341825 NoteUsageEvent(UsageEvent::VIDEO_ADDED);
Steve Anton02ee47c2018-01-11 00:26:061826 }
Steve Anton111fdfd2018-06-25 20:03:361827 bool set_track_succeeded = sender->SetTrack(track);
1828 RTC_DCHECK(set_track_succeeded);
Henrik Andreasssoncc189172019-05-20 09:01:381829 sender->internal()->set_stream_ids(stream_ids);
Florent Castelli892acf02018-10-01 20:47:201830 sender->internal()->set_init_send_encodings(send_encodings);
Steve Anton02ee47c2018-01-11 00:26:061831 return sender;
1832}
1833
1834rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1835PeerConnection::CreateReceiver(cricket::MediaType media_type,
1836 const std::string& receiver_id) {
Steve Anton9158ef62017-11-27 21:01:521837 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1838 receiver;
Steve Anton9158ef62017-11-27 21:01:521839 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton9158ef62017-11-27 21:01:521840 receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
Henrik Boström199e27b2018-07-04 18:51:531841 signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id,
1842 std::vector<std::string>({})));
Harald Alvestrand8ebba742018-05-31 12:00:341843 NoteUsageEvent(UsageEvent::AUDIO_ADDED);
Steve Anton9158ef62017-11-27 21:01:521844 } else {
Steve Anton02ee47c2018-01-11 00:26:061845 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
Steve Anton9158ef62017-11-27 21:01:521846 receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
Henrik Boström199e27b2018-07-04 18:51:531847 signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id,
1848 std::vector<std::string>({})));
Harald Alvestrand8ebba742018-05-31 12:00:341849 NoteUsageEvent(UsageEvent::VIDEO_ADDED);
Steve Anton9158ef62017-11-27 21:01:521850 }
Steve Anton02ee47c2018-01-11 00:26:061851 return receiver;
1852}
1853
1854rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1855PeerConnection::CreateAndAddTransceiver(
1856 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
1857 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1858 receiver) {
Steve Anton07563732018-06-26 18:13:501859 // Ensure that the new sender does not have an ID that is already in use by
1860 // another sender.
1861 // Allow receiver IDs to conflict since those come from remote SDP (which
1862 // could be invalid, but should not cause a crash).
1863 RTC_DCHECK(!FindSenderById(sender->id()));
Steve Anton02ee47c2018-01-11 00:26:061864 auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create(
Florent Castelli2d9d82e2019-04-23 17:25:511865 signaling_thread(),
Markus Handell0357b3e2020-03-16 12:40:511866 new RtpTransceiver(
1867 sender, receiver, channel_manager(),
1868 sender->media_type() == cricket::MEDIA_TYPE_AUDIO
1869 ? channel_manager()->GetSupportedAudioRtpHeaderExtensions()
1870 : channel_manager()->GetSupportedVideoRtpHeaderExtensions()));
Steve Anton9158ef62017-11-27 21:01:521871 transceivers_.push_back(transceiver);
Steve Anton52d86772018-02-20 23:48:121872 transceiver->internal()->SignalNegotiationNeeded.connect(
1873 this, &PeerConnection::OnNegotiationNeeded);
Steve Antonf9381f02017-12-14 18:23:571874 return transceiver;
Steve Anton9158ef62017-11-27 21:01:521875}
1876
Steve Anton52d86772018-02-20 23:48:121877void PeerConnection::OnNegotiationNeeded() {
1878 RTC_DCHECK_RUN_ON(signaling_thread());
1879 RTC_DCHECK(!IsClosed());
Guido Urdaneta70c2db12019-04-16 10:24:141880 UpdateNegotiationNeeded();
Steve Anton52d86772018-02-20 23:48:121881}
1882
deadbeeffac06552015-11-25 19:26:011883rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender(
deadbeefbd7d8f72015-12-19 00:58:441884 const std::string& kind,
1885 const std::string& stream_id) {
Karl Wiberg5966c502019-02-21 22:55:091886 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 21:48:581887 RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified "
1888 "Plan SdpSemantics. Please use AddTransceiver "
1889 "instead.";
Peter Boström1a9d6152015-12-08 21:15:171890 TRACE_EVENT0("webrtc", "PeerConnection::CreateSender");
zhihuang29ff8442016-07-27 18:07:251891 if (IsClosed()) {
1892 return nullptr;
1893 }
Steve Anton4171afb2017-11-20 18:20:221894
Seth Hampson5b4f0752018-04-02 23:31:361895 // Internally we need to have one stream with Plan B semantics, so we
1896 // generate a random stream ID if not specified.
Seth Hampson845e8782018-03-02 19:34:101897 std::vector<std::string> stream_ids;
Seth Hampson5b4f0752018-04-02 23:31:361898 if (stream_id.empty()) {
1899 stream_ids.push_back(rtc::CreateRandomUuid());
1900 RTC_LOG(LS_INFO)
1901 << "No stream_id specified for sender. Generated stream ID: "
1902 << stream_ids[0];
1903 } else {
Seth Hampson845e8782018-03-02 19:34:101904 stream_ids.push_back(stream_id);
Steve Anton02ee47c2018-01-11 00:26:061905 }
1906
Steve Anton4171afb2017-11-20 18:20:221907 // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver.
deadbeefa601f5c2016-06-06 21:27:391908 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender;
deadbeeffac06552015-11-25 19:26:011909 if (kind == MediaStreamTrackInterface::kAudioKind) {
Amit Hilbuchea7ef2a2019-02-19 23:20:211910 auto audio_sender = AudioRtpSender::Create(
Guido Urdaneta1ff16c82019-05-20 17:31:531911 worker_thread(), rtc::CreateRandomUuid(), stats_.get(), this);
Amit Hilbuchdd9390c2018-11-14 00:26:051912 audio_sender->SetMediaChannel(voice_media_channel());
deadbeefa601f5c2016-06-06 21:27:391913 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Steve Anton02ee47c2018-01-11 00:26:061914 signaling_thread(), audio_sender);
Steve Anton4171afb2017-11-20 18:20:221915 GetAudioTransceiver()->internal()->AddSender(new_sender);
deadbeeffac06552015-11-25 19:26:011916 } else if (kind == MediaStreamTrackInterface::kVideoKind) {
Amit Hilbuchea7ef2a2019-02-19 23:20:211917 auto video_sender =
Guido Urdaneta1ff16c82019-05-20 17:31:531918 VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), this);
Amit Hilbuchdd9390c2018-11-14 00:26:051919 video_sender->SetMediaChannel(video_media_channel());
deadbeefa601f5c2016-06-06 21:27:391920 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Steve Anton02ee47c2018-01-11 00:26:061921 signaling_thread(), video_sender);
Steve Anton4171afb2017-11-20 18:20:221922 GetVideoTransceiver()->internal()->AddSender(new_sender);
deadbeeffac06552015-11-25 19:26:011923 } else {
Mirko Bonadei675513b2017-11-09 10:09:251924 RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind;
Steve Anton4171afb2017-11-20 18:20:221925 return nullptr;
deadbeeffac06552015-11-25 19:26:011926 }
Henrik Andreasssoncc189172019-05-20 09:01:381927 new_sender->internal()->set_stream_ids(stream_ids);
Steve Anton4171afb2017-11-20 18:20:221928
deadbeefe1f9d832016-01-14 23:35:421929 return new_sender;
deadbeeffac06552015-11-25 19:26:011930}
1931
deadbeef70ab1a12015-09-28 23:53:551932std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders()
1933 const {
Karl Wiberga58e1692019-03-26 12:33:431934 RTC_DCHECK_RUN_ON(signaling_thread());
deadbeefa601f5c2016-06-06 21:27:391935 std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret;
Mirko Bonadei739baf02019-01-27 16:29:421936 for (const auto& sender : GetSendersInternal()) {
Steve Anton4171afb2017-11-20 18:20:221937 ret.push_back(sender);
deadbeefa601f5c2016-06-06 21:27:391938 }
1939 return ret;
deadbeef70ab1a12015-09-28 23:53:551940}
1941
Steve Anton4171afb2017-11-20 18:20:221942std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
1943PeerConnection::GetSendersInternal() const {
1944 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
1945 all_senders;
Mirko Bonadei739baf02019-01-27 16:29:421946 for (const auto& transceiver : transceivers_) {
Harald Alvestrand6060df52020-08-11 07:54:021947 if (IsUnifiedPlan() && transceiver->internal()->stopped())
1948 continue;
1949
Steve Anton4171afb2017-11-20 18:20:221950 auto senders = transceiver->internal()->senders();
1951 all_senders.insert(all_senders.end(), senders.begin(), senders.end());
1952 }
1953 return all_senders;
1954}
1955
deadbeef70ab1a12015-09-28 23:53:551956std::vector<rtc::scoped_refptr<RtpReceiverInterface>>
1957PeerConnection::GetReceivers() const {
Karl Wiberga58e1692019-03-26 12:33:431958 RTC_DCHECK_RUN_ON(signaling_thread());
deadbeefa601f5c2016-06-06 21:27:391959 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret;
Steve Anton4171afb2017-11-20 18:20:221960 for (const auto& receiver : GetReceiversInternal()) {
1961 ret.push_back(receiver);
deadbeefa601f5c2016-06-06 21:27:391962 }
1963 return ret;
deadbeef70ab1a12015-09-28 23:53:551964}
1965
Steve Anton4171afb2017-11-20 18:20:221966std::vector<
1967 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
1968PeerConnection::GetReceiversInternal() const {
1969 std::vector<
1970 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
1971 all_receivers;
Mirko Bonadei739baf02019-01-27 16:29:421972 for (const auto& transceiver : transceivers_) {
Harald Alvestrand6060df52020-08-11 07:54:021973 if (IsUnifiedPlan() && transceiver->internal()->stopped())
1974 continue;
1975
Steve Anton4171afb2017-11-20 18:20:221976 auto receivers = transceiver->internal()->receivers();
1977 all_receivers.insert(all_receivers.end(), receivers.begin(),
1978 receivers.end());
1979 }
1980 return all_receivers;
1981}
1982
Steve Anton9158ef62017-11-27 21:01:521983std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>
1984PeerConnection::GetTransceivers() const {
Karl Wiberg5966c502019-02-21 22:55:091985 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 21:48:581986 RTC_CHECK(IsUnifiedPlan())
1987 << "GetTransceivers is only supported with Unified Plan SdpSemantics.";
Steve Anton9158ef62017-11-27 21:01:521988 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers;
Mirko Bonadei739baf02019-01-27 16:29:421989 for (const auto& transceiver : transceivers_) {
Harald Alvestrand6060df52020-08-11 07:54:021990 // Temporary fix: Do not show stopped transceivers.
1991 // The long term fix is to remove them from transceivers_, but this
1992 // turns out to cause issues with audio channel lifetimes.
1993 // TODO(https://crbug.com/webrtc/11840): Fix issue.
1994 if (!transceiver->stopped()) {
1995 all_transceivers.push_back(transceiver);
1996 }
Steve Anton9158ef62017-11-27 21:01:521997 }
1998 return all_transceivers;
1999}
2000
henrike@webrtc.org28e20752013-07-10 00:45:362001bool PeerConnection::GetStats(StatsObserver* observer,
wu@webrtc.orgb9a088b2014-02-13 23:18:492002 MediaStreamTrackInterface* track,
2003 StatsOutputLevel level) {
Peter Boström1a9d6152015-12-08 21:15:172004 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
Karl Wiberg6cab5c82019-03-26 08:57:012005 RTC_DCHECK_RUN_ON(signaling_thread());
nisse7ce109a2017-01-31 08:57:562006 if (!observer) {
Mirko Bonadei675513b2017-11-09 10:09:252007 RTC_LOG(LS_ERROR) << "GetStats - observer is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:362008 return false;
2009 }
2010
tommi@webrtc.org03505bc2014-07-14 20:15:262011 stats_->UpdateStats(level);
zhihuange9e94c32016-11-04 18:38:152012 // The StatsCollector is used to tell if a track is valid because it may
2013 // remember tracks that the PeerConnection previously removed.
2014 if (track && !stats_->IsValidTrack(track->id())) {
Mirko Bonadei675513b2017-11-09 10:09:252015 RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: "
2016 << track->id();
zhihuange9e94c32016-11-04 18:38:152017 return false;
2018 }
Steve Antonad182762018-09-05 20:22:402019 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS,
2020 new GetStatsMsg(observer, track));
henrike@webrtc.org28e20752013-07-10 00:45:362021 return true;
2022}
2023
hbos74e1a4f2016-09-16 06:33:012024void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) {
Henrik Boström1df1bf82018-03-20 12:24:202025 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
Karl Wiberg6cab5c82019-03-26 08:57:012026 RTC_DCHECK_RUN_ON(signaling_thread());
hbos74e1a4f2016-09-16 06:33:012027 RTC_DCHECK(stats_collector_);
Henrik Boström1df1bf82018-03-20 12:24:202028 RTC_DCHECK(callback);
hbos74e1a4f2016-09-16 06:33:012029 stats_collector_->GetStatsReport(callback);
2030}
2031
Henrik Boström1df1bf82018-03-20 12:24:202032void PeerConnection::GetStats(
2033 rtc::scoped_refptr<RtpSenderInterface> selector,
2034 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
2035 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
Karl Wiberg6cab5c82019-03-26 08:57:012036 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boström1df1bf82018-03-20 12:24:202037 RTC_DCHECK(callback);
2038 RTC_DCHECK(stats_collector_);
2039 rtc::scoped_refptr<RtpSenderInternal> internal_sender;
2040 if (selector) {
2041 for (const auto& proxy_transceiver : transceivers_) {
2042 for (const auto& proxy_sender :
2043 proxy_transceiver->internal()->senders()) {
2044 if (proxy_sender == selector) {
2045 internal_sender = proxy_sender->internal();
2046 break;
2047 }
2048 }
2049 if (internal_sender)
2050 break;
2051 }
2052 }
Sebastian Jansson6eb8a162018-11-16 10:29:552053 // If there is no |internal_sender| then |selector| is either null or does not
Henrik Boström1df1bf82018-03-20 12:24:202054 // belong to the PeerConnection (in Plan B, senders can be removed from the
2055 // PeerConnection). This means that "all the stats objects representing the
2056 // selector" is an empty set. Invoking GetStatsReport() with a null selector
2057 // produces an empty stats report.
2058 stats_collector_->GetStatsReport(internal_sender, callback);
2059}
2060
2061void PeerConnection::GetStats(
2062 rtc::scoped_refptr<RtpReceiverInterface> selector,
2063 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
2064 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
Karl Wiberg6cab5c82019-03-26 08:57:012065 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boström1df1bf82018-03-20 12:24:202066 RTC_DCHECK(callback);
2067 RTC_DCHECK(stats_collector_);
2068 rtc::scoped_refptr<RtpReceiverInternal> internal_receiver;
2069 if (selector) {
2070 for (const auto& proxy_transceiver : transceivers_) {
2071 for (const auto& proxy_receiver :
2072 proxy_transceiver->internal()->receivers()) {
2073 if (proxy_receiver == selector) {
2074 internal_receiver = proxy_receiver->internal();
2075 break;
2076 }
2077 }
2078 if (internal_receiver)
2079 break;
2080 }
2081 }
Sebastian Jansson6eb8a162018-11-16 10:29:552082 // If there is no |internal_receiver| then |selector| is either null or does
Henrik Boström1df1bf82018-03-20 12:24:202083 // not belong to the PeerConnection (in Plan B, receivers can be removed from
2084 // the PeerConnection). This means that "all the stats objects representing
2085 // the selector" is an empty set. Invoking GetStatsReport() with a null
2086 // selector produces an empty stats report.
2087 stats_collector_->GetStatsReport(internal_receiver, callback);
2088}
2089
henrike@webrtc.org28e20752013-07-10 00:45:362090PeerConnectionInterface::SignalingState PeerConnection::signaling_state() {
Karl Wiberg8d2e2282019-02-17 12:00:072091 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:362092 return signaling_state_;
2093}
2094
henrike@webrtc.org28e20752013-07-10 00:45:362095PeerConnectionInterface::IceConnectionState
2096PeerConnection::ice_connection_state() {
Karl Wiberg8d2e2282019-02-17 12:00:072097 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:362098 return ice_connection_state_;
2099}
2100
Alex Loiko9289eda2018-11-23 16:18:592101PeerConnectionInterface::IceConnectionState
2102PeerConnection::standardized_ice_connection_state() {
Karl Wiberg8d2e2282019-02-17 12:00:072103 RTC_DCHECK_RUN_ON(signaling_thread());
Alex Loiko9289eda2018-11-23 16:18:592104 return standardized_ice_connection_state_;
2105}
2106
Jonas Olsson635474e2018-10-18 13:58:172107PeerConnectionInterface::PeerConnectionState
2108PeerConnection::peer_connection_state() {
Karl Wiberg8d2e2282019-02-17 12:00:072109 RTC_DCHECK_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 13:58:172110 return connection_state_;
2111}
2112
henrike@webrtc.org28e20752013-07-10 00:45:362113PeerConnectionInterface::IceGatheringState
2114PeerConnection::ice_gathering_state() {
Karl Wiberg8d2e2282019-02-17 12:00:072115 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:362116 return ice_gathering_state_;
2117}
2118
Harald Alvestrand61f74d92020-03-02 10:20:002119absl::optional<bool> PeerConnection::can_trickle_ice_candidates() {
2120 RTC_DCHECK_RUN_ON(signaling_thread());
2121 SessionDescriptionInterface* description = current_remote_description_.get();
2122 if (!description) {
2123 description = pending_remote_description_.get();
2124 }
2125 if (!description) {
2126 return absl::nullopt;
2127 }
2128 // TODO(bugs.webrtc.org/7443): Change to retrieve from session-level option.
2129 if (description->description()->transport_infos().size() < 1) {
2130 return absl::nullopt;
2131 }
2132 return description->description()->transport_infos()[0].description.HasOption(
2133 "trickle");
2134}
2135
Yves Gerey665174f2018-06-19 13:03:052136rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:362137 const std::string& label,
2138 const DataChannelInit* config) {
Karl Wiberg106d92d2019-02-14 09:17:472139 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 21:15:172140 TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel");
zhihuang9763d562016-08-05 18:14:502141
Harald Alvestrand05e4d082019-12-03 13:04:212142 bool first_datachannel = !data_channel_controller_.HasDataChannels();
jiayl@webrtc.org001fd2d2014-05-29 15:31:112143
kwibergd1fe2812016-04-27 13:47:292144 std::unique_ptr<InternalDataChannelInit> internal_config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:582145 if (config) {
2146 internal_config.reset(new InternalDataChannelInit(*config));
2147 }
Taylor Brandstetter3a034e12020-07-09 22:32:342148 rtc::scoped_refptr<DataChannelInterface> channel(
2149 data_channel_controller_.InternalCreateDataChannelWithProxy(
Harald Alvestrand00cf34c2019-12-02 08:56:022150 label, internal_config.get()));
deadbeefab9b2d12015-10-14 18:33:112151 if (!channel.get()) {
2152 return nullptr;
2153 }
henrike@webrtc.org28e20752013-07-10 00:45:362154
jiayl@webrtc.org001fd2d2014-05-29 15:31:112155 // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or
2156 // the first SCTP DataChannel.
Steve Anton75737c02017-11-06 18:37:172157 if (data_channel_type() == cricket::DCT_RTP || first_datachannel) {
Guido Urdaneta70c2db12019-04-16 10:24:142158 UpdateNegotiationNeeded();
jiayl@webrtc.org001fd2d2014-05-29 15:31:112159 }
Harald Alvestrand8ebba742018-05-31 12:00:342160 NoteUsageEvent(UsageEvent::DATA_ADDED);
Taylor Brandstetter3a034e12020-07-09 22:32:342161 return channel;
henrike@webrtc.org28e20752013-07-10 00:45:362162}
2163
Henrik Boström79b69802019-07-18 09:16:562164void PeerConnection::RestartIce() {
2165 RTC_DCHECK_RUN_ON(signaling_thread());
2166 local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions(
2167 current_local_description_.get(), pending_local_description_.get());
2168 UpdateNegotiationNeeded();
2169}
2170
henrike@webrtc.org28e20752013-07-10 00:45:362171void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer,
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:162172 const RTCOfferAnswerOptions& options) {
Karl Wiberg5966c502019-02-21 22:55:092173 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boströma3728d32019-10-28 11:09:492174 // Chain this operation. If asynchronous operations are pending on the chain,
2175 // this operation will be queued to be invoked, otherwise the contents of the
2176 // lambda will execute immediately.
2177 operations_chain_->ChainOperation(
2178 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2179 observer_refptr =
2180 rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
2181 options](std::function<void()> operations_chain_callback) {
2182 // Abort early if |this_weak_ptr| is no longer valid.
2183 if (!this_weak_ptr) {
2184 observer_refptr->OnFailure(
2185 RTCError(RTCErrorType::INTERNAL_ERROR,
2186 "CreateOffer failed because the session was shut down"));
2187 operations_chain_callback();
2188 return;
2189 }
2190 // The operation completes asynchronously when the wrapper is invoked.
2191 rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
2192 observer_wrapper(new rtc::RefCountedObject<
2193 CreateSessionDescriptionObserverOperationWrapper>(
2194 std::move(observer_refptr),
2195 std::move(operations_chain_callback)));
2196 this_weak_ptr->DoCreateOffer(options, observer_wrapper);
2197 });
2198}
2199
2200void PeerConnection::DoCreateOffer(
2201 const RTCOfferAnswerOptions& options,
2202 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
2203 RTC_DCHECK_RUN_ON(signaling_thread());
2204 TRACE_EVENT0("webrtc", "PeerConnection::DoCreateOffer");
Steve Anton8d3444d2017-10-20 22:30:512205
nisse7ce109a2017-01-31 08:57:562206 if (!observer) {
Mirko Bonadei675513b2017-11-09 10:09:252207 RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:162208 return;
2209 }
deadbeefab9b2d12015-10-14 18:33:112210
Steve Anton8d3444d2017-10-20 22:30:512211 if (IsClosed()) {
2212 std::string error = "CreateOffer called when PeerConnection is closed.";
Mirko Bonadei675513b2017-11-09 10:09:252213 RTC_LOG(LS_ERROR) << error;
Harald Alvestrand5081c0c2018-03-09 14:18:032214 PostCreateSessionDescriptionFailure(
Harald Alvestrand3725d542018-04-13 13:02:102215 observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
Steve Anton8d3444d2017-10-20 22:30:512216 return;
2217 }
2218
Steve Anton25ca0ac2019-06-25 17:40:482219 // If a session error has occurred the PeerConnection is in a possibly
2220 // inconsistent state so fail right away.
2221 if (session_error() != SessionError::kNone) {
2222 std::string error_message = GetSessionErrorMsg();
2223 RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message;
2224 PostCreateSessionDescriptionFailure(
2225 observer,
2226 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2227 return;
2228 }
2229
zhihuang1c378ed2017-08-17 21:10:502230 if (!ValidateOfferAnswerOptions(options)) {
deadbeefab9b2d12015-10-14 18:33:112231 std::string error = "CreateOffer called with invalid options.";
Mirko Bonadei675513b2017-11-09 10:09:252232 RTC_LOG(LS_ERROR) << error;
Harald Alvestrand5081c0c2018-03-09 14:18:032233 PostCreateSessionDescriptionFailure(
Harald Alvestrand3725d542018-04-13 13:02:102234 observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error)));
deadbeefab9b2d12015-10-14 18:33:112235 return;
2236 }
2237
Steve Anton22da89f2018-01-25 21:58:072238 // Legacy handling for offer_to_receive_audio and offer_to_receive_video.
2239 // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions".
2240 if (IsUnifiedPlan()) {
2241 RTCError error = HandleLegacyOfferOptions(options);
2242 if (!error.ok()) {
Harald Alvestrand5081c0c2018-03-09 14:18:032243 PostCreateSessionDescriptionFailure(observer, std::move(error));
Steve Anton22da89f2018-01-25 21:58:072244 return;
2245 }
2246 }
2247
zhihuang1c378ed2017-08-17 21:10:502248 cricket::MediaSessionOptions session_options;
2249 GetOptionsForOffer(options, &session_options);
Steve Antond25da372017-11-06 22:50:292250 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:362251}
2252
Steve Anton22da89f2018-01-25 21:58:072253RTCError PeerConnection::HandleLegacyOfferOptions(
2254 const RTCOfferAnswerOptions& options) {
2255 RTC_DCHECK(IsUnifiedPlan());
2256
2257 if (options.offer_to_receive_audio == 0) {
2258 RemoveRecvDirectionFromReceivingTransceiversOfType(
2259 cricket::MEDIA_TYPE_AUDIO);
2260 } else if (options.offer_to_receive_audio == 1) {
2261 AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2262 } else if (options.offer_to_receive_audio > 1) {
2263 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
2264 "offer_to_receive_audio > 1 is not supported.");
2265 }
2266
2267 if (options.offer_to_receive_video == 0) {
2268 RemoveRecvDirectionFromReceivingTransceiversOfType(
2269 cricket::MEDIA_TYPE_VIDEO);
2270 } else if (options.offer_to_receive_video == 1) {
2271 AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2272 } else if (options.offer_to_receive_video > 1) {
2273 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
2274 "offer_to_receive_video > 1 is not supported.");
2275 }
2276
2277 return RTCError::OK();
2278}
2279
2280void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType(
2281 cricket::MediaType media_type) {
Mirko Bonadei739baf02019-01-27 16:29:422282 for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) {
Steve Anton3d954a62018-04-02 18:27:232283 RtpTransceiverDirection new_direction =
2284 RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false);
2285 if (new_direction != transceiver->direction()) {
2286 RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type)
2287 << " transceiver (MID="
2288 << transceiver->mid().value_or("<not set>") << ") from "
2289 << RtpTransceiverDirectionToString(
2290 transceiver->direction())
2291 << " to "
2292 << RtpTransceiverDirectionToString(new_direction)
2293 << " since CreateOffer specified offer_to_receive=0";
2294 transceiver->internal()->set_direction(new_direction);
2295 }
Steve Anton22da89f2018-01-25 21:58:072296 }
2297}
2298
2299void PeerConnection::AddUpToOneReceivingTransceiverOfType(
2300 cricket::MediaType media_type) {
Karl Wiberg744310f2019-02-14 09:18:562301 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 21:58:072302 if (GetReceivingTransceiversOfType(media_type).empty()) {
Steve Anton3d954a62018-04-02 18:27:232303 RTC_LOG(LS_INFO)
2304 << "Adding one recvonly " << cricket::MediaTypeToString(media_type)
2305 << " transceiver since CreateOffer specified offer_to_receive=1";
Steve Anton22da89f2018-01-25 21:58:072306 RtpTransceiverInit init;
2307 init.direction = RtpTransceiverDirection::kRecvOnly;
Guido Urdaneta70c2db12019-04-16 10:24:142308 AddTransceiver(media_type, nullptr, init,
2309 /*update_negotiation_needed=*/false);
Steve Anton22da89f2018-01-25 21:58:072310 }
2311}
2312
2313std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
2314PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) {
2315 std::vector<
2316 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
2317 receiving_transceivers;
Mirko Bonadei739baf02019-01-27 16:29:422318 for (const auto& transceiver : transceivers_) {
Steve Anton69470252018-02-09 19:43:082319 if (!transceiver->stopped() && transceiver->media_type() == media_type &&
Steve Anton22da89f2018-01-25 21:58:072320 RtpTransceiverDirectionHasRecv(transceiver->direction())) {
2321 receiving_transceivers.push_back(transceiver);
2322 }
2323 }
2324 return receiving_transceivers;
2325}
2326
htaa2a49d92016-03-04 10:51:392327void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer,
2328 const RTCOfferAnswerOptions& options) {
Karl Wiberg8d2e2282019-02-17 12:00:072329 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boströma3728d32019-10-28 11:09:492330 // Chain this operation. If asynchronous operations are pending on the chain,
2331 // this operation will be queued to be invoked, otherwise the contents of the
2332 // lambda will execute immediately.
2333 operations_chain_->ChainOperation(
2334 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2335 observer_refptr =
2336 rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
2337 options](std::function<void()> operations_chain_callback) {
2338 // Abort early if |this_weak_ptr| is no longer valid.
2339 if (!this_weak_ptr) {
2340 observer_refptr->OnFailure(RTCError(
2341 RTCErrorType::INTERNAL_ERROR,
2342 "CreateAnswer failed because the session was shut down"));
2343 operations_chain_callback();
2344 return;
2345 }
2346 // The operation completes asynchronously when the wrapper is invoked.
2347 rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
2348 observer_wrapper(new rtc::RefCountedObject<
2349 CreateSessionDescriptionObserverOperationWrapper>(
2350 std::move(observer_refptr),
2351 std::move(operations_chain_callback)));
2352 this_weak_ptr->DoCreateAnswer(options, observer_wrapper);
2353 });
2354}
2355
2356void PeerConnection::DoCreateAnswer(
2357 const RTCOfferAnswerOptions& options,
2358 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
2359 RTC_DCHECK_RUN_ON(signaling_thread());
2360 TRACE_EVENT0("webrtc", "PeerConnection::DoCreateAnswer");
nisse7ce109a2017-01-31 08:57:562361 if (!observer) {
Mirko Bonadei675513b2017-11-09 10:09:252362 RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
htaa2a49d92016-03-04 10:51:392363 return;
2364 }
2365
Steve Anton25ca0ac2019-06-25 17:40:482366 // If a session error has occurred the PeerConnection is in a possibly
2367 // inconsistent state so fail right away.
2368 if (session_error() != SessionError::kNone) {
2369 std::string error_message = GetSessionErrorMsg();
2370 RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message;
2371 PostCreateSessionDescriptionFailure(
2372 observer,
2373 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2374 return;
2375 }
2376
Steve Antondffead82018-02-06 18:31:292377 if (!(signaling_state_ == kHaveRemoteOffer ||
2378 signaling_state_ == kHaveLocalPrAnswer)) {
2379 std::string error =
2380 "PeerConnection cannot create an answer in a state other than "
2381 "have-remote-offer or have-local-pranswer.";
Mirko Bonadei675513b2017-11-09 10:09:252382 RTC_LOG(LS_ERROR) << error;
Harald Alvestrand5081c0c2018-03-09 14:18:032383 PostCreateSessionDescriptionFailure(
Harald Alvestrand3725d542018-04-13 13:02:102384 observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
Steve Anton8d3444d2017-10-20 22:30:512385 return;
2386 }
2387
Steve Antondffead82018-02-06 18:31:292388 // The remote description should be set if we're in the right state.
2389 RTC_DCHECK(remote_description());
Steve Anton8d3444d2017-10-20 22:30:512390
Steve Anton22da89f2018-01-25 21:58:072391 if (IsUnifiedPlan()) {
2392 if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) {
2393 RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not "
2394 "supported with Unified Plan semantics. Use the "
2395 "RtpTransceiver API instead.";
2396 }
2397 if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) {
2398 RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not "
2399 "supported with Unified Plan semantics. Use the "
2400 "RtpTransceiver API instead.";
2401 }
2402 }
2403
htaa2a49d92016-03-04 10:51:392404 cricket::MediaSessionOptions session_options;
zhihuang1c378ed2017-08-17 21:10:502405 GetOptionsForAnswer(options, &session_options);
htaa2a49d92016-03-04 10:51:392406
Steve Antond25da372017-11-06 22:50:292407 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:362408}
2409
2410void PeerConnection::SetLocalDescription(
2411 SetSessionDescriptionObserver* observer,
Steve Anton80dd7b52018-02-17 01:08:422412 SessionDescriptionInterface* desc_ptr) {
Karl Wiberg5966c502019-02-21 22:55:092413 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boströma3728d32019-10-28 11:09:492414 // Chain this operation. If asynchronous operations are pending on the chain,
2415 // this operation will be queued to be invoked, otherwise the contents of the
2416 // lambda will execute immediately.
2417 operations_chain_->ChainOperation(
2418 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2419 observer_refptr =
2420 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
2421 desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
2422 std::function<void()> operations_chain_callback) mutable {
2423 // Abort early if |this_weak_ptr| is no longer valid.
2424 if (!this_weak_ptr) {
Henrik Boström831ae4e2020-07-29 10:04:002425 // For consistency with SetSessionDescriptionObserverAdapter whose
2426 // posted messages doesn't get processed when the PC is destroyed, we
2427 // do not inform |observer_refptr| that the operation failed.
Henrik Boströma3728d32019-10-28 11:09:492428 operations_chain_callback();
2429 return;
2430 }
Henrik Boström831ae4e2020-07-29 10:04:002431 // SetSessionDescriptionObserverAdapter takes care of making sure the
2432 // |observer_refptr| is invoked in a posted message.
2433 this_weak_ptr->DoSetLocalDescription(
2434 std::move(desc),
2435 rtc::scoped_refptr<SetLocalDescriptionObserverInterface>(
2436 new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>(
2437 this_weak_ptr, observer_refptr)));
Henrik Boströma3728d32019-10-28 11:09:492438 // For backwards-compatability reasons, we declare the operation as
Henrik Boström831ae4e2020-07-29 10:04:002439 // completed here (rather than in a post), so that the operation chain
2440 // is not blocked by this operation when the observer is invoked. This
2441 // allows the observer to trigger subsequent offer/answer operations
2442 // synchronously if the operation chain is now empty.
2443 operations_chain_callback();
2444 });
2445}
2446
2447void PeerConnection::SetLocalDescription(
2448 std::unique_ptr<SessionDescriptionInterface> desc,
2449 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) {
2450 RTC_DCHECK_RUN_ON(signaling_thread());
2451 // Chain this operation. If asynchronous operations are pending on the chain,
2452 // this operation will be queued to be invoked, otherwise the contents of the
2453 // lambda will execute immediately.
2454 operations_chain_->ChainOperation(
2455 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer,
2456 desc = std::move(desc)](
2457 std::function<void()> operations_chain_callback) mutable {
2458 // Abort early if |this_weak_ptr| is no longer valid.
2459 if (!this_weak_ptr) {
2460 observer->OnSetLocalDescriptionComplete(RTCError(
2461 RTCErrorType::INTERNAL_ERROR,
2462 "SetLocalDescription failed because the session was shut down"));
2463 operations_chain_callback();
2464 return;
2465 }
2466 this_weak_ptr->DoSetLocalDescription(std::move(desc), observer);
2467 // DoSetLocalDescription() is implemented as a synchronous operation.
2468 // The |observer| will already have been informed that it completed, and
2469 // we can mark this operation as complete without any loose ends.
Henrik Boströma3728d32019-10-28 11:09:492470 operations_chain_callback();
2471 });
2472}
Steve Anton8a006912017-12-04 23:25:562473
Henrik Boström4e196702019-10-30 09:35:502474void PeerConnection::SetLocalDescription(
2475 SetSessionDescriptionObserver* observer) {
2476 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boström831ae4e2020-07-29 10:04:002477 SetLocalDescription(
2478 new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>(
2479 weak_ptr_factory_.GetWeakPtr(), observer));
2480}
2481
2482void PeerConnection::SetLocalDescription(
2483 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) {
2484 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boström4e196702019-10-30 09:35:502485 // The |create_sdp_observer| handles performing DoSetLocalDescription() with
2486 // the resulting description as well as completing the operation.
2487 rtc::scoped_refptr<ImplicitCreateSessionDescriptionObserver>
2488 create_sdp_observer(
2489 new rtc::RefCountedObject<ImplicitCreateSessionDescriptionObserver>(
Henrik Boström831ae4e2020-07-29 10:04:002490 weak_ptr_factory_.GetWeakPtr(), observer));
Henrik Boström4e196702019-10-30 09:35:502491 // Chain this operation. If asynchronous operations are pending on the chain,
2492 // this operation will be queued to be invoked, otherwise the contents of the
2493 // lambda will execute immediately.
2494 operations_chain_->ChainOperation(
2495 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2496 create_sdp_observer](std::function<void()> operations_chain_callback) {
2497 // The |create_sdp_observer| is responsible for completing the
2498 // operation.
2499 create_sdp_observer->SetOperationCompleteCallback(
2500 std::move(operations_chain_callback));
2501 // Abort early if |this_weak_ptr| is no longer valid. This triggers the
2502 // same code path as if DoCreateOffer() or DoCreateAnswer() failed.
2503 if (!this_weak_ptr) {
2504 create_sdp_observer->OnFailure(RTCError(
2505 RTCErrorType::INTERNAL_ERROR,
2506 "SetLocalDescription failed because the session was shut down"));
2507 return;
2508 }
2509 switch (this_weak_ptr->signaling_state()) {
2510 case PeerConnectionInterface::kStable:
2511 case PeerConnectionInterface::kHaveLocalOffer:
2512 case PeerConnectionInterface::kHaveRemotePrAnswer:
2513 // TODO(hbos): If [LastCreatedOffer] exists and still represents the
2514 // current state of the system, use that instead of creating another
2515 // offer.
2516 this_weak_ptr->DoCreateOffer(RTCOfferAnswerOptions(),
2517 create_sdp_observer);
2518 break;
2519 case PeerConnectionInterface::kHaveLocalPrAnswer:
2520 case PeerConnectionInterface::kHaveRemoteOffer:
2521 // TODO(hbos): If [LastCreatedAnswer] exists and still represents
2522 // the current state of the system, use that instead of creating
2523 // another answer.
2524 this_weak_ptr->DoCreateAnswer(RTCOfferAnswerOptions(),
2525 create_sdp_observer);
2526 break;
2527 case PeerConnectionInterface::kClosed:
2528 create_sdp_observer->OnFailure(RTCError(
2529 RTCErrorType::INVALID_STATE,
2530 "SetLocalDescription called when PeerConnection is closed."));
2531 break;
2532 }
2533 });
2534}
2535
Henrik Boströma3728d32019-10-28 11:09:492536void PeerConnection::DoSetLocalDescription(
2537 std::unique_ptr<SessionDescriptionInterface> desc,
Henrik Boström831ae4e2020-07-29 10:04:002538 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) {
Henrik Boströma3728d32019-10-28 11:09:492539 RTC_DCHECK_RUN_ON(signaling_thread());
2540 TRACE_EVENT0("webrtc", "PeerConnection::DoSetLocalDescription");
Steve Anton80dd7b52018-02-17 01:08:422541
nisse7ce109a2017-01-31 08:57:562542 if (!observer) {
Mirko Bonadei675513b2017-11-09 10:09:252543 RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:362544 return;
2545 }
Steve Anton8a006912017-12-04 23:25:562546
henrike@webrtc.org28e20752013-07-10 00:45:362547 if (!desc) {
Henrik Boström831ae4e2020-07-29 10:04:002548 observer->OnSetLocalDescriptionComplete(
Harald Alvestrand5081c0c2018-03-09 14:18:032549 RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL."));
henrike@webrtc.org28e20752013-07-10 00:45:362550 return;
2551 }
Steve Anton8d3444d2017-10-20 22:30:512552
Steve Anton80dd7b52018-02-17 01:08:422553 // If a session error has occurred the PeerConnection is in a possibly
2554 // inconsistent state so fail right away.
2555 if (session_error() != SessionError::kNone) {
2556 std::string error_message = GetSessionErrorMsg();
2557 RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message;
Henrik Boström831ae4e2020-07-29 10:04:002558 observer->OnSetLocalDescriptionComplete(
Harald Alvestrand5081c0c2018-03-09 14:18:032559 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
Steve Anton80dd7b52018-02-17 01:08:422560 return;
2561 }
Steve Anton8d3444d2017-10-20 22:30:512562
Eldar Rello5ab79e62019-10-09 15:29:442563 // For SLD we support only explicit rollback.
2564 if (desc->GetType() == SdpType::kRollback) {
2565 if (IsUnifiedPlan()) {
Henrik Boström831ae4e2020-07-29 10:04:002566 observer->OnSetLocalDescriptionComplete(Rollback(desc->GetType()));
Eldar Rello5ab79e62019-10-09 15:29:442567 } else {
Henrik Boström831ae4e2020-07-29 10:04:002568 observer->OnSetLocalDescriptionComplete(
2569 RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
2570 "Rollback not supported in Plan B"));
Eldar Rello5ab79e62019-10-09 15:29:442571 }
2572 return;
2573 }
2574
Steve Anton80dd7b52018-02-17 01:08:422575 RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL);
2576 if (!error.ok()) {
2577 std::string error_message = GetSetDescriptionErrorMessage(
2578 cricket::CS_LOCAL, desc->GetType(), error);
2579 RTC_LOG(LS_ERROR) << error_message;
Henrik Boström831ae4e2020-07-29 10:04:002580 observer->OnSetLocalDescriptionComplete(
Harald Alvestrand5081c0c2018-03-09 14:18:032581 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
Steve Anton80dd7b52018-02-17 01:08:422582 return;
2583 }
2584
2585 // Grab the description type before moving ownership to ApplyLocalDescription,
2586 // which may destroy it before returning.
2587 const SdpType type = desc->GetType();
2588
2589 error = ApplyLocalDescription(std::move(desc));
Steve Anton8a006912017-12-04 23:25:562590 // |desc| may be destroyed at this point.
2591
2592 if (!error.ok()) {
Steve Anton80dd7b52018-02-17 01:08:422593 // If ApplyLocalDescription fails, the PeerConnection could be in an
2594 // inconsistent state, so act conservatively here and set the session error
2595 // so that future calls to SetLocalDescription/SetRemoteDescription fail.
2596 SetSessionError(SessionError::kContent, error.message());
2597 std::string error_message =
2598 GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error);
2599 RTC_LOG(LS_ERROR) << error_message;
Henrik Boström831ae4e2020-07-29 10:04:002600 observer->OnSetLocalDescriptionComplete(
Harald Alvestrand5081c0c2018-03-09 14:18:032601 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
Steve Anton8d3444d2017-10-20 22:30:512602 return;
2603 }
Steve Anton8a006912017-12-04 23:25:562604 RTC_DCHECK(local_description());
2605
Steve Antona3a92c22017-12-07 18:27:412606 if (local_description()->GetType() == SdpType::kAnswer) {
Harald Alvestrand6060df52020-08-11 07:54:022607 // 3.2.10.1: For each transceiver in the connection's set of transceivers
2608 // run the following steps:
2609 if (IsUnifiedPlan()) {
2610 for (auto it = transceivers_.begin(); it != transceivers_.end();) {
2611 const auto& transceiver = *it;
2612 // 3.2.10.1.1: If transceiver is stopped, associated with an m= section
2613 // and the associated m= section is rejected in
2614 // connection.[[CurrentLocalDescription]] or
2615 // connection.[[CurrentRemoteDescription]], remove the
2616 // transceiver from the connection's set of transceivers.
2617 if (transceiver->stopped()) {
2618 const ContentInfo* content =
2619 FindMediaSectionForTransceiver(transceiver, local_description());
2620
2621 if (content && content->rejected) {
2622 RTC_LOG(LS_INFO) << "Dissociating transceiver"
2623 << " since the media section is being recycled.";
2624 (*it)->internal()->set_mid(absl::nullopt);
2625 (*it)->internal()->set_mline_index(absl::nullopt);
2626 it = transceivers_.erase(it);
2627 } else {
2628 ++it;
2629 }
2630 } else {
2631 ++it;
2632 }
2633 }
2634 }
2635
Steve Anton8a006912017-12-04 23:25:562636 // TODO(deadbeef): We already had to hop to the network thread for
2637 // MaybeStartGathering...
2638 network_thread()->Invoke<void>(
2639 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
2640 port_allocator_.get()));
Steve Anton0ffaaa22018-02-23 18:31:302641 // Make UMA notes about what was agreed to.
2642 ReportNegotiatedSdpSemantics(*local_description());
Steve Anton8a006912017-12-04 23:25:562643 }
Guido Urdaneta70c2db12019-04-16 10:24:142644
Henrik Boströme574a312020-08-25 08:20:112645 observer->OnSetLocalDescriptionComplete(RTCError::OK());
2646 NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED);
2647
2648 // Check if negotiation is needed. We must do this after informing the
2649 // observer that SetLocalDescription() has completed to ensure negotiation is
2650 // not needed prior to the promise resolving.
Guido Urdaneta70c2db12019-04-16 10:24:142651 if (IsUnifiedPlan()) {
2652 bool was_negotiation_needed = is_negotiation_needed_;
2653 UpdateNegotiationNeeded();
2654 if (signaling_state() == kStable && was_negotiation_needed &&
2655 is_negotiation_needed_) {
Henrik Boströme574a312020-08-25 08:20:112656 // Legacy version.
Guido Urdaneta70c2db12019-04-16 10:24:142657 Observer()->OnRenegotiationNeeded();
Henrik Boströme574a312020-08-25 08:20:112658 // Spec-compliant version; the event may get invalidated before firing.
2659 GenerateNegotiationNeededEvent();
Guido Urdaneta70c2db12019-04-16 10:24:142660 }
2661 }
2662
Henrik Boström831ae4e2020-07-29 10:04:002663 // MaybeStartGathering needs to be called after informing the observer so that
2664 // we don't signal any candidates before signaling that SetLocalDescription
2665 // completed.
2666 transport_controller_->MaybeStartGathering();
Steve Anton8a006912017-12-04 23:25:562667}
2668
2669RTCError PeerConnection::ApplyLocalDescription(
2670 std::unique_ptr<SessionDescriptionInterface> desc) {
2671 RTC_DCHECK_RUN_ON(signaling_thread());
2672 RTC_DCHECK(desc);
2673
henrike@webrtc.org28e20752013-07-10 00:45:362674 // Update stats here so that we have the most recent stats for tracks and
2675 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:262676 stats_->UpdateStats(kStatsOutputLevelStandard);
Steve Anton8a006912017-12-04 23:25:562677
Steve Antondcc3c022017-12-23 00:02:542678 // Take a reference to the old local description since it's used below to
2679 // compare against the new local description. When setting the new local
2680 // description, grab ownership of the replaced session description in case it
2681 // is the same as |old_local_description|, to keep it alive for the duration
2682 // of the method.
2683 const SessionDescriptionInterface* old_local_description =
2684 local_description();
2685 std::unique_ptr<SessionDescriptionInterface> replaced_local_description;
Zhi Huange830e682018-03-30 17:48:352686 SdpType type = desc->GetType();
Steve Anton3828c062017-12-06 18:34:512687 if (type == SdpType::kAnswer) {
Steve Antondcc3c022017-12-23 00:02:542688 replaced_local_description = pending_local_description_
2689 ? std::move(pending_local_description_)
2690 : std::move(current_local_description_);
Steve Anton8a006912017-12-04 23:25:562691 current_local_description_ = std::move(desc);
2692 pending_local_description_ = nullptr;
2693 current_remote_description_ = std::move(pending_remote_description_);
2694 } else {
Steve Antondcc3c022017-12-23 00:02:542695 replaced_local_description = std::move(pending_local_description_);
Steve Anton8a006912017-12-04 23:25:562696 pending_local_description_ = std::move(desc);
2697 }
2698 // The session description to apply now must be accessed by
2699 // |local_description()|.
Henrik Boströmfdb92012017-11-09 18:55:442700 RTC_DCHECK(local_description());
deadbeefab9b2d12015-10-14 18:33:112701
Amit Hilbuche2a284d2019-03-05 20:36:312702 // Report statistics about any use of simulcast.
2703 ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription,
2704 *local_description()->description());
2705
Bjorn Mellem175aa2e2018-11-08 19:23:222706 if (!is_caller_) {
2707 if (remote_description()) {
2708 // Remote description was applied first, so this PC is the callee.
2709 is_caller_ = false;
2710 } else {
2711 // Local description is applied first, so this PC is the caller.
2712 is_caller_ = true;
2713 }
2714 }
2715
Zhi Huange830e682018-03-30 17:48:352716 RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type);
2717 if (!error.ok()) {
2718 return error;
2719 }
2720
Steve Antondcc3c022017-12-23 00:02:542721 if (IsUnifiedPlan()) {
2722 RTCError error = UpdateTransceiversAndDataChannels(
Seth Hampsonae8a90a2018-02-13 23:33:482723 cricket::CS_LOCAL, *local_description(), old_local_description,
2724 remote_description());
Steve Anton8a006912017-12-04 23:25:562725 if (!error.ok()) {
2726 return error;
2727 }
Steve Anton0f5400a2018-07-17 21:25:362728 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
2729 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
Mirko Bonadei739baf02019-01-27 16:29:422730 for (const auto& transceiver : transceivers_) {
Harald Alvestrand6060df52020-08-11 07:54:022731 if (transceiver->stopped()) {
2732 continue;
2733 }
2734
Harald Alvestrand4a7b3ac2019-01-17 09:39:402735 // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots.
2736 // Note that code paths that don't set MID won't be able to use
2737 // information about DTLS transports.
2738 if (transceiver->mid()) {
2739 auto dtls_transport =
2740 LookupDtlsTransportByMidInternal(*transceiver->mid());
2741 transceiver->internal()->sender_internal()->set_transport(
2742 dtls_transport);
2743 transceiver->internal()->receiver_internal()->set_transport(
2744 dtls_transport);
2745 }
2746
Steve Antondcc3c022017-12-23 00:02:542747 const ContentInfo* content =
2748 FindMediaSectionForTransceiver(transceiver, local_description());
2749 if (!content) {
2750 continue;
2751 }
2752 const MediaContentDescription* media_desc = content->media_description();
Steve Anton0f5400a2018-07-17 21:25:362753 // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run
2754 // the following steps:
Steve Antondcc3c022017-12-23 00:02:542755 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
Steve Anton0f5400a2018-07-17 21:25:362756 // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and
2757 // transceiver's [[FiredDirection]] slot is either "sendrecv" or
2758 // "recvonly", process the removal of a remote track for the media
2759 // description, given transceiver, removeList, and muteTracks.
2760 if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
2761 (transceiver->internal()->fired_direction() &&
2762 RtpTransceiverDirectionHasRecv(
2763 *transceiver->internal()->fired_direction()))) {
2764 ProcessRemovalOfRemoteTrack(transceiver, &remove_list,
2765 &removed_streams);
2766 }
2767 // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and
2768 // [[FiredDirection]] slots to direction.
Steve Antondcc3c022017-12-23 00:02:542769 transceiver->internal()->set_current_direction(media_desc->direction());
Steve Anton0f5400a2018-07-17 21:25:362770 transceiver->internal()->set_fired_direction(media_desc->direction());
Steve Antondcc3c022017-12-23 00:02:542771 }
Steve Anton0f5400a2018-07-17 21:25:362772 }
Harald Alvestrand7a1c7f72018-08-01 08:50:162773 auto observer = Observer();
Mirko Bonadei739baf02019-01-27 16:29:422774 for (const auto& transceiver : remove_list) {
Harald Alvestrand7a1c7f72018-08-01 08:50:162775 observer->OnRemoveTrack(transceiver->receiver());
Steve Anton0f5400a2018-07-17 21:25:362776 }
Mirko Bonadei739baf02019-01-27 16:29:422777 for (const auto& stream : removed_streams) {
Harald Alvestrand7a1c7f72018-08-01 08:50:162778 observer->OnRemoveStream(stream);
Steve Antondcc3c022017-12-23 00:02:542779 }
2780 } else {
Zhi Huange830e682018-03-30 17:48:352781 // Media channels will be created only when offer is set. These may use new
2782 // transports just created by PushdownTransportDescription.
Steve Antondcc3c022017-12-23 00:02:542783 if (type == SdpType::kOffer) {
2784 // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local
2785 // description is applied. Restore back to old description.
2786 RTCError error = CreateChannels(*local_description()->description());
2787 if (!error.ok()) {
2788 return error;
2789 }
2790 }
Steve Antondcc3c022017-12-23 00:02:542791 // Remove unused channels if MediaContentDescription is rejected.
2792 RemoveUnusedChannels(local_description()->description());
2793 }
Steve Anton8a006912017-12-04 23:25:562794
Zhi Huange830e682018-03-30 17:48:352795 error = UpdateSessionState(type, cricket::CS_LOCAL,
2796 local_description()->description());
Steve Anton8a006912017-12-04 23:25:562797 if (!error.ok()) {
2798 return error;
2799 }
Steve Antondcc3c022017-12-23 00:02:542800
Steve Anton8a006912017-12-04 23:25:562801 if (remote_description()) {
2802 // Now that we have a local description, we can push down remote candidates.
2803 UseCandidatesInSessionDescription(remote_description());
2804 }
2805
2806 pending_ice_restarts_.clear();
2807 if (session_error() != SessionError::kNone) {
2808 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
2809 }
2810
deadbeefab9b2d12015-10-14 18:33:112811 // If setting the description decided our SSL role, allocate any necessary
2812 // SCTP sids.
2813 rtc::SSLRole role;
Taylor Brandstetter3a034e12020-07-09 22:32:342814 if (IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) {
Harald Alvestrand00cf34c2019-12-02 08:56:022815 data_channel_controller_.AllocateSctpSids(role);
deadbeefab9b2d12015-10-14 18:33:112816 }
2817
Steve Antond3679212018-01-18 01:41:022818 if (IsUnifiedPlan()) {
Mirko Bonadei739baf02019-01-27 16:29:422819 for (const auto& transceiver : transceivers_) {
Harald Alvestrand6060df52020-08-11 07:54:022820 if (transceiver->stopped()) {
2821 continue;
2822 }
Steve Antond3679212018-01-18 01:41:022823 const ContentInfo* content =
2824 FindMediaSectionForTransceiver(transceiver, local_description());
2825 if (!content) {
2826 continue;
2827 }
Amit Hilbuchbcd39d42019-01-26 01:13:562828 cricket::ChannelInterface* channel = transceiver->internal()->channel();
2829 if (content->rejected || !channel || channel->local_streams().empty()) {
Steve Anton60b6c1d2018-06-13 18:32:272830 // 0 is a special value meaning "this sender has no associated send
2831 // stream". Need to call this so the sender won't attempt to configure
2832 // a no longer existing stream and run into DCHECKs in the lower
2833 // layers.
2834 transceiver->internal()->sender_internal()->SetSsrc(0);
Amit Hilbuchbcd39d42019-01-26 01:13:562835 } else {
2836 // Get the StreamParams from the channel which could generate SSRCs.
2837 const std::vector<StreamParams>& streams = channel->local_streams();
Henrik Andreasssoncc189172019-05-20 09:01:382838 transceiver->internal()->sender_internal()->set_stream_ids(
Amit Hilbuchbcd39d42019-01-26 01:13:562839 streams[0].stream_ids());
2840 transceiver->internal()->sender_internal()->SetSsrc(
2841 streams[0].first_ssrc());
Steve Antond3679212018-01-18 01:41:022842 }
2843 }
2844 } else {
2845 // Plan B semantics.
2846
Steve Antondcc3c022017-12-23 00:02:542847 // Update state and SSRC of local MediaStreams and DataChannels based on the
2848 // local session description.
2849 const cricket::ContentInfo* audio_content =
2850 GetFirstAudioContent(local_description()->description());
2851 if (audio_content) {
2852 if (audio_content->rejected) {
2853 RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
2854 } else {
2855 const cricket::AudioContentDescription* audio_desc =
2856 audio_content->media_description()->as_audio();
2857 UpdateLocalSenders(audio_desc->streams(), audio_desc->type());
2858 }
deadbeeffaac4972015-11-12 23:33:072859 }
deadbeefab9b2d12015-10-14 18:33:112860
Steve Antondcc3c022017-12-23 00:02:542861 const cricket::ContentInfo* video_content =
2862 GetFirstVideoContent(local_description()->description());
2863 if (video_content) {
2864 if (video_content->rejected) {
2865 RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
2866 } else {
2867 const cricket::VideoContentDescription* video_desc =
2868 video_content->media_description()->as_video();
2869 UpdateLocalSenders(video_desc->streams(), video_desc->type());
2870 }
deadbeeffaac4972015-11-12 23:33:072871 }
deadbeefab9b2d12015-10-14 18:33:112872 }
2873
2874 const cricket::ContentInfo* data_content =
Henrik Boströmfdb92012017-11-09 18:55:442875 GetFirstDataContent(local_description()->description());
deadbeefab9b2d12015-10-14 18:33:112876 if (data_content) {
Harald Alvestrand5fc28b12019-05-13 11:36:162877 const cricket::RtpDataContentDescription* rtp_data_desc =
2878 data_content->media_description()->as_rtp_data();
2879 // rtp_data_desc will be null if this is an SCTP description.
2880 if (rtp_data_desc) {
Harald Alvestrand05e4d082019-12-03 13:04:212881 data_channel_controller_.UpdateLocalRtpDataChannels(
2882 rtp_data_desc->streams());
deadbeefab9b2d12015-10-14 18:33:112883 }
2884 }
2885
Henrik Boström79b69802019-07-18 09:16:562886 if (type == SdpType::kAnswer &&
2887 local_ice_credentials_to_replace_->SatisfiesIceRestart(
2888 *current_local_description_)) {
2889 local_ice_credentials_to_replace_->ClearIceCredentials();
2890 }
2891
Steve Anton8a006912017-12-04 23:25:562892 return RTCError::OK();
henrike@webrtc.org28e20752013-07-10 00:45:362893}
2894
Steve Anton06817cd2018-12-18 23:55:302895// The SDP parser used to populate these values by default for the 'content
2896// name' if an a=mid line was absent.
2897static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) {
2898 switch (media_type) {
2899 case cricket::MEDIA_TYPE_AUDIO:
2900 return cricket::CN_AUDIO;
2901 case cricket::MEDIA_TYPE_VIDEO:
2902 return cricket::CN_VIDEO;
2903 case cricket::MEDIA_TYPE_DATA:
2904 return cricket::CN_DATA;
2905 }
2906 RTC_NOTREACHED();
2907 return "";
2908}
2909
2910void PeerConnection::FillInMissingRemoteMids(
Steve Antond7180cc2019-02-07 18:44:532911 cricket::SessionDescription* new_remote_description) {
2912 RTC_DCHECK(new_remote_description);
Harald Alvestrand1716d392019-06-03 18:35:452913 const cricket::ContentInfos no_infos;
Steve Anton06817cd2018-12-18 23:55:302914 const cricket::ContentInfos& local_contents =
2915 (local_description() ? local_description()->description()->contents()
Harald Alvestrand1716d392019-06-03 18:35:452916 : no_infos);
Steve Antond7180cc2019-02-07 18:44:532917 const cricket::ContentInfos& remote_contents =
2918 (remote_description() ? remote_description()->description()->contents()
Harald Alvestrand1716d392019-06-03 18:35:452919 : no_infos);
Steve Antond7180cc2019-02-07 18:44:532920 for (size_t i = 0; i < new_remote_description->contents().size(); ++i) {
2921 cricket::ContentInfo& content = new_remote_description->contents()[i];
Steve Anton06817cd2018-12-18 23:55:302922 if (!content.name.empty()) {
2923 continue;
2924 }
Amit Hilbuchae3df542019-01-07 20:13:082925 std::string new_mid;
Steve Anton06817cd2018-12-18 23:55:302926 absl::string_view source_explanation;
2927 if (IsUnifiedPlan()) {
2928 if (i < local_contents.size()) {
2929 new_mid = local_contents[i].name;
2930 source_explanation = "from the matching local media section";
Steve Antond7180cc2019-02-07 18:44:532931 } else if (i < remote_contents.size()) {
2932 new_mid = remote_contents[i].name;
2933 source_explanation = "from the matching previous remote media section";
Steve Anton06817cd2018-12-18 23:55:302934 } else {
Amit Hilbuchae3df542019-01-07 20:13:082935 new_mid = mid_generator_();
Steve Anton06817cd2018-12-18 23:55:302936 source_explanation = "generated just now";
2937 }
2938 } else {
Amit Hilbuchae3df542019-01-07 20:13:082939 new_mid = std::string(
2940 GetDefaultMidForPlanB(content.media_description()->type()));
Steve Anton06817cd2018-12-18 23:55:302941 source_explanation = "to match pre-existing behavior";
2942 }
Steve Antond7180cc2019-02-07 18:44:532943 RTC_DCHECK(!new_mid.empty());
Amit Hilbuchae3df542019-01-07 20:13:082944 content.name = new_mid;
Steve Antond7180cc2019-02-07 18:44:532945 new_remote_description->transport_infos()[i].content_name = new_mid;
Steve Anton06817cd2018-12-18 23:55:302946 RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i
2947 << " is missing an a=mid line. Filling in the value '"
2948 << new_mid << "' " << source_explanation << ".";
2949 }
2950}
2951
henrike@webrtc.org28e20752013-07-10 00:45:362952void PeerConnection::SetRemoteDescription(
Henrik Boströma4ecf552017-11-23 14:17:072953 SetSessionDescriptionObserver* observer,
Henrik Boströma3728d32019-10-28 11:09:492954 SessionDescriptionInterface* desc_ptr) {
2955 RTC_DCHECK_RUN_ON(signaling_thread());
2956 // Chain this operation. If asynchronous operations are pending on the chain,
2957 // this operation will be queued to be invoked, otherwise the contents of the
2958 // lambda will execute immediately.
2959 operations_chain_->ChainOperation(
2960 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2961 observer_refptr =
2962 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
2963 desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
2964 std::function<void()> operations_chain_callback) mutable {
2965 // Abort early if |this_weak_ptr| is no longer valid.
2966 if (!this_weak_ptr) {
Henrik Boström831ae4e2020-07-29 10:04:002967 // For consistency with SetSessionDescriptionObserverAdapter whose
2968 // posted messages doesn't get processed when the PC is destroyed, we
2969 // do not inform |observer_refptr| that the operation failed.
Henrik Boströma3728d32019-10-28 11:09:492970 operations_chain_callback();
2971 return;
2972 }
Henrik Boström831ae4e2020-07-29 10:04:002973 // SetSessionDescriptionObserverAdapter takes care of making sure the
2974 // |observer_refptr| is invoked in a posted message.
Henrik Boströma3728d32019-10-28 11:09:492975 this_weak_ptr->DoSetRemoteDescription(
2976 std::move(desc),
2977 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>(
Henrik Boström831ae4e2020-07-29 10:04:002978 new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>(
2979 this_weak_ptr, observer_refptr)));
Henrik Boströma3728d32019-10-28 11:09:492980 // For backwards-compatability reasons, we declare the operation as
Henrik Boström831ae4e2020-07-29 10:04:002981 // completed here (rather than in a post), so that the operation chain
2982 // is not blocked by this operation when the observer is invoked. This
2983 // allows the observer to trigger subsequent offer/answer operations
2984 // synchronously if the operation chain is now empty.
Henrik Boströma3728d32019-10-28 11:09:492985 operations_chain_callback();
2986 });
Henrik Boström31638672017-11-23 16:48:322987}
2988
2989void PeerConnection::SetRemoteDescription(
2990 std::unique_ptr<SessionDescriptionInterface> desc,
2991 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
Karl Wiberg5966c502019-02-21 22:55:092992 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boströma3728d32019-10-28 11:09:492993 // Chain this operation. If asynchronous operations are pending on the chain,
2994 // this operation will be queued to be invoked, otherwise the contents of the
2995 // lambda will execute immediately.
2996 operations_chain_->ChainOperation(
2997 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer,
2998 desc = std::move(desc)](
2999 std::function<void()> operations_chain_callback) mutable {
3000 // Abort early if |this_weak_ptr| is no longer valid.
3001 if (!this_weak_ptr) {
Henrik Boströma3728d32019-10-28 11:09:493002 observer->OnSetRemoteDescriptionComplete(RTCError(
Henrik Boström831ae4e2020-07-29 10:04:003003 RTCErrorType::INTERNAL_ERROR,
3004 "SetRemoteDescription failed because the session was shut down"));
Henrik Boströma3728d32019-10-28 11:09:493005 operations_chain_callback();
3006 return;
3007 }
3008 this_weak_ptr->DoSetRemoteDescription(std::move(desc),
3009 std::move(observer));
Henrik Boström831ae4e2020-07-29 10:04:003010 // DoSetRemoteDescription() is implemented as a synchronous operation.
3011 // The |observer| will already have been informed that it completed, and
3012 // we can mark this operation as complete without any loose ends.
Henrik Boströma3728d32019-10-28 11:09:493013 operations_chain_callback();
3014 });
3015}
3016
3017void PeerConnection::DoSetRemoteDescription(
3018 std::unique_ptr<SessionDescriptionInterface> desc,
3019 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
3020 RTC_DCHECK_RUN_ON(signaling_thread());
3021 TRACE_EVENT0("webrtc", "PeerConnection::DoSetRemoteDescription");
Steve Anton8a006912017-12-04 23:25:563022
nisse7ce109a2017-01-31 08:57:563023 if (!observer) {
Mirko Bonadei675513b2017-11-09 10:09:253024 RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:363025 return;
3026 }
Steve Anton8a006912017-12-04 23:25:563027
henrike@webrtc.org28e20752013-07-10 00:45:363028 if (!desc) {
Henrik Boström31638672017-11-23 16:48:323029 observer->OnSetRemoteDescriptionComplete(RTCError(
Steve Anton8a006912017-12-04 23:25:563030 RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL."));
henrike@webrtc.org28e20752013-07-10 00:45:363031 return;
3032 }
Steve Anton8d3444d2017-10-20 22:30:513033
Steve Anton80dd7b52018-02-17 01:08:423034 // If a session error has occurred the PeerConnection is in a possibly
3035 // inconsistent state so fail right away.
3036 if (session_error() != SessionError::kNone) {
3037 std::string error_message = GetSessionErrorMsg();
3038 RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message;
3039 observer->OnSetRemoteDescriptionComplete(
3040 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
3041 return;
3042 }
Eldar Rello5ab79e62019-10-09 15:29:443043 if (IsUnifiedPlan()) {
3044 if (configuration_.enable_implicit_rollback) {
3045 if (desc->GetType() == SdpType::kOffer &&
3046 signaling_state() == kHaveLocalOffer) {
Eldar Relloead0ec92019-10-21 20:01:313047 Rollback(desc->GetType());
Eldar Rello5ab79e62019-10-09 15:29:443048 }
3049 }
3050 // Explicit rollback.
3051 if (desc->GetType() == SdpType::kRollback) {
Eldar Relloead0ec92019-10-21 20:01:313052 observer->OnSetRemoteDescriptionComplete(Rollback(desc->GetType()));
Eldar Rello5ab79e62019-10-09 15:29:443053 return;
3054 }
3055 } else if (desc->GetType() == SdpType::kRollback) {
3056 observer->OnSetRemoteDescriptionComplete(
3057 RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
3058 "Rollback not supported in Plan B"));
3059 return;
3060 }
Steve Antonba42e992018-04-09 21:10:013061 if (desc->GetType() == SdpType::kOffer) {
3062 // Report to UMA the format of the received offer.
3063 ReportSdpFormatReceived(*desc);
3064 }
3065
Steve Anton06817cd2018-12-18 23:55:303066 // Handle remote descriptions missing a=mid lines for interop with legacy end
3067 // points.
3068 FillInMissingRemoteMids(desc->description());
3069
Steve Anton80dd7b52018-02-17 01:08:423070 RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE);
Steve Anton71439a62018-02-15 19:53:063071 if (!error.ok()) {
Steve Anton80dd7b52018-02-17 01:08:423072 std::string error_message = GetSetDescriptionErrorMessage(
3073 cricket::CS_REMOTE, desc->GetType(), error);
3074 RTC_LOG(LS_ERROR) << error_message;
Steve Anton71439a62018-02-15 19:53:063075 observer->OnSetRemoteDescriptionComplete(
3076 RTCError(error.type(), std::move(error_message)));
3077 return;
3078 }
Steve Anton71439a62018-02-15 19:53:063079
Steve Anton80dd7b52018-02-17 01:08:423080 // Grab the description type before moving ownership to
3081 // ApplyRemoteDescription, which may destroy it before returning.
3082 const SdpType type = desc->GetType();
3083
3084 error = ApplyRemoteDescription(std::move(desc));
3085 // |desc| may be destroyed at this point.
3086
3087 if (!error.ok()) {
3088 // If ApplyRemoteDescription fails, the PeerConnection could be in an
3089 // inconsistent state, so act conservatively here and set the session error
3090 // so that future calls to SetLocalDescription/SetRemoteDescription fail.
3091 SetSessionError(SessionError::kContent, error.message());
3092 std::string error_message =
3093 GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error);
3094 RTC_LOG(LS_ERROR) << error_message;
3095 observer->OnSetRemoteDescriptionComplete(
3096 RTCError(error.type(), std::move(error_message)));
3097 return;
3098 }
3099 RTC_DCHECK(remote_description());
3100
3101 if (type == SdpType::kAnswer) {
Steve Anton8a006912017-12-04 23:25:563102 // TODO(deadbeef): We already had to hop to the network thread for
3103 // MaybeStartGathering...
3104 network_thread()->Invoke<void>(
3105 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
3106 port_allocator_.get()));
Harald Alvestrand5dbb5862018-02-13 22:48:003107 // Make UMA notes about what was agreed to.
Steve Anton0ffaaa22018-02-23 18:31:303108 ReportNegotiatedSdpSemantics(*remote_description());
Steve Anton8a006912017-12-04 23:25:563109 }
3110
Henrik Boströme574a312020-08-25 08:20:113111 observer->OnSetRemoteDescriptionComplete(RTCError::OK());
3112 NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED);
3113
3114 // Check if negotiation is needed. We must do this after informing the
3115 // observer that SetRemoteDescription() has completed to ensure negotiation is
3116 // not needed prior to the promise resolving.
Guido Urdaneta70c2db12019-04-16 10:24:143117 if (IsUnifiedPlan()) {
3118 bool was_negotiation_needed = is_negotiation_needed_;
3119 UpdateNegotiationNeeded();
3120 if (signaling_state() == kStable && was_negotiation_needed &&
3121 is_negotiation_needed_) {
Henrik Boströme574a312020-08-25 08:20:113122 // Legacy version.
Guido Urdaneta70c2db12019-04-16 10:24:143123 Observer()->OnRenegotiationNeeded();
Henrik Boströme574a312020-08-25 08:20:113124 // Spec-compliant version; the event may get invalidated before firing.
3125 GenerateNegotiationNeededEvent();
Guido Urdaneta70c2db12019-04-16 10:24:143126 }
3127 }
Steve Anton8a006912017-12-04 23:25:563128}
3129
3130RTCError PeerConnection::ApplyRemoteDescription(
3131 std::unique_ptr<SessionDescriptionInterface> desc) {
3132 RTC_DCHECK_RUN_ON(signaling_thread());
3133 RTC_DCHECK(desc);
3134
henrike@webrtc.org28e20752013-07-10 00:45:363135 // Update stats here so that we have the most recent stats for tracks and
3136 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:263137 stats_->UpdateStats(kStatsOutputLevelStandard);
Steve Anton8a006912017-12-04 23:25:563138
Steve Antondcc3c022017-12-23 00:02:543139 // Take a reference to the old remote description since it's used below to
3140 // compare against the new remote description. When setting the new remote
3141 // description, grab ownership of the replaced session description in case it
3142 // is the same as |old_remote_description|, to keep it alive for the duration
3143 // of the method.
Steve Anton8a006912017-12-04 23:25:563144 const SessionDescriptionInterface* old_remote_description =
3145 remote_description();
Steve Anton8a006912017-12-04 23:25:563146 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
Steve Anton3828c062017-12-06 18:34:513147 SdpType type = desc->GetType();
3148 if (type == SdpType::kAnswer) {
Steve Anton8a006912017-12-04 23:25:563149 replaced_remote_description = pending_remote_description_
3150 ? std::move(pending_remote_description_)
3151 : std::move(current_remote_description_);
3152 current_remote_description_ = std::move(desc);
3153 pending_remote_description_ = nullptr;
3154 current_local_description_ = std::move(pending_local_description_);
3155 } else {
3156 replaced_remote_description = std::move(pending_remote_description_);
3157 pending_remote_description_ = std::move(desc);
henrike@webrtc.org28e20752013-07-10 00:45:363158 }
Steve Anton8a006912017-12-04 23:25:563159 // The session description to apply now must be accessed by
3160 // |remote_description()|.
Henrik Boströmfdb92012017-11-09 18:55:443161 RTC_DCHECK(remote_description());
henrike@webrtc.org28e20752013-07-10 00:45:363162
Amit Hilbuche2a284d2019-03-05 20:36:313163 // Report statistics about any use of simulcast.
3164 ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription,
3165 *remote_description()->description());
3166
Zhi Huange830e682018-03-30 17:48:353167 RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type);
3168 if (!error.ok()) {
3169 return error;
3170 }
Steve Anton8a006912017-12-04 23:25:563171 // Transport and Media channels will be created only when offer is set.
Steve Antondcc3c022017-12-23 00:02:543172 if (IsUnifiedPlan()) {
3173 RTCError error = UpdateTransceiversAndDataChannels(
Seth Hampsonae8a90a2018-02-13 23:33:483174 cricket::CS_REMOTE, *remote_description(), local_description(),
3175 old_remote_description);
Steve Anton8a006912017-12-04 23:25:563176 if (!error.ok()) {
3177 return error;
3178 }
Steve Antondcc3c022017-12-23 00:02:543179 } else {
Zhi Huange830e682018-03-30 17:48:353180 // Media channels will be created only when offer is set. These may use new
3181 // transports just created by PushdownTransportDescription.
Steve Antondcc3c022017-12-23 00:02:543182 if (type == SdpType::kOffer) {
Zhi Huange830e682018-03-30 17:48:353183 // TODO(mallinath) - Handle CreateChannel failure, as new local
Steve Antondcc3c022017-12-23 00:02:543184 // description is applied. Restore back to old description.
3185 RTCError error = CreateChannels(*remote_description()->description());
3186 if (!error.ok()) {
3187 return error;
3188 }
3189 }
Steve Antondcc3c022017-12-23 00:02:543190 // Remove unused channels if MediaContentDescription is rejected.
3191 RemoveUnusedChannels(remote_description()->description());
3192 }
Steve Anton8a006912017-12-04 23:25:563193
Zhi Huange830e682018-03-30 17:48:353194 // NOTE: Candidates allocation will be initiated only when
3195 // SetLocalDescription is called.
3196 error = UpdateSessionState(type, cricket::CS_REMOTE,
3197 remote_description()->description());
Steve Anton8a006912017-12-04 23:25:563198 if (!error.ok()) {
3199 return error;
3200 }
3201
3202 if (local_description() &&
3203 !UseCandidatesInSessionDescription(remote_description())) {
3204 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates);
3205 }
3206
3207 if (old_remote_description) {
3208 for (const cricket::ContentInfo& content :
3209 old_remote_description->description()->contents()) {
3210 // Check if this new SessionDescription contains new ICE ufrag and
3211 // password that indicates the remote peer requests an ICE restart.
3212 // TODO(deadbeef): When we start storing both the current and pending
3213 // remote description, this should reset pending_ice_restarts and compare
3214 // against the current description.
3215 if (CheckForRemoteIceRestart(old_remote_description, remote_description(),
3216 content.name)) {
Steve Anton3828c062017-12-06 18:34:513217 if (type == SdpType::kOffer) {
Steve Anton8a006912017-12-04 23:25:563218 pending_ice_restarts_.insert(content.name);
3219 }
3220 } else {
3221 // We retain all received candidates only if ICE is not restarted.
3222 // When ICE is restarted, all previous candidates belong to an old
3223 // generation and should not be kept.
3224 // TODO(deadbeef): This goes against the W3C spec which says the remote
3225 // description should only contain candidates from the last set remote
3226 // description plus any candidates added since then. We should remove
3227 // this once we're sure it won't break anything.
3228 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
3229 old_remote_description, content.name, mutable_remote_description());
3230 }
3231 }
3232 }
3233
3234 if (session_error() != SessionError::kNone) {
3235 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
3236 }
3237
3238 // Set the the ICE connection state to connecting since the connection may
3239 // become writable with peer reflexive candidates before any remote candidate
3240 // is signaled.
3241 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
3242 // is to have a new signal the indicates a change in checking state from the
3243 // transport and expose a new checking() member from transport that can be
3244 // read to determine the current checking state. The existing SignalConnecting
3245 // actually means "gathering candidates", so cannot be be used here.
Steve Antona3a92c22017-12-07 18:27:413246 if (remote_description()->GetType() != SdpType::kOffer &&
Steve Antonf764cf42018-05-01 21:32:173247 remote_description()->number_of_mediasections() > 0u &&
Steve Anton8a006912017-12-04 23:25:563248 ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) {
3249 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
3250 }
3251
deadbeefab9b2d12015-10-14 18:33:113252 // If setting the description decided our SSL role, allocate any necessary
3253 // SCTP sids.
3254 rtc::SSLRole role;
Taylor Brandstetter3a034e12020-07-09 22:32:343255 if (IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) {
Harald Alvestrand00cf34c2019-12-02 08:56:023256 data_channel_controller_.AllocateSctpSids(role);
deadbeefab9b2d12015-10-14 18:33:113257 }
3258
Steve Antondcc3c022017-12-23 00:02:543259 if (IsUnifiedPlan()) {
Steve Anton8b815cd2018-02-17 00:14:423260 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>
Steve Anton3172c032018-05-03 22:30:183261 now_receiving_transceivers;
Steve Anton0f5400a2018-07-17 21:25:363262 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
Steve Antonc49bcd92018-02-14 22:28:133263 std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
Steve Anton3172c032018-05-03 22:30:183264 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
Mirko Bonadei739baf02019-01-27 16:29:423265 for (const auto& transceiver : transceivers_) {
Steve Antondcc3c022017-12-23 00:02:543266 const ContentInfo* content =
3267 FindMediaSectionForTransceiver(transceiver, remote_description());
3268 if (!content) {
3269 continue;
3270 }
3271 const MediaContentDescription* media_desc = content->media_description();
3272 RtpTransceiverDirection local_direction =
3273 RtpTransceiverDirectionReversed(media_desc->direction());
Henrik Boströmafa07dd2018-12-20 10:06:023274 // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the
3275 // RTCSessionDescription: Set the associated remote streams given
3276 // transceiver.[[Receiver]], msids, addList, and removeList".
3277 // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription
3278 if (RtpTransceiverDirectionHasRecv(local_direction)) {
3279 std::vector<std::string> stream_ids;
3280 if (!media_desc->streams().empty()) {
3281 // The remote description has signaled the stream IDs.
3282 stream_ids = media_desc->streams()[0].stream_ids();
Steve Antonef65ef12018-01-11 01:15:203283 }
Eldar Rello353a7182019-11-25 16:49:443284 transceiver_stable_states_by_transceivers_[transceiver]
3285 .SetRemoteStreamIdsIfUnset(transceiver->receiver()->stream_ids());
3286
Henrik Boströmafa07dd2018-12-20 10:06:023287 RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name
3288 << " (" << GetStreamIdsString(stream_ids) << ").";
3289 SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(),
3290 stream_ids, &added_streams,
3291 &removed_streams);
3292 // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6
3293 // "Set the RTCSessionDescription: If direction is sendrecv or recvonly,
3294 // and transceiver's current direction is neither sendrecv nor recvonly,
3295 // process the addition of a remote track for the media description.
3296 if (!transceiver->fired_direction() ||
3297 !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) {
3298 RTC_LOG(LS_INFO)
3299 << "Processing the addition of a remote track for MID="
3300 << content->name << ".";
3301 now_receiving_transceivers.push_back(transceiver);
Henrik Boström5b147782018-12-04 10:25:053302 }
Steve Antondcc3c022017-12-23 00:02:543303 }
Harald Alvestrand4a7b3ac2019-01-17 09:39:403304 // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's
Steve Anton0f5400a2018-07-17 21:25:363305 // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the
3306 // removal of a remote track for the media description, given transceiver,
3307 // removeList, and muteTracks.
Steve Antondcc3c022017-12-23 00:02:543308 if (!RtpTransceiverDirectionHasRecv(local_direction) &&
Steve Anton0f5400a2018-07-17 21:25:363309 (transceiver->fired_direction() &&
3310 RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) {
3311 ProcessRemovalOfRemoteTrack(transceiver, &remove_list,
3312 &removed_streams);
Steve Antondcc3c022017-12-23 00:02:543313 }
Harald Alvestrand4a7b3ac2019-01-17 09:39:403314 // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction.
Steve Anton0f5400a2018-07-17 21:25:363315 transceiver->internal()->set_fired_direction(local_direction);
Harald Alvestrand4a7b3ac2019-01-17 09:39:403316 // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run
Steve Anton0f5400a2018-07-17 21:25:363317 // the following steps:
Steve Antondcc3c022017-12-23 00:02:543318 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
Harald Alvestrand4a7b3ac2019-01-17 09:39:403319 // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to
Steve Anton0f5400a2018-07-17 21:25:363320 // direction.
Steve Antondcc3c022017-12-23 00:02:543321 transceiver->internal()->set_current_direction(local_direction);
Harald Alvestrand4a7b3ac2019-01-17 09:39:403322 // 2.2.8.1.11.[3-6]: Set the transport internal slots.
3323 if (transceiver->mid()) {
3324 auto dtls_transport =
3325 LookupDtlsTransportByMidInternal(*transceiver->mid());
3326 transceiver->internal()->sender_internal()->set_transport(
3327 dtls_transport);
3328 transceiver->internal()->receiver_internal()->set_transport(
3329 dtls_transport);
3330 }
Steve Antondcc3c022017-12-23 00:02:543331 }
Harald Alvestrand4a7b3ac2019-01-17 09:39:403332 // 2.2.8.1.12: If the media description is rejected, and transceiver is
Steve Anton0f5400a2018-07-17 21:25:363333 // not already stopped, stop the RTCRtpTransceiver transceiver.
Steve Antondcc3c022017-12-23 00:02:543334 if (content->rejected && !transceiver->stopped()) {
Steve Anton3d954a62018-04-02 18:27:233335 RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name
3336 << " since the media section was rejected.";
Harald Alvestrandc75c4282020-08-26 12:17:543337 transceiver->internal()->StopTransceiverProcedure();
Steve Antondcc3c022017-12-23 00:02:543338 }
Seth Hampson2f0d7022018-02-20 19:54:423339 if (!content->rejected &&
3340 RtpTransceiverDirectionHasRecv(local_direction)) {
Seth Hampson5897a6e2018-04-03 18:16:333341 if (!media_desc->streams().empty() &&
3342 media_desc->streams()[0].has_ssrcs()) {
Saurav Das7262fc22019-09-11 23:23:053343 uint32_t ssrc = media_desc->streams()[0].first_ssrc();
3344 transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc);
3345 } else {
3346 transceiver->internal()
3347 ->receiver_internal()
3348 ->SetupUnsignaledMediaChannel();
Seth Hampson2f0d7022018-02-20 19:54:423349 }
Steve Antond3679212018-01-18 01:41:023350 }
Steve Antondcc3c022017-12-23 00:02:543351 }
Steve Antonc49bcd92018-02-14 22:28:133352 // Once all processing has finished, fire off callbacks.
Harald Alvestrand7a1c7f72018-08-01 08:50:163353 auto observer = Observer();
Mirko Bonadei739baf02019-01-27 16:29:423354 for (const auto& transceiver : now_receiving_transceivers) {
Steve Anton6e221372018-02-20 20:59:163355 stats_->AddTrack(transceiver->receiver()->track());
Harald Alvestrand7a1c7f72018-08-01 08:50:163356 observer->OnTrack(transceiver);
3357 observer->OnAddTrack(transceiver->receiver(),
3358 transceiver->receiver()->streams());
Steve Antonef65ef12018-01-11 01:15:203359 }
Mirko Bonadei739baf02019-01-27 16:29:423360 for (const auto& stream : added_streams) {
Harald Alvestrand7a1c7f72018-08-01 08:50:163361 observer->OnAddStream(stream);
Steve Antonc49bcd92018-02-14 22:28:133362 }
Mirko Bonadei739baf02019-01-27 16:29:423363 for (const auto& transceiver : remove_list) {
Harald Alvestrand7a1c7f72018-08-01 08:50:163364 observer->OnRemoveTrack(transceiver->receiver());
Steve Anton3172c032018-05-03 22:30:183365 }
Mirko Bonadei739baf02019-01-27 16:29:423366 for (const auto& stream : removed_streams) {
Harald Alvestrand7a1c7f72018-08-01 08:50:163367 observer->OnRemoveStream(stream);
Steve Anton3172c032018-05-03 22:30:183368 }
Steve Antondcc3c022017-12-23 00:02:543369 }
3370
Henrik Boströmfdb92012017-11-09 18:55:443371 const cricket::ContentInfo* audio_content =
3372 GetFirstAudioContent(remote_description()->description());
3373 const cricket::ContentInfo* video_content =
3374 GetFirstVideoContent(remote_description()->description());
deadbeefbda7e0b2015-12-09 01:13:403375 const cricket::AudioContentDescription* audio_desc =
Henrik Boströmfdb92012017-11-09 18:55:443376 GetFirstAudioContentDescription(remote_description()->description());
deadbeefbda7e0b2015-12-09 01:13:403377 const cricket::VideoContentDescription* video_desc =
Henrik Boströmfdb92012017-11-09 18:55:443378 GetFirstVideoContentDescription(remote_description()->description());
Harald Alvestrand5fc28b12019-05-13 11:36:163379 const cricket::RtpDataContentDescription* rtp_data_desc =
3380 GetFirstRtpDataContentDescription(remote_description()->description());
deadbeefbda7e0b2015-12-09 01:13:403381
3382 // Check if the descriptions include streams, just in case the peer supports
3383 // MSID, but doesn't indicate so with "a=msid-semantic".
Henrik Boströmfdb92012017-11-09 18:55:443384 if (remote_description()->description()->msid_supported() ||
deadbeefbda7e0b2015-12-09 01:13:403385 (audio_desc && !audio_desc->streams().empty()) ||
3386 (video_desc && !video_desc->streams().empty())) {
3387 remote_peer_supports_msid_ = true;
3388 }
deadbeefab9b2d12015-10-14 18:33:113389
3390 // We wait to signal new streams until we finish processing the description,
3391 // since only at that point will new streams have all their tracks.
3392 rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create());
3393
Steve Antondcc3c022017-12-23 00:02:543394 if (!IsUnifiedPlan()) {
3395 // TODO(steveanton): When removing RTP senders/receivers in response to a
3396 // rejected media section, there is some cleanup logic that expects the
3397 // voice/ video channel to still be set. But in this method the voice/video
3398 // channel would have been destroyed by the SetRemoteDescription caller
3399 // above so the cleanup that relies on them fails to run. The RemoveSenders
3400 // calls should be moved to right before the DestroyChannel calls to fix
3401 // this.
Steve Anton8d3444d2017-10-20 22:30:513402
Steve Antondcc3c022017-12-23 00:02:543403 // Find all audio rtp streams and create corresponding remote AudioTracks
3404 // and MediaStreams.
3405 if (audio_content) {
3406 if (audio_content->rejected) {
3407 RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
3408 } else {
3409 bool default_audio_track_needed =
3410 !remote_peer_supports_msid_ &&
3411 RtpTransceiverDirectionHasSend(audio_desc->direction());
3412 UpdateRemoteSendersList(GetActiveStreams(audio_desc),
3413 default_audio_track_needed, audio_desc->type(),
3414 new_streams);
3415 }
deadbeeffaac4972015-11-12 23:33:073416 }
deadbeefab9b2d12015-10-14 18:33:113417
Steve Antondcc3c022017-12-23 00:02:543418 // Find all video rtp streams and create corresponding remote VideoTracks
3419 // and MediaStreams.
3420 if (video_content) {
3421 if (video_content->rejected) {
3422 RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
3423 } else {
3424 bool default_video_track_needed =
3425 !remote_peer_supports_msid_ &&
3426 RtpTransceiverDirectionHasSend(video_desc->direction());
3427 UpdateRemoteSendersList(GetActiveStreams(video_desc),
3428 default_video_track_needed, video_desc->type(),
3429 new_streams);
3430 }
deadbeeffaac4972015-11-12 23:33:073431 }
deadbeefab9b2d12015-10-14 18:33:113432
Harald Alvestrand5fc28b12019-05-13 11:36:163433 // If this is an RTP data transport, update the DataChannels with the
3434 // information from the remote peer.
3435 if (rtp_data_desc) {
Harald Alvestrand05e4d082019-12-03 13:04:213436 data_channel_controller_.UpdateRemoteRtpDataChannels(
3437 GetActiveStreams(rtp_data_desc));
deadbeefab9b2d12015-10-14 18:33:113438 }
deadbeefab9b2d12015-10-14 18:33:113439
Steve Antondcc3c022017-12-23 00:02:543440 // Iterate new_streams and notify the observer about new MediaStreams.
Harald Alvestrand7a1c7f72018-08-01 08:50:163441 auto observer = Observer();
Steve Antondcc3c022017-12-23 00:02:543442 for (size_t i = 0; i < new_streams->count(); ++i) {
3443 MediaStreamInterface* new_stream = new_streams->at(i);
3444 stats_->AddStream(new_stream);
Harald Alvestrand7a1c7f72018-08-01 08:50:163445 observer->OnAddStream(
Steve Antondcc3c022017-12-23 00:02:543446 rtc::scoped_refptr<MediaStreamInterface>(new_stream));
3447 }
deadbeefab9b2d12015-10-14 18:33:113448
Steve Antondcc3c022017-12-23 00:02:543449 UpdateEndedRemoteMediaStreams();
3450 }
deadbeefab9b2d12015-10-14 18:33:113451
Henrik Boström79b69802019-07-18 09:16:563452 if (type == SdpType::kAnswer &&
3453 local_ice_credentials_to_replace_->SatisfiesIceRestart(
3454 *current_local_description_)) {
3455 local_ice_credentials_to_replace_->ClearIceCredentials();
3456 }
3457
Steve Anton8a006912017-12-04 23:25:563458 return RTCError::OK();
deadbeeffc648b62015-10-13 23:42:333459}
3460
Henrik Boströmafa07dd2018-12-20 10:06:023461void PeerConnection::SetAssociatedRemoteStreams(
3462 rtc::scoped_refptr<RtpReceiverInternal> receiver,
3463 const std::vector<std::string>& stream_ids,
3464 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
3465 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
3466 std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams;
3467 for (const std::string& stream_id : stream_ids) {
3468 rtc::scoped_refptr<MediaStreamInterface> stream =
3469 remote_streams_->find(stream_id);
3470 if (!stream) {
3471 stream = MediaStreamProxy::Create(rtc::Thread::Current(),
3472 MediaStream::Create(stream_id));
3473 remote_streams_->AddStream(stream);
3474 added_streams->push_back(stream);
3475 }
3476 media_streams.push_back(stream);
3477 }
3478 // Special case: "a=msid" missing, use random stream ID.
3479 if (media_streams.empty() &&
3480 !(remote_description()->description()->msid_signaling() &
3481 cricket::kMsidSignalingMediaSection)) {
3482 if (!missing_msid_default_stream_) {
3483 missing_msid_default_stream_ = MediaStreamProxy::Create(
3484 rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid()));
3485 added_streams->push_back(missing_msid_default_stream_);
3486 }
3487 media_streams.push_back(missing_msid_default_stream_);
3488 }
3489 std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
3490 receiver->streams();
3491 // SetStreams() will add/remove the receiver's track to/from the streams. This
3492 // differs from the spec - the spec uses an "addList" and "removeList" to
3493 // update the stream-track relationships in a later step. We do this earlier,
3494 // changing the order of things, but the end-result is the same.
3495 // TODO(hbos): When we remove remote_streams(), use set_stream_ids()
3496 // instead. https://crbug.com/webrtc/9480
3497 receiver->SetStreams(media_streams);
3498 RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
3499}
3500
Steve Anton0f5400a2018-07-17 21:25:363501void PeerConnection::ProcessRemovalOfRemoteTrack(
3502 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3503 transceiver,
3504 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
3505 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
3506 RTC_DCHECK(transceiver->mid());
3507 RTC_LOG(LS_INFO) << "Processing the removal of a track for MID="
3508 << *transceiver->mid();
Henrik Boströmafa07dd2018-12-20 10:06:023509 std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
Steve Anton0f5400a2018-07-17 21:25:363510 transceiver->internal()->receiver_internal()->streams();
3511 // This will remove the remote track from the streams.
3512 transceiver->internal()->receiver_internal()->set_stream_ids({});
3513 remove_list->push_back(transceiver);
Henrik Boströmafa07dd2018-12-20 10:06:023514 RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
3515}
3516
3517void PeerConnection::RemoveRemoteStreamsIfEmpty(
3518 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams,
3519 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
3520 // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of
3521 // streams, see if the stream was removed by checking if this was the last
3522 // receiver with that stream ID.
Mirko Bonadei739baf02019-01-27 16:29:423523 for (const auto& remote_stream : remote_streams) {
Henrik Boströmafa07dd2018-12-20 10:06:023524 if (remote_stream->GetAudioTracks().empty() &&
3525 remote_stream->GetVideoTracks().empty()) {
3526 remote_streams_->RemoveStream(remote_stream);
3527 removed_streams->push_back(remote_stream);
Steve Anton0f5400a2018-07-17 21:25:363528 }
3529 }
3530}
3531
Steve Antondcc3c022017-12-23 00:02:543532RTCError PeerConnection::UpdateTransceiversAndDataChannels(
3533 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 23:33:483534 const SessionDescriptionInterface& new_session,
3535 const SessionDescriptionInterface* old_local_description,
3536 const SessionDescriptionInterface* old_remote_description) {
Steve Antondcc3c022017-12-23 00:02:543537 RTC_DCHECK(IsUnifiedPlan());
3538
Steve Anton7464fca2018-01-19 19:10:373539 const cricket::ContentGroup* bundle_group = nullptr;
3540 if (new_session.GetType() == SdpType::kOffer) {
3541 auto bundle_group_or_error =
3542 GetEarlyBundleGroup(*new_session.description());
3543 if (!bundle_group_or_error.ok()) {
3544 return bundle_group_or_error.MoveError();
3545 }
3546 bundle_group = bundle_group_or_error.MoveValue();
Steve Antondcc3c022017-12-23 00:02:543547 }
Steve Antondcc3c022017-12-23 00:02:543548
Steve Antondcc3c022017-12-23 00:02:543549 const ContentInfos& new_contents = new_session.description()->contents();
Steve Antondcc3c022017-12-23 00:02:543550 for (size_t i = 0; i < new_contents.size(); ++i) {
3551 const cricket::ContentInfo& new_content = new_contents[i];
Steve Antondcc3c022017-12-23 00:02:543552 cricket::MediaType media_type = new_content.media_description()->type();
Amit Hilbuchae3df542019-01-07 20:13:083553 mid_generator_.AddKnownId(new_content.name);
Steve Antondcc3c022017-12-23 00:02:543554 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
3555 media_type == cricket::MEDIA_TYPE_VIDEO) {
Seth Hampsonae8a90a2018-02-13 23:33:483556 const cricket::ContentInfo* old_local_content = nullptr;
3557 if (old_local_description &&
3558 i < old_local_description->description()->contents().size()) {
3559 old_local_content =
3560 &old_local_description->description()->contents()[i];
3561 }
3562 const cricket::ContentInfo* old_remote_content = nullptr;
3563 if (old_remote_description &&
3564 i < old_remote_description->description()->contents().size()) {
3565 old_remote_content =
3566 &old_remote_description->description()->contents()[i];
3567 }
Steve Antondcc3c022017-12-23 00:02:543568 auto transceiver_or_error =
Seth Hampsonae8a90a2018-02-13 23:33:483569 AssociateTransceiver(source, new_session.GetType(), i, new_content,
3570 old_local_content, old_remote_content);
Steve Antondcc3c022017-12-23 00:02:543571 if (!transceiver_or_error.ok()) {
3572 return transceiver_or_error.MoveError();
3573 }
3574 auto transceiver = transceiver_or_error.MoveValue();
Steve Antondcc3c022017-12-23 00:02:543575 RTCError error =
3576 UpdateTransceiverChannel(transceiver, new_content, bundle_group);
3577 if (!error.ok()) {
3578 return error;
3579 }
3580 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonfa2260d2017-12-29 00:38:233581 if (GetDataMid() && new_content.name != *GetDataMid()) {
3582 // Ignore all but the first data section.
Steve Anton3d954a62018-04-02 18:27:233583 RTC_LOG(LS_INFO) << "Ignoring data media section with MID="
3584 << new_content.name;
Steve Antonfa2260d2017-12-29 00:38:233585 continue;
3586 }
3587 RTCError error = UpdateDataChannel(source, new_content, bundle_group);
3588 if (!error.ok()) {
3589 return error;
3590 }
Steve Antondcc3c022017-12-23 00:02:543591 } else {
3592 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3593 "Unknown section type.");
3594 }
3595 }
3596
3597 return RTCError::OK();
3598}
3599
3600RTCError PeerConnection::UpdateTransceiverChannel(
3601 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3602 transceiver,
3603 const cricket::ContentInfo& content,
3604 const cricket::ContentGroup* bundle_group) {
3605 RTC_DCHECK(IsUnifiedPlan());
3606 RTC_DCHECK(transceiver);
Amit Hilbuchdd9390c2018-11-14 00:26:053607 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Antondcc3c022017-12-23 00:02:543608 if (content.rejected) {
3609 if (channel) {
3610 transceiver->internal()->SetChannel(nullptr);
Amit Hilbuchdd9390c2018-11-14 00:26:053611 DestroyChannelInterface(channel);
Steve Antondcc3c022017-12-23 00:02:543612 }
3613 } else {
3614 if (!channel) {
Steve Anton69470252018-02-09 19:43:083615 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Zhi Huange830e682018-03-30 17:48:353616 channel = CreateVoiceChannel(content.name);
Steve Antondcc3c022017-12-23 00:02:543617 } else {
Steve Anton69470252018-02-09 19:43:083618 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type());
Zhi Huange830e682018-03-30 17:48:353619 channel = CreateVideoChannel(content.name);
Steve Antondcc3c022017-12-23 00:02:543620 }
3621 if (!channel) {
3622 LOG_AND_RETURN_ERROR(
3623 RTCErrorType::INTERNAL_ERROR,
3624 "Failed to create channel for mid=" + content.name);
3625 }
3626 transceiver->internal()->SetChannel(channel);
3627 }
3628 }
3629 return RTCError::OK();
3630}
3631
Steve Antonfa2260d2017-12-29 00:38:233632RTCError PeerConnection::UpdateDataChannel(
3633 cricket::ContentSource source,
3634 const cricket::ContentInfo& content,
3635 const cricket::ContentGroup* bundle_group) {
Harald Alvestrand05e4d082019-12-03 13:04:213636 if (data_channel_type() == cricket::DCT_NONE) {
Steve Antondbf9d032018-01-19 23:23:403637 // If data channels are disabled, ignore this media section. CreateAnswer
3638 // will take care of rejecting it.
3639 return RTCError::OK();
Steve Antonfa2260d2017-12-29 00:38:233640 }
3641 if (content.rejected) {
Bjorn A Mellemb689af42019-08-21 17:44:593642 RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid();
Harald Alvestrand408cb4b2019-11-16 11:09:083643 DestroyDataChannelTransport();
Steve Antonfa2260d2017-12-29 00:38:233644 } else {
Harald Alvestrand00cf34c2019-12-02 08:56:023645 if (!data_channel_controller_.rtp_data_channel() &&
3646 !data_channel_controller_.data_channel_transport()) {
Bjorn A Mellemb689af42019-08-21 17:44:593647 RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid();
Zhi Huange830e682018-03-30 17:48:353648 if (!CreateDataChannel(content.name)) {
Steve Antonfa2260d2017-12-29 00:38:233649 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3650 "Failed to create data channel.");
3651 }
3652 }
3653 if (source == cricket::CS_REMOTE) {
3654 const MediaContentDescription* data_desc = content.media_description();
3655 if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) {
Harald Alvestrand05e4d082019-12-03 13:04:213656 data_channel_controller_.UpdateRemoteRtpDataChannels(
3657 GetActiveStreams(data_desc));
Steve Antonfa2260d2017-12-29 00:38:233658 }
3659 }
3660 }
3661 return RTCError::OK();
3662}
3663
Amit Hilbuchbcd39d42019-01-26 01:13:563664// This method will extract any send encodings that were sent by the remote
3665// connection. This is currently only relevant for Simulcast scenario (where
3666// the number of layers may be communicated by the server).
3667static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription(
3668 const MediaContentDescription& desc) {
3669 if (!desc.HasSimulcast()) {
3670 return {};
3671 }
3672 std::vector<RtpEncodingParameters> result;
3673 const SimulcastDescription& simulcast = desc.simulcast_description();
3674
3675 // This is a remote description, the parameters we are after should appear
3676 // as receive streams.
3677 for (const auto& alternatives : simulcast.receive_layers()) {
3678 RTC_DCHECK(!alternatives.empty());
3679 // There is currently no way to specify or choose from alternatives.
3680 // We will always use the first alternative, which is the most preferred.
3681 const SimulcastLayer& layer = alternatives[0];
3682 RtpEncodingParameters parameters;
3683 parameters.rid = layer.rid;
3684 parameters.active = !layer.is_paused;
3685 result.push_back(parameters);
3686 }
3687
3688 return result;
3689}
3690
3691static RTCError UpdateSimulcastLayerStatusInSender(
3692 const std::vector<SimulcastLayer>& layers,
Amit Hilbuch2297d332019-02-19 20:49:223693 rtc::scoped_refptr<RtpSenderInternal> sender) {
Amit Hilbuchbcd39d42019-01-26 01:13:563694 RTC_DCHECK(sender);
Amit Hilbuch619b2942019-02-26 23:55:193695 RtpParameters parameters = sender->GetParametersInternal();
Amit Hilbuch2297d332019-02-19 20:49:223696 std::vector<std::string> disabled_layers;
Amit Hilbuchbcd39d42019-01-26 01:13:563697
3698 // The simulcast envelope cannot be changed, only the status of the streams.
3699 // So we will iterate over the send encodings rather than the layers.
3700 for (RtpEncodingParameters& encoding : parameters.encodings) {
3701 auto iter = std::find_if(layers.begin(), layers.end(),
3702 [&encoding](const SimulcastLayer& layer) {
3703 return layer.rid == encoding.rid;
3704 });
3705 // A layer that cannot be found may have been removed by the remote party.
Amit Hilbuch2297d332019-02-19 20:49:223706 if (iter == layers.end()) {
3707 disabled_layers.push_back(encoding.rid);
3708 continue;
3709 }
3710
3711 encoding.active = !iter->is_paused;
Amit Hilbuchbcd39d42019-01-26 01:13:563712 }
3713
Amit Hilbuch619b2942019-02-26 23:55:193714 RTCError result = sender->SetParametersInternal(parameters);
Amit Hilbuch2297d332019-02-19 20:49:223715 if (result.ok()) {
3716 result = sender->DisableEncodingLayers(disabled_layers);
3717 }
3718
3719 return result;
Amit Hilbuchbcd39d42019-01-26 01:13:563720}
3721
Amit Hilbuchaabd0362019-03-01 21:14:463722static bool SimulcastIsRejected(
3723 const ContentInfo* local_content,
3724 const MediaContentDescription& answer_media_desc) {
3725 bool simulcast_offered = local_content &&
3726 local_content->media_description() &&
3727 local_content->media_description()->HasSimulcast();
3728 bool simulcast_answered = answer_media_desc.HasSimulcast();
3729 bool rids_supported = RtpExtension::FindHeaderExtensionByUri(
3730 answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri);
3731 return simulcast_offered && (!simulcast_answered || !rids_supported);
3732}
3733
Amit Hilbuch2297d332019-02-19 20:49:223734static RTCError DisableSimulcastInSender(
3735 rtc::scoped_refptr<RtpSenderInternal> sender) {
Amit Hilbuchbcd39d42019-01-26 01:13:563736 RTC_DCHECK(sender);
Amit Hilbuch619b2942019-02-26 23:55:193737 RtpParameters parameters = sender->GetParametersInternal();
Amit Hilbuch2297d332019-02-19 20:49:223738 if (parameters.encodings.size() <= 1) {
Amit Hilbuchbcd39d42019-01-26 01:13:563739 return RTCError::OK();
3740 }
3741
Amit Hilbuch2297d332019-02-19 20:49:223742 std::vector<std::string> disabled_layers;
3743 std::transform(
3744 parameters.encodings.begin() + 1, parameters.encodings.end(),
3745 std::back_inserter(disabled_layers),
3746 [](const RtpEncodingParameters& encoding) { return encoding.rid; });
3747 return sender->DisableEncodingLayers(disabled_layers);
Amit Hilbuchbcd39d42019-01-26 01:13:563748}
3749
Steve Antondcc3c022017-12-23 00:02:543750RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
3751PeerConnection::AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 23:33:483752 SdpType type,
Steve Antondcc3c022017-12-23 00:02:543753 size_t mline_index,
3754 const ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 23:33:483755 const ContentInfo* old_local_content,
3756 const ContentInfo* old_remote_content) {
Steve Antondcc3c022017-12-23 00:02:543757 RTC_DCHECK(IsUnifiedPlan());
Seth Hampsonae8a90a2018-02-13 23:33:483758 // If this is an offer then the m= section might be recycled. If the m=
3759 // section is being recycled (defined as: rejected in the current local or
3760 // remote description and not rejected in new description), dissociate the
3761 // currently associated RtpTransceiver by setting its mid property to null,
3762 // and discard the mapping between the transceiver and its m= section index.
3763 if (IsMediaSectionBeingRecycled(type, content, old_local_content,
3764 old_remote_content)) {
3765 // We want to dissociate the transceiver that has the rejected mid.
3766 const std::string& old_mid =
3767 (old_local_content && old_local_content->rejected)
3768 ? old_local_content->name
3769 : old_remote_content->name;
3770 auto old_transceiver = GetAssociatedTransceiver(old_mid);
Steve Antondcc3c022017-12-23 00:02:543771 if (old_transceiver) {
Steve Anton3d954a62018-04-02 18:27:233772 RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid
3773 << " since the media section is being recycled.";
Danil Chapovalov66cadcc2018-06-19 14:47:433774 old_transceiver->internal()->set_mid(absl::nullopt);
3775 old_transceiver->internal()->set_mline_index(absl::nullopt);
Steve Antondcc3c022017-12-23 00:02:543776 }
3777 }
3778 const MediaContentDescription* media_desc = content.media_description();
3779 auto transceiver = GetAssociatedTransceiver(content.name);
3780 if (source == cricket::CS_LOCAL) {
3781 // Find the RtpTransceiver that corresponds to this m= section, using the
3782 // mapping between transceivers and m= section indices established when
3783 // creating the offer.
3784 if (!transceiver) {
3785 transceiver = GetTransceiverByMLineIndex(mline_index);
3786 }
3787 if (!transceiver) {
3788 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3789 "Unknown transceiver");
3790 }
3791 } else {
3792 RTC_DCHECK_EQ(source, cricket::CS_REMOTE);
3793 // If the m= section is sendrecv or recvonly, and there are RtpTransceivers
3794 // of the same type...
Amit Hilbuchaa584152019-02-07 01:09:523795 // When simulcast is requested, a transceiver cannot be associated because
3796 // AddTrack cannot be called to initialize it.
Steve Antondcc3c022017-12-23 00:02:543797 if (!transceiver &&
Amit Hilbuchaa584152019-02-07 01:09:523798 RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
3799 !media_desc->HasSimulcast()) {
Steve Antondcc3c022017-12-23 00:02:543800 transceiver = FindAvailableTransceiverToReceive(media_desc->type());
3801 }
3802 // If no RtpTransceiver was found in the previous step, create one with a
3803 // recvonly direction.
3804 if (!transceiver) {
Steve Anton3d954a62018-04-02 18:27:233805 RTC_LOG(LS_INFO) << "Adding "
3806 << cricket::MediaTypeToString(media_desc->type())
3807 << " transceiver for MID=" << content.name
3808 << " at i=" << mline_index
3809 << " in response to the remote description.";
Steve Anton111fdfd2018-06-25 20:03:363810 std::string sender_id = rtc::CreateRandomUuid();
Amit Hilbuchbcd39d42019-01-26 01:13:563811 std::vector<RtpEncodingParameters> send_encodings =
3812 GetSendEncodingsFromRemoteDescription(*media_desc);
3813 auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {},
3814 send_encodings);
Steve Anton5f94aa22018-02-01 18:58:303815 std::string receiver_id;
3816 if (!media_desc->streams().empty()) {
3817 receiver_id = media_desc->streams()[0].id;
3818 } else {
3819 receiver_id = rtc::CreateRandomUuid();
3820 }
3821 auto receiver = CreateReceiver(media_desc->type(), receiver_id);
Steve Anton02ee47c2018-01-11 00:26:063822 transceiver = CreateAndAddTransceiver(sender, receiver);
Steve Antondcc3c022017-12-23 00:02:543823 transceiver->internal()->set_direction(
3824 RtpTransceiverDirection::kRecvOnly);
Eldar Rello5ab79e62019-10-09 15:29:443825 if (type == SdpType::kOffer) {
Eldar Rello353a7182019-11-25 16:49:443826 transceiver_stable_states_by_transceivers_[transceiver]
3827 .set_newly_created();
Eldar Rello5ab79e62019-10-09 15:29:443828 }
Steve Antondcc3c022017-12-23 00:02:543829 }
Amit Hilbuchbcd39d42019-01-26 01:13:563830 // Check if the offer indicated simulcast but the answer rejected it.
3831 // This can happen when simulcast is not supported on the remote party.
Amit Hilbuchaabd0362019-03-01 21:14:463832 if (SimulcastIsRejected(old_local_content, *media_desc)) {
Amit Hilbuche2a284d2019-03-05 20:36:313833 RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true);
Amit Hilbuchbcd39d42019-01-26 01:13:563834 RTCError error =
Amit Hilbuch2297d332019-02-19 20:49:223835 DisableSimulcastInSender(transceiver->internal()->sender_internal());
Amit Hilbuchbcd39d42019-01-26 01:13:563836 if (!error.ok()) {
3837 RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast.";
3838 return std::move(error);
3839 }
3840 }
Steve Antondcc3c022017-12-23 00:02:543841 }
3842 RTC_DCHECK(transceiver);
Steve Anton69470252018-02-09 19:43:083843 if (transceiver->media_type() != media_desc->type()) {
Steve Antondcc3c022017-12-23 00:02:543844 LOG_AND_RETURN_ERROR(
3845 RTCErrorType::INVALID_PARAMETER,
3846 "Transceiver type does not match media description type.");
3847 }
Amit Hilbuchbcd39d42019-01-26 01:13:563848 if (media_desc->HasSimulcast()) {
3849 std::vector<SimulcastLayer> layers =
3850 source == cricket::CS_LOCAL
3851 ? media_desc->simulcast_description().send_layers().GetAllLayers()
3852 : media_desc->simulcast_description()
3853 .receive_layers()
3854 .GetAllLayers();
3855 RTCError error = UpdateSimulcastLayerStatusInSender(
Amit Hilbuch2297d332019-02-19 20:49:223856 layers, transceiver->internal()->sender_internal());
Amit Hilbuchbcd39d42019-01-26 01:13:563857 if (!error.ok()) {
3858 RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers.";
3859 return std::move(error);
3860 }
3861 }
Eldar Rello5ab79e62019-10-09 15:29:443862 if (type == SdpType::kOffer) {
Eldar Rello353a7182019-11-25 16:49:443863 bool state_changes = transceiver->internal()->mid() != content.name ||
3864 transceiver->internal()->mline_index() != mline_index;
3865 if (state_changes) {
3866 transceiver_stable_states_by_transceivers_[transceiver]
3867 .SetMSectionIfUnset(transceiver->internal()->mid(),
3868 transceiver->internal()->mline_index());
Eldar Rello5ab79e62019-10-09 15:29:443869 }
3870 }
Steve Antondcc3c022017-12-23 00:02:543871 // Associate the found or created RtpTransceiver with the m= section by
3872 // setting the value of the RtpTransceiver's mid property to the MID of the m=
3873 // section, and establish a mapping between the transceiver and the index of
3874 // the m= section.
3875 transceiver->internal()->set_mid(content.name);
3876 transceiver->internal()->set_mline_index(mline_index);
3877 return std::move(transceiver);
3878}
3879
3880rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3881PeerConnection::GetAssociatedTransceiver(const std::string& mid) const {
3882 RTC_DCHECK(IsUnifiedPlan());
3883 for (auto transceiver : transceivers_) {
3884 if (transceiver->mid() == mid) {
3885 return transceiver;
3886 }
3887 }
3888 return nullptr;
3889}
3890
3891rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3892PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const {
3893 RTC_DCHECK(IsUnifiedPlan());
3894 for (auto transceiver : transceivers_) {
3895 if (transceiver->internal()->mline_index() == mline_index) {
3896 return transceiver;
3897 }
3898 }
3899 return nullptr;
3900}
3901
3902rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3903PeerConnection::FindAvailableTransceiverToReceive(
3904 cricket::MediaType media_type) const {
3905 RTC_DCHECK(IsUnifiedPlan());
3906 // From JSEP section 5.10 (Applying a Remote Description):
3907 // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of
3908 // the same type that were added to the PeerConnection by addTrack and are not
3909 // associated with any m= section and are not stopped, find the first such
3910 // RtpTransceiver.
3911 for (auto transceiver : transceivers_) {
Steve Anton69470252018-02-09 19:43:083912 if (transceiver->media_type() == media_type &&
Steve Antondcc3c022017-12-23 00:02:543913 transceiver->internal()->created_by_addtrack() && !transceiver->mid() &&
3914 !transceiver->stopped()) {
3915 return transceiver;
3916 }
3917 }
3918 return nullptr;
3919}
3920
Steve Antoned10bd92017-12-05 18:52:593921const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver(
3922 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3923 transceiver,
3924 const SessionDescriptionInterface* sdesc) const {
3925 RTC_DCHECK(transceiver);
3926 RTC_DCHECK(sdesc);
3927 if (IsUnifiedPlan()) {
3928 if (!transceiver->internal()->mid()) {
3929 // This transceiver is not associated with a media section yet.
3930 return nullptr;
3931 }
3932 return sdesc->description()->GetContentByName(
3933 *transceiver->internal()->mid());
3934 } else {
3935 // Plan B only allows at most one audio and one video section, so use the
3936 // first media section of that type.
3937 return cricket::GetFirstMediaContent(sdesc->description()->contents(),
Steve Anton69470252018-02-09 19:43:083938 transceiver->media_type());
Steve Antoned10bd92017-12-05 18:52:593939 }
3940}
3941
deadbeef46c73892016-11-17 03:42:043942PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() {
Karl Wiberg5966c502019-02-21 22:55:093943 RTC_DCHECK_RUN_ON(signaling_thread());
deadbeef46c73892016-11-17 03:42:043944 return configuration_;
3945}
3946
Niels Möller2579f0c2019-08-19 07:58:173947RTCError PeerConnection::SetConfiguration(
3948 const RTCConfiguration& configuration) {
Karl Wiberg5966c502019-02-21 22:55:093949 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 21:15:173950 TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration");
Steve Antonc79268f2018-04-24 16:54:103951 if (IsClosed()) {
Niels Möller2579f0c2019-08-19 07:58:173952 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
3953 "SetConfiguration: PeerConnection is closed.");
Steve Antonc79268f2018-04-24 16:54:103954 }
3955
Qingsi Wanga2d60672018-04-11 23:57:453956 // According to JSEP, after setLocalDescription, changing the candidate pool
3957 // size is not allowed, and changing the set of ICE servers will not result
3958 // in new candidates being gathered.
Steve Anton75737c02017-11-06 18:37:173959 if (local_description() && configuration.ice_candidate_pool_size !=
3960 configuration_.ice_candidate_pool_size) {
Niels Möller2579f0c2019-08-19 07:58:173961 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
3962 "Can't change candidate pool size after calling "
3963 "SetLocalDescription.");
buildbot@webrtc.org41451d42014-05-03 05:39:453964 }
Taylor Brandstettera1c30352016-05-13 15:15:113965
Piotr (Peter) Slatalaaa1e7c22018-10-16 17:04:453966 if (local_description() &&
Benjamin Wright8c27cca2018-10-25 17:16:443967 configuration.crypto_options != configuration_.crypto_options) {
Niels Möller2579f0c2019-08-19 07:58:173968 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
3969 "Can't change crypto_options after calling "
3970 "SetLocalDescription.");
Benjamin Wright8c27cca2018-10-25 17:16:443971 }
3972
deadbeef293e9262017-01-11 20:28:303973 // The simplest (and most future-compatible) way to tell if the config was
3974 // modified in an invalid way is to copy each property we do support
3975 // modifying, then use operator==. There are far more properties we don't
3976 // support modifying than those we do, and more could be added.
3977 RTCConfiguration modified_config = configuration_;
3978 modified_config.servers = configuration.servers;
3979 modified_config.type = configuration.type;
3980 modified_config.ice_candidate_pool_size =
3981 configuration.ice_candidate_pool_size;
3982 modified_config.prune_turn_ports = configuration.prune_turn_ports;
Honghai Zhangf8998cf2019-10-14 18:27:503983 modified_config.turn_port_prune_policy = configuration.turn_port_prune_policy;
Qingsi Wangbca14852019-06-26 21:56:023984 modified_config.surface_ice_candidates_on_ice_transport_type_changed =
3985 configuration.surface_ice_candidates_on_ice_transport_type_changed;
skvladd1f5fda2017-02-04 00:54:053986 modified_config.ice_check_min_interval = configuration.ice_check_min_interval;
Qingsi Wange6826d22018-03-08 22:55:143987 modified_config.ice_check_interval_strong_connectivity =
3988 configuration.ice_check_interval_strong_connectivity;
3989 modified_config.ice_check_interval_weak_connectivity =
3990 configuration.ice_check_interval_weak_connectivity;
Qingsi Wang22e623a2018-03-13 17:53:573991 modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout;
3992 modified_config.ice_unwritable_min_checks =
3993 configuration.ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-07 07:30:173994 modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout;
Qingsi Wangdb53f8e2018-02-20 22:45:493995 modified_config.stun_candidate_keepalive_interval =
3996 configuration.stun_candidate_keepalive_interval;
Jonas Orelandbdcee282017-10-10 12:01:403997 modified_config.turn_customizer = configuration.turn_customizer;
Qingsi Wang9a5c6f82018-02-01 18:38:403998 modified_config.network_preference = configuration.network_preference;
Zhi Huangb57e1692018-06-12 18:41:113999 modified_config.active_reset_srtp_params =
4000 configuration.active_reset_srtp_params;
Jonas Oreland3c028422019-08-22 14:16:354001 modified_config.turn_logging_id = configuration.turn_logging_id;
philipel16cec3b2019-10-25 10:23:024002 modified_config.allow_codec_switching = configuration.allow_codec_switching;
deadbeef293e9262017-01-11 20:28:304003 if (configuration != modified_config) {
Niels Möller2579f0c2019-08-19 07:58:174004 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
4005 "Modifying the configuration in an unsupported way.");
deadbeef293e9262017-01-11 20:28:304006 }
4007
Steve Anton038834f2017-07-14 22:59:594008 // Validate the modified configuration.
4009 RTCError validate_error = ValidateConfiguration(modified_config);
4010 if (!validate_error.ok()) {
Niels Möller2579f0c2019-08-19 07:58:174011 return validate_error;
Steve Anton038834f2017-07-14 22:59:594012 }
4013
deadbeef293e9262017-01-11 20:28:304014 // Note that this isn't possible through chromium, since it's an unsigned
4015 // short in WebIDL.
4016 if (configuration.ice_candidate_pool_size < 0 ||
Wez939eb802018-05-03 10:34:174017 configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) {
Niels Möller2579f0c2019-08-19 07:58:174018 return RTCError(RTCErrorType::INVALID_RANGE);
deadbeef293e9262017-01-11 20:28:304019 }
4020
4021 // Parse ICE servers before hopping to network thread.
4022 cricket::ServerAddresses stun_servers;
4023 std::vector<cricket::RelayServerConfig> turn_servers;
4024 RTCErrorType parse_error =
4025 ParseIceServers(configuration.servers, &stun_servers, &turn_servers);
4026 if (parse_error != RTCErrorType::NONE) {
Niels Möller2579f0c2019-08-19 07:58:174027 return RTCError(parse_error);
deadbeef293e9262017-01-11 20:28:304028 }
Jonas Oreland3c028422019-08-22 14:16:354029 // Add the turn logging id to all turn servers
4030 for (cricket::RelayServerConfig& turn_server : turn_servers) {
4031 turn_server.turn_logging_id = configuration.turn_logging_id;
4032 }
4033
Harald Alvestrand8ebba742018-05-31 12:00:344034 // Note if STUN or TURN servers were supplied.
4035 if (!stun_servers.empty()) {
4036 NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED);
4037 }
4038 if (!turn_servers.empty()) {
4039 NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED);
4040 }
deadbeef293e9262017-01-11 20:28:304041
4042 // In theory this shouldn't fail.
4043 if (!network_thread()->Invoke<bool>(
4044 RTC_FROM_HERE,
4045 rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this,
4046 stun_servers, turn_servers, modified_config.type,
4047 modified_config.ice_candidate_pool_size,
Honghai Zhangf8998cf2019-10-14 18:27:504048 modified_config.GetTurnPortPrunePolicy(),
Qingsi Wangdb53f8e2018-02-20 22:45:494049 modified_config.turn_customizer,
Karl Wiberg739506e2019-04-03 09:37:284050 modified_config.stun_candidate_keepalive_interval,
4051 static_cast<bool>(local_description())))) {
Niels Möller2579f0c2019-08-19 07:58:174052 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4053 "Failed to apply configuration to PortAllocator.");
deadbeef293e9262017-01-11 20:28:304054 }
Honghai Zhang4cedf2b2016-08-31 15:18:114055
deadbeefd1a38b52016-12-10 21:15:334056 // As described in JSEP, calling setConfiguration with new ICE servers or
4057 // candidate policy must set a "needs-ice-restart" bit so that the next offer
4058 // triggers an ICE restart which will pick up the changes.
deadbeef293e9262017-01-11 20:28:304059 if (modified_config.servers != configuration_.servers ||
Jonas Orelande3096512020-05-27 07:01:054060 NeedIceRestart(
4061 configuration_.surface_ice_candidates_on_ice_transport_type_changed,
4062 configuration_.type, modified_config.type) ||
Honghai Zhangf8998cf2019-10-14 18:27:504063 modified_config.GetTurnPortPrunePolicy() !=
4064 configuration_.GetTurnPortPrunePolicy()) {
Steve Antond25da372017-11-06 22:50:294065 transport_controller_->SetNeedsIceRestartFlag();
deadbeefd1a38b52016-12-10 21:15:334066 }
skvladd1f5fda2017-02-04 00:54:054067
Qingsi Wang9c98f0c2018-02-15 23:10:594068 transport_controller_->SetIceConfig(ParseIceConfig(modified_config));
Bjorn A Mellem5985a042019-06-28 21:19:384069
Zhi Huangb57e1692018-06-12 18:41:114070 if (configuration_.active_reset_srtp_params !=
4071 modified_config.active_reset_srtp_params) {
4072 transport_controller_->SetActiveResetSrtpParams(
4073 modified_config.active_reset_srtp_params);
4074 }
4075
philipel16cec3b2019-10-25 10:23:024076 if (modified_config.allow_codec_switching.has_value()) {
Taylor Brandstetter6b381c72020-08-24 20:28:474077 for (const auto& transceiver : transceivers_) {
4078 if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO ||
4079 !transceiver->internal()->channel()) {
4080 continue;
4081 }
4082 auto* video_channel = static_cast<cricket::VideoChannel*>(
4083 transceiver->internal()->channel());
4084 video_channel->media_channel()->SetVideoCodecSwitchingEnabled(
philipel01294f02019-11-14 12:03:254085 *modified_config.allow_codec_switching);
4086 }
philipel16cec3b2019-10-25 10:23:024087 }
4088
deadbeef293e9262017-01-11 20:28:304089 configuration_ = modified_config;
Niels Möller2579f0c2019-08-19 07:58:174090 return RTCError::OK();
buildbot@webrtc.org41451d42014-05-03 05:39:454091}
4092
henrike@webrtc.org28e20752013-07-10 00:45:364093bool PeerConnection::AddIceCandidate(
4094 const IceCandidateInterface* ice_candidate) {
Karl Wiberg744310f2019-02-14 09:18:564095 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 21:15:174096 TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate");
zhihuang29ff8442016-07-27 18:07:254097 if (IsClosed()) {
Steve Antonc79268f2018-04-24 16:54:104098 RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed.";
Harald Alvestrand76829d72018-07-18 21:24:364099 NoteAddIceCandidateResult(kAddIceCandidateFailClosed);
zhihuang29ff8442016-07-27 18:07:254100 return false;
4101 }
Steve Antond25da372017-11-06 22:50:294102
4103 if (!remote_description()) {
Steve Antonc79268f2018-04-24 16:54:104104 RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added "
Jonas Olsson45cc8902018-02-13 09:37:074105 "without any remote session description.";
Harald Alvestrand76829d72018-07-18 21:24:364106 NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription);
Steve Antond25da372017-11-06 22:50:294107 return false;
4108 }
4109
4110 if (!ice_candidate) {
Steve Antonc79268f2018-04-24 16:54:104111 RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null.";
Harald Alvestrand76829d72018-07-18 21:24:364112 NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate);
Steve Antond25da372017-11-06 22:50:294113 return false;
4114 }
4115
4116 bool valid = false;
4117 bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid);
4118 if (!valid) {
Harald Alvestrand76829d72018-07-18 21:24:364119 NoteAddIceCandidateResult(kAddIceCandidateFailNotValid);
Steve Antond25da372017-11-06 22:50:294120 return false;
4121 }
4122
4123 // Add this candidate to the remote session description.
4124 if (!mutable_remote_description()->AddCandidate(ice_candidate)) {
Steve Antonc79268f2018-04-24 16:54:104125 RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used.";
Harald Alvestrand76829d72018-07-18 21:24:364126 NoteAddIceCandidateResult(kAddIceCandidateFailInAddition);
Steve Antond25da372017-11-06 22:50:294127 return false;
4128 }
4129
4130 if (ready) {
Harald Alvestrand76829d72018-07-18 21:24:364131 bool result = UseCandidate(ice_candidate);
4132 if (result) {
Qingsi Wang1ba5dec2019-08-19 18:57:174133 NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED);
Harald Alvestrand76829d72018-07-18 21:24:364134 NoteAddIceCandidateResult(kAddIceCandidateSuccess);
4135 } else {
4136 NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable);
4137 }
4138 return result;
Steve Antond25da372017-11-06 22:50:294139 } else {
Steve Antonc79268f2018-04-24 16:54:104140 RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate.";
Harald Alvestrand76829d72018-07-18 21:24:364141 NoteAddIceCandidateResult(kAddIceCandidateFailNotReady);
Steve Antond25da372017-11-06 22:50:294142 return true;
4143 }
henrike@webrtc.org28e20752013-07-10 00:45:364144}
4145
Henrik Boströmee6f4f62019-11-06 11:36:124146void PeerConnection::AddIceCandidate(
4147 std::unique_ptr<IceCandidateInterface> candidate,
4148 std::function<void(RTCError)> callback) {
4149 RTC_DCHECK_RUN_ON(signaling_thread());
4150 // Chain this operation. If asynchronous operations are pending on the chain,
4151 // this operation will be queued to be invoked, otherwise the contents of the
4152 // lambda will execute immediately.
4153 operations_chain_->ChainOperation(
4154 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
4155 candidate = std::move(candidate), callback = std::move(callback)](
4156 std::function<void()> operations_chain_callback) {
4157 if (!this_weak_ptr) {
4158 operations_chain_callback();
4159 callback(RTCError(
4160 RTCErrorType::INVALID_STATE,
4161 "AddIceCandidate failed because the session was shut down"));
4162 return;
4163 }
4164 if (!this_weak_ptr->AddIceCandidate(candidate.get())) {
4165 operations_chain_callback();
4166 // Fail with an error type and message consistent with Chromium.
4167 // TODO(hbos): Fail with error types according to spec.
4168 callback(RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
4169 "Error processing ICE candidate"));
4170 return;
4171 }
4172 operations_chain_callback();
4173 callback(RTCError::OK());
4174 });
4175}
4176
Honghai Zhang7fb69db2016-03-14 18:59:184177bool PeerConnection::RemoveIceCandidates(
4178 const std::vector<cricket::Candidate>& candidates) {
4179 TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates");
Karl Wiberg2cc368f2019-04-02 09:31:564180 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonc79268f2018-04-24 16:54:104181 if (IsClosed()) {
4182 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed.";
4183 return false;
4184 }
4185
Steve Antond25da372017-11-06 22:50:294186 if (!remote_description()) {
Steve Antonc79268f2018-04-24 16:54:104187 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed "
4188 "without any remote session description.";
Steve Antond25da372017-11-06 22:50:294189 return false;
4190 }
4191
4192 if (candidates.empty()) {
Steve Antonc79268f2018-04-24 16:54:104193 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty.";
Steve Antond25da372017-11-06 22:50:294194 return false;
4195 }
4196
4197 size_t number_removed =
4198 mutable_remote_description()->RemoveCandidates(candidates);
4199 if (number_removed != candidates.size()) {
Mirko Bonadei675513b2017-11-09 10:09:254200 RTC_LOG(LS_ERROR)
Steve Antonc79268f2018-04-24 16:54:104201 << "RemoveIceCandidates: Failed to remove candidates. Requested "
Jonas Olsson45cc8902018-02-13 09:37:074202 << candidates.size() << " but only " << number_removed
Mirko Bonadei675513b2017-11-09 10:09:254203 << " are removed.";
Steve Antond25da372017-11-06 22:50:294204 }
4205
4206 // Remove the candidates from the transport controller.
Zhi Huange830e682018-03-30 17:48:354207 RTCError error = transport_controller_->RemoveRemoteCandidates(candidates);
4208 if (!error.ok()) {
Steve Antonc79268f2018-04-24 16:54:104209 RTC_LOG(LS_ERROR)
4210 << "RemoveIceCandidates: Error when removing remote candidates: "
4211 << error.message();
Steve Antond25da372017-11-06 22:50:294212 }
4213 return true;
Honghai Zhang7fb69db2016-03-14 18:59:184214}
4215
Niels Möller0c4f7be2018-05-07 12:01:374216RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) {
Steve Anton978b8762017-09-29 19:15:024217 if (!worker_thread()->IsCurrent()) {
4218 return worker_thread()->Invoke<RTCError>(
Yves Gerey665174f2018-06-19 13:03:054219 RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); });
zstein4b979802017-06-02 21:37:374220 }
Karl Wiberg6cab5c82019-03-26 08:57:014221 RTC_DCHECK_RUN_ON(worker_thread());
zstein4b979802017-06-02 21:37:374222
Niels Möller0c4f7be2018-05-07 12:01:374223 const bool has_min = bitrate.min_bitrate_bps.has_value();
4224 const bool has_start = bitrate.start_bitrate_bps.has_value();
4225 const bool has_max = bitrate.max_bitrate_bps.has_value();
zstein4b979802017-06-02 21:37:374226 if (has_min && *bitrate.min_bitrate_bps < 0) {
4227 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4228 "min_bitrate_bps <= 0");
4229 }
Niels Möller0c4f7be2018-05-07 12:01:374230 if (has_start) {
4231 if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) {
zstein4b979802017-06-02 21:37:374232 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
Niels Möller0c4f7be2018-05-07 12:01:374233 "start_bitrate_bps < min_bitrate_bps");
4234 } else if (*bitrate.start_bitrate_bps < 0) {
zstein4b979802017-06-02 21:37:374235 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4236 "curent_bitrate_bps < 0");
4237 }
4238 }
4239 if (has_max) {
Yves Gerey665174f2018-06-19 13:03:054240 if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) {
zstein4b979802017-06-02 21:37:374241 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
Niels Möller0c4f7be2018-05-07 12:01:374242 "max_bitrate_bps < start_bitrate_bps");
zstein4b979802017-06-02 21:37:374243 } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) {
4244 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4245 "max_bitrate_bps < min_bitrate_bps");
4246 } else if (*bitrate.max_bitrate_bps < 0) {
4247 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4248 "max_bitrate_bps < 0");
4249 }
4250 }
4251
zstein4b979802017-06-02 21:37:374252 RTC_DCHECK(call_.get());
Piotr (Peter) Slatala7fbfaa42019-03-18 17:31:544253 call_->SetClientBitratePreferences(bitrate);
zstein4b979802017-06-02 21:37:374254
4255 return RTCError::OK();
4256}
4257
henrika5f6bf242017-11-01 10:06:564258void PeerConnection::SetAudioPlayout(bool playout) {
4259 if (!worker_thread()->IsCurrent()) {
4260 worker_thread()->Invoke<void>(
4261 RTC_FROM_HERE,
4262 rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout));
4263 return;
4264 }
4265 auto audio_state =
Sebastian Jansson6eb8a162018-11-16 10:29:554266 factory_->channel_manager()->media_engine()->voice().GetAudioState();
henrika5f6bf242017-11-01 10:06:564267 audio_state->SetPlayout(playout);
4268}
4269
4270void PeerConnection::SetAudioRecording(bool recording) {
4271 if (!worker_thread()->IsCurrent()) {
4272 worker_thread()->Invoke<void>(
4273 RTC_FROM_HERE,
4274 rtc::Bind(&PeerConnection::SetAudioRecording, this, recording));
4275 return;
4276 }
4277 auto audio_state =
Sebastian Jansson6eb8a162018-11-16 10:29:554278 factory_->channel_manager()->media_engine()->voice().GetAudioState();
henrika5f6bf242017-11-01 10:06:564279 audio_state->SetRecording(recording);
4280}
4281
Steve Anton8c0f7a72017-10-03 17:03:104282std::unique_ptr<rtc::SSLCertificate>
4283PeerConnection::GetRemoteAudioSSLCertificate() {
Taylor Brandstetterc3928662018-02-23 21:04:514284 std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain();
4285 if (!chain || !chain->GetSize()) {
Steve Anton8c0f7a72017-10-03 17:03:104286 return nullptr;
4287 }
Steve Antonf25303e2018-10-16 22:23:314288 return chain->Get(0).Clone();
Steve Anton8c0f7a72017-10-03 17:03:104289}
4290
Zhi Huang70b820f2018-01-27 22:16:154291std::unique_ptr<rtc::SSLCertChain>
4292PeerConnection::GetRemoteAudioSSLCertChain() {
Karl Wiberga58e1692019-03-26 12:33:434293 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonafb0bb72018-02-20 19:35:374294 auto audio_transceiver = GetFirstAudioTransceiver();
4295 if (!audio_transceiver || !audio_transceiver->internal()->channel()) {
Zhi Huang70b820f2018-01-27 22:16:154296 return nullptr;
4297 }
Zhi Huang70b820f2018-01-27 22:16:154298 return transport_controller_->GetRemoteSSLCertChain(
Steve Antonafb0bb72018-02-20 19:35:374299 audio_transceiver->internal()->channel()->transport_name());
4300}
4301
4302rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
4303PeerConnection::GetFirstAudioTransceiver() const {
4304 for (auto transceiver : transceivers_) {
4305 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
4306 return transceiver;
4307 }
4308 }
4309 return nullptr;
Zhi Huang70b820f2018-01-27 22:16:154310}
4311
Henrik Boström4c1e7cc2020-06-11 10:26:534312void PeerConnection::AddAdaptationResource(
4313 rtc::scoped_refptr<Resource> resource) {
4314 if (!worker_thread()->IsCurrent()) {
4315 return worker_thread()->Invoke<void>(RTC_FROM_HERE, [this, resource]() {
4316 return AddAdaptationResource(resource);
4317 });
4318 }
4319 RTC_DCHECK_RUN_ON(worker_thread());
4320 if (!call_) {
4321 // The PeerConnection has been closed.
4322 return;
4323 }
4324 call_->AddAdaptationResource(resource);
4325}
4326
Bjorn Tereliusde939432017-11-20 16:38:144327bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
4328 int64_t output_period_ms) {
Bjorn Tereliusde939432017-11-20 16:38:144329 return worker_thread()->Invoke<bool>(
Danil Chapovalov116ffe72019-09-05 08:21:114330 RTC_FROM_HERE,
4331 [this, output = std::move(output), output_period_ms]() mutable {
4332 return StartRtcEventLog_w(std::move(output), output_period_ms);
4333 });
ivoc14d5dbe2016-07-04 14:06:554334}
4335
Niels Möllerf00ca1a2019-05-10 09:33:124336bool PeerConnection::StartRtcEventLog(
4337 std::unique_ptr<RtcEventLogOutput> output) {
Niels Möller695cf6a2019-05-13 10:27:234338 int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput;
4339 if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) {
4340 output_period_ms = 5000;
4341 }
4342 return StartRtcEventLog(std::move(output), output_period_ms);
Niels Möllerf00ca1a2019-05-10 09:33:124343}
4344
ivoc14d5dbe2016-07-04 14:06:554345void PeerConnection::StopRtcEventLog() {
Steve Anton978b8762017-09-29 19:15:024346 worker_thread()->Invoke<void>(
ivoc14d5dbe2016-07-04 14:06:554347 RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this));
4348}
4349
Harald Alvestrandad88c882018-11-28 15:47:464350rtc::scoped_refptr<DtlsTransportInterface>
4351PeerConnection::LookupDtlsTransportByMid(const std::string& mid) {
Karl Wiberg2cc368f2019-04-02 09:31:564352 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrandad88c882018-11-28 15:47:464353 return transport_controller_->LookupDtlsTransportByMid(mid);
4354}
4355
Harald Alvestrand4a7b3ac2019-01-17 09:39:404356rtc::scoped_refptr<DtlsTransport>
4357PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) {
Karl Wiberg2cc368f2019-04-02 09:31:564358 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand4a7b3ac2019-01-17 09:39:404359 return transport_controller_->LookupDtlsTransportByMid(mid);
4360}
4361
Harald Alvestrandc85328f2019-02-28 06:51:004362rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport()
4363 const {
Karl Wiberg2cc368f2019-04-02 09:31:564364 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand7a829a82020-02-12 06:38:214365 if (!sctp_mid_s_) {
Bjorn A Mellembc3eebc2019-09-23 21:53:544366 return nullptr;
4367 }
Harald Alvestrand7a829a82020-02-12 06:38:214368 return transport_controller_->GetSctpTransport(*sctp_mid_s_);
Harald Alvestrandc85328f2019-02-28 06:51:004369}
4370
henrike@webrtc.org28e20752013-07-10 00:45:364371const SessionDescriptionInterface* PeerConnection::local_description() const {
Karl Wiberg739506e2019-04-03 09:37:284372 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:174373 return pending_local_description_ ? pending_local_description_.get()
4374 : current_local_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:364375}
4376
4377const SessionDescriptionInterface* PeerConnection::remote_description() const {
Karl Wiberg739506e2019-04-03 09:37:284378 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:174379 return pending_remote_description_ ? pending_remote_description_.get()
4380 : current_remote_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:364381}
4382
deadbeeffe4a8a42016-12-21 01:56:174383const SessionDescriptionInterface* PeerConnection::current_local_description()
4384 const {
Karl Wiberg739506e2019-04-03 09:37:284385 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:174386 return current_local_description_.get();
deadbeeffe4a8a42016-12-21 01:56:174387}
4388
4389const SessionDescriptionInterface* PeerConnection::current_remote_description()
4390 const {
Karl Wiberg739506e2019-04-03 09:37:284391 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:174392 return current_remote_description_.get();
deadbeeffe4a8a42016-12-21 01:56:174393}
4394
4395const SessionDescriptionInterface* PeerConnection::pending_local_description()
4396 const {
Karl Wiberg739506e2019-04-03 09:37:284397 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:174398 return pending_local_description_.get();
deadbeeffe4a8a42016-12-21 01:56:174399}
4400
4401const SessionDescriptionInterface* PeerConnection::pending_remote_description()
4402 const {
Karl Wiberg739506e2019-04-03 09:37:284403 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:174404 return pending_remote_description_.get();
deadbeeffe4a8a42016-12-21 01:56:174405}
4406
henrike@webrtc.org28e20752013-07-10 00:45:364407void PeerConnection::Close() {
Karl Wiberg744310f2019-02-14 09:18:564408 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 21:15:174409 TRACE_EVENT0("webrtc", "PeerConnection::Close");
henrike@webrtc.org28e20752013-07-10 00:45:364410 // Update stats here so that we have the most recent stats for tracks and
4411 // streams before the channels are closed.
tommi@webrtc.org03505bc2014-07-14 20:15:264412 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:364413
Steve Anton75737c02017-11-06 18:37:174414 ChangeSignalingState(PeerConnectionInterface::kClosed);
Harald Alvestrand8ebba742018-05-31 12:00:344415 NoteUsageEvent(UsageEvent::CLOSE_CALLED);
Steve Anton3fe1b152017-12-12 18:20:084416
Mirko Bonadei739baf02019-01-27 16:29:424417 for (const auto& transceiver : transceivers_) {
Harald Alvestrand6060df52020-08-11 07:54:024418 transceiver->internal()->SetPeerConnectionClosed();
4419 if (!transceiver->stopped())
4420 transceiver->StopInternal();
Steve Anton8af21862017-12-15 19:20:134421 }
Steve Anton25cfeb92018-04-26 18:44:004422
4423 // Ensure that all asynchronous stats requests are completed before destroying
4424 // the transport controller below.
4425 if (stats_collector_) {
4426 stats_collector_->WaitForPendingRequest();
4427 }
4428
4429 // Don't destroy BaseChannels until after stats has been cleaned up so that
4430 // the last stats request can still read from the channels.
Steve Anton8af21862017-12-15 19:20:134431 DestroyAllChannels();
Steve Anton75737c02017-11-06 18:37:174432
Qingsi Wang93a84392018-01-31 01:13:094433 // The event log is used in the transport controller, which must be outlived
4434 // by the former. CreateOffer by the peer connection is implemented
4435 // asynchronously and if the peer connection is closed without resetting the
4436 // WebRTC session description factory, the session description factory would
4437 // call the transport controller.
4438 webrtc_session_desc_factory_.reset();
4439 transport_controller_.reset();
4440
deadbeef42a42632017-03-10 23:18:004441 network_thread()->Invoke<void>(
Yves Gerey665174f2018-06-19 13:03:054442 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
4443 port_allocator_.get()));
nisseeaabdf62017-05-05 09:23:024444
Steve Anton978b8762017-09-29 19:15:024445 worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
Karl Wibergb03ab712019-02-14 10:59:574446 RTC_DCHECK_RUN_ON(worker_thread());
eladalon248fd4f2017-09-06 12:18:154447 call_.reset();
4448 // The event log must outlive call (and any other object that uses it).
4449 event_log_.reset();
4450 });
Harald Alvestrand8ebba742018-05-31 12:00:344451 ReportUsagePattern();
Harald Alvestrand7a1c7f72018-08-01 08:50:164452 // The .h file says that observer can be discarded after close() returns.
4453 // Make sure this is true.
4454 observer_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:364455}
4456
Steve Antonad182762018-09-05 20:22:404457void PeerConnection::OnMessage(rtc::Message* msg) {
Karl Wiberg744310f2019-02-14 09:18:564458 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonad182762018-09-05 20:22:404459 switch (msg->message_id) {
4460 case MSG_SET_SESSIONDESCRIPTION_SUCCESS: {
4461 SetSessionDescriptionMsg* param =
4462 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
4463 param->observer->OnSuccess();
4464 delete param;
4465 break;
4466 }
4467 case MSG_SET_SESSIONDESCRIPTION_FAILED: {
4468 SetSessionDescriptionMsg* param =
4469 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
4470 param->observer->OnFailure(std::move(param->error));
4471 delete param;
4472 break;
4473 }
4474 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
4475 CreateSessionDescriptionMsg* param =
4476 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
4477 param->observer->OnFailure(std::move(param->error));
4478 delete param;
4479 break;
4480 }
4481 case MSG_GETSTATS: {
4482 GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata);
4483 StatsReports reports;
4484 stats_->GetStats(param->track, &reports);
4485 param->observer->OnComplete(reports);
4486 delete param;
4487 break;
4488 }
Steve Antonad182762018-09-05 20:22:404489 case MSG_REPORT_USAGE_PATTERN: {
4490 ReportUsagePattern();
4491 break;
4492 }
4493 default:
4494 RTC_NOTREACHED() << "Not implemented";
4495 break;
4496 }
4497}
4498
Steve Antonafb0bb72018-02-20 19:35:374499cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const {
4500 RTC_DCHECK(!IsUnifiedPlan());
4501 auto* voice_channel = static_cast<cricket::VoiceChannel*>(
4502 GetAudioTransceiver()->internal()->channel());
4503 if (voice_channel) {
4504 return voice_channel->media_channel();
4505 } else {
4506 return nullptr;
4507 }
4508}
4509
4510cricket::VideoMediaChannel* PeerConnection::video_media_channel() const {
4511 RTC_DCHECK(!IsUnifiedPlan());
4512 auto* video_channel = static_cast<cricket::VideoChannel*>(
4513 GetVideoTransceiver()->internal()->channel());
4514 if (video_channel) {
4515 return video_channel->media_channel();
4516 } else {
4517 return nullptr;
4518 }
4519}
4520
Steve Anton4171afb2017-11-20 18:20:224521void PeerConnection::CreateAudioReceiver(
4522 MediaStreamInterface* stream,
4523 const RtpSenderInfo& remote_sender_info) {
Henrik Boström9e6fd2b2017-11-21 12:41:514524 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
4525 streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
Henrik Boström199e27b2018-07-04 18:51:534526 // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use
4527 // the constructor taking stream IDs instead.
Steve Antond3679212018-01-18 01:41:024528 auto* audio_receiver = new AudioRtpReceiver(
4529 worker_thread(), remote_sender_info.sender_id, streams);
Amit Hilbuchdd9390c2018-11-14 00:26:054530 audio_receiver->SetMediaChannel(voice_media_channel());
Saurav Das7262fc22019-09-11 23:23:054531 if (remote_sender_info.sender_id == kDefaultAudioSenderId) {
4532 audio_receiver->SetupUnsignaledMediaChannel();
4533 } else {
4534 audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc);
4535 }
Steve Antond3679212018-01-18 01:41:024536 auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
4537 signaling_thread(), audio_receiver);
Steve Anton4171afb2017-11-20 18:20:224538 GetAudioTransceiver()->internal()->AddReceiver(receiver);
Mirko Bonadei05cf6be2019-01-31 20:38:124539 Observer()->OnAddTrack(receiver, streams);
Harald Alvestrand8ebba742018-05-31 12:00:344540 NoteUsageEvent(UsageEvent::AUDIO_ADDED);
henrike@webrtc.org28e20752013-07-10 00:45:364541}
4542
Steve Anton4171afb2017-11-20 18:20:224543void PeerConnection::CreateVideoReceiver(
4544 MediaStreamInterface* stream,
4545 const RtpSenderInfo& remote_sender_info) {
Henrik Boström9e6fd2b2017-11-21 12:41:514546 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
4547 streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
Henrik Boström199e27b2018-07-04 18:51:534548 // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use
4549 // the constructor taking stream IDs instead.
Steve Antond3679212018-01-18 01:41:024550 auto* video_receiver = new VideoRtpReceiver(
4551 worker_thread(), remote_sender_info.sender_id, streams);
Amit Hilbuchdd9390c2018-11-14 00:26:054552 video_receiver->SetMediaChannel(video_media_channel());
Saurav Das7262fc22019-09-11 23:23:054553 if (remote_sender_info.sender_id == kDefaultVideoSenderId) {
4554 video_receiver->SetupUnsignaledMediaChannel();
4555 } else {
4556 video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc);
4557 }
Steve Antond3679212018-01-18 01:41:024558 auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
4559 signaling_thread(), video_receiver);
Steve Anton4171afb2017-11-20 18:20:224560 GetVideoTransceiver()->internal()->AddReceiver(receiver);
Mirko Bonadei05cf6be2019-01-31 20:38:124561 Observer()->OnAddTrack(receiver, streams);
Harald Alvestrand8ebba742018-05-31 12:00:344562 NoteUsageEvent(UsageEvent::VIDEO_ADDED);
henrike@webrtc.org28e20752013-07-10 00:45:364563}
4564
deadbeef70ab1a12015-09-28 23:53:554565// TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote
4566// description.
Henrik Boström933d8b02017-10-10 17:05:164567rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver(
Steve Anton4171afb2017-11-20 18:20:224568 const RtpSenderInfo& remote_sender_info) {
4569 auto receiver = FindReceiverById(remote_sender_info.sender_id);
4570 if (!receiver) {
4571 RTC_LOG(LS_WARNING) << "RtpReceiver for track with id "
4572 << remote_sender_info.sender_id << " doesn't exist.";
Henrik Boström933d8b02017-10-10 17:05:164573 return nullptr;
deadbeef70ab1a12015-09-28 23:53:554574 }
Steve Anton4171afb2017-11-20 18:20:224575 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
4576 GetAudioTransceiver()->internal()->RemoveReceiver(receiver);
4577 } else {
4578 GetVideoTransceiver()->internal()->RemoveReceiver(receiver);
4579 }
Henrik Boström933d8b02017-10-10 17:05:164580 return receiver;
henrike@webrtc.org28e20752013-07-10 00:45:364581}
4582
korniltsev.anatolyec390b52017-07-25 00:00:254583void PeerConnection::AddAudioTrack(AudioTrackInterface* track,
4584 MediaStreamInterface* stream) {
4585 RTC_DCHECK(!IsClosed());
Steve Anton111fdfd2018-06-25 20:03:364586 RTC_DCHECK(track);
4587 RTC_DCHECK(stream);
korniltsev.anatolyec390b52017-07-25 00:00:254588 auto sender = FindSenderForTrack(track);
Steve Anton4171afb2017-11-20 18:20:224589 if (sender) {
korniltsev.anatolyec390b52017-07-25 00:00:254590 // We already have a sender for this track, so just change the stream_id
4591 // so that it's correct in the next call to CreateOffer.
Henrik Andreasssoncc189172019-05-20 09:01:384592 sender->internal()->set_stream_ids({stream->id()});
korniltsev.anatolyec390b52017-07-25 00:00:254593 return;
4594 }
4595
4596 // Normal case; we've never seen this track before.
Steve Anton111fdfd2018-06-25 20:03:364597 auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track,
Florent Castelli892acf02018-10-01 20:47:204598 {stream->id()}, {});
Amit Hilbuchdd9390c2018-11-14 00:26:054599 new_sender->internal()->SetMediaChannel(voice_media_channel());
Steve Anton4171afb2017-11-20 18:20:224600 GetAudioTransceiver()->internal()->AddSender(new_sender);
korniltsev.anatolyec390b52017-07-25 00:00:254601 // If the sender has already been configured in SDP, we call SetSsrc,
4602 // which will connect the sender to the underlying transport. This can
4603 // occur if a local session description that contains the ID of the sender
4604 // is set before AddStream is called. It can also occur if the local
4605 // session description is not changed and RemoveStream is called, and
4606 // later AddStream is called again with the same stream.
Steve Anton4171afb2017-11-20 18:20:224607 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609eaa2018-03-27 21:57:184608 FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id());
Steve Anton4171afb2017-11-20 18:20:224609 if (sender_info) {
4610 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
korniltsev.anatolyec390b52017-07-25 00:00:254611 }
4612}
4613
4614// TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around
4615// indefinitely, when we have unified plan SDP.
4616void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track,
4617 MediaStreamInterface* stream) {
4618 RTC_DCHECK(!IsClosed());
4619 auto sender = FindSenderForTrack(track);
Steve Anton4171afb2017-11-20 18:20:224620 if (!sender) {
Mirko Bonadei675513b2017-11-09 10:09:254621 RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
4622 << " doesn't exist.";
korniltsev.anatolyec390b52017-07-25 00:00:254623 return;
4624 }
Steve Anton4171afb2017-11-20 18:20:224625 GetAudioTransceiver()->internal()->RemoveSender(sender);
korniltsev.anatolyec390b52017-07-25 00:00:254626}
4627
4628void PeerConnection::AddVideoTrack(VideoTrackInterface* track,
4629 MediaStreamInterface* stream) {
4630 RTC_DCHECK(!IsClosed());
Steve Anton111fdfd2018-06-25 20:03:364631 RTC_DCHECK(track);
4632 RTC_DCHECK(stream);
korniltsev.anatolyec390b52017-07-25 00:00:254633 auto sender = FindSenderForTrack(track);
Steve Anton4171afb2017-11-20 18:20:224634 if (sender) {
korniltsev.anatolyec390b52017-07-25 00:00:254635 // We already have a sender for this track, so just change the stream_id
4636 // so that it's correct in the next call to CreateOffer.
Henrik Andreasssoncc189172019-05-20 09:01:384637 sender->internal()->set_stream_ids({stream->id()});
korniltsev.anatolyec390b52017-07-25 00:00:254638 return;
4639 }
4640
4641 // Normal case; we've never seen this track before.
Steve Anton111fdfd2018-06-25 20:03:364642 auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track,
Florent Castelli892acf02018-10-01 20:47:204643 {stream->id()}, {});
Amit Hilbuchdd9390c2018-11-14 00:26:054644 new_sender->internal()->SetMediaChannel(video_media_channel());
Steve Anton4171afb2017-11-20 18:20:224645 GetVideoTransceiver()->internal()->AddSender(new_sender);
4646 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609eaa2018-03-27 21:57:184647 FindSenderInfo(local_video_sender_infos_, stream->id(), track->id());
Steve Anton4171afb2017-11-20 18:20:224648 if (sender_info) {
4649 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
korniltsev.anatolyec390b52017-07-25 00:00:254650 }
4651}
4652
4653void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track,
4654 MediaStreamInterface* stream) {
4655 RTC_DCHECK(!IsClosed());
4656 auto sender = FindSenderForTrack(track);
Steve Anton4171afb2017-11-20 18:20:224657 if (!sender) {
Mirko Bonadei675513b2017-11-09 10:09:254658 RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
4659 << " doesn't exist.";
korniltsev.anatolyec390b52017-07-25 00:00:254660 return;
4661 }
Steve Anton4171afb2017-11-20 18:20:224662 GetVideoTransceiver()->internal()->RemoveSender(sender);
korniltsev.anatolyec390b52017-07-25 00:00:254663}
4664
Steve Antonba818672017-11-06 18:21:574665void PeerConnection::SetIceConnectionState(IceConnectionState new_state) {
Steve Antonba818672017-11-06 18:21:574666 if (ice_connection_state_ == new_state) {
4667 return;
4668 }
4669
deadbeefcbecd352015-09-23 18:50:274670 // After transitioning to "closed", ignore any additional states from
Steve Antonba818672017-11-06 18:21:574671 // TransportController (such as "disconnected").
deadbeefab9b2d12015-10-14 18:33:114672 if (IsClosed()) {
deadbeefcbecd352015-09-23 18:50:274673 return;
4674 }
Steve Antonba818672017-11-06 18:21:574675
Mirko Bonadei675513b2017-11-09 10:09:254676 RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
4677 << " => " << new_state;
Steve Antonba818672017-11-06 18:21:574678 RTC_DCHECK(ice_connection_state_ !=
4679 PeerConnectionInterface::kIceConnectionClosed);
4680
henrike@webrtc.org28e20752013-07-10 00:45:364681 ice_connection_state_ = new_state;
Harald Alvestrand7a1c7f72018-08-01 08:50:164682 Observer()->OnIceConnectionChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:364683}
4684
Alex Loiko9289eda2018-11-23 16:18:594685void PeerConnection::SetStandardizedIceConnectionState(
4686 PeerConnectionInterface::IceConnectionState new_state) {
Qingsi Wang36e31472019-05-29 18:37:264687 if (standardized_ice_connection_state_ == new_state) {
Alex Loiko9289eda2018-11-23 16:18:594688 return;
Qingsi Wang36e31472019-05-29 18:37:264689 }
4690
4691 if (IsClosed()) {
Alex Loiko9289eda2018-11-23 16:18:594692 return;
Qingsi Wang36e31472019-05-29 18:37:264693 }
4694
4695 RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState "
4696 << standardized_ice_connection_state_ << " => " << new_state;
4697
Alex Loiko9289eda2018-11-23 16:18:594698 standardized_ice_connection_state_ = new_state;
Jonas Olsson12046902018-12-06 10:25:144699 Observer()->OnStandardizedIceConnectionChange(new_state);
Alex Loiko9289eda2018-11-23 16:18:594700}
4701
Jonas Olsson635474e2018-10-18 13:58:174702void PeerConnection::SetConnectionState(
4703 PeerConnectionInterface::PeerConnectionState new_state) {
Jonas Olsson635474e2018-10-18 13:58:174704 if (connection_state_ == new_state)
4705 return;
4706 if (IsClosed())
4707 return;
4708 connection_state_ = new_state;
4709 Observer()->OnConnectionChange(new_state);
4710}
4711
henrike@webrtc.org28e20752013-07-10 00:45:364712void PeerConnection::OnIceGatheringChange(
4713 PeerConnectionInterface::IceGatheringState new_state) {
4714 if (IsClosed()) {
4715 return;
4716 }
4717 ice_gathering_state_ = new_state;
Harald Alvestrand7a1c7f72018-08-01 08:50:164718 Observer()->OnIceGatheringChange(ice_gathering_state_);
henrike@webrtc.org28e20752013-07-10 00:45:364719}
4720
jbauch81bf7b02017-03-25 15:31:124721void PeerConnection::OnIceCandidate(
4722 std::unique_ptr<IceCandidateInterface> candidate) {
zhihuang29ff8442016-07-27 18:07:254723 if (IsClosed()) {
4724 return;
4725 }
Qingsi Wang1ba5dec2019-08-19 18:57:174726 ReportIceCandidateCollected(candidate->candidate());
Harald Alvestrand7a1c7f72018-08-01 08:50:164727 Observer()->OnIceCandidate(candidate.get());
henrike@webrtc.org28e20752013-07-10 00:45:364728}
4729
Eldar Rello0095d372019-12-02 20:22:074730void PeerConnection::OnIceCandidateError(const std::string& address,
4731 int port,
Eldar Relloda13ea22019-06-01 09:23:434732 const std::string& url,
4733 int error_code,
4734 const std::string& error_text) {
4735 if (IsClosed()) {
4736 return;
4737 }
Eldar Rello0095d372019-12-02 20:22:074738 Observer()->OnIceCandidateError(address, port, url, error_code, error_text);
4739 // Leftover not to break wpt test during migration to the new API.
4740 Observer()->OnIceCandidateError(address + ":", url, error_code, error_text);
Eldar Relloda13ea22019-06-01 09:23:434741}
4742
Honghai Zhang7fb69db2016-03-14 18:59:184743void PeerConnection::OnIceCandidatesRemoved(
4744 const std::vector<cricket::Candidate>& candidates) {
zhihuang29ff8442016-07-27 18:07:254745 if (IsClosed()) {
4746 return;
4747 }
Harald Alvestrand7a1c7f72018-08-01 08:50:164748 Observer()->OnIceCandidatesRemoved(candidates);
Honghai Zhang7fb69db2016-03-14 18:59:184749}
4750
Alex Drake00c7ecf2019-08-06 17:54:474751void PeerConnection::OnSelectedCandidatePairChanged(
4752 const cricket::CandidatePairChangeEvent& event) {
4753 if (IsClosed()) {
4754 return;
4755 }
Qingsi Wang1ba5dec2019-08-19 18:57:174756
Qingsi Wangcc46b10c2019-09-12 18:19:014757 if (event.selected_candidate_pair.local_candidate().type() ==
4758 LOCAL_PORT_TYPE &&
4759 event.selected_candidate_pair.remote_candidate().type() ==
4760 LOCAL_PORT_TYPE) {
4761 NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED);
4762 }
4763
Alex Drake00c7ecf2019-08-06 17:54:474764 Observer()->OnIceSelectedCandidatePairChanged(event);
4765}
4766
henrike@webrtc.org28e20752013-07-10 00:45:364767void PeerConnection::ChangeSignalingState(
4768 PeerConnectionInterface::SignalingState signaling_state) {
Steve Antonba818672017-11-06 18:21:574769 if (signaling_state_ == signaling_state) {
4770 return;
4771 }
Mirko Bonadei675513b2017-11-09 10:09:254772 RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: "
4773 << GetSignalingStateString(signaling_state_)
4774 << " New state: "
4775 << GetSignalingStateString(signaling_state);
henrike@webrtc.org28e20752013-07-10 00:45:364776 signaling_state_ = signaling_state;
4777 if (signaling_state == kClosed) {
4778 ice_connection_state_ = kIceConnectionClosed;
Harald Alvestrand7a1c7f72018-08-01 08:50:164779 Observer()->OnIceConnectionChange(ice_connection_state_);
Alex Loiko9289eda2018-11-23 16:18:594780 standardized_ice_connection_state_ =
4781 PeerConnectionInterface::IceConnectionState::kIceConnectionClosed;
Jonas Olsson635474e2018-10-18 13:58:174782 connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed;
4783 Observer()->OnConnectionChange(connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:364784 }
Harald Alvestrand7a1c7f72018-08-01 08:50:164785 Observer()->OnSignalingChange(signaling_state_);
henrike@webrtc.org28e20752013-07-10 00:45:364786}
4787
deadbeefeb459812015-12-16 03:24:434788void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track,
4789 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 18:07:254790 if (IsClosed()) {
4791 return;
4792 }
korniltsev.anatolyec390b52017-07-25 00:00:254793 AddAudioTrack(track, stream);
Guido Urdaneta70c2db12019-04-16 10:24:144794 UpdateNegotiationNeeded();
deadbeefeb459812015-12-16 03:24:434795}
4796
deadbeefeb459812015-12-16 03:24:434797void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track,
4798 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 18:07:254799 if (IsClosed()) {
4800 return;
4801 }
korniltsev.anatolyec390b52017-07-25 00:00:254802 RemoveAudioTrack(track, stream);
Guido Urdaneta70c2db12019-04-16 10:24:144803 UpdateNegotiationNeeded();
deadbeefeb459812015-12-16 03:24:434804}
4805
4806void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track,
4807 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 18:07:254808 if (IsClosed()) {
4809 return;
4810 }
korniltsev.anatolyec390b52017-07-25 00:00:254811 AddVideoTrack(track, stream);
Guido Urdaneta70c2db12019-04-16 10:24:144812 UpdateNegotiationNeeded();
deadbeefeb459812015-12-16 03:24:434813}
4814
4815void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track,
4816 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 18:07:254817 if (IsClosed()) {
4818 return;
4819 }
korniltsev.anatolyec390b52017-07-25 00:00:254820 RemoveVideoTrack(track, stream);
Guido Urdaneta70c2db12019-04-16 10:24:144821 UpdateNegotiationNeeded();
deadbeefeb459812015-12-16 03:24:434822}
4823
Henrik Boström31638672017-11-23 16:48:324824void PeerConnection::PostSetSessionDescriptionSuccess(
4825 SetSessionDescriptionObserver* observer) {
Steve Antonad182762018-09-05 20:22:404826 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
4827 signaling_thread()->Post(RTC_FROM_HERE, this,
4828 MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
Henrik Boström31638672017-11-23 16:48:324829}
4830
deadbeefab9b2d12015-10-14 18:33:114831void PeerConnection::PostSetSessionDescriptionFailure(
4832 SetSessionDescriptionObserver* observer,
Steve Antonad182762018-09-05 20:22:404833 RTCError&& error) {
Harald Alvestrand5081c0c2018-03-09 14:18:034834 RTC_DCHECK(!error.ok());
Steve Antonad182762018-09-05 20:22:404835 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
4836 msg->error = std::move(error);
4837 signaling_thread()->Post(RTC_FROM_HERE, this,
4838 MSG_SET_SESSIONDESCRIPTION_FAILED, msg);
deadbeefab9b2d12015-10-14 18:33:114839}
4840
4841void PeerConnection::PostCreateSessionDescriptionFailure(
4842 CreateSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 14:18:034843 RTCError error) {
4844 RTC_DCHECK(!error.ok());
Steve Antonad182762018-09-05 20:22:404845 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
4846 msg->error = std::move(error);
4847 signaling_thread()->Post(RTC_FROM_HERE, this,
4848 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
deadbeefab9b2d12015-10-14 18:33:114849}
4850
zhihuang1c378ed2017-08-17 21:10:504851void PeerConnection::GetOptionsForOffer(
Steve Antondcc3c022017-12-23 00:02:544852 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
deadbeefab9b2d12015-10-14 18:33:114853 cricket::MediaSessionOptions* session_options) {
Steve Antondcc3c022017-12-23 00:02:544854 ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
zhihuang1c378ed2017-08-17 21:10:504855
Steve Antondcc3c022017-12-23 00:02:544856 if (IsUnifiedPlan()) {
4857 GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options);
4858 } else {
4859 GetOptionsForPlanBOffer(offer_answer_options, session_options);
4860 }
4861
Steve Antonfa2260d2017-12-29 00:38:234862 // Intentionally unset the data channel type for RTP data channel with the
4863 // second condition. Otherwise the RTP data channels would be successfully
4864 // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail
4865 // when building with chromium. We want to leave RTP data channels broken, so
4866 // people won't try to use them.
Harald Alvestrand05e4d082019-12-03 13:04:214867 if (data_channel_controller_.HasRtpDataChannels() ||
4868 data_channel_type() != cricket::DCT_RTP) {
Steve Antonfa2260d2017-12-29 00:38:234869 session_options->data_channel_type = data_channel_type();
4870 }
4871
Steve Antondcc3c022017-12-23 00:02:544872 // Apply ICE restart flag and renomination flag.
Henrik Boström79b69802019-07-18 09:16:564873 bool ice_restart = offer_answer_options.ice_restart ||
4874 local_ice_credentials_to_replace_->HasIceCredentials();
Steve Antondcc3c022017-12-23 00:02:544875 for (auto& options : session_options->media_description_options) {
Henrik Boström79b69802019-07-18 09:16:564876 options.transport_options.ice_restart = ice_restart;
Steve Antondcc3c022017-12-23 00:02:544877 options.transport_options.enable_ice_renomination =
4878 configuration_.enable_ice_renomination;
4879 }
4880
4881 session_options->rtcp_cname = rtcp_cname_;
Benjamin Wright8c27cca2018-10-25 17:16:444882 session_options->crypto_options = GetCryptoOptions();
Jonas Oreland1cd39fa2018-10-11 05:47:124883 session_options->pooled_ice_credentials =
4884 network_thread()->Invoke<std::vector<cricket::IceParameters>>(
4885 RTC_FROM_HERE,
4886 rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials,
4887 port_allocator_.get()));
Johannes Kron89f874e2018-11-12 09:25:484888 session_options->offer_extmap_allow_mixed =
4889 configuration_.offer_extmap_allow_mixed;
Piotr (Peter) Slatalab1ae10b2019-03-01 19:14:054890
Harald Alvestrand4aa11922019-05-14 20:00:014891 // Allow fallback for using obsolete SCTP syntax.
4892 // Note that the default in |session_options| is true, while
4893 // the default in |options| is false.
4894 session_options->use_obsolete_sctp_sdp =
4895 offer_answer_options.use_obsolete_sctp_sdp;
Steve Antondcc3c022017-12-23 00:02:544896}
4897
4898void PeerConnection::GetOptionsForPlanBOffer(
4899 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
4900 cricket::MediaSessionOptions* session_options) {
zhihuang1c378ed2017-08-17 21:10:504901 // Figure out transceiver directional preferences.
Philipp Hancke08000102020-07-10 11:26:444902 bool send_audio = !GetAudioTransceiver()->internal()->senders().empty();
4903 bool send_video = !GetVideoTransceiver()->internal()->senders().empty();
zhihuang1c378ed2017-08-17 21:10:504904
4905 // By default, generate sendrecv/recvonly m= sections.
4906 bool recv_audio = true;
4907 bool recv_video = true;
4908
4909 // By default, only offer a new m= section if we have media to send with it.
4910 bool offer_new_audio_description = send_audio;
4911 bool offer_new_video_description = send_video;
Harald Alvestrand05e4d082019-12-03 13:04:214912 bool offer_new_data_description = data_channel_controller_.HasDataChannels();
zhihuang1c378ed2017-08-17 21:10:504913
4914 // The "offer_to_receive_X" options allow those defaults to be overridden.
Steve Antondcc3c022017-12-23 00:02:544915 if (offer_answer_options.offer_to_receive_audio !=
4916 RTCOfferAnswerOptions::kUndefined) {
4917 recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
zhihuang1c378ed2017-08-17 21:10:504918 offer_new_audio_description =
Steve Antondcc3c022017-12-23 00:02:544919 offer_new_audio_description ||
4920 (offer_answer_options.offer_to_receive_audio > 0);
zhihuang1c378ed2017-08-17 21:10:504921 }
Steve Antondcc3c022017-12-23 00:02:544922 if (offer_answer_options.offer_to_receive_video !=
4923 RTCOfferAnswerOptions::kUndefined) {
4924 recv_video = (offer_answer_options.offer_to_receive_video > 0);
zhihuang1c378ed2017-08-17 21:10:504925 offer_new_video_description =
Steve Antondcc3c022017-12-23 00:02:544926 offer_new_video_description ||
4927 (offer_answer_options.offer_to_receive_video > 0);
zhihuang1c378ed2017-08-17 21:10:504928 }
4929
Danil Chapovalov66cadcc2018-06-19 14:47:434930 absl::optional<size_t> audio_index;
4931 absl::optional<size_t> video_index;
4932 absl::optional<size_t> data_index;
zhihuang1c378ed2017-08-17 21:10:504933 // If a current description exists, generate m= sections in the same order,
4934 // using the first audio/video/data section that appears and rejecting
4935 // extraneous ones.
Steve Anton75737c02017-11-06 18:37:174936 if (local_description()) {
zhihuang1c378ed2017-08-17 21:10:504937 GenerateMediaDescriptionOptions(
Steve Anton75737c02017-11-06 18:37:174938 local_description(),
Steve Anton1d03a752017-11-27 22:30:094939 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
4940 RtpTransceiverDirectionFromSendRecv(send_video, recv_video),
4941 &audio_index, &video_index, &data_index, session_options);
deadbeefab9b2d12015-10-14 18:33:114942 }
4943
zhihuang1c378ed2017-08-17 21:10:504944 // Add audio/video/data m= sections to the end if needed.
4945 if (!audio_index && offer_new_audio_description) {
Markus Handell755c65d2020-06-23 23:06:104946 cricket::MediaDescriptionOptions options(
4947 cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO,
4948 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), false);
4949 options.header_extensions =
4950 channel_manager()->GetSupportedAudioRtpHeaderExtensions();
4951 session_options->media_description_options.push_back(options);
Oskar Sundbom9b28a032017-11-16 09:53:304952 audio_index = session_options->media_description_options.size() - 1;
deadbeefc80741f2015-10-22 20:14:454953 }
zhihuang1c378ed2017-08-17 21:10:504954 if (!video_index && offer_new_video_description) {
Markus Handell755c65d2020-06-23 23:06:104955 cricket::MediaDescriptionOptions options(
4956 cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO,
4957 RtpTransceiverDirectionFromSendRecv(send_video, recv_video), false);
4958 options.header_extensions =
4959 channel_manager()->GetSupportedVideoRtpHeaderExtensions();
4960 session_options->media_description_options.push_back(options);
Oskar Sundbom9b28a032017-11-16 09:53:304961 video_index = session_options->media_description_options.size() - 1;
deadbeefc80741f2015-10-22 20:14:454962 }
zhihuang1c378ed2017-08-17 21:10:504963 if (!data_index && offer_new_data_description) {
4964 session_options->media_description_options.push_back(
Steve Antonfa2260d2017-12-29 00:38:234965 GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA));
Oskar Sundbom9b28a032017-11-16 09:53:304966 data_index = session_options->media_description_options.size() - 1;
zhihuang1c378ed2017-08-17 21:10:504967 }
4968
4969 cricket::MediaDescriptionOptions* audio_media_description_options =
4970 !audio_index ? nullptr
4971 : &session_options->media_description_options[*audio_index];
4972 cricket::MediaDescriptionOptions* video_media_description_options =
4973 !video_index ? nullptr
4974 : &session_options->media_description_options[*video_index];
zhihuang1c378ed2017-08-17 21:10:504975
Amit Hilbuchc63ddb22019-01-02 18:13:584976 AddPlanBRtpSenderOptions(GetSendersInternal(),
4977 audio_media_description_options,
4978 video_media_description_options,
4979 offer_answer_options.num_simulcast_layers);
Steve Antondcc3c022017-12-23 00:02:544980}
4981
Steve Antondcc3c022017-12-23 00:02:544982static cricket::MediaDescriptionOptions
4983GetMediaDescriptionOptionsForTransceiver(
4984 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
4985 transceiver,
Harald Alvestrand6060df52020-08-11 07:54:024986 const std::string& mid,
4987 bool is_create_offer) {
4988 // NOTE: a stopping transceiver should be treated as a stopped one in
4989 // createOffer as specified in
4990 // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer.
4991 bool stopped =
4992 is_create_offer ? transceiver->stopping() : transceiver->stopped();
Steve Antondcc3c022017-12-23 00:02:544993 cricket::MediaDescriptionOptions media_description_options(
Harald Alvestrand6060df52020-08-11 07:54:024994 transceiver->media_type(), mid, transceiver->direction(), stopped);
Florent Castelli2d9d82e2019-04-23 17:25:514995 media_description_options.codec_preferences =
4996 transceiver->codec_preferences();
Markus Handell755c65d2020-06-23 23:06:104997 media_description_options.header_extensions =
4998 transceiver->HeaderExtensionsToOffer();
Steve Anton5f94aa22018-02-01 18:58:304999 // This behavior is specified in JSEP. The gist is that:
5000 // 1. The MSID is included if the RtpTransceiver's direction is sendonly or
5001 // sendrecv.
5002 // 2. If the MSID is included, then it must be included in any subsequent
5003 // offer/answer exactly the same until the RtpTransceiver is stopped.
Harald Alvestrand6060df52020-08-11 07:54:025004 if (stopped || (!RtpTransceiverDirectionHasSend(transceiver->direction()) &&
5005 !transceiver->internal()->has_ever_been_used_to_send())) {
Amit Hilbuchbcd39d42019-01-26 01:13:565006 return media_description_options;
Steve Anton5f94aa22018-02-01 18:58:305007 }
Amit Hilbuchbcd39d42019-01-26 01:13:565008
5009 cricket::SenderOptions sender_options;
5010 sender_options.track_id = transceiver->sender()->id();
5011 sender_options.stream_ids = transceiver->sender()->stream_ids();
5012
5013 // The following sets up RIDs and Simulcast.
5014 // RIDs are included if Simulcast is requested or if any RID was specified.
Amit Hilbuch619b2942019-02-26 23:55:195015 RtpParameters send_parameters =
5016 transceiver->internal()->sender_internal()->GetParametersInternal();
Amit Hilbuchbcd39d42019-01-26 01:13:565017 bool has_rids = std::any_of(send_parameters.encodings.begin(),
5018 send_parameters.encodings.end(),
5019 [](const RtpEncodingParameters& encoding) {
5020 return !encoding.rid.empty();
5021 });
5022
Amit Hilbuchb7446ed2019-01-28 20:25:255023 std::vector<RidDescription> send_rids;
5024 SimulcastLayerList send_layers;
5025 for (const RtpEncodingParameters& encoding : send_parameters.encodings) {
5026 if (encoding.rid.empty()) {
5027 continue;
5028 }
5029 send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend));
5030 send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active));
5031 }
5032
Amit Hilbuchbcd39d42019-01-26 01:13:565033 if (has_rids) {
5034 sender_options.rids = send_rids;
5035 }
5036
5037 sender_options.simulcast_layers = send_layers;
Amit Hilbuchbcd39d42019-01-26 01:13:565038 // When RIDs are configured, we must set num_sim_layers to 0 to.
5039 // Otherwise, num_sim_layers must be 1 because either there is no
5040 // simulcast, or simulcast is acheived by munging the SDP.
5041 sender_options.num_sim_layers = has_rids ? 0 : 1;
5042 media_description_options.sender_options.push_back(sender_options);
5043
Steve Antondcc3c022017-12-23 00:02:545044 return media_description_options;
5045}
5046
Steve Anton5c72e712018-12-10 22:25:305047// Returns the ContentInfo at mline index |i|, or null if none exists.
5048static const ContentInfo* GetContentByIndex(
5049 const SessionDescriptionInterface* sdesc,
5050 size_t i) {
5051 if (!sdesc) {
5052 return nullptr;
5053 }
5054 const ContentInfos& contents = sdesc->description()->contents();
5055 return (i < contents.size() ? &contents[i] : nullptr);
5056}
5057
Steve Antondcc3c022017-12-23 00:02:545058void PeerConnection::GetOptionsForUnifiedPlanOffer(
5059 const RTCOfferAnswerOptions& offer_answer_options,
5060 cricket::MediaSessionOptions* session_options) {
5061 // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial
5062 // Offers) and 5.2.2 (Subsequent Offers).
5063 RTC_DCHECK_EQ(session_options->media_description_options.size(), 0);
Harald Alvestrand1716d392019-06-03 18:35:455064 const ContentInfos no_infos;
Steve Antondcc3c022017-12-23 00:02:545065 const ContentInfos& local_contents =
5066 (local_description() ? local_description()->description()->contents()
Harald Alvestrand1716d392019-06-03 18:35:455067 : no_infos);
Steve Antondcc3c022017-12-23 00:02:545068 const ContentInfos& remote_contents =
5069 (remote_description() ? remote_description()->description()->contents()
Harald Alvestrand1716d392019-06-03 18:35:455070 : no_infos);
Steve Antondcc3c022017-12-23 00:02:545071 // The mline indices that can be recycled. New transceivers should reuse these
5072 // slots first.
5073 std::queue<size_t> recycleable_mline_indices;
Steve Antondcc3c022017-12-23 00:02:545074 // First, go through each media section that exists in either the local or
5075 // remote description and generate a media section in this offer for the
5076 // associated transceiver. If a media section can be recycled, generate a
5077 // default, rejected media section here that can be later overwritten.
5078 for (size_t i = 0;
5079 i < std::max(local_contents.size(), remote_contents.size()); ++i) {
5080 // Either |local_content| or |remote_content| is non-null.
5081 const ContentInfo* local_content =
5082 (i < local_contents.size() ? &local_contents[i] : nullptr);
Steve Anton5c72e712018-12-10 22:25:305083 const ContentInfo* current_local_content =
5084 GetContentByIndex(current_local_description(), i);
Steve Antondcc3c022017-12-23 00:02:545085 const ContentInfo* remote_content =
5086 (i < remote_contents.size() ? &remote_contents[i] : nullptr);
Steve Anton5c72e712018-12-10 22:25:305087 const ContentInfo* current_remote_content =
5088 GetContentByIndex(current_remote_description(), i);
5089 bool had_been_rejected =
5090 (current_local_content && current_local_content->rejected) ||
5091 (current_remote_content && current_remote_content->rejected);
Steve Antondcc3c022017-12-23 00:02:545092 const std::string& mid =
5093 (local_content ? local_content->name : remote_content->name);
5094 cricket::MediaType media_type =
5095 (local_content ? local_content->media_description()->type()
5096 : remote_content->media_description()->type());
5097 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
5098 media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antondcc3c022017-12-23 00:02:545099 // A media section is considered eligible for recycling if it is marked as
Steve Anton5c72e712018-12-10 22:25:305100 // rejected in either the current local or current remote description.
Harald Alvestrand6060df52020-08-11 07:54:025101 auto transceiver = GetAssociatedTransceiver(mid);
5102 if (!transceiver) {
5103 // No associated transceiver. The media section has been stopped.
5104 recycleable_mline_indices.push(i);
Steve Antondcc3c022017-12-23 00:02:545105 session_options->media_description_options.push_back(
Harald Alvestrand6060df52020-08-11 07:54:025106 cricket::MediaDescriptionOptions(media_type, mid,
Steve Anton69470252018-02-09 19:43:085107 RtpTransceiverDirection::kInactive,
5108 /*stopped=*/true));
Steve Antondcc3c022017-12-23 00:02:545109 } else {
Harald Alvestrand6060df52020-08-11 07:54:025110 // NOTE: a stopping transceiver should be treated as a stopped one in
5111 // createOffer as specified in
5112 // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer.
5113 if (had_been_rejected && transceiver->stopping()) {
5114 session_options->media_description_options.push_back(
5115 cricket::MediaDescriptionOptions(
5116 transceiver->media_type(), mid,
5117 RtpTransceiverDirection::kInactive,
5118 /*stopped=*/true));
5119 recycleable_mline_indices.push(i);
5120 } else {
5121 session_options->media_description_options.push_back(
5122 GetMediaDescriptionOptionsForTransceiver(
5123 transceiver, mid,
5124 /*is_create_offer=*/true));
5125 // CreateOffer shouldn't really cause any state changes in
5126 // PeerConnection, but we need a way to match new transceivers to new
5127 // media sections in SetLocalDescription and JSEP specifies this is
5128 // done by recording the index of the media section generated for the
5129 // transceiver in the offer.
5130 transceiver->internal()->set_mline_index(i);
5131 }
Steve Antondcc3c022017-12-23 00:02:545132 }
5133 } else {
5134 RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
Steve Antonc8ff1602020-02-05 21:53:385135 if (had_been_rejected) {
Steve Antonfa2260d2017-12-29 00:38:235136 session_options->media_description_options.push_back(
5137 GetMediaDescriptionOptionsForRejectedData(mid));
5138 } else {
Steve Antonc8ff1602020-02-05 21:53:385139 RTC_CHECK(GetDataMid());
5140 if (mid == *GetDataMid()) {
5141 session_options->media_description_options.push_back(
5142 GetMediaDescriptionOptionsForActiveData(mid));
5143 } else {
5144 session_options->media_description_options.push_back(
5145 GetMediaDescriptionOptionsForRejectedData(mid));
5146 }
Steve Antonfa2260d2017-12-29 00:38:235147 }
Steve Antondcc3c022017-12-23 00:02:545148 }
5149 }
Amit Hilbuchae3df542019-01-07 20:13:085150
Steve Antondcc3c022017-12-23 00:02:545151 // Next, look for transceivers that are newly added (that is, are not stopped
5152 // and not associated). Reuse media sections marked as recyclable first,
5153 // otherwise append to the end of the offer. New media sections should be
5154 // added in the order they were added to the PeerConnection.
Mirko Bonadei739baf02019-01-27 16:29:425155 for (const auto& transceiver : transceivers_) {
Harald Alvestrand6060df52020-08-11 07:54:025156 if (transceiver->mid() || transceiver->stopping()) {
Steve Antondcc3c022017-12-23 00:02:545157 continue;
5158 }
5159 size_t mline_index;
5160 if (!recycleable_mline_indices.empty()) {
5161 mline_index = recycleable_mline_indices.front();
5162 recycleable_mline_indices.pop();
5163 session_options->media_description_options[mline_index] =
Harald Alvestrand6060df52020-08-11 07:54:025164 GetMediaDescriptionOptionsForTransceiver(
5165 transceiver, mid_generator_(), /*is_create_offer=*/true);
Steve Antondcc3c022017-12-23 00:02:545166 } else {
5167 mline_index = session_options->media_description_options.size();
5168 session_options->media_description_options.push_back(
Harald Alvestrand6060df52020-08-11 07:54:025169 GetMediaDescriptionOptionsForTransceiver(
5170 transceiver, mid_generator_(), /*is_create_offer=*/true));
Steve Antondcc3c022017-12-23 00:02:545171 }
5172 // See comment above for why CreateOffer changes the transceiver's state.
5173 transceiver->internal()->set_mline_index(mline_index);
5174 }
Steve Antonfa2260d2017-12-29 00:38:235175 // Lastly, add a m-section if we have local data channels and an m section
5176 // does not already exist.
Harald Alvestrand05e4d082019-12-03 13:04:215177 if (!GetDataMid() && data_channel_controller_.HasDataChannels()) {
Steve Antonfa2260d2017-12-29 00:38:235178 session_options->media_description_options.push_back(
Amit Hilbuchae3df542019-01-07 20:13:085179 GetMediaDescriptionOptionsForActiveData(mid_generator_()));
Steve Antonfa2260d2017-12-29 00:38:235180 }
Steve Antondcc3c022017-12-23 00:02:545181}
5182
5183void PeerConnection::GetOptionsForAnswer(
5184 const RTCOfferAnswerOptions& offer_answer_options,
5185 cricket::MediaSessionOptions* session_options) {
5186 ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
5187
5188 if (IsUnifiedPlan()) {
5189 GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options);
5190 } else {
5191 GetOptionsForPlanBAnswer(offer_answer_options, session_options);
5192 }
5193
Steve Antonfa2260d2017-12-29 00:38:235194 // Intentionally unset the data channel type for RTP data channel. Otherwise
5195 // the RTP data channels would be successfully negotiated by default and the
5196 // unit tests in WebRtcDataBrowserTest will fail when building with chromium.
5197 // We want to leave RTP data channels broken, so people won't try to use them.
Harald Alvestrand05e4d082019-12-03 13:04:215198 if (data_channel_controller_.HasRtpDataChannels() ||
5199 data_channel_type() != cricket::DCT_RTP) {
Steve Antonfa2260d2017-12-29 00:38:235200 session_options->data_channel_type = data_channel_type();
5201 }
5202
Steve Antondcc3c022017-12-23 00:02:545203 // Apply ICE renomination flag.
5204 for (auto& options : session_options->media_description_options) {
5205 options.transport_options.enable_ice_renomination =
5206 configuration_.enable_ice_renomination;
5207 }
zhihuang8f65cdf2016-05-07 01:40:305208
5209 session_options->rtcp_cname = rtcp_cname_;
Benjamin Wright8c27cca2018-10-25 17:16:445210 session_options->crypto_options = GetCryptoOptions();
Jonas Oreland1cd39fa2018-10-11 05:47:125211 session_options->pooled_ice_credentials =
5212 network_thread()->Invoke<std::vector<cricket::IceParameters>>(
5213 RTC_FROM_HERE,
5214 rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials,
5215 port_allocator_.get()));
deadbeefab9b2d12015-10-14 18:33:115216}
5217
Steve Antondcc3c022017-12-23 00:02:545218void PeerConnection::GetOptionsForPlanBAnswer(
5219 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
Honghai Zhang4cedf2b2016-08-31 15:18:115220 cricket::MediaSessionOptions* session_options) {
zhihuang1c378ed2017-08-17 21:10:505221 // Figure out transceiver directional preferences.
Philipp Hancke08000102020-07-10 11:26:445222 bool send_audio = !GetAudioTransceiver()->internal()->senders().empty();
5223 bool send_video = !GetVideoTransceiver()->internal()->senders().empty();
zhihuang1c378ed2017-08-17 21:10:505224
5225 // By default, generate sendrecv/recvonly m= sections. The direction is also
5226 // restricted by the direction in the offer.
5227 bool recv_audio = true;
5228 bool recv_video = true;
5229
5230 // The "offer_to_receive_X" options allow those defaults to be overridden.
Steve Antondcc3c022017-12-23 00:02:545231 if (offer_answer_options.offer_to_receive_audio !=
5232 RTCOfferAnswerOptions::kUndefined) {
5233 recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
deadbeef0ed85b22016-02-24 01:24:525234 }
Steve Antondcc3c022017-12-23 00:02:545235 if (offer_answer_options.offer_to_receive_video !=
5236 RTCOfferAnswerOptions::kUndefined) {
5237 recv_video = (offer_answer_options.offer_to_receive_video > 0);
zhihuang1c378ed2017-08-17 21:10:505238 }
5239
Danil Chapovalov66cadcc2018-06-19 14:47:435240 absl::optional<size_t> audio_index;
5241 absl::optional<size_t> video_index;
5242 absl::optional<size_t> data_index;
Steve Antondffead82018-02-06 18:31:295243
5244 // Generate m= sections that match those in the offer.
5245 // Note that mediasession.cc will handle intersection our preferred
5246 // direction with the offered direction.
5247 GenerateMediaDescriptionOptions(
5248 remote_description(),
5249 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
5250 RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index,
5251 &video_index, &data_index, session_options);
zhihuang1c378ed2017-08-17 21:10:505252
5253 cricket::MediaDescriptionOptions* audio_media_description_options =
5254 !audio_index ? nullptr
5255 : &session_options->media_description_options[*audio_index];
5256 cricket::MediaDescriptionOptions* video_media_description_options =
5257 !video_index ? nullptr
5258 : &session_options->media_description_options[*video_index];
zhihuang1c378ed2017-08-17 21:10:505259
Amit Hilbuchc63ddb22019-01-02 18:13:585260 AddPlanBRtpSenderOptions(GetSendersInternal(),
5261 audio_media_description_options,
5262 video_media_description_options,
5263 offer_answer_options.num_simulcast_layers);
Steve Antondcc3c022017-12-23 00:02:545264}
zhihuangaf388472016-11-02 23:49:485265
Steve Antondcc3c022017-12-23 00:02:545266void PeerConnection::GetOptionsForUnifiedPlanAnswer(
5267 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
5268 cricket::MediaSessionOptions* session_options) {
5269 // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial
5270 // Answers) and 5.3.2 (Subsequent Answers).
5271 RTC_DCHECK(remote_description());
5272 RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer);
5273 for (const ContentInfo& content :
5274 remote_description()->description()->contents()) {
5275 cricket::MediaType media_type = content.media_description()->type();
5276 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
5277 media_type == cricket::MEDIA_TYPE_VIDEO) {
5278 auto transceiver = GetAssociatedTransceiver(content.name);
5279 RTC_CHECK(transceiver);
5280 session_options->media_description_options.push_back(
Harald Alvestrand6060df52020-08-11 07:54:025281 GetMediaDescriptionOptionsForTransceiver(transceiver, content.name,
5282 /*is_create_offer=*/false));
Steve Antondcc3c022017-12-23 00:02:545283 } else {
5284 RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
Steve Antondbf9d032018-01-19 23:23:405285 // Reject all data sections if data channels are disabled.
5286 // Reject a data section if it has already been rejected.
5287 // Reject all data sections except for the first one.
Harald Alvestrand05e4d082019-12-03 13:04:215288 if (data_channel_type() == cricket::DCT_NONE || content.rejected ||
Steve Antondbf9d032018-01-19 23:23:405289 content.name != *GetDataMid()) {
Steve Antonfa2260d2017-12-29 00:38:235290 session_options->media_description_options.push_back(
5291 GetMediaDescriptionOptionsForRejectedData(content.name));
5292 } else {
5293 session_options->media_description_options.push_back(
5294 GetMediaDescriptionOptionsForActiveData(content.name));
5295 }
Steve Antondcc3c022017-12-23 00:02:545296 }
5297 }
htaa2a49d92016-03-04 10:51:395298}
5299
zhihuang1c378ed2017-08-17 21:10:505300void PeerConnection::GenerateMediaDescriptionOptions(
5301 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 22:30:095302 RtpTransceiverDirection audio_direction,
5303 RtpTransceiverDirection video_direction,
Danil Chapovalov66cadcc2018-06-19 14:47:435304 absl::optional<size_t>* audio_index,
5305 absl::optional<size_t>* video_index,
5306 absl::optional<size_t>* data_index,
htaa2a49d92016-03-04 10:51:395307 cricket::MediaSessionOptions* session_options) {
zhihuang1c378ed2017-08-17 21:10:505308 for (const cricket::ContentInfo& content :
5309 session_desc->description()->contents()) {
5310 if (IsAudioContent(&content)) {
5311 // If we already have an audio m= section, reject this extra one.
5312 if (*audio_index) {
5313 session_options->media_description_options.push_back(
5314 cricket::MediaDescriptionOptions(
5315 cricket::MEDIA_TYPE_AUDIO, content.name,
Steve Anton5c72e712018-12-10 22:25:305316 RtpTransceiverDirection::kInactive, /*stopped=*/true));
zhihuang1c378ed2017-08-17 21:10:505317 } else {
Steve Anton5c72e712018-12-10 22:25:305318 bool stopped = (audio_direction == RtpTransceiverDirection::kInactive);
zhihuang1c378ed2017-08-17 21:10:505319 session_options->media_description_options.push_back(
Steve Anton5c72e712018-12-10 22:25:305320 cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO,
5321 content.name, audio_direction,
5322 stopped));
Oskar Sundbom9b28a032017-11-16 09:53:305323 *audio_index = session_options->media_description_options.size() - 1;
zhihuang1c378ed2017-08-17 21:10:505324 }
Markus Handell755c65d2020-06-23 23:06:105325 session_options->media_description_options.back().header_extensions =
5326 channel_manager()->GetSupportedAudioRtpHeaderExtensions();
zhihuang1c378ed2017-08-17 21:10:505327 } else if (IsVideoContent(&content)) {
5328 // If we already have an video m= section, reject this extra one.
5329 if (*video_index) {
5330 session_options->media_description_options.push_back(
5331 cricket::MediaDescriptionOptions(
5332 cricket::MEDIA_TYPE_VIDEO, content.name,
Steve Anton5c72e712018-12-10 22:25:305333 RtpTransceiverDirection::kInactive, /*stopped=*/true));
zhihuang1c378ed2017-08-17 21:10:505334 } else {
Steve Anton5c72e712018-12-10 22:25:305335 bool stopped = (video_direction == RtpTransceiverDirection::kInactive);
zhihuang1c378ed2017-08-17 21:10:505336 session_options->media_description_options.push_back(
Steve Anton5c72e712018-12-10 22:25:305337 cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO,
5338 content.name, video_direction,
5339 stopped));
Oskar Sundbom9b28a032017-11-16 09:53:305340 *video_index = session_options->media_description_options.size() - 1;
zhihuang1c378ed2017-08-17 21:10:505341 }
Markus Handell755c65d2020-06-23 23:06:105342 session_options->media_description_options.back().header_extensions =
5343 channel_manager()->GetSupportedVideoRtpHeaderExtensions();
zhihuang1c378ed2017-08-17 21:10:505344 } else {
5345 RTC_DCHECK(IsDataContent(&content));
5346 // If we already have an data m= section, reject this extra one.
5347 if (*data_index) {
5348 session_options->media_description_options.push_back(
Steve Antonfa2260d2017-12-29 00:38:235349 GetMediaDescriptionOptionsForRejectedData(content.name));
zhihuang1c378ed2017-08-17 21:10:505350 } else {
5351 session_options->media_description_options.push_back(
Steve Antonfa2260d2017-12-29 00:38:235352 GetMediaDescriptionOptionsForActiveData(content.name));
Oskar Sundbom9b28a032017-11-16 09:53:305353 *data_index = session_options->media_description_options.size() - 1;
zhihuang1c378ed2017-08-17 21:10:505354 }
5355 }
htaa2a49d92016-03-04 10:51:395356 }
deadbeefab9b2d12015-10-14 18:33:115357}
5358
Steve Antonfa2260d2017-12-29 00:38:235359cricket::MediaDescriptionOptions
5360PeerConnection::GetMediaDescriptionOptionsForActiveData(
5361 const std::string& mid) const {
5362 // Direction for data sections is meaningless, but legacy endpoints might
5363 // expect sendrecv.
5364 cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
5365 RtpTransceiverDirection::kSendRecv,
5366 /*stopped=*/false);
Harald Alvestrand05e4d082019-12-03 13:04:215367 AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(),
5368 &options);
Steve Antonfa2260d2017-12-29 00:38:235369 return options;
5370}
5371
5372cricket::MediaDescriptionOptions
5373PeerConnection::GetMediaDescriptionOptionsForRejectedData(
5374 const std::string& mid) const {
5375 cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
5376 RtpTransceiverDirection::kInactive,
5377 /*stopped=*/true);
Harald Alvestrand05e4d082019-12-03 13:04:215378 AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(),
5379 &options);
Steve Antonfa2260d2017-12-29 00:38:235380 return options;
5381}
5382
Danil Chapovalov66cadcc2018-06-19 14:47:435383absl::optional<std::string> PeerConnection::GetDataMid() const {
Harald Alvestrand05e4d082019-12-03 13:04:215384 switch (data_channel_type()) {
Steve Antonfa2260d2017-12-29 00:38:235385 case cricket::DCT_RTP:
Harald Alvestrand00cf34c2019-12-02 08:56:025386 if (!data_channel_controller_.rtp_data_channel()) {
Danil Chapovalov66cadcc2018-06-19 14:47:435387 return absl::nullopt;
Steve Antonfa2260d2017-12-29 00:38:235388 }
Harald Alvestrand00cf34c2019-12-02 08:56:025389 return data_channel_controller_.rtp_data_channel()->content_name();
Steve Antonfa2260d2017-12-29 00:38:235390 case cricket::DCT_SCTP:
Harald Alvestrand7a829a82020-02-12 06:38:215391 return sctp_mid_s_;
Steve Antonfa2260d2017-12-29 00:38:235392 default:
Danil Chapovalov66cadcc2018-06-19 14:47:435393 return absl::nullopt;
Steve Antonfa2260d2017-12-29 00:38:235394 }
5395}
5396
Steve Anton4171afb2017-11-20 18:20:225397void PeerConnection::RemoveSenders(cricket::MediaType media_type) {
5398 UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type);
5399 UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false,
deadbeefbda7e0b2015-12-09 01:13:405400 media_type, nullptr);
deadbeeffaac4972015-11-12 23:33:075401}
5402
Steve Anton4171afb2017-11-20 18:20:225403void PeerConnection::UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 18:33:115404 const cricket::StreamParamsVec& streams,
Steve Anton4171afb2017-11-20 18:20:225405 bool default_sender_needed,
deadbeefab9b2d12015-10-14 18:33:115406 cricket::MediaType media_type,
5407 StreamCollection* new_streams) {
Seth Hampson5b4f0752018-04-02 23:31:365408 RTC_DCHECK(!IsUnifiedPlan());
5409
Steve Anton4171afb2017-11-20 18:20:225410 std::vector<RtpSenderInfo>* current_senders =
5411 GetRemoteSenderInfos(media_type);
deadbeefab9b2d12015-10-14 18:33:115412
Steve Anton4171afb2017-11-20 18:20:225413 // Find removed senders. I.e., senders where the sender id or ssrc don't match
deadbeeffac06552015-11-25 19:26:015414 // the new StreamParam.
Steve Anton4171afb2017-11-20 18:20:225415 for (auto sender_it = current_senders->begin();
5416 sender_it != current_senders->end();
5417 /* incremented manually */) {
5418 const RtpSenderInfo& info = *sender_it;
deadbeefab9b2d12015-10-14 18:33:115419 const cricket::StreamParams* params =
Steve Anton4171afb2017-11-20 18:20:225420 cricket::GetStreamBySsrc(streams, info.first_ssrc);
Seth Hampson83d676b2018-04-06 01:12:095421 std::string params_stream_id;
5422 if (params) {
5423 params_stream_id =
5424 (!params->first_stream_id().empty() ? params->first_stream_id()
5425 : kDefaultStreamId);
5426 }
Seth Hampson5b4f0752018-04-02 23:31:365427 bool sender_exists = params && params->id == info.sender_id &&
Seth Hampson83d676b2018-04-06 01:12:095428 params_stream_id == info.stream_id;
deadbeefbda7e0b2015-12-09 01:13:405429 // If this is a default track, and we still need it, don't remove it.
Seth Hampson845e8782018-03-02 19:34:105430 if ((info.stream_id == kDefaultStreamId && default_sender_needed) ||
Steve Anton4171afb2017-11-20 18:20:225431 sender_exists) {
5432 ++sender_it;
deadbeefbda7e0b2015-12-09 01:13:405433 } else {
Steve Anton4171afb2017-11-20 18:20:225434 OnRemoteSenderRemoved(info, media_type);
5435 sender_it = current_senders->erase(sender_it);
deadbeefab9b2d12015-10-14 18:33:115436 }
5437 }
5438
Steve Anton4171afb2017-11-20 18:20:225439 // Find new and active senders.
deadbeefab9b2d12015-10-14 18:33:115440 for (const cricket::StreamParams& params : streams) {
Seth Hampson5897a6e2018-04-03 18:16:335441 if (!params.has_ssrcs()) {
5442 // The remote endpoint has streams, but didn't signal ssrcs. For an active
5443 // sender, this means it is coming from a Unified Plan endpoint,so we just
5444 // create a default.
5445 default_sender_needed = true;
5446 break;
5447 }
5448
Seth Hampson845e8782018-03-02 19:34:105449 // |params.id| is the sender id and the stream id uses the first of
Seth Hampson5b4f0752018-04-02 23:31:365450 // |params.stream_ids|. The remote description could come from a Unified
Seth Hampson5897a6e2018-04-03 18:16:335451 // Plan endpoint, with multiple or no stream_ids() signaled. Since this is
5452 // not supported in Plan B, we just take the first here and create the
5453 // default stream ID if none is specified.
Seth Hampson845e8782018-03-02 19:34:105454 const std::string& stream_id =
Seth Hampson83d676b2018-04-06 01:12:095455 (!params.first_stream_id().empty() ? params.first_stream_id()
5456 : kDefaultStreamId);
Steve Anton4171afb2017-11-20 18:20:225457 const std::string& sender_id = params.id;
deadbeefab9b2d12015-10-14 18:33:115458 uint32_t ssrc = params.first_ssrc();
5459
5460 rtc::scoped_refptr<MediaStreamInterface> stream =
Seth Hampson845e8782018-03-02 19:34:105461 remote_streams_->find(stream_id);
deadbeefab9b2d12015-10-14 18:33:115462 if (!stream) {
5463 // This is a new MediaStream. Create a new remote MediaStream.
perkjd61bf802016-03-24 10:16:195464 stream = MediaStreamProxy::Create(rtc::Thread::Current(),
Seth Hampson845e8782018-03-02 19:34:105465 MediaStream::Create(stream_id));
deadbeefab9b2d12015-10-14 18:33:115466 remote_streams_->AddStream(stream);
5467 new_streams->AddStream(stream);
5468 }
5469
Steve Anton4171afb2017-11-20 18:20:225470 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609eaa2018-03-27 21:57:185471 FindSenderInfo(*current_senders, stream_id, sender_id);
Steve Anton4171afb2017-11-20 18:20:225472 if (!sender_info) {
Seth Hampson845e8782018-03-02 19:34:105473 current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
Steve Anton4171afb2017-11-20 18:20:225474 OnRemoteSenderAdded(current_senders->back(), media_type);
deadbeefab9b2d12015-10-14 18:33:115475 }
5476 }
deadbeefbda7e0b2015-12-09 01:13:405477
Steve Anton4171afb2017-11-20 18:20:225478 // Add default sender if necessary.
5479 if (default_sender_needed) {
deadbeefbda7e0b2015-12-09 01:13:405480 rtc::scoped_refptr<MediaStreamInterface> default_stream =
Seth Hampson845e8782018-03-02 19:34:105481 remote_streams_->find(kDefaultStreamId);
deadbeefbda7e0b2015-12-09 01:13:405482 if (!default_stream) {
5483 // Create the new default MediaStream.
perkjd61bf802016-03-24 10:16:195484 default_stream = MediaStreamProxy::Create(
Seth Hampson845e8782018-03-02 19:34:105485 rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId));
deadbeefbda7e0b2015-12-09 01:13:405486 remote_streams_->AddStream(default_stream);
5487 new_streams->AddStream(default_stream);
5488 }
Steve Anton4171afb2017-11-20 18:20:225489 std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO)
5490 ? kDefaultAudioSenderId
5491 : kDefaultVideoSenderId;
Seth Hampson845e8782018-03-02 19:34:105492 const RtpSenderInfo* default_sender_info =
Emircan Uysalerbc609eaa2018-03-27 21:57:185493 FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id);
Steve Anton4171afb2017-11-20 18:20:225494 if (!default_sender_info) {
5495 current_senders->push_back(
Ruslan Burakov7ea46052019-02-16 01:07:055496 RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0));
Steve Anton4171afb2017-11-20 18:20:225497 OnRemoteSenderAdded(current_senders->back(), media_type);
deadbeefbda7e0b2015-12-09 01:13:405498 }
5499 }
deadbeefab9b2d12015-10-14 18:33:115500}
5501
Steve Anton4171afb2017-11-20 18:20:225502void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
5503 cricket::MediaType media_type) {
Steve Anton3d954a62018-04-02 18:27:235504 RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type)
5505 << " receiver for track_id=" << sender_info.sender_id
5506 << " and stream_id=" << sender_info.stream_id;
deadbeefab9b2d12015-10-14 18:33:115507
Steve Anton3d954a62018-04-02 18:27:235508 MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id);
deadbeefab9b2d12015-10-14 18:33:115509 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton4171afb2017-11-20 18:20:225510 CreateAudioReceiver(stream, sender_info);
deadbeefab9b2d12015-10-14 18:33:115511 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Anton4171afb2017-11-20 18:20:225512 CreateVideoReceiver(stream, sender_info);
deadbeefab9b2d12015-10-14 18:33:115513 } else {
nisseeb4ca4e2017-01-12 10:24:275514 RTC_NOTREACHED() << "Invalid media type";
deadbeefab9b2d12015-10-14 18:33:115515 }
5516}
5517
Steve Anton4171afb2017-11-20 18:20:225518void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
5519 cricket::MediaType media_type) {
Seth Hampson83d676b2018-04-06 01:12:095520 RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type)
5521 << " receiver for track_id=" << sender_info.sender_id
5522 << " and stream_id=" << sender_info.stream_id;
5523
Seth Hampson845e8782018-03-02 19:34:105524 MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id);
deadbeefab9b2d12015-10-14 18:33:115525
Henrik Boström933d8b02017-10-10 17:05:165526 rtc::scoped_refptr<RtpReceiverInterface> receiver;
deadbeefab9b2d12015-10-14 18:33:115527 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
perkjd61bf802016-03-24 10:16:195528 // When the MediaEngine audio channel is destroyed, the RemoteAudioSource
5529 // will be notified which will end the AudioRtpReceiver::track().
Steve Anton4171afb2017-11-20 18:20:225530 receiver = RemoveAndStopReceiver(sender_info);
deadbeefab9b2d12015-10-14 18:33:115531 rtc::scoped_refptr<AudioTrackInterface> audio_track =
Steve Anton4171afb2017-11-20 18:20:225532 stream->FindAudioTrack(sender_info.sender_id);
deadbeefab9b2d12015-10-14 18:33:115533 if (audio_track) {
deadbeefab9b2d12015-10-14 18:33:115534 stream->RemoveTrack(audio_track);
deadbeefab9b2d12015-10-14 18:33:115535 }
5536 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
perkjd61bf802016-03-24 10:16:195537 // Stopping or destroying a VideoRtpReceiver will end the
5538 // VideoRtpReceiver::track().
Steve Anton4171afb2017-11-20 18:20:225539 receiver = RemoveAndStopReceiver(sender_info);
deadbeefab9b2d12015-10-14 18:33:115540 rtc::scoped_refptr<VideoTrackInterface> video_track =
Steve Anton4171afb2017-11-20 18:20:225541 stream->FindVideoTrack(sender_info.sender_id);
deadbeefab9b2d12015-10-14 18:33:115542 if (video_track) {
perkjd61bf802016-03-24 10:16:195543 // There's no guarantee the track is still available, e.g. the track may
5544 // have been removed from the stream by an application.
deadbeefab9b2d12015-10-14 18:33:115545 stream->RemoveTrack(video_track);
deadbeefab9b2d12015-10-14 18:33:115546 }
5547 } else {
nisseede5da42017-01-12 13:15:365548 RTC_NOTREACHED() << "Invalid media type";
deadbeefab9b2d12015-10-14 18:33:115549 }
Henrik Boström933d8b02017-10-10 17:05:165550 if (receiver) {
Harald Alvestrand7a1c7f72018-08-01 08:50:165551 Observer()->OnRemoveTrack(receiver);
Henrik Boström933d8b02017-10-10 17:05:165552 }
deadbeefab9b2d12015-10-14 18:33:115553}
5554
5555void PeerConnection::UpdateEndedRemoteMediaStreams() {
5556 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove;
5557 for (size_t i = 0; i < remote_streams_->count(); ++i) {
5558 MediaStreamInterface* stream = remote_streams_->at(i);
5559 if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) {
5560 streams_to_remove.push_back(stream);
5561 }
5562 }
5563
Taylor Brandstetter98cde262016-05-31 20:02:215564 for (auto& stream : streams_to_remove) {
deadbeefab9b2d12015-10-14 18:33:115565 remote_streams_->RemoveStream(stream);
Harald Alvestrand7a1c7f72018-08-01 08:50:165566 Observer()->OnRemoveStream(std::move(stream));
deadbeefab9b2d12015-10-14 18:33:115567 }
5568}
5569
Steve Anton4171afb2017-11-20 18:20:225570void PeerConnection::UpdateLocalSenders(
deadbeefab9b2d12015-10-14 18:33:115571 const std::vector<cricket::StreamParams>& streams,
5572 cricket::MediaType media_type) {
Steve Anton4171afb2017-11-20 18:20:225573 std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type);
deadbeefab9b2d12015-10-14 18:33:115574
Seth Hampson845e8782018-03-02 19:34:105575 // Find removed tracks. I.e., tracks where the track id, stream id or ssrc
deadbeefab9b2d12015-10-14 18:33:115576 // don't match the new StreamParam.
Steve Anton4171afb2017-11-20 18:20:225577 for (auto sender_it = current_senders->begin();
5578 sender_it != current_senders->end();
5579 /* incremented manually */) {
5580 const RtpSenderInfo& info = *sender_it;
deadbeefab9b2d12015-10-14 18:33:115581 const cricket::StreamParams* params =
Steve Anton4171afb2017-11-20 18:20:225582 cricket::GetStreamBySsrc(streams, info.first_ssrc);
5583 if (!params || params->id != info.sender_id ||
Seth Hampson845e8782018-03-02 19:34:105584 params->first_stream_id() != info.stream_id) {
Steve Anton4171afb2017-11-20 18:20:225585 OnLocalSenderRemoved(info, media_type);
5586 sender_it = current_senders->erase(sender_it);
deadbeefab9b2d12015-10-14 18:33:115587 } else {
Steve Anton4171afb2017-11-20 18:20:225588 ++sender_it;
deadbeefab9b2d12015-10-14 18:33:115589 }
5590 }
5591
Steve Anton4171afb2017-11-20 18:20:225592 // Find new and active senders.
deadbeefab9b2d12015-10-14 18:33:115593 for (const cricket::StreamParams& params : streams) {
5594 // The sync_label is the MediaStream label and the |stream.id| is the
Steve Anton4171afb2017-11-20 18:20:225595 // sender id.
Seth Hampson845e8782018-03-02 19:34:105596 const std::string& stream_id = params.first_stream_id();
Steve Anton4171afb2017-11-20 18:20:225597 const std::string& sender_id = params.id;
deadbeefab9b2d12015-10-14 18:33:115598 uint32_t ssrc = params.first_ssrc();
Steve Anton4171afb2017-11-20 18:20:225599 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609eaa2018-03-27 21:57:185600 FindSenderInfo(*current_senders, stream_id, sender_id);
Steve Anton4171afb2017-11-20 18:20:225601 if (!sender_info) {
Seth Hampson845e8782018-03-02 19:34:105602 current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
Steve Anton4171afb2017-11-20 18:20:225603 OnLocalSenderAdded(current_senders->back(), media_type);
deadbeefab9b2d12015-10-14 18:33:115604 }
5605 }
5606}
5607
Steve Anton4171afb2017-11-20 18:20:225608void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info,
5609 cricket::MediaType media_type) {
Seth Hampson5b4f0752018-04-02 23:31:365610 RTC_DCHECK(!IsUnifiedPlan());
Steve Anton4171afb2017-11-20 18:20:225611 auto sender = FindSenderById(sender_info.sender_id);
deadbeeffac06552015-11-25 19:26:015612 if (!sender) {
Steve Anton4171afb2017-11-20 18:20:225613 RTC_LOG(LS_WARNING) << "An unknown RtpSender with id "
5614 << sender_info.sender_id
Mirko Bonadei675513b2017-11-09 10:09:255615 << " has been configured in the local description.";
deadbeefab9b2d12015-10-14 18:33:115616 return;
5617 }
5618
deadbeeffac06552015-11-25 19:26:015619 if (sender->media_type() != media_type) {
Mirko Bonadei675513b2017-11-09 10:09:255620 RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local"
Jonas Olsson45cc8902018-02-13 09:37:075621 " description with an unexpected media type.";
deadbeeffac06552015-11-25 19:26:015622 return;
deadbeefab9b2d12015-10-14 18:33:115623 }
deadbeeffac06552015-11-25 19:26:015624
Henrik Andreasssoncc189172019-05-20 09:01:385625 sender->internal()->set_stream_ids({sender_info.stream_id});
Steve Anton4171afb2017-11-20 18:20:225626 sender->internal()->SetSsrc(sender_info.first_ssrc);
deadbeefab9b2d12015-10-14 18:33:115627}
5628
Steve Anton4171afb2017-11-20 18:20:225629void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
5630 cricket::MediaType media_type) {
5631 auto sender = FindSenderById(sender_info.sender_id);
deadbeeffac06552015-11-25 19:26:015632 if (!sender) {
5633 // This is the normal case. I.e., RemoveStream has been called and the
deadbeefab9b2d12015-10-14 18:33:115634 // SessionDescriptions has been renegotiated.
5635 return;
5636 }
deadbeeffac06552015-11-25 19:26:015637
5638 // A sender has been removed from the SessionDescription but it's still
5639 // associated with the PeerConnection. This only occurs if the SDP doesn't
5640 // match with the calls to CreateSender, AddStream and RemoveStream.
5641 if (sender->media_type() != media_type) {
Mirko Bonadei675513b2017-11-09 10:09:255642 RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local"
Jonas Olsson45cc8902018-02-13 09:37:075643 " description with an unexpected media type.";
deadbeeffac06552015-11-25 19:26:015644 return;
deadbeefab9b2d12015-10-14 18:33:115645 }
deadbeeffac06552015-11-25 19:26:015646
Steve Anton4171afb2017-11-20 18:20:225647 sender->internal()->SetSsrc(0);
deadbeefab9b2d12015-10-14 18:33:115648}
5649
Taylor Brandstetter3a034e12020-07-09 22:32:345650void PeerConnection::OnSctpDataChannelClosed(DataChannelInterface* channel) {
Harald Alvestrand00cf34c2019-12-02 08:56:025651 // Since data_channel_controller doesn't do signals, this
5652 // signal is relayed here.
Taylor Brandstetter3a034e12020-07-09 22:32:345653 data_channel_controller_.OnSctpDataChannelClosed(
5654 static_cast<SctpDataChannel*>(channel));
deadbeefab9b2d12015-10-14 18:33:115655}
5656
Steve Anton4171afb2017-11-20 18:20:225657rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
5658PeerConnection::GetAudioTransceiver() const {
5659 // This method only works with Plan B SDP, where there is a single
5660 // audio/video transceiver.
5661 RTC_DCHECK(!IsUnifiedPlan());
5662 for (auto transceiver : transceivers_) {
Steve Anton69470252018-02-09 19:43:085663 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton4171afb2017-11-20 18:20:225664 return transceiver;
5665 }
5666 }
5667 RTC_NOTREACHED();
5668 return nullptr;
5669}
5670
5671rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
5672PeerConnection::GetVideoTransceiver() const {
5673 // This method only works with Plan B SDP, where there is a single
5674 // audio/video transceiver.
5675 RTC_DCHECK(!IsUnifiedPlan());
5676 for (auto transceiver : transceivers_) {
Steve Anton69470252018-02-09 19:43:085677 if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
Steve Anton4171afb2017-11-20 18:20:225678 return transceiver;
5679 }
5680 }
5681 RTC_NOTREACHED();
5682 return nullptr;
5683}
5684
Steve Anton4171afb2017-11-20 18:20:225685rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
5686PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const {
Mirko Bonadei739baf02019-01-27 16:29:425687 for (const auto& transceiver : transceivers_) {
Steve Anton4171afb2017-11-20 18:20:225688 for (auto sender : transceiver->internal()->senders()) {
5689 if (sender->track() == track) {
5690 return sender;
5691 }
5692 }
5693 }
5694 return nullptr;
deadbeeffac06552015-11-25 19:26:015695}
5696
Steve Anton4171afb2017-11-20 18:20:225697rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
5698PeerConnection::FindSenderById(const std::string& sender_id) const {
Mirko Bonadei739baf02019-01-27 16:29:425699 for (const auto& transceiver : transceivers_) {
Steve Anton4171afb2017-11-20 18:20:225700 for (auto sender : transceiver->internal()->senders()) {
5701 if (sender->id() == sender_id) {
5702 return sender;
5703 }
5704 }
5705 }
5706 return nullptr;
deadbeef70ab1a12015-09-28 23:53:555707}
5708
Steve Anton4171afb2017-11-20 18:20:225709rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
5710PeerConnection::FindReceiverById(const std::string& receiver_id) const {
Mirko Bonadei739baf02019-01-27 16:29:425711 for (const auto& transceiver : transceivers_) {
Steve Anton4171afb2017-11-20 18:20:225712 for (auto receiver : transceiver->internal()->receivers()) {
5713 if (receiver->id() == receiver_id) {
5714 return receiver;
5715 }
5716 }
5717 }
5718 return nullptr;
deadbeef70ab1a12015-09-28 23:53:555719}
5720
Steve Anton4171afb2017-11-20 18:20:225721std::vector<PeerConnection::RtpSenderInfo>*
5722PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) {
5723 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
5724 media_type == cricket::MEDIA_TYPE_VIDEO);
5725 return (media_type == cricket::MEDIA_TYPE_AUDIO)
5726 ? &remote_audio_sender_infos_
5727 : &remote_video_sender_infos_;
5728}
5729
5730std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos(
deadbeefab9b2d12015-10-14 18:33:115731 cricket::MediaType media_type) {
5732 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
5733 media_type == cricket::MEDIA_TYPE_VIDEO);
Steve Anton4171afb2017-11-20 18:20:225734 return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_
5735 : &local_video_sender_infos_;
deadbeefab9b2d12015-10-14 18:33:115736}
5737
Steve Anton4171afb2017-11-20 18:20:225738const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo(
5739 const std::vector<PeerConnection::RtpSenderInfo>& infos,
Emircan Uysalerbc609eaa2018-03-27 21:57:185740 const std::string& stream_id,
Steve Anton4171afb2017-11-20 18:20:225741 const std::string sender_id) const {
5742 for (const RtpSenderInfo& sender_info : infos) {
Emircan Uysalerbc609eaa2018-03-27 21:57:185743 if (sender_info.stream_id == stream_id &&
5744 sender_info.sender_id == sender_id) {
Steve Anton4171afb2017-11-20 18:20:225745 return &sender_info;
deadbeefab9b2d12015-10-14 18:33:115746 }
5747 }
5748 return nullptr;
5749}
5750
Taylor Brandstetter3a034e12020-07-09 22:32:345751SctpDataChannel* PeerConnection::FindDataChannelBySid(int sid) const {
Harald Alvestrand05e4d082019-12-03 13:04:215752 return data_channel_controller_.FindDataChannelBySid(sid);
deadbeefab9b2d12015-10-14 18:33:115753}
5754
Karl Wibergfb3be392019-03-22 13:13:225755PeerConnection::InitializePortAllocatorResult
5756PeerConnection::InitializePortAllocator_n(
Harald Alvestrandb2a74782018-06-28 11:54:075757 const cricket::ServerAddresses& stun_servers,
5758 const std::vector<cricket::RelayServerConfig>& turn_servers,
Taylor Brandstettera1c30352016-05-13 15:15:115759 const RTCConfiguration& configuration) {
Karl Wibergfb3be392019-03-22 13:13:225760 RTC_DCHECK_RUN_ON(network_thread());
5761
Taylor Brandstetterf8e65772016-06-28 00:20:155762 port_allocator_->Initialize();
Taylor Brandstettera1c30352016-05-13 15:15:115763 // To handle both internal and externally created port allocator, we will
5764 // enable BUNDLE here.
Karl Wibergfb3be392019-03-22 13:13:225765 int port_allocator_flags = port_allocator_->flags();
5766 port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
5767 cricket::PORTALLOCATOR_ENABLE_IPV6 |
5768 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI;
Taylor Brandstettera1c30352016-05-13 15:15:115769 // If the disable-IPv6 flag was specified, we'll not override it
5770 // by experiment.
5771 if (configuration.disable_ipv6) {
Karl Wibergfb3be392019-03-22 13:13:225772 port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
Mirko Bonadei57cabed2020-04-01 10:03:115773 } else if (absl::StartsWith(
5774 webrtc::field_trial::FindFullName("WebRTC-IPv6Default"),
5775 "Disabled")) {
Karl Wibergfb3be392019-03-22 13:13:225776 port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
Taylor Brandstettera1c30352016-05-13 15:15:115777 }
5778
zhihuangb09b3f92017-03-07 22:40:515779 if (configuration.disable_ipv6_on_wifi) {
Karl Wibergfb3be392019-03-22 13:13:225780 port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Mirko Bonadei675513b2017-11-09 10:09:255781 RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled.";
zhihuangb09b3f92017-03-07 22:40:515782 }
5783
Taylor Brandstettera1c30352016-05-13 15:15:115784 if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) {
Karl Wibergfb3be392019-03-22 13:13:225785 port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
Mirko Bonadei675513b2017-11-09 10:09:255786 RTC_LOG(LS_INFO) << "TCP candidates are disabled.";
Taylor Brandstettera1c30352016-05-13 15:15:115787 }
5788
honghaiz60347052016-06-01 01:29:125789 if (configuration.candidate_network_policy ==
5790 kCandidateNetworkPolicyLowCost) {
Karl Wibergfb3be392019-03-22 13:13:225791 port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS;
Mirko Bonadei675513b2017-11-09 10:09:255792 RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks";
honghaiz60347052016-06-01 01:29:125793 }
5794
Daniel Lazarenko2870b0a2018-01-25 09:30:225795 if (configuration.disable_link_local_networks) {
Karl Wibergfb3be392019-03-22 13:13:225796 port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS;
Daniel Lazarenko2870b0a2018-01-25 09:30:225797 RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces.";
5798 }
5799
Karl Wibergfb3be392019-03-22 13:13:225800 port_allocator_->set_flags(port_allocator_flags);
Taylor Brandstettera1c30352016-05-13 15:15:115801 // No step delay is used while allocating ports.
5802 port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
Qingsi Wangc129c352019-04-18 17:41:585803 port_allocator_->SetCandidateFilter(
Taylor Brandstettera1c30352016-05-13 15:15:115804 ConvertIceTransportTypeToCandidateFilter(configuration.type));
deadbeefd21eab3e2017-07-26 23:50:115805 port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks);
Taylor Brandstettera1c30352016-05-13 15:15:115806
Harald Alvestrandb2a74782018-06-28 11:54:075807 auto turn_servers_copy = turn_servers;
Benjamin Wright6f80f092018-08-14 00:06:265808 for (auto& turn_server : turn_servers_copy) {
5809 turn_server.tls_cert_verifier = tls_cert_verifier_.get();
Benjamin Wrightd6f86e82018-05-08 20:12:255810 }
Taylor Brandstettera1c30352016-05-13 15:15:115811 // Call this last since it may create pooled allocator sessions using the
5812 // properties set above.
Qingsi Wangdb53f8e2018-02-20 22:45:495813 port_allocator_->SetConfiguration(
Benjamin Wright6f80f092018-08-14 00:06:265814 stun_servers, std::move(turn_servers_copy),
Honghai Zhangf8998cf2019-10-14 18:27:505815 configuration.ice_candidate_pool_size,
5816 configuration.GetTurnPortPrunePolicy(), configuration.turn_customizer,
Qingsi Wangdb53f8e2018-02-20 22:45:495817 configuration.stun_candidate_keepalive_interval);
Karl Wibergfb3be392019-03-22 13:13:225818
5819 InitializePortAllocatorResult res;
5820 res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6;
5821 return res;
Taylor Brandstettera1c30352016-05-13 15:15:115822}
5823
deadbeef91dd5672016-05-18 23:55:305824bool PeerConnection::ReconfigurePortAllocator_n(
deadbeef293e9262017-01-11 20:28:305825 const cricket::ServerAddresses& stun_servers,
5826 const std::vector<cricket::RelayServerConfig>& turn_servers,
5827 IceTransportsType type,
5828 int candidate_pool_size,
Honghai Zhangf8998cf2019-10-14 18:27:505829 PortPrunePolicy turn_port_prune_policy,
Qingsi Wangdb53f8e2018-02-20 22:45:495830 webrtc::TurnCustomizer* turn_customizer,
Karl Wiberg739506e2019-04-03 09:37:285831 absl::optional<int> stun_candidate_keepalive_interval,
5832 bool have_local_description) {
Qingsi Wangc129c352019-04-18 17:41:585833 port_allocator_->SetCandidateFilter(
deadbeef293e9262017-01-11 20:28:305834 ConvertIceTransportTypeToCandidateFilter(type));
Qingsi Wanga2d60672018-04-11 23:57:455835 // According to JSEP, after setLocalDescription, changing the candidate pool
5836 // size is not allowed, and changing the set of ICE servers will not result
5837 // in new candidates being gathered.
Karl Wiberg739506e2019-04-03 09:37:285838 if (have_local_description) {
Qingsi Wanga2d60672018-04-11 23:57:455839 port_allocator_->FreezeCandidatePool();
5840 }
Benjamin Wright6f80f092018-08-14 00:06:265841 // Add the custom tls turn servers if they exist.
5842 auto turn_servers_copy = turn_servers;
5843 for (auto& turn_server : turn_servers_copy) {
5844 turn_server.tls_cert_verifier = tls_cert_verifier_.get();
5845 }
Taylor Brandstettera1c30352016-05-13 15:15:115846 // Call this last since it may create pooled allocator sessions using the
5847 // candidate filter set above.
deadbeef6de92f92016-12-13 02:49:325848 return port_allocator_->SetConfiguration(
Benjamin Wright6f80f092018-08-14 00:06:265849 stun_servers, std::move(turn_servers_copy), candidate_pool_size,
Honghai Zhangf8998cf2019-10-14 18:27:505850 turn_port_prune_policy, turn_customizer,
5851 stun_candidate_keepalive_interval);
Taylor Brandstettera1c30352016-05-13 15:15:115852}
5853
Steve Antonba818672017-11-06 18:21:575854cricket::ChannelManager* PeerConnection::channel_manager() const {
5855 return factory_->channel_manager();
5856}
5857
Elad Alon99c3fe52017-10-13 14:29:405858bool PeerConnection::StartRtcEventLog_w(
Bjorn Tereliusde939432017-11-20 16:38:145859 std::unique_ptr<RtcEventLogOutput> output,
5860 int64_t output_period_ms) {
Karl Wibergb03ab712019-02-14 10:59:575861 RTC_DCHECK_RUN_ON(worker_thread());
zhihuang77985012017-02-07 23:45:165862 if (!event_log_) {
5863 return false;
5864 }
Bjorn Tereliusde939432017-11-20 16:38:145865 return event_log_->StartLogging(std::move(output), output_period_ms);
ivoc14d5dbe2016-07-04 14:06:555866}
5867
5868void PeerConnection::StopRtcEventLog_w() {
Karl Wibergb03ab712019-02-14 10:59:575869 RTC_DCHECK_RUN_ON(worker_thread());
zhihuang77985012017-02-07 23:45:165870 if (event_log_) {
5871 event_log_->StopLogging();
5872 }
ivoc14d5dbe2016-07-04 14:06:555873}
nisseeaabdf62017-05-05 09:23:025874
Amit Hilbuchdd9390c2018-11-14 00:26:055875cricket::ChannelInterface* PeerConnection::GetChannel(
Steve Anton75737c02017-11-06 18:37:175876 const std::string& content_name) {
Mirko Bonadei739baf02019-01-27 16:29:425877 for (const auto& transceiver : transceivers_) {
Amit Hilbuchdd9390c2018-11-14 00:26:055878 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Antondcc3c022017-12-23 00:02:545879 if (channel && channel->content_name() == content_name) {
5880 return channel;
5881 }
Steve Anton75737c02017-11-06 18:37:175882 }
5883 if (rtp_data_channel() &&
5884 rtp_data_channel()->content_name() == content_name) {
5885 return rtp_data_channel();
5886 }
5887 return nullptr;
5888}
5889
5890bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) {
Bjorn Mellem175aa2e2018-11-08 19:23:225891 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:175892 if (!local_description() || !remote_description()) {
Harald Alvestrand977b2652019-12-12 12:40:505893 RTC_LOG(LS_VERBOSE)
Mirko Bonadei675513b2017-11-09 10:09:255894 << "Local and Remote descriptions must be applied to get the "
Jonas Olsson45cc8902018-02-13 09:37:075895 "SSL Role of the SCTP transport.";
Steve Anton75737c02017-11-06 18:37:175896 return false;
5897 }
Harald Alvestrand00cf34c2019-12-02 08:56:025898 if (!data_channel_controller_.data_channel_transport()) {
Mirko Bonadei675513b2017-11-09 10:09:255899 RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
Jonas Olsson45cc8902018-02-13 09:37:075900 "SSL Role of the SCTP transport.";
Steve Anton75737c02017-11-06 18:37:175901 return false;
5902 }
5903
Bjorn Mellem175aa2e2018-11-08 19:23:225904 absl::optional<rtc::SSLRole> dtls_role;
Harald Alvestrand7a829a82020-02-12 06:38:215905 if (sctp_mid_s_) {
5906 dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_s_);
Bjorn A Mellembc3eebc2019-09-23 21:53:545907 if (!dtls_role && is_caller_.has_value()) {
5908 dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT;
5909 }
Zhi Huange830e682018-03-30 17:48:355910 *role = *dtls_role;
5911 return true;
5912 }
5913 return false;
Steve Anton75737c02017-11-06 18:37:175914}
5915
5916bool PeerConnection::GetSslRole(const std::string& content_name,
5917 rtc::SSLRole* role) {
Karl Wiberg2cc368f2019-04-02 09:31:565918 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:175919 if (!local_description() || !remote_description()) {
Mirko Bonadei675513b2017-11-09 10:09:255920 RTC_LOG(LS_INFO)
5921 << "Local and Remote descriptions must be applied to get the "
Jonas Olsson45cc8902018-02-13 09:37:075922 "SSL Role of the session.";
Steve Anton75737c02017-11-06 18:37:175923 return false;
5924 }
5925
Zhi Huange830e682018-03-30 17:48:355926 auto dtls_role = transport_controller_->GetDtlsRole(content_name);
5927 if (dtls_role) {
5928 *role = *dtls_role;
5929 return true;
5930 }
5931 return false;
Steve Anton75737c02017-11-06 18:37:175932}
5933
Steve Antonf8470812017-12-04 18:46:215934void PeerConnection::SetSessionError(SessionError error,
5935 const std::string& error_desc) {
5936 RTC_DCHECK_RUN_ON(signaling_thread());
5937 if (error != session_error_) {
5938 session_error_ = error;
5939 session_error_desc_ = error_desc;
Steve Anton75737c02017-11-06 18:37:175940 }
5941}
5942
Zhi Huange830e682018-03-30 17:48:355943RTCError PeerConnection::UpdateSessionState(
5944 SdpType type,
5945 cricket::ContentSource source,
5946 const cricket::SessionDescription* description) {
Steve Anton8a006912017-12-04 23:25:565947 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:175948
5949 // If there's already a pending error then no state transition should happen.
5950 // But all call-sites should be verifying this before calling us!
Steve Antonf8470812017-12-04 18:46:215951 RTC_DCHECK(session_error() == SessionError::kNone);
Steve Anton6d6a2ae2017-12-05 01:19:475952
Steve Anton6d6a2ae2017-12-05 01:19:475953 // If this is answer-ish we're ready to let media flow.
Steve Anton3828c062017-12-06 18:34:515954 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
Steve Antoned10bd92017-12-05 18:52:595955 EnableSending();
Steve Anton6d6a2ae2017-12-05 01:19:475956 }
5957
5958 // Update the signaling state according to the specified state machine (see
5959 // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum).
Steve Anton3828c062017-12-06 18:34:515960 if (type == SdpType::kOffer) {
Steve Anton6d6a2ae2017-12-05 01:19:475961 ChangeSignalingState(source == cricket::CS_LOCAL
5962 ? PeerConnectionInterface::kHaveLocalOffer
5963 : PeerConnectionInterface::kHaveRemoteOffer);
Steve Anton3828c062017-12-06 18:34:515964 } else if (type == SdpType::kPrAnswer) {
Steve Anton6d6a2ae2017-12-05 01:19:475965 ChangeSignalingState(source == cricket::CS_LOCAL
5966 ? PeerConnectionInterface::kHaveLocalPrAnswer
5967 : PeerConnectionInterface::kHaveRemotePrAnswer);
5968 } else {
Steve Anton3828c062017-12-06 18:34:515969 RTC_DCHECK(type == SdpType::kAnswer);
Steve Anton6d6a2ae2017-12-05 01:19:475970 ChangeSignalingState(PeerConnectionInterface::kStable);
Eldar Rello5ab79e62019-10-09 15:29:445971 transceiver_stable_states_by_transceivers_.clear();
Eldar Rellod9ebe012020-03-18 18:41:455972 have_pending_rtp_data_channel_ = false;
Steve Anton6d6a2ae2017-12-05 01:19:475973 }
5974
5975 // Update internal objects according to the session description's media
5976 // descriptions.
Zhi Huange830e682018-03-30 17:48:355977 RTCError error = PushdownMediaDescription(type, source);
Steve Anton6d6a2ae2017-12-05 01:19:475978 if (!error.ok()) {
Steve Anton80dd7b52018-02-17 01:08:425979 return error;
Steve Anton6d6a2ae2017-12-05 01:19:475980 }
5981
Steve Anton8a006912017-12-04 23:25:565982 return RTCError::OK();
Steve Anton75737c02017-11-06 18:37:175983}
5984
Steve Anton8a006912017-12-04 23:25:565985RTCError PeerConnection::PushdownMediaDescription(
Steve Anton3828c062017-12-06 18:34:515986 SdpType type,
Steve Anton8a006912017-12-04 23:25:565987 cricket::ContentSource source) {
Steve Antoned10bd92017-12-05 18:52:595988 const SessionDescriptionInterface* sdesc =
5989 (source == cricket::CS_LOCAL ? local_description()
5990 : remote_description());
Steve Anton75737c02017-11-06 18:37:175991 RTC_DCHECK(sdesc);
Steve Antoned10bd92017-12-05 18:52:595992
5993 // Push down the new SDP media section for each audio/video transceiver.
Mirko Bonadei739baf02019-01-27 16:29:425994 for (const auto& transceiver : transceivers_) {
Steve Anton75737c02017-11-06 18:37:175995 const ContentInfo* content_info =
Steve Antoned10bd92017-12-05 18:52:595996 FindMediaSectionForTransceiver(transceiver, sdesc);
Amit Hilbuchdd9390c2018-11-14 00:26:055997 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Antoned10bd92017-12-05 18:52:595998 if (!channel || !content_info || content_info->rejected) {
Steve Anton75737c02017-11-06 18:37:175999 continue;
6000 }
6001 const MediaContentDescription* content_desc =
Steve Antonb1c1de12017-12-21 23:14:306002 content_info->media_description();
Steve Antoned10bd92017-12-05 18:52:596003 if (!content_desc) {
6004 continue;
6005 }
6006 std::string error;
Yves Gerey665174f2018-06-19 13:03:056007 bool success = (source == cricket::CS_LOCAL)
6008 ? channel->SetLocalContent(content_desc, type, &error)
6009 : channel->SetRemoteContent(content_desc, type, &error);
Steve Antoned10bd92017-12-05 18:52:596010 if (!success) {
Yves Gereyae6e0b22019-01-22 20:48:576011 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error);
Steve Antoned10bd92017-12-05 18:52:596012 }
6013 }
6014
6015 // If using the RtpDataChannel, push down the new SDP section for it too.
Harald Alvestrand00cf34c2019-12-02 08:56:026016 if (data_channel_controller_.rtp_data_channel()) {
Steve Antoned10bd92017-12-05 18:52:596017 const ContentInfo* data_content =
6018 cricket::GetFirstDataContent(sdesc->description());
6019 if (data_content && !data_content->rejected) {
6020 const MediaContentDescription* data_desc =
Steve Antonb1c1de12017-12-21 23:14:306021 data_content->media_description();
Steve Antoned10bd92017-12-05 18:52:596022 if (data_desc) {
6023 std::string error;
6024 bool success =
6025 (source == cricket::CS_LOCAL)
Harald Alvestrand00cf34c2019-12-02 08:56:026026 ? data_channel_controller_.rtp_data_channel()->SetLocalContent(
6027 data_desc, type, &error)
6028 : data_channel_controller_.rtp_data_channel()->SetRemoteContent(
6029 data_desc, type, &error);
Steve Antoned10bd92017-12-05 18:52:596030 if (!success) {
Yves Gereyae6e0b22019-01-22 20:48:576031 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error);
Steve Antoned10bd92017-12-05 18:52:596032 }
Steve Anton75737c02017-11-06 18:37:176033 }
6034 }
6035 }
Steve Antoned10bd92017-12-05 18:52:596036
Steve Anton75737c02017-11-06 18:37:176037 // Need complete offer/answer with an SCTP m= section before starting SCTP,
6038 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
Harald Alvestrand7a829a82020-02-12 06:38:216039 if (sctp_mid_s_ && local_description() && remote_description()) {
Bjorn A Mellembc3eebc2019-09-23 21:53:546040 rtc::scoped_refptr<SctpTransport> sctp_transport =
Harald Alvestrand7a829a82020-02-12 06:38:216041 transport_controller_->GetSctpTransport(*sctp_mid_s_);
Harald Alvestrandfbb45bd2019-05-15 06:07:476042 auto local_sctp_description = cricket::GetFirstSctpDataContentDescription(
6043 local_description()->description());
6044 auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription(
6045 remote_description()->description());
Bjorn A Mellembc3eebc2019-09-23 21:53:546046 if (sctp_transport && local_sctp_description && remote_sctp_description) {
Harald Alvestrand8d3d6cf2019-05-16 09:49:176047 int max_message_size;
6048 // A remote max message size of zero means "any size supported".
6049 // We configure the connection with our own max message size.
6050 if (remote_sctp_description->max_message_size() == 0) {
6051 max_message_size = local_sctp_description->max_message_size();
6052 } else {
6053 max_message_size =
6054 std::min(local_sctp_description->max_message_size(),
6055 remote_sctp_description->max_message_size());
Harald Alvestrandfbb45bd2019-05-15 06:07:476056 }
Bjorn A Mellembc3eebc2019-09-23 21:53:546057 sctp_transport->Start(local_sctp_description->port(),
6058 remote_sctp_description->port(), max_message_size);
Steve Anton8a006912017-12-04 23:25:566059 }
Steve Anton75737c02017-11-06 18:37:176060 }
Steve Antoned10bd92017-12-05 18:52:596061
Steve Anton8a006912017-12-04 23:25:566062 return RTCError::OK();
Steve Anton75737c02017-11-06 18:37:176063}
6064
Steve Anton8a006912017-12-04 23:25:566065RTCError PeerConnection::PushdownTransportDescription(
6066 cricket::ContentSource source,
Steve Anton3828c062017-12-06 18:34:516067 SdpType type) {
Steve Anton8a006912017-12-04 23:25:566068 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:176069
Zhi Huange830e682018-03-30 17:48:356070 if (source == cricket::CS_LOCAL) {
6071 const SessionDescriptionInterface* sdesc = local_description();
6072 RTC_DCHECK(sdesc);
6073 return transport_controller_->SetLocalDescription(type,
6074 sdesc->description());
6075 } else {
6076 const SessionDescriptionInterface* sdesc = remote_description();
6077 RTC_DCHECK(sdesc);
6078 return transport_controller_->SetRemoteDescription(type,
6079 sdesc->description());
Steve Anton75737c02017-11-06 18:37:176080 }
Steve Anton75737c02017-11-06 18:37:176081}
6082
6083bool PeerConnection::GetTransportDescription(
6084 const SessionDescription* description,
6085 const std::string& content_name,
6086 cricket::TransportDescription* tdesc) {
6087 if (!description || !tdesc) {
6088 return false;
6089 }
6090 const TransportInfo* transport_info =
6091 description->GetTransportInfoByName(content_name);
6092 if (!transport_info) {
6093 return false;
6094 }
6095 *tdesc = transport_info->description;
6096 return true;
6097}
6098
Steve Anton75737c02017-11-06 18:37:176099cricket::IceConfig PeerConnection::ParseIceConfig(
6100 const PeerConnectionInterface::RTCConfiguration& config) const {
6101 cricket::ContinualGatheringPolicy gathering_policy;
Steve Anton75737c02017-11-06 18:37:176102 switch (config.continual_gathering_policy) {
6103 case PeerConnectionInterface::GATHER_ONCE:
6104 gathering_policy = cricket::GATHER_ONCE;
6105 break;
6106 case PeerConnectionInterface::GATHER_CONTINUALLY:
6107 gathering_policy = cricket::GATHER_CONTINUALLY;
6108 break;
6109 default:
6110 RTC_NOTREACHED();
6111 gathering_policy = cricket::GATHER_ONCE;
6112 }
Qingsi Wang9a5c6f82018-02-01 18:38:406113
Steve Anton75737c02017-11-06 18:37:176114 cricket::IceConfig ice_config;
Qingsi Wang866e08d2018-03-23 00:54:236115 ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt(
6116 config.ice_connection_receiving_timeout);
Steve Anton75737c02017-11-06 18:37:176117 ice_config.prioritize_most_likely_candidate_pairs =
6118 config.prioritize_most_likely_ice_candidate_pairs;
6119 ice_config.backup_connection_ping_interval =
Qingsi Wang866e08d2018-03-23 00:54:236120 RTCConfigurationToIceConfigOptionalInt(
6121 config.ice_backup_candidate_pair_ping_interval);
Steve Anton75737c02017-11-06 18:37:176122 ice_config.continual_gathering_policy = gathering_policy;
6123 ice_config.presume_writable_when_fully_relayed =
6124 config.presume_writable_when_fully_relayed;
Qingsi Wang1fe119f2019-05-31 23:55:336125 ice_config.surface_ice_candidates_on_ice_transport_type_changed =
6126 config.surface_ice_candidates_on_ice_transport_type_changed;
Qingsi Wange6826d22018-03-08 22:55:146127 ice_config.ice_check_interval_strong_connectivity =
6128 config.ice_check_interval_strong_connectivity;
6129 ice_config.ice_check_interval_weak_connectivity =
6130 config.ice_check_interval_weak_connectivity;
Steve Anton75737c02017-11-06 18:37:176131 ice_config.ice_check_min_interval = config.ice_check_min_interval;
Jiawei Oucc887372018-11-30 07:08:516132 ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout;
6133 ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-07 07:30:176134 ice_config.ice_inactive_timeout = config.ice_inactive_timeout;
Qingsi Wangdb53f8e2018-02-20 22:45:496135 ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval;
Qingsi Wang9a5c6f82018-02-01 18:38:406136 ice_config.network_preference = config.network_preference;
Steve Anton75737c02017-11-06 18:37:176137 return ice_config;
6138}
6139
Taylor Brandstetter3a034e12020-07-09 22:32:346140std::vector<DataChannelStats> PeerConnection::GetDataChannelStats() const {
Tomas Gunnarsson2e94de52020-06-16 14:54:106141 RTC_DCHECK_RUN_ON(signaling_thread());
6142 return data_channel_controller_.GetDataChannelStats();
6143}
6144
Danil Chapovalov66cadcc2018-06-19 14:47:436145absl::optional<std::string> PeerConnection::sctp_transport_name() const {
Karl Wiberg2cc368f2019-04-02 09:31:566146 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand7a829a82020-02-12 06:38:216147 if (sctp_mid_s_ && transport_controller_) {
6148 auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_s_);
Zhi Huange830e682018-03-30 17:48:356149 if (dtls_transport) {
6150 return dtls_transport->transport_name();
6151 }
Danil Chapovalov66cadcc2018-06-19 14:47:436152 return absl::optional<std::string>();
Zhi Huange830e682018-03-30 17:48:356153 }
Danil Chapovalov66cadcc2018-06-19 14:47:436154 return absl::optional<std::string>();
Zhi Huange830e682018-03-30 17:48:356155}
6156
Qingsi Wang72a43a12018-02-21 00:03:186157cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const {
6158 cricket::CandidateStatsList candidate_states_list;
Qingsi Wanga2d60672018-04-11 23:57:456159 network_thread()->Invoke<void>(
6160 RTC_FROM_HERE,
6161 rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions,
6162 port_allocator_.get(), &candidate_states_list));
Qingsi Wang72a43a12018-02-21 00:03:186163 return candidate_states_list;
6164}
6165
Steve Anton5dfde182018-02-06 18:34:406166std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid()
6167 const {
Karl Wiberga58e1692019-03-26 12:33:436168 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton5dfde182018-02-06 18:34:406169 std::map<std::string, std::string> transport_names_by_mid;
Mirko Bonadei739baf02019-01-27 16:29:426170 for (const auto& transceiver : transceivers_) {
Amit Hilbuchdd9390c2018-11-14 00:26:056171 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton5dfde182018-02-06 18:34:406172 if (channel) {
6173 transport_names_by_mid[channel->content_name()] =
6174 channel->transport_name();
6175 }
Steve Anton75737c02017-11-06 18:37:176176 }
Harald Alvestrand00cf34c2019-12-02 08:56:026177 if (data_channel_controller_.rtp_data_channel()) {
6178 transport_names_by_mid[data_channel_controller_.rtp_data_channel()
6179 ->content_name()] =
6180 data_channel_controller_.rtp_data_channel()->transport_name();
Steve Anton75737c02017-11-06 18:37:176181 }
Harald Alvestrand00cf34c2019-12-02 08:56:026182 if (data_channel_controller_.data_channel_transport()) {
Danil Chapovalov66cadcc2018-06-19 14:47:436183 absl::optional<std::string> transport_name = sctp_transport_name();
Zhi Huange830e682018-03-30 17:48:356184 RTC_DCHECK(transport_name);
Harald Alvestrand7a829a82020-02-12 06:38:216185 transport_names_by_mid[*sctp_mid_s_] = *transport_name;
Steve Anton75737c02017-11-06 18:37:176186 }
Steve Anton5dfde182018-02-06 18:34:406187 return transport_names_by_mid;
Steve Anton75737c02017-11-06 18:37:176188}
6189
Steve Anton5dfde182018-02-06 18:34:406190std::map<std::string, cricket::TransportStats>
6191PeerConnection::GetTransportStatsByNames(
6192 const std::set<std::string>& transport_names) {
6193 if (!network_thread()->IsCurrent()) {
6194 return network_thread()
6195 ->Invoke<std::map<std::string, cricket::TransportStats>>(
6196 RTC_FROM_HERE,
6197 [&] { return GetTransportStatsByNames(transport_names); });
Steve Anton75737c02017-11-06 18:37:176198 }
Karl Wiberg2cc368f2019-04-02 09:31:566199 RTC_DCHECK_RUN_ON(network_thread());
Steve Anton5dfde182018-02-06 18:34:406200 std::map<std::string, cricket::TransportStats> transport_stats_by_name;
6201 for (const std::string& transport_name : transport_names) {
6202 cricket::TransportStats transport_stats;
6203 bool success =
6204 transport_controller_->GetStats(transport_name, &transport_stats);
6205 if (success) {
6206 transport_stats_by_name[transport_name] = std::move(transport_stats);
6207 } else {
6208 RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name="
6209 << transport_name;
6210 }
6211 }
6212 return transport_stats_by_name;
Steve Anton75737c02017-11-06 18:37:176213}
6214
6215bool PeerConnection::GetLocalCertificate(
6216 const std::string& transport_name,
6217 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
Zhi Huange830e682018-03-30 17:48:356218 if (!certificate) {
6219 return false;
6220 }
6221 *certificate = transport_controller_->GetLocalCertificate(transport_name);
6222 return *certificate != nullptr;
Steve Anton75737c02017-11-06 18:37:176223}
6224
Taylor Brandstetterc3928662018-02-23 21:04:516225std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain(
Steve Anton75737c02017-11-06 18:37:176226 const std::string& transport_name) {
Taylor Brandstetterc3928662018-02-23 21:04:516227 return transport_controller_->GetRemoteSSLCertChain(transport_name);
Steve Anton75737c02017-11-06 18:37:176228}
6229
6230cricket::DataChannelType PeerConnection::data_channel_type() const {
Harald Alvestrand05e4d082019-12-03 13:04:216231 return data_channel_controller_.data_channel_type();
Steve Anton75737c02017-11-06 18:37:176232}
6233
6234bool PeerConnection::IceRestartPending(const std::string& content_name) const {
Karl Wibergf73f7d62019-04-08 13:36:536235 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 18:37:176236 return pending_ice_restarts_.find(content_name) !=
6237 pending_ice_restarts_.end();
6238}
6239
Steve Anton75737c02017-11-06 18:37:176240bool PeerConnection::NeedsIceRestart(const std::string& content_name) const {
6241 return transport_controller_->NeedsIceRestart(content_name);
6242}
6243
6244void PeerConnection::OnCertificateReady(
6245 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
6246 transport_controller_->SetLocalCertificate(certificate);
6247}
6248
6249void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
Steve Antonf8470812017-12-04 18:46:216250 SetSessionError(SessionError::kTransport,
6251 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
Steve Anton75737c02017-11-06 18:37:176252}
6253
Alex Loiko9289eda2018-11-23 16:18:596254void PeerConnection::OnTransportControllerConnectionState(
6255 cricket::IceConnectionState state) {
6256 switch (state) {
6257 case cricket::kIceConnectionConnecting:
6258 // If the current state is Connected or Completed, then there were
6259 // writable channels but now there are not, so the next state must
6260 // be Disconnected.
6261 // kIceConnectionConnecting is currently used as the default,
6262 // un-connected state by the TransportController, so its only use is
6263 // detecting disconnections.
6264 if (ice_connection_state_ ==
6265 PeerConnectionInterface::kIceConnectionConnected ||
6266 ice_connection_state_ ==
6267 PeerConnectionInterface::kIceConnectionCompleted) {
6268 SetIceConnectionState(
6269 PeerConnectionInterface::kIceConnectionDisconnected);
6270 }
6271 break;
6272 case cricket::kIceConnectionFailed:
6273 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
6274 break;
6275 case cricket::kIceConnectionConnected:
6276 RTC_LOG(LS_INFO) << "Changing to ICE connected state because "
6277 "all transports are writable.";
6278 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
6279 NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED);
6280 break;
6281 case cricket::kIceConnectionCompleted:
6282 RTC_LOG(LS_INFO) << "Changing to ICE completed state because "
6283 "all transports are complete.";
6284 if (ice_connection_state_ !=
6285 PeerConnectionInterface::kIceConnectionConnected) {
6286 // If jumping directly from "checking" to "connected",
6287 // signal "connected" first.
6288 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
6289 }
6290 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
6291 NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED);
6292 ReportTransportStats();
6293 break;
6294 default:
6295 RTC_NOTREACHED();
6296 }
6297}
6298
Steve Anton75737c02017-11-06 18:37:176299void PeerConnection::OnTransportControllerCandidatesGathered(
6300 const std::string& transport_name,
6301 const cricket::Candidates& candidates) {
Steve Anton75737c02017-11-06 18:37:176302 int sdp_mline_index;
6303 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
Mirko Bonadei675513b2017-11-09 10:09:256304 RTC_LOG(LS_ERROR)
6305 << "OnTransportControllerCandidatesGathered: content name "
6306 << transport_name << " not found";
Steve Anton75737c02017-11-06 18:37:176307 return;
6308 }
6309
6310 for (cricket::Candidates::const_iterator citer = candidates.begin();
6311 citer != candidates.end(); ++citer) {
6312 // Use transport_name as the candidate media id.
6313 std::unique_ptr<JsepIceCandidate> candidate(
6314 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
6315 if (local_description()) {
6316 mutable_local_description()->AddCandidate(candidate.get());
6317 }
6318 OnIceCandidate(std::move(candidate));
6319 }
6320}
6321
Eldar Relloda13ea22019-06-01 09:23:436322void PeerConnection::OnTransportControllerCandidateError(
6323 const cricket::IceCandidateErrorEvent& event) {
Eldar Rello0095d372019-12-02 20:22:076324 OnIceCandidateError(event.address, event.port, event.url, event.error_code,
Eldar Relloda13ea22019-06-01 09:23:436325 event.error_text);
6326}
6327
Steve Anton75737c02017-11-06 18:37:176328void PeerConnection::OnTransportControllerCandidatesRemoved(
6329 const std::vector<cricket::Candidate>& candidates) {
Steve Anton75737c02017-11-06 18:37:176330 // Sanity check.
6331 for (const cricket::Candidate& candidate : candidates) {
6332 if (candidate.transport_name().empty()) {
Mirko Bonadei675513b2017-11-09 10:09:256333 RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
Jonas Olsson45cc8902018-02-13 09:37:076334 "empty content name in candidate "
Mirko Bonadei675513b2017-11-09 10:09:256335 << candidate.ToString();
Steve Anton75737c02017-11-06 18:37:176336 return;
6337 }
6338 }
6339
6340 if (local_description()) {
6341 mutable_local_description()->RemoveCandidates(candidates);
6342 }
6343 OnIceCandidatesRemoved(candidates);
6344}
6345
Alex Drake00c7ecf2019-08-06 17:54:476346void PeerConnection::OnTransportControllerCandidateChanged(
6347 const cricket::CandidatePairChangeEvent& event) {
6348 OnSelectedCandidatePairChanged(event);
6349}
6350
Steve Anton75737c02017-11-06 18:37:176351void PeerConnection::OnTransportControllerDtlsHandshakeError(
6352 rtc::SSLHandshakeError error) {
Qingsi Wang7fc821d2018-07-12 19:54:536353 RTC_HISTOGRAM_ENUMERATION(
6354 "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error),
6355 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
Steve Anton75737c02017-11-06 18:37:176356}
6357
Steve Antoned10bd92017-12-05 18:52:596358void PeerConnection::EnableSending() {
Mirko Bonadei739baf02019-01-27 16:29:426359 for (const auto& transceiver : transceivers_) {
Amit Hilbuchdd9390c2018-11-14 00:26:056360 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Antoned10bd92017-12-05 18:52:596361 if (channel && !channel->enabled()) {
6362 channel->Enable(true);
6363 }
Steve Anton75737c02017-11-06 18:37:176364 }
6365
Harald Alvestrand00cf34c2019-12-02 08:56:026366 if (data_channel_controller_.rtp_data_channel() &&
6367 !data_channel_controller_.rtp_data_channel()->enabled()) {
6368 data_channel_controller_.rtp_data_channel()->Enable(true);
Steve Anton4171afb2017-11-20 18:20:226369 }
Steve Anton75737c02017-11-06 18:37:176370}
6371
6372// Returns the media index for a local ice candidate given the content name.
6373bool PeerConnection::GetLocalCandidateMediaIndex(
6374 const std::string& content_name,
6375 int* sdp_mline_index) {
6376 if (!local_description() || !sdp_mline_index) {
6377 return false;
6378 }
6379
6380 bool content_found = false;
6381 const ContentInfos& contents = local_description()->description()->contents();
6382 for (size_t index = 0; index < contents.size(); ++index) {
6383 if (contents[index].name == content_name) {
6384 *sdp_mline_index = static_cast<int>(index);
6385 content_found = true;
6386 break;
6387 }
6388 }
6389 return content_found;
6390}
6391
6392bool PeerConnection::UseCandidatesInSessionDescription(
6393 const SessionDescriptionInterface* remote_desc) {
6394 if (!remote_desc) {
6395 return true;
6396 }
6397 bool ret = true;
6398
6399 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
6400 const IceCandidateCollection* candidates = remote_desc->candidates(m);
6401 for (size_t n = 0; n < candidates->count(); ++n) {
6402 const IceCandidateInterface* candidate = candidates->at(n);
6403 bool valid = false;
6404 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
6405 if (valid) {
Mirko Bonadei675513b2017-11-09 10:09:256406 RTC_LOG(LS_INFO)
6407 << "UseCandidatesInSessionDescription: Not ready to use "
Jonas Olsson45cc8902018-02-13 09:37:076408 "candidate.";
Steve Anton75737c02017-11-06 18:37:176409 }
6410 continue;
6411 }
6412 ret = UseCandidate(candidate);
6413 if (!ret) {
6414 break;
6415 }
6416 }
6417 }
6418 return ret;
6419}
6420
6421bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) {
Guido Urdaneta41633172019-05-23 18:12:296422 RTCErrorOr<const cricket::ContentInfo*> result =
6423 FindContentInfo(remote_description(), candidate);
6424 if (!result.ok()) {
6425 RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. "
6426 << result.error().message();
Steve Anton75737c02017-11-06 18:37:176427 return false;
6428 }
Steve Anton75737c02017-11-06 18:37:176429 std::vector<cricket::Candidate> candidates;
6430 candidates.push_back(candidate->candidate());
6431 // Invoking BaseSession method to handle remote candidates.
Guido Urdaneta41633172019-05-23 18:12:296432 RTCError error = transport_controller_->AddRemoteCandidates(
6433 result.value()->name, candidates);
Henrik Boström5d8f8fa2018-04-13 15:22:506434 if (error.ok()) {
Qingsi Wang1ba5dec2019-08-19 18:57:176435 ReportRemoteIceCandidateAdded(candidate->candidate());
Henrik Boström5d8f8fa2018-04-13 15:22:506436 // Candidates successfully submitted for checking.
6437 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
6438 ice_connection_state_ ==
6439 PeerConnectionInterface::kIceConnectionDisconnected) {
6440 // If state is New, then the session has just gotten its first remote ICE
6441 // candidates, so go to Checking.
6442 // If state is Disconnected, the session is re-using old candidates or
6443 // receiving additional ones, so go to Checking.
6444 // If state is Connected, stay Connected.
6445 // TODO(bemasc): If state is Connected, and the new candidates are for a
6446 // newly added transport, then the state actually _should_ move to
6447 // checking. Add a way to distinguish that case.
6448 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
6449 }
6450 // TODO(bemasc): If state is Completed, go back to Connected.
Jonas Olsson941a07c2018-09-13 08:07:076451 } else {
Zhi Huange830e682018-03-30 17:48:356452 RTC_LOG(LS_WARNING) << error.message();
Steve Anton75737c02017-11-06 18:37:176453 }
6454 return true;
6455}
6456
Guido Urdaneta41633172019-05-23 18:12:296457RTCErrorOr<const cricket::ContentInfo*> PeerConnection::FindContentInfo(
6458 const SessionDescriptionInterface* description,
6459 const IceCandidateInterface* candidate) {
6460 if (candidate->sdp_mline_index() >= 0) {
6461 size_t mediacontent_index =
6462 static_cast<size_t>(candidate->sdp_mline_index());
6463 size_t content_size = description->description()->contents().size();
6464 if (mediacontent_index < content_size) {
6465 return &description->description()->contents()[mediacontent_index];
6466 } else {
6467 return RTCError(RTCErrorType::INVALID_RANGE,
6468 "Media line index (" +
6469 rtc::ToString(candidate->sdp_mline_index()) +
6470 ") out of range (number of mlines: " +
6471 rtc::ToString(content_size) + ").");
6472 }
6473 } else if (!candidate->sdp_mid().empty()) {
6474 auto& contents = description->description()->contents();
6475 auto it = absl::c_find_if(
6476 contents, [candidate](const cricket::ContentInfo& content_info) {
6477 return content_info.mid() == candidate->sdp_mid();
6478 });
6479 if (it == contents.end()) {
6480 return RTCError(
6481 RTCErrorType::INVALID_PARAMETER,
6482 "Mid " + candidate->sdp_mid() +
6483 " specified but no media section with that mid found.");
6484 } else {
6485 return &*it;
6486 }
6487 }
6488
6489 return RTCError(RTCErrorType::INVALID_PARAMETER,
6490 "Neither sdp_mline_index nor sdp_mid specified.");
6491}
6492
Steve Anton75737c02017-11-06 18:37:176493void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) {
Steve Anton75737c02017-11-06 18:37:176494 // Destroy video channel first since it may have a pointer to the
6495 // voice channel.
6496 const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc);
Steve Anton6fec8802017-12-04 18:37:296497 if (!video_info || video_info->rejected) {
6498 DestroyTransceiverChannel(GetVideoTransceiver());
Steve Anton75737c02017-11-06 18:37:176499 }
6500
Steve Anton6fec8802017-12-04 18:37:296501 const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc);
6502 if (!audio_info || audio_info->rejected) {
6503 DestroyTransceiverChannel(GetAudioTransceiver());
Steve Anton75737c02017-11-06 18:37:176504 }
6505
6506 const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc);
6507 if (!data_info || data_info->rejected) {
Harald Alvestrand408cb4b2019-11-16 11:09:086508 DestroyDataChannelTransport();
Steve Anton75737c02017-11-06 18:37:176509 }
6510}
6511
Steve Antondcc3c022017-12-23 00:02:546512RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup(
6513 const SessionDescription& desc) const {
Steve Anton75737c02017-11-06 18:37:176514 const cricket::ContentGroup* bundle_group = nullptr;
6515 if (configuration_.bundle_policy ==
6516 PeerConnectionInterface::kBundlePolicyMaxBundle) {
Steve Antondcc3c022017-12-23 00:02:546517 bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
Steve Anton75737c02017-11-06 18:37:176518 if (!bundle_group) {
Steve Anton8a006912017-12-04 23:25:566519 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6520 "max-bundle configured but session description "
6521 "has no BUNDLE group");
Steve Anton75737c02017-11-06 18:37:176522 }
6523 }
Mirko Bonadei9f3a44f2019-01-30 12:47:426524 return bundle_group;
Steve Antondcc3c022017-12-23 00:02:546525}
6526
6527RTCError PeerConnection::CreateChannels(const SessionDescription& desc) {
Zhi Huange830e682018-03-30 17:48:356528 // Creating the media channels. Transports should already have been created
6529 // at this point.
Steve Antondcc3c022017-12-23 00:02:546530 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc);
Steve Antoneda6ccd2017-12-04 18:21:556531 if (voice && !voice->rejected &&
6532 !GetAudioTransceiver()->internal()->channel()) {
Zhi Huange830e682018-03-30 17:48:356533 cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name);
Steve Antoneda6ccd2017-12-04 18:21:556534 if (!voice_channel) {
Steve Anton8a006912017-12-04 23:25:566535 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
6536 "Failed to create voice channel.");
Steve Antoneda6ccd2017-12-04 18:21:556537 }
6538 GetAudioTransceiver()->internal()->SetChannel(voice_channel);
6539 }
6540
Steve Antondcc3c022017-12-23 00:02:546541 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc);
Steve Antoneda6ccd2017-12-04 18:21:556542 if (video && !video->rejected &&
6543 !GetVideoTransceiver()->internal()->channel()) {
Zhi Huange830e682018-03-30 17:48:356544 cricket::VideoChannel* video_channel = CreateVideoChannel(video->name);
Steve Antoneda6ccd2017-12-04 18:21:556545 if (!video_channel) {
Steve Anton8a006912017-12-04 23:25:566546 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
6547 "Failed to create video channel.");
Steve Anton75737c02017-11-06 18:37:176548 }
Steve Antoneda6ccd2017-12-04 18:21:556549 GetVideoTransceiver()->internal()->SetChannel(video_channel);
Steve Anton75737c02017-11-06 18:37:176550 }
6551
Steve Antondcc3c022017-12-23 00:02:546552 const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc);
Harald Alvestrand05e4d082019-12-03 13:04:216553 if (data_channel_type() != cricket::DCT_NONE && data && !data->rejected &&
Harald Alvestrand00cf34c2019-12-02 08:56:026554 !data_channel_controller_.rtp_data_channel() &&
6555 !data_channel_controller_.data_channel_transport()) {
Zhi Huange830e682018-03-30 17:48:356556 if (!CreateDataChannel(data->name)) {
Steve Anton8a006912017-12-04 23:25:566557 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
6558 "Failed to create data channel.");
Steve Anton75737c02017-11-06 18:37:176559 }
6560 }
6561
Steve Anton8a006912017-12-04 23:25:566562 return RTCError::OK();
Steve Anton75737c02017-11-06 18:37:176563}
6564
Steve Anton4171afb2017-11-20 18:20:226565// TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
Steve Antoneda6ccd2017-12-04 18:21:556566cricket::VoiceChannel* PeerConnection::CreateVoiceChannel(
Zhi Huange830e682018-03-30 17:48:356567 const std::string& mid) {
Anton Sukhanov98a462c2018-10-17 20:15:426568 RtpTransportInternal* rtp_transport = GetRtpTransport(mid);
Anton Sukhanov98a462c2018-10-17 20:15:426569
Steve Anton75737c02017-11-06 18:37:176570 cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel(
Niels Möller2a707032020-06-16 14:39:136571 call_ptr_, configuration_.media_config, rtp_transport, signaling_thread(),
6572 mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_,
6573 audio_options_);
Steve Anton75737c02017-11-06 18:37:176574 if (!voice_channel) {
Steve Antoneda6ccd2017-12-04 18:21:556575 return nullptr;
Steve Anton75737c02017-11-06 18:37:176576 }
Steve Anton75737c02017-11-06 18:37:176577 voice_channel->SignalDtlsSrtpSetupFailure.connect(
6578 this, &PeerConnection::OnDtlsSrtpSetupFailure);
Steve Anton75737c02017-11-06 18:37:176579 voice_channel->SignalSentPacket.connect(this,
6580 &PeerConnection::OnSentPacket_w);
Zhi Huange830e682018-03-30 17:48:356581 voice_channel->SetRtpTransport(rtp_transport);
Steve Anton4171afb2017-11-20 18:20:226582
Steve Antoneda6ccd2017-12-04 18:21:556583 return voice_channel;
Steve Anton75737c02017-11-06 18:37:176584}
6585
Steve Anton4171afb2017-11-20 18:20:226586// TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
Steve Antoneda6ccd2017-12-04 18:21:556587cricket::VideoChannel* PeerConnection::CreateVideoChannel(
Zhi Huange830e682018-03-30 17:48:356588 const std::string& mid) {
Anton Sukhanov98a462c2018-10-17 20:15:426589 RtpTransportInternal* rtp_transport = GetRtpTransport(mid);
6590
Steve Anton75737c02017-11-06 18:37:176591 cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel(
Niels Möller2a707032020-06-16 14:39:136592 call_ptr_, configuration_.media_config, rtp_transport, signaling_thread(),
6593 mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_, video_options_,
Anton Sukhanov4f08faa2019-05-21 18:12:576594 video_bitrate_allocator_factory_.get());
Steve Anton75737c02017-11-06 18:37:176595 if (!video_channel) {
Steve Antoneda6ccd2017-12-04 18:21:556596 return nullptr;
Steve Anton75737c02017-11-06 18:37:176597 }
Steve Anton75737c02017-11-06 18:37:176598 video_channel->SignalDtlsSrtpSetupFailure.connect(
6599 this, &PeerConnection::OnDtlsSrtpSetupFailure);
Steve Anton75737c02017-11-06 18:37:176600 video_channel->SignalSentPacket.connect(this,
6601 &PeerConnection::OnSentPacket_w);
Zhi Huange830e682018-03-30 17:48:356602 video_channel->SetRtpTransport(rtp_transport);
Steve Anton4171afb2017-11-20 18:20:226603
Steve Antoneda6ccd2017-12-04 18:21:556604 return video_channel;
Steve Anton75737c02017-11-06 18:37:176605}
6606
Zhi Huange830e682018-03-30 17:48:356607bool PeerConnection::CreateDataChannel(const std::string& mid) {
Harald Alvestrand05e4d082019-12-03 13:04:216608 switch (data_channel_type()) {
Bjorn Mellem175aa2e2018-11-08 19:23:226609 case cricket::DCT_SCTP:
Harald Alvestrand7a829a82020-02-12 06:38:216610 if (network_thread()->Invoke<bool>(
Bjorn A Mellembc3eebc2019-09-23 21:53:546611 RTC_FROM_HERE,
6612 rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, this,
6613 mid))) {
Harald Alvestrand7a829a82020-02-12 06:38:216614 sctp_mid_s_ = mid;
6615 } else {
Qingsi Wang437077d2019-09-10 17:52:266616 return false;
6617 }
Bjorn A Mellembc3eebc2019-09-23 21:53:546618 return true;
Bjorn Mellem175aa2e2018-11-08 19:23:226619 case cricket::DCT_RTP:
6620 default:
6621 RtpTransportInternal* rtp_transport = GetRtpTransport(mid);
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:426622 // TODO(bugs.webrtc.org/9987): set_rtp_data_channel() should be called on
6623 // the network thread like set_data_channel_transport is.
Harald Alvestrand00cf34c2019-12-02 08:56:026624 data_channel_controller_.set_rtp_data_channel(
6625 channel_manager()->CreateRtpDataChannel(
6626 configuration_.media_config, rtp_transport, signaling_thread(),
6627 mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_));
6628 if (!data_channel_controller_.rtp_data_channel()) {
Bjorn Mellem175aa2e2018-11-08 19:23:226629 return false;
6630 }
Harald Alvestrand00cf34c2019-12-02 08:56:026631 data_channel_controller_.rtp_data_channel()
6632 ->SignalDtlsSrtpSetupFailure.connect(
6633 this, &PeerConnection::OnDtlsSrtpSetupFailure);
6634 data_channel_controller_.rtp_data_channel()->SignalSentPacket.connect(
Bjorn Mellem175aa2e2018-11-08 19:23:226635 this, &PeerConnection::OnSentPacket_w);
Harald Alvestrand00cf34c2019-12-02 08:56:026636 data_channel_controller_.rtp_data_channel()->SetRtpTransport(
6637 rtp_transport);
Eldar Rellod9ebe012020-03-18 18:41:456638 have_pending_rtp_data_channel_ = true;
Bjorn Mellem175aa2e2018-11-08 19:23:226639 return true;
Steve Anton75737c02017-11-06 18:37:176640 }
Bjorn A Mellembc3eebc2019-09-23 21:53:546641 return false;
Steve Anton75737c02017-11-06 18:37:176642}
6643
6644Call::Stats PeerConnection::GetCallStats() {
6645 if (!worker_thread()->IsCurrent()) {
6646 return worker_thread()->Invoke<Call::Stats>(
6647 RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this));
6648 }
Karl Wiberg6cab5c82019-03-26 08:57:016649 RTC_DCHECK_RUN_ON(worker_thread());
Henrik Boströme88c95e2020-07-08 09:18:506650 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Anton75737c02017-11-06 18:37:176651 if (call_) {
6652 return call_->GetStats();
6653 } else {
6654 return Call::Stats();
6655 }
6656}
6657
Bjorn A Mellemb689af42019-08-21 17:44:596658bool PeerConnection::SetupDataChannelTransport_n(const std::string& mid) {
Bjorn A Mellembc3eebc2019-09-23 21:53:546659 DataChannelTransportInterface* transport =
6660 transport_controller_->GetDataChannelTransport(mid);
6661 if (!transport) {
Piotr (Peter) Slatalab1ae10b2019-03-01 19:14:056662 RTC_LOG(LS_ERROR)
Bjorn A Mellemb689af42019-08-21 17:44:596663 << "Data channel transport is not available for data channels, mid="
6664 << mid;
Bjorn Mellem175aa2e2018-11-08 19:23:226665 return false;
6666 }
Bjorn A Mellemb689af42019-08-21 17:44:596667 RTC_LOG(LS_INFO) << "Setting up data channel transport for mid=" << mid;
Bjorn Mellem175aa2e2018-11-08 19:23:226668
Harald Alvestrand00cf34c2019-12-02 08:56:026669 data_channel_controller_.set_data_channel_transport(transport);
6670 data_channel_controller_.SetupDataChannelTransport_n();
Harald Alvestrand7a829a82020-02-12 06:38:216671 sctp_mid_n_ = mid;
Bjorn A Mellembc3eebc2019-09-23 21:53:546672
6673 // Note: setting the data sink and checking initial state must be done last,
6674 // after setting up the data channel. Setting the data sink may trigger
6675 // callbacks to PeerConnection which require the transport to be completely
6676 // set up (eg. OnReadyToSend()).
Harald Alvestrand00cf34c2019-12-02 08:56:026677 transport->SetDataSink(&data_channel_controller_);
Bjorn Mellem175aa2e2018-11-08 19:23:226678 return true;
6679}
6680
Bjorn A Mellemb689af42019-08-21 17:44:596681void PeerConnection::TeardownDataChannelTransport_n() {
Harald Alvestrand7a829a82020-02-12 06:38:216682 if (!sctp_mid_n_ && !data_channel_controller_.data_channel_transport()) {
Bjorn Mellem175aa2e2018-11-08 19:23:226683 return;
6684 }
Bjorn A Mellemb689af42019-08-21 17:44:596685 RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid="
Harald Alvestrand7a829a82020-02-12 06:38:216686 << *sctp_mid_n_;
Bjorn A Mellemb689af42019-08-21 17:44:596687
Bjorn A Mellemb689af42019-08-21 17:44:596688 // |sctp_mid_| may still be active through an SCTP transport. If not, unset
6689 // it.
Harald Alvestrand7a829a82020-02-12 06:38:216690 sctp_mid_n_.reset();
Harald Alvestrand00cf34c2019-12-02 08:56:026691 data_channel_controller_.TeardownDataChannelTransport_n();
Bjorn Mellem175aa2e2018-11-08 19:23:226692}
6693
Steve Anton75737c02017-11-06 18:37:176694// Returns false if bundle is enabled and rtcp_mux is disabled.
6695bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) {
6696 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
6697 if (!bundle_enabled)
6698 return true;
6699
6700 const cricket::ContentGroup* bundle_group =
6701 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
6702 RTC_DCHECK(bundle_group != NULL);
6703
6704 const cricket::ContentInfos& contents = desc->contents();
6705 for (cricket::ContentInfos::const_iterator citer = contents.begin();
6706 citer != contents.end(); ++citer) {
6707 const cricket::ContentInfo* content = (&*citer);
6708 RTC_DCHECK(content != NULL);
6709 if (bundle_group->HasContentName(content->name) && !content->rejected &&
Steve Anton5adfafd2017-12-21 00:34:006710 content->type == MediaProtocolType::kRtp) {
Steve Anton75737c02017-11-06 18:37:176711 if (!HasRtcpMuxEnabled(content))
6712 return false;
6713 }
6714 }
6715 // RTCP-MUX is enabled in all the contents.
6716 return true;
6717}
6718
6719bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 23:14:306720 return content->media_description()->rtcp_mux();
Steve Anton75737c02017-11-06 18:37:176721}
6722
Steve Anton06817cd2018-12-18 23:55:306723static RTCError ValidateMids(const cricket::SessionDescription& description) {
6724 std::set<std::string> mids;
6725 for (const cricket::ContentInfo& content : description.contents()) {
Steve Antonceac0152018-12-19 19:32:206726 if (content.name.empty()) {
6727 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6728 "A media section is missing a MID attribute.");
6729 }
Steve Anton06817cd2018-12-18 23:55:306730 if (!mids.insert(content.name).second) {
6731 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6732 "Duplicate a=mid value '" + content.name + "'.");
6733 }
6734 }
6735 return RTCError::OK();
6736}
6737
Steve Anton8a006912017-12-04 23:25:566738RTCError PeerConnection::ValidateSessionDescription(
Steve Anton75737c02017-11-06 18:37:176739 const SessionDescriptionInterface* sdesc,
Steve Anton8a006912017-12-04 23:25:566740 cricket::ContentSource source) {
Steve Antonf8470812017-12-04 18:46:216741 if (session_error() != SessionError::kNone) {
Steve Anton8a006912017-12-04 23:25:566742 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
Steve Anton75737c02017-11-06 18:37:176743 }
6744
6745 if (!sdesc || !sdesc->description()) {
Steve Anton8a006912017-12-04 23:25:566746 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
Steve Anton75737c02017-11-06 18:37:176747 }
6748
Steve Anton3828c062017-12-06 18:34:516749 SdpType type = sdesc->GetType();
6750 if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) ||
6751 (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) {
Steve Anton8a006912017-12-04 23:25:566752 LOG_AND_RETURN_ERROR(
Harald Alvestrand5081c0c2018-03-09 14:18:036753 RTCErrorType::INVALID_STATE,
Steve Anton8a006912017-12-04 23:25:566754 "Called in wrong state: " + GetSignalingStateString(signaling_state()));
Steve Anton75737c02017-11-06 18:37:176755 }
6756
Steve Anton06817cd2018-12-18 23:55:306757 RTCError error = ValidateMids(*sdesc->description());
6758 if (!error.ok()) {
6759 return error;
6760 }
6761
Steve Anton75737c02017-11-06 18:37:176762 // Verify crypto settings.
6763 std::string crypto_error;
Steve Anton8a006912017-12-04 23:25:566764 if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
6765 dtls_enabled_) {
Qingsi Wang7fc821d2018-07-12 19:54:536766 RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_);
Steve Anton8a006912017-12-04 23:25:566767 if (!crypto_error.ok()) {
6768 return crypto_error;
6769 }
Steve Anton75737c02017-11-06 18:37:176770 }
6771
6772 // Verify ice-ufrag and ice-pwd.
6773 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
Steve Anton8a006912017-12-04 23:25:566774 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6775 kSdpWithoutIceUfragPwd);
Steve Anton75737c02017-11-06 18:37:176776 }
6777
6778 if (!ValidateBundleSettings(sdesc->description())) {
Steve Anton8a006912017-12-04 23:25:566779 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6780 kBundleWithoutRtcpMux);
Steve Anton75737c02017-11-06 18:37:176781 }
6782
6783 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
6784 // m-lines that do not rtcp-mux enabled.
6785
6786 // Verify m-lines in Answer when compared against Offer.
Steve Anton3828c062017-12-06 18:34:516787 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
Seth Hampsonae8a90a2018-02-13 23:33:486788 // With an answer we want to compare the new answer session description with
6789 // the offer's session description from the current negotiation.
Steve Anton75737c02017-11-06 18:37:176790 const cricket::SessionDescription* offer_desc =
6791 (source == cricket::CS_LOCAL) ? remote_description()->description()
6792 : local_description()->description();
Seth Hampsonae8a90a2018-02-13 23:33:486793 if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) ||
6794 !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(),
6795 type)) {
Steve Anton8a006912017-12-04 23:25:566796 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6797 kMlineMismatchInAnswer);
Steve Anton75737c02017-11-06 18:37:176798 }
6799 } else {
Steve Anton75737c02017-11-06 18:37:176800 // The re-offers should respect the order of m= sections in current
6801 // description. See RFC3264 Section 8 paragraph 4 for more details.
Seth Hampsonae8a90a2018-02-13 23:33:486802 // With a re-offer, either the current local or current remote descriptions
6803 // could be the most up to date, so we would like to check against both of
6804 // them if they exist. It could be the case that one of them has a 0 port
6805 // for a media section, but the other does not. This is important to check
6806 // against in the case that we are recycling an m= section.
6807 const cricket::SessionDescription* current_desc = nullptr;
6808 const cricket::SessionDescription* secondary_current_desc = nullptr;
6809 if (local_description()) {
6810 current_desc = local_description()->description();
6811 if (remote_description()) {
6812 secondary_current_desc = remote_description()->description();
6813 }
6814 } else if (remote_description()) {
6815 current_desc = remote_description()->description();
6816 }
Steve Anton75737c02017-11-06 18:37:176817 if (current_desc &&
Seth Hampsonae8a90a2018-02-13 23:33:486818 !MediaSectionsInSameOrder(*current_desc, secondary_current_desc,
6819 *sdesc->description(), type)) {
Steve Anton8a006912017-12-04 23:25:566820 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6821 kMlineMismatchInSubsequentOffer);
Steve Anton75737c02017-11-06 18:37:176822 }
6823 }
6824
Steve Antonba42e992018-04-09 21:10:016825 if (IsUnifiedPlan()) {
6826 // Ensure that each audio and video media section has at most one
6827 // "StreamParams". This will return an error if receiving a session
6828 // description from a "Plan B" endpoint which adds multiple tracks of the
6829 // same type. With Unified Plan, there can only be at most one track per
6830 // media section.
6831 for (const ContentInfo& content : sdesc->description()->contents()) {
Harald Alvestrand1716d392019-06-03 18:35:456832 const MediaContentDescription& desc = *content.media_description();
Steve Antonba42e992018-04-09 21:10:016833 if ((desc.type() == cricket::MEDIA_TYPE_AUDIO ||
6834 desc.type() == cricket::MEDIA_TYPE_VIDEO) &&
6835 desc.streams().size() > 1u) {
6836 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6837 "Media section has more than one track specified "
6838 "with a=ssrc lines which is not supported with "
6839 "Unified Plan.");
6840 }
6841 }
6842 }
6843
Steve Anton8a006912017-12-04 23:25:566844 return RTCError::OK();
Steve Anton75737c02017-11-06 18:37:176845}
6846
Steve Anton3828c062017-12-06 18:34:516847bool PeerConnection::ExpectSetLocalDescription(SdpType type) {
Steve Anton75737c02017-11-06 18:37:176848 PeerConnectionInterface::SignalingState state = signaling_state();
Steve Anton3828c062017-12-06 18:34:516849 if (type == SdpType::kOffer) {
Steve Anton75737c02017-11-06 18:37:176850 return (state == PeerConnectionInterface::kStable) ||
6851 (state == PeerConnectionInterface::kHaveLocalOffer);
Steve Anton20393062017-12-05 00:24:526852 } else {
Steve Anton3828c062017-12-06 18:34:516853 RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
Steve Anton75737c02017-11-06 18:37:176854 return (state == PeerConnectionInterface::kHaveRemoteOffer) ||
6855 (state == PeerConnectionInterface::kHaveLocalPrAnswer);
6856 }
6857}
6858
Steve Anton3828c062017-12-06 18:34:516859bool PeerConnection::ExpectSetRemoteDescription(SdpType type) {
Steve Anton75737c02017-11-06 18:37:176860 PeerConnectionInterface::SignalingState state = signaling_state();
Steve Anton3828c062017-12-06 18:34:516861 if (type == SdpType::kOffer) {
Steve Anton75737c02017-11-06 18:37:176862 return (state == PeerConnectionInterface::kStable) ||
6863 (state == PeerConnectionInterface::kHaveRemoteOffer);
Steve Anton20393062017-12-05 00:24:526864 } else {
Steve Anton3828c062017-12-06 18:34:516865 RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
Steve Anton75737c02017-11-06 18:37:176866 return (state == PeerConnectionInterface::kHaveLocalOffer) ||
6867 (state == PeerConnectionInterface::kHaveRemotePrAnswer);
6868 }
6869}
6870
Steve Antonf8470812017-12-04 18:46:216871const char* PeerConnection::SessionErrorToString(SessionError error) const {
6872 switch (error) {
6873 case SessionError::kNone:
6874 return "ERROR_NONE";
6875 case SessionError::kContent:
6876 return "ERROR_CONTENT";
6877 case SessionError::kTransport:
6878 return "ERROR_TRANSPORT";
6879 }
6880 RTC_NOTREACHED();
6881 return "";
6882}
6883
Steve Anton75737c02017-11-06 18:37:176884std::string PeerConnection::GetSessionErrorMsg() {
Jonas Olsson366a50c2018-09-06 11:41:306885 rtc::StringBuilder desc;
Steve Antonf8470812017-12-04 18:46:216886 desc << kSessionError << SessionErrorToString(session_error()) << ". ";
6887 desc << kSessionErrorDesc << session_error_desc() << ".";
Jonas Olsson84df1c72018-09-14 14:59:326888 return desc.Release();
Steve Anton75737c02017-11-06 18:37:176889}
6890
Steve Anton8e20f172018-03-06 18:55:046891void PeerConnection::ReportSdpFormatReceived(
6892 const SessionDescriptionInterface& remote_offer) {
Steve Anton8e20f172018-03-06 18:55:046893 int num_audio_mlines = 0;
6894 int num_video_mlines = 0;
6895 int num_audio_tracks = 0;
6896 int num_video_tracks = 0;
6897 for (const ContentInfo& content : remote_offer.description()->contents()) {
6898 cricket::MediaType media_type = content.media_description()->type();
6899 int num_tracks = std::max(
6900 1, static_cast<int>(content.media_description()->streams().size()));
6901 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
6902 num_audio_mlines += 1;
6903 num_audio_tracks += num_tracks;
6904 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
6905 num_video_mlines += 1;
6906 num_video_tracks += num_tracks;
6907 }
6908 }
6909 SdpFormatReceived format = kSdpFormatReceivedNoTracks;
6910 if (num_audio_mlines > 1 || num_video_mlines > 1) {
6911 format = kSdpFormatReceivedComplexUnifiedPlan;
6912 } else if (num_audio_tracks > 1 || num_video_tracks > 1) {
6913 format = kSdpFormatReceivedComplexPlanB;
6914 } else if (num_audio_tracks > 0 || num_video_tracks > 0) {
6915 format = kSdpFormatReceivedSimple;
6916 }
Qingsi Wang7fc821d2018-07-12 19:54:536917 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format,
6918 kSdpFormatReceivedMax);
Steve Anton8e20f172018-03-06 18:55:046919}
6920
Qingsi Wang1ba5dec2019-08-19 18:57:176921void PeerConnection::ReportIceCandidateCollected(
6922 const cricket::Candidate& candidate) {
6923 NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED);
6924 if (candidate.address().IsPrivateIP()) {
6925 NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED);
6926 }
6927 if (candidate.address().IsUnresolvedIP()) {
6928 NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED);
6929 }
6930 if (candidate.address().family() == AF_INET6) {
6931 NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED);
6932 }
6933}
6934
6935void PeerConnection::ReportRemoteIceCandidateAdded(
6936 const cricket::Candidate& candidate) {
6937 NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED);
6938 if (candidate.address().IsPrivateIP()) {
6939 NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED);
6940 }
6941 if (candidate.address().IsUnresolvedIP()) {
6942 NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED);
6943 }
6944 if (candidate.address().family() == AF_INET6) {
6945 NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED);
6946 }
6947}
6948
Harald Alvestrand8ebba742018-05-31 12:00:346949void PeerConnection::NoteUsageEvent(UsageEvent event) {
6950 RTC_DCHECK_RUN_ON(signaling_thread());
6951 usage_event_accumulator_ |= static_cast<int>(event);
6952}
6953
6954void PeerConnection::ReportUsagePattern() const {
6955 RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_;
Qingsi Wang7fc821d2018-07-12 19:54:536956 RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern",
6957 usage_event_accumulator_,
6958 static_cast<int>(UsageEvent::MAX_VALUE));
Harald Alvestrandc0e97252018-07-26 08:39:556959 const int bad_bits =
Qingsi Wang1ba5dec2019-08-19 18:57:176960 static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED) |
Harald Alvestrandc0e97252018-07-26 08:39:556961 static_cast<int>(UsageEvent::CANDIDATE_COLLECTED);
6962 const int good_bits =
Qingsi Wang1ba5dec2019-08-19 18:57:176963 static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED) |
Harald Alvestrandc0e97252018-07-26 08:39:556964 static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) |
6965 static_cast<int>(UsageEvent::ICE_STATE_CONNECTED);
6966 if ((usage_event_accumulator_ & bad_bits) == bad_bits &&
6967 (usage_event_accumulator_ & good_bits) == 0) {
Harald Alvestrand7a1c7f72018-08-01 08:50:166968 // If called after close(), we can't report, because observer may have
6969 // been deallocated, and therefore pointer is null. Write to log instead.
6970 if (observer_) {
6971 Observer()->OnInterestingUsage(usage_event_accumulator_);
6972 } else {
6973 RTC_LOG(LS_INFO) << "Interesting usage signature "
6974 << usage_event_accumulator_
6975 << " observed after observer shutdown";
6976 }
Harald Alvestrandc0e97252018-07-26 08:39:556977 }
Harald Alvestrand8ebba742018-05-31 12:00:346978}
6979
Steve Anton0ffaaa22018-02-23 18:31:306980void PeerConnection::ReportNegotiatedSdpSemantics(
6981 const SessionDescriptionInterface& answer) {
Qingsi Wang7fc821d2018-07-12 19:54:536982 SdpSemanticNegotiated semantics_negotiated;
Steve Anton0ffaaa22018-02-23 18:31:306983 switch (answer.description()->msid_signaling()) {
6984 case 0:
Qingsi Wang7fc821d2018-07-12 19:54:536985 semantics_negotiated = kSdpSemanticNegotiatedNone;
Steve Anton0ffaaa22018-02-23 18:31:306986 break;
6987 case cricket::kMsidSignalingMediaSection:
Qingsi Wang7fc821d2018-07-12 19:54:536988 semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan;
Steve Anton0ffaaa22018-02-23 18:31:306989 break;
6990 case cricket::kMsidSignalingSsrcAttribute:
Qingsi Wang7fc821d2018-07-12 19:54:536991 semantics_negotiated = kSdpSemanticNegotiatedPlanB;
Steve Anton0ffaaa22018-02-23 18:31:306992 break;
6993 case cricket::kMsidSignalingMediaSection |
6994 cricket::kMsidSignalingSsrcAttribute:
Qingsi Wang7fc821d2018-07-12 19:54:536995 semantics_negotiated = kSdpSemanticNegotiatedMixed;
Steve Anton0ffaaa22018-02-23 18:31:306996 break;
6997 default:
6998 RTC_NOTREACHED();
6999 }
Qingsi Wang7fc821d2018-07-12 19:54:537000 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated",
7001 semantics_negotiated, kSdpSemanticNegotiatedMax);
Steve Anton0ffaaa22018-02-23 18:31:307002}
7003
Steve Anton75737c02017-11-06 18:37:177004// We need to check the local/remote description for the Transport instead of
7005// the session, because a new Transport added during renegotiation may have
7006// them unset while the session has them set from the previous negotiation.
7007// Not doing so may trigger the auto generation of transport description and
7008// mess up DTLS identity information, ICE credential, etc.
7009bool PeerConnection::ReadyToUseRemoteCandidate(
7010 const IceCandidateInterface* candidate,
7011 const SessionDescriptionInterface* remote_desc,
7012 bool* valid) {
7013 *valid = true;
7014
7015 const SessionDescriptionInterface* current_remote_desc =
7016 remote_desc ? remote_desc : remote_description();
7017
7018 if (!current_remote_desc) {
7019 return false;
7020 }
7021
Guido Urdaneta41633172019-05-23 18:12:297022 RTCErrorOr<const cricket::ContentInfo*> result =
7023 FindContentInfo(current_remote_desc, candidate);
7024 if (!result.ok()) {
7025 RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. "
7026 << result.error().message();
Steve Anton75737c02017-11-06 18:37:177027
7028 *valid = false;
7029 return false;
7030 }
7031
Guido Urdaneta41633172019-05-23 18:12:297032 std::string transport_name = GetTransportName(result.value()->name);
7033 return !transport_name.empty();
Steve Anton75737c02017-11-06 18:37:177034}
7035
7036bool PeerConnection::SrtpRequired() const {
Niels Möller2a707032020-06-16 14:39:137037 return (dtls_enabled_ ||
Anton Sukhanov316f3ac2019-05-23 22:50:387038 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED);
Steve Anton75737c02017-11-06 18:37:177039}
7040
7041void PeerConnection::OnTransportControllerGatheringState(
7042 cricket::IceGatheringState state) {
7043 RTC_DCHECK(signaling_thread()->IsCurrent());
7044 if (state == cricket::kIceGatheringGathering) {
7045 OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering);
7046 } else if (state == cricket::kIceGatheringComplete) {
7047 OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete);
Harald Alvestrandbedb6052020-08-20 12:50:107048 } else if (state == cricket::kIceGatheringNew) {
7049 OnIceGatheringChange(PeerConnectionInterface::kIceGatheringNew);
7050 } else {
7051 RTC_LOG(LS_ERROR) << "Unknown state received: " << state;
7052 RTC_NOTREACHED();
Steve Anton75737c02017-11-06 18:37:177053 }
7054}
7055
7056void PeerConnection::ReportTransportStats() {
Steve Antonc7b964c2018-02-01 22:39:457057 std::map<std::string, std::set<cricket::MediaType>>
7058 media_types_by_transport_name;
Mirko Bonadei739baf02019-01-27 16:29:427059 for (const auto& transceiver : transceivers_) {
Steve Antonc7b964c2018-02-01 22:39:457060 if (transceiver->internal()->channel()) {
7061 const std::string& transport_name =
7062 transceiver->internal()->channel()->transport_name();
7063 media_types_by_transport_name[transport_name].insert(
Steve Anton69470252018-02-09 19:43:087064 transceiver->media_type());
Steve Antonc7b964c2018-02-01 22:39:457065 }
Steve Anton75737c02017-11-06 18:37:177066 }
7067 if (rtp_data_channel()) {
Steve Antonc7b964c2018-02-01 22:39:457068 media_types_by_transport_name[rtp_data_channel()->transport_name()].insert(
7069 cricket::MEDIA_TYPE_DATA);
Steve Anton75737c02017-11-06 18:37:177070 }
Zhi Huange830e682018-03-30 17:48:357071
Danil Chapovalov66cadcc2018-06-19 14:47:437072 absl::optional<std::string> transport_name = sctp_transport_name();
Zhi Huange830e682018-03-30 17:48:357073 if (transport_name) {
7074 media_types_by_transport_name[*transport_name].insert(
Steve Antonc7b964c2018-02-01 22:39:457075 cricket::MEDIA_TYPE_DATA);
Steve Anton75737c02017-11-06 18:37:177076 }
Zhi Huange830e682018-03-30 17:48:357077
Steve Antonc7b964c2018-02-01 22:39:457078 for (const auto& entry : media_types_by_transport_name) {
7079 const std::string& transport_name = entry.first;
7080 const std::set<cricket::MediaType> media_types = entry.second;
Steve Anton75737c02017-11-06 18:37:177081 cricket::TransportStats stats;
Steve Antonc7b964c2018-02-01 22:39:457082 if (transport_controller_->GetStats(transport_name, &stats)) {
Steve Anton75737c02017-11-06 18:37:177083 ReportBestConnectionState(stats);
Steve Antonc7b964c2018-02-01 22:39:457084 ReportNegotiatedCiphers(stats, media_types);
Steve Anton75737c02017-11-06 18:37:177085 }
7086 }
7087}
7088// Walk through the ConnectionInfos to gather best connection usage
7089// for IPv4 and IPv6.
7090void PeerConnection::ReportBestConnectionState(
7091 const cricket::TransportStats& stats) {
Steve Antonc7b964c2018-02-01 22:39:457092 for (const cricket::TransportChannelStats& channel_stats :
7093 stats.channel_stats) {
7094 for (const cricket::ConnectionInfo& connection_info :
Jonas Oreland149dc722019-08-28 06:10:277095 channel_stats.ice_transport_stats.connection_infos) {
Steve Antonc7b964c2018-02-01 22:39:457096 if (!connection_info.best_connection) {
Steve Anton75737c02017-11-06 18:37:177097 continue;
7098 }
7099
Steve Antonc7b964c2018-02-01 22:39:457100 const cricket::Candidate& local = connection_info.local_candidate;
7101 const cricket::Candidate& remote = connection_info.remote_candidate;
Steve Anton75737c02017-11-06 18:37:177102
7103 // Increment the counter for IceCandidatePairType.
7104 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
7105 (local.type() == RELAY_PORT_TYPE &&
7106 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
Qingsi Wang7fc821d2018-07-12 19:54:537107 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP",
7108 GetIceCandidatePairCounter(local, remote),
7109 kIceCandidatePairMax);
Steve Anton75737c02017-11-06 18:37:177110 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
Qingsi Wang7fc821d2018-07-12 19:54:537111 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP",
7112 GetIceCandidatePairCounter(local, remote),
7113 kIceCandidatePairMax);
Steve Anton75737c02017-11-06 18:37:177114 } else {
7115 RTC_CHECK(0);
7116 }
Steve Anton75737c02017-11-06 18:37:177117
7118 // Increment the counter for IP type.
7119 if (local.address().family() == AF_INET) {
Qingsi Wang7fc821d2018-07-12 19:54:537120 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics",
7121 kBestConnections_IPv4,
7122 kPeerConnectionAddressFamilyCounter_Max);
Steve Anton75737c02017-11-06 18:37:177123 } else if (local.address().family() == AF_INET6) {
Qingsi Wang7fc821d2018-07-12 19:54:537124 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics",
7125 kBestConnections_IPv6,
7126 kPeerConnectionAddressFamilyCounter_Max);
Steve Anton75737c02017-11-06 18:37:177127 } else {
Qingsi Wang1dac6d82018-12-12 23:28:477128 RTC_CHECK(!local.address().hostname().empty() &&
7129 local.address().IsUnresolvedIP());
Steve Anton75737c02017-11-06 18:37:177130 }
7131
7132 return;
7133 }
7134 }
7135}
7136
7137void PeerConnection::ReportNegotiatedCiphers(
Steve Antonc7b964c2018-02-01 22:39:457138 const cricket::TransportStats& stats,
7139 const std::set<cricket::MediaType>& media_types) {
Steve Anton75737c02017-11-06 18:37:177140 if (!dtls_enabled_ || stats.channel_stats.empty()) {
7141 return;
7142 }
7143
7144 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
7145 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
7146 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
7147 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
7148 return;
7149 }
7150
Qingsi Wang7fc821d2018-07-12 19:54:537151 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
7152 for (cricket::MediaType media_type : media_types) {
7153 switch (media_type) {
7154 case cricket::MEDIA_TYPE_AUDIO:
7155 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7156 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite,
7157 rtc::SRTP_CRYPTO_SUITE_MAX_VALUE);
7158 break;
7159 case cricket::MEDIA_TYPE_VIDEO:
7160 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7161 "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite,
7162 rtc::SRTP_CRYPTO_SUITE_MAX_VALUE);
7163 break;
7164 case cricket::MEDIA_TYPE_DATA:
7165 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7166 "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite,
7167 rtc::SRTP_CRYPTO_SUITE_MAX_VALUE);
7168 break;
7169 default:
7170 RTC_NOTREACHED();
7171 continue;
7172 }
Steve Antonc7b964c2018-02-01 22:39:457173 }
Qingsi Wang7fc821d2018-07-12 19:54:537174 }
7175
7176 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
7177 for (cricket::MediaType media_type : media_types) {
7178 switch (media_type) {
7179 case cricket::MEDIA_TYPE_AUDIO:
7180 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7181 "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite,
7182 rtc::SSL_CIPHER_SUITE_MAX_VALUE);
7183 break;
7184 case cricket::MEDIA_TYPE_VIDEO:
7185 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7186 "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite,
7187 rtc::SSL_CIPHER_SUITE_MAX_VALUE);
7188 break;
7189 case cricket::MEDIA_TYPE_DATA:
7190 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7191 "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite,
7192 rtc::SSL_CIPHER_SUITE_MAX_VALUE);
7193 break;
7194 default:
7195 RTC_NOTREACHED();
7196 continue;
7197 }
Steve Antonc7b964c2018-02-01 22:39:457198 }
Steve Anton75737c02017-11-06 18:37:177199 }
7200}
7201
7202void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
Karl Wiberg6cab5c82019-03-26 08:57:017203 RTC_DCHECK_RUN_ON(worker_thread());
Steve Anton75737c02017-11-06 18:37:177204 RTC_DCHECK(call_);
7205 call_->OnSentPacket(sent_packet);
7206}
7207
7208const std::string PeerConnection::GetTransportName(
7209 const std::string& content_name) {
Amit Hilbuchdd9390c2018-11-14 00:26:057210 cricket::ChannelInterface* channel = GetChannel(content_name);
Steve Anton6fec8802017-12-04 18:37:297211 if (channel) {
7212 return channel->transport_name();
Steve Anton75737c02017-11-06 18:37:177213 }
Harald Alvestrand00cf34c2019-12-02 08:56:027214 if (data_channel_controller_.data_channel_transport()) {
Harald Alvestrand7a829a82020-02-12 06:38:217215 RTC_DCHECK(sctp_mid_s_);
7216 if (content_name == *sctp_mid_s_) {
Zhi Huange830e682018-03-30 17:48:357217 return *sctp_transport_name();
Steve Anton6fec8802017-12-04 18:37:297218 }
7219 }
7220 // Return an empty string if failed to retrieve the transport name.
7221 return "";
Steve Anton75737c02017-11-06 18:37:177222}
7223
Steve Anton6fec8802017-12-04 18:37:297224void PeerConnection::DestroyTransceiverChannel(
7225 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
7226 transceiver) {
7227 RTC_DCHECK(transceiver);
Steve Anton75737c02017-11-06 18:37:177228
Amit Hilbuchdd9390c2018-11-14 00:26:057229 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton6fec8802017-12-04 18:37:297230 if (channel) {
7231 transceiver->internal()->SetChannel(nullptr);
Amit Hilbuchdd9390c2018-11-14 00:26:057232 DestroyChannelInterface(channel);
Steve Anton75737c02017-11-06 18:37:177233 }
7234}
7235
Harald Alvestrand408cb4b2019-11-16 11:09:087236void PeerConnection::DestroyDataChannelTransport() {
Harald Alvestrand00cf34c2019-12-02 08:56:027237 if (data_channel_controller_.rtp_data_channel()) {
Harald Alvestrand05e4d082019-12-03 13:04:217238 data_channel_controller_.OnTransportChannelClosed();
Harald Alvestrand00cf34c2019-12-02 08:56:027239 DestroyChannelInterface(data_channel_controller_.rtp_data_channel());
7240 data_channel_controller_.set_rtp_data_channel(nullptr);
Steve Anton6fec8802017-12-04 18:37:297241 }
7242
7243 // Note: Cannot use rtc::Bind to create a functor to invoke because it will
7244 // grab a reference to this PeerConnection. If this is called from the
7245 // PeerConnection destructor, the RefCountedObject vtable will have already
7246 // been destroyed (since it is a subclass of PeerConnection) and using
7247 // rtc::Bind will cause "Pure virtual function called" error to appear.
7248
Harald Alvestrand7a829a82020-02-12 06:38:217249 if (sctp_mid_s_) {
Harald Alvestrand05e4d082019-12-03 13:04:217250 data_channel_controller_.OnTransportChannelClosed();
Bjorn Mellem175aa2e2018-11-08 19:23:227251 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
7252 RTC_DCHECK_RUN_ON(network_thread());
Bjorn A Mellemb689af42019-08-21 17:44:597253 TeardownDataChannelTransport_n();
Bjorn Mellem175aa2e2018-11-08 19:23:227254 });
Harald Alvestrand7a829a82020-02-12 06:38:217255 sctp_mid_s_.reset();
Bjorn Mellem175aa2e2018-11-08 19:23:227256 }
Steve Anton6fec8802017-12-04 18:37:297257}
7258
Amit Hilbuchdd9390c2018-11-14 00:26:057259void PeerConnection::DestroyChannelInterface(
7260 cricket::ChannelInterface* channel) {
Steve Anton6fec8802017-12-04 18:37:297261 RTC_DCHECK(channel);
Steve Anton6fec8802017-12-04 18:37:297262 switch (channel->media_type()) {
7263 case cricket::MEDIA_TYPE_AUDIO:
7264 channel_manager()->DestroyVoiceChannel(
7265 static_cast<cricket::VoiceChannel*>(channel));
7266 break;
7267 case cricket::MEDIA_TYPE_VIDEO:
7268 channel_manager()->DestroyVideoChannel(
7269 static_cast<cricket::VideoChannel*>(channel));
7270 break;
7271 case cricket::MEDIA_TYPE_DATA:
7272 channel_manager()->DestroyRtpDataChannel(
7273 static_cast<cricket::RtpDataChannel*>(channel));
7274 break;
7275 default:
7276 RTC_NOTREACHED() << "Unknown media type: " << channel->media_type();
7277 break;
7278 }
Zhi Huange830e682018-03-30 17:48:357279}
Steve Anton6fec8802017-12-04 18:37:297280
Taylor Brandstettercbaa2542018-04-16 23:42:147281bool PeerConnection::OnTransportChanged(
Zhi Huange830e682018-03-30 17:48:357282 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 23:42:147283 RtpTransportInternal* rtp_transport,
Harald Alvestrandc85328f2019-02-28 06:51:007284 rtc::scoped_refptr<DtlsTransport> dtls_transport,
Bjorn A Mellembc3eebc2019-09-23 21:53:547285 DataChannelTransportInterface* data_channel_transport) {
Karl Wibergac025892019-03-26 12:08:377286 RTC_DCHECK_RUN_ON(network_thread());
Taylor Brandstettercbaa2542018-04-16 23:42:147287 bool ret = true;
Zhi Huange830e682018-03-30 17:48:357288 auto base_channel = GetChannel(mid);
7289 if (base_channel) {
Taylor Brandstettercbaa2542018-04-16 23:42:147290 ret = base_channel->SetRtpTransport(rtp_transport);
Zhi Huange830e682018-03-30 17:48:357291 }
Harald Alvestrand7a829a82020-02-12 06:38:217292 if (mid == sctp_mid_n_) {
Harald Alvestrand00cf34c2019-12-02 08:56:027293 data_channel_controller_.OnTransportChanged(data_channel_transport);
Bjorn A Mellemb689af42019-08-21 17:44:597294 }
Taylor Brandstettercbaa2542018-04-16 23:42:147295 return ret;
Steve Anton75737c02017-11-06 18:37:177296}
7297
Guido Urdaneta1ff16c82019-05-20 17:31:537298void PeerConnection::OnSetStreams() {
7299 RTC_DCHECK_RUN_ON(signaling_thread());
7300 if (IsUnifiedPlan())
7301 UpdateNegotiationNeeded();
7302}
7303
Harald Alvestrand7a1c7f72018-08-01 08:50:167304PeerConnectionObserver* PeerConnection::Observer() const {
Harald Alvestrand05e4d082019-12-03 13:04:217305 RTC_DCHECK_RUN_ON(signaling_thread());
7306 RTC_DCHECK(observer_);
Harald Alvestrand7a1c7f72018-08-01 08:50:167307 return observer_;
7308}
7309
Benjamin Wright8c27cca2018-10-25 17:16:447310CryptoOptions PeerConnection::GetCryptoOptions() {
7311 // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions
7312 // after it has been removed.
7313 return configuration_.crypto_options.has_value()
7314 ? *configuration_.crypto_options
7315 : factory_->options().crypto_options;
7316}
7317
Harald Alvestrand89061872018-01-02 13:08:347318void PeerConnection::ClearStatsCache() {
Karl Wiberg6cab5c82019-03-26 08:57:017319 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand89061872018-01-02 13:08:347320 if (stats_collector_) {
7321 stats_collector_->ClearCachedStatsReport();
7322 }
7323}
7324
Harald Alvestrand7a1c7f72018-08-01 08:50:167325void PeerConnection::RequestUsagePatternReportForTesting() {
Steve Antonad182762018-09-05 20:22:407326 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN,
7327 nullptr);
Harald Alvestrand7a1c7f72018-08-01 08:50:167328}
7329
Guido Urdaneta70c2db12019-04-16 10:24:147330void PeerConnection::UpdateNegotiationNeeded() {
7331 RTC_DCHECK_RUN_ON(signaling_thread());
7332 if (!IsUnifiedPlan()) {
7333 Observer()->OnRenegotiationNeeded();
Henrik Boströme574a312020-08-25 08:20:117334 GenerateNegotiationNeededEvent();
Guido Urdaneta70c2db12019-04-16 10:24:147335 return;
7336 }
7337
Henrik Boströme574a312020-08-25 08:20:117338 // In the spec, a task is queued here to run the following steps - this is
7339 // meant to ensure we do not fire onnegotiationneeded prematurely if multiple
7340 // changes are being made at once. In order to support Chromium's
7341 // implementation where the JavaScript representation of the PeerConnection
7342 // lives on a separate thread though, the queuing of a task is instead
7343 // performed by the PeerConnectionObserver posting from the signaling thread
7344 // to the JavaScript main thread that negotiation is needed. And because the
7345 // Operations Chain lives on the WebRTC signaling thread,
7346 // ShouldFireNegotiationNeededEvent() must be called before firing the event
7347 // to ensure the Operations Chain is still empty and the event has not been
7348 // invalidated.
7349
Guido Urdaneta70c2db12019-04-16 10:24:147350 // If connection's [[IsClosed]] slot is true, abort these steps.
7351 if (IsClosed())
7352 return;
7353
7354 // If connection's signaling state is not "stable", abort these steps.
7355 if (signaling_state() != kStable)
7356 return;
7357
7358 // NOTE
7359 // The negotiation-needed flag will be updated once the state transitions to
7360 // "stable", as part of the steps for setting an RTCSessionDescription.
7361
7362 // If the result of checking if negotiation is needed is false, clear the
7363 // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot
7364 // to false, and abort these steps.
7365 bool is_negotiation_needed = CheckIfNegotiationIsNeeded();
7366 if (!is_negotiation_needed) {
7367 is_negotiation_needed_ = false;
Henrik Boströme574a312020-08-25 08:20:117368 // Invalidate any negotiation needed event that may previosuly have been
7369 // generated.
7370 ++negotiation_needed_event_id_;
Guido Urdaneta70c2db12019-04-16 10:24:147371 return;
7372 }
7373
7374 // If connection's [[NegotiationNeeded]] slot is already true, abort these
7375 // steps.
7376 if (is_negotiation_needed_)
7377 return;
7378
7379 // Set connection's [[NegotiationNeeded]] slot to true.
7380 is_negotiation_needed_ = true;
7381
7382 // Queue a task that runs the following steps:
7383 // If connection's [[IsClosed]] slot is true, abort these steps.
7384 // If connection's [[NegotiationNeeded]] slot is false, abort these steps.
7385 // Fire an event named negotiationneeded at connection.
7386 Observer()->OnRenegotiationNeeded();
Henrik Boströme574a312020-08-25 08:20:117387 // Fire the spec-compliant version; when ShouldFireNegotiationNeededEvent() is
7388 // used in the task queued by the observer, this event will only fire when the
7389 // chain is empty.
7390 GenerateNegotiationNeededEvent();
Guido Urdaneta70c2db12019-04-16 10:24:147391}
7392
7393bool PeerConnection::CheckIfNegotiationIsNeeded() {
7394 RTC_DCHECK_RUN_ON(signaling_thread());
7395 // 1. If any implementation-specific negotiation is required, as described at
7396 // the start of this section, return true.
7397
Harald Alvestrand6060df52020-08-11 07:54:027398 // 2. If connection.[[LocalIceCredentialsToReplace]] is not empty, return
7399 // true.
Henrik Boström79b69802019-07-18 09:16:567400 if (local_ice_credentials_to_replace_->HasIceCredentials()) {
7401 return true;
7402 }
7403
7404 // 3. Let description be connection.[[CurrentLocalDescription]].
Guido Urdaneta70c2db12019-04-16 10:24:147405 const SessionDescriptionInterface* description = current_local_description();
7406 if (!description)
7407 return true;
7408
Henrik Boström79b69802019-07-18 09:16:567409 // 4. If connection has created any RTCDataChannels, and no m= section in
Guido Urdaneta70c2db12019-04-16 10:24:147410 // description has been negotiated yet for data, return true.
Harald Alvestrand05e4d082019-12-03 13:04:217411 if (data_channel_controller_.HasSctpDataChannels()) {
Guido Urdaneta70c2db12019-04-16 10:24:147412 if (!cricket::GetFirstDataContent(description->description()->contents()))
7413 return true;
7414 }
7415
Henrik Boström79b69802019-07-18 09:16:567416 // 5. For each transceiver in connection's set of transceivers, perform the
Guido Urdaneta70c2db12019-04-16 10:24:147417 // following checks:
7418 for (const auto& transceiver : transceivers_) {
7419 const ContentInfo* current_local_msection =
7420 FindTransceiverMSection(transceiver.get(), description);
7421
7422 const ContentInfo* current_remote_msection = FindTransceiverMSection(
7423 transceiver.get(), current_remote_description());
7424
Harald Alvestrand6060df52020-08-11 07:54:027425 // 5.4 If transceiver is stopped and is associated with an m= section,
Guido Urdaneta70c2db12019-04-16 10:24:147426 // but the associated m= section is not yet rejected in
7427 // connection.[[CurrentLocalDescription]] or
7428 // connection.[[CurrentRemoteDescription]], return true.
7429 if (transceiver->stopped()) {
Harald Alvestrand6060df52020-08-11 07:54:027430 RTC_DCHECK(transceiver->stopping());
Guido Urdaneta70c2db12019-04-16 10:24:147431 if (current_local_msection && !current_local_msection->rejected &&
7432 ((current_remote_msection && !current_remote_msection->rejected) ||
7433 !current_remote_msection)) {
7434 return true;
7435 }
7436 continue;
7437 }
7438
Harald Alvestrand6060df52020-08-11 07:54:027439 // 5.1 If transceiver.[[Stopping]] is true and transceiver.[[Stopped]] is
7440 // false, return true.
7441 if (transceiver->stopping() && !transceiver->stopped())
7442 return true;
7443
7444 // 5.2 If transceiver isn't stopped and isn't yet associated with an m=
Guido Urdaneta70c2db12019-04-16 10:24:147445 // section in description, return true.
7446 if (!current_local_msection)
7447 return true;
7448
7449 const MediaContentDescription* current_local_media_description =
7450 current_local_msection->media_description();
Harald Alvestrand6060df52020-08-11 07:54:027451 // 5.3 If transceiver isn't stopped and is associated with an m= section
Guido Urdaneta70c2db12019-04-16 10:24:147452 // in description then perform the following checks:
7453
Harald Alvestrand6060df52020-08-11 07:54:027454 // 5.3.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the
Guido Urdaneta70c2db12019-04-16 10:24:147455 // associated m= section in description either doesn't contain a single
7456 // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this
7457 // m= section, or the MSID values themselves, differ from what is in
7458 // transceiver.sender.[[AssociatedMediaStreamIds]], return true.
7459 if (RtpTransceiverDirectionHasSend(transceiver->direction())) {
7460 if (current_local_media_description->streams().size() == 0)
7461 return true;
7462
7463 std::vector<std::string> msection_msids;
7464 for (const auto& stream : current_local_media_description->streams()) {
7465 for (const std::string& msid : stream.stream_ids())
7466 msection_msids.push_back(msid);
7467 }
7468
7469 std::vector<std::string> transceiver_msids =
7470 transceiver->sender()->stream_ids();
7471 if (msection_msids.size() != transceiver_msids.size())
7472 return true;
7473
7474 absl::c_sort(transceiver_msids);
7475 absl::c_sort(msection_msids);
7476 if (transceiver_msids != msection_msids)
7477 return true;
7478 }
7479
Harald Alvestrand6060df52020-08-11 07:54:027480 // 5.3.2 If description is of type "offer", and the direction of the
Guido Urdaneta70c2db12019-04-16 10:24:147481 // associated m= section in neither connection.[[CurrentLocalDescription]]
7482 // nor connection.[[CurrentRemoteDescription]] matches
7483 // transceiver.[[Direction]], return true.
7484 if (description->GetType() == SdpType::kOffer) {
7485 if (!current_remote_description())
7486 return true;
7487
7488 if (!current_remote_msection)
7489 return true;
7490
7491 RtpTransceiverDirection current_local_direction =
7492 current_local_media_description->direction();
7493 RtpTransceiverDirection current_remote_direction =
7494 current_remote_msection->media_description()->direction();
7495 if (transceiver->direction() != current_local_direction &&
7496 transceiver->direction() !=
7497 RtpTransceiverDirectionReversed(current_remote_direction)) {
7498 return true;
7499 }
7500 }
7501
Harald Alvestrand6060df52020-08-11 07:54:027502 // 5.3.3 If description is of type "answer", and the direction of the
Guido Urdaneta70c2db12019-04-16 10:24:147503 // associated m= section in the description does not match
7504 // transceiver.[[Direction]] intersected with the offered direction (as
7505 // described in [JSEP] (section 5.3.1.)), return true.
7506 if (description->GetType() == SdpType::kAnswer) {
7507 if (!remote_description())
7508 return true;
7509
7510 const ContentInfo* offered_remote_msection =
7511 FindTransceiverMSection(transceiver.get(), remote_description());
7512
7513 RtpTransceiverDirection offered_direction =
7514 offered_remote_msection
7515 ? offered_remote_msection->media_description()->direction()
7516 : RtpTransceiverDirection::kInactive;
7517
7518 if (current_local_media_description->direction() !=
7519 (RtpTransceiverDirectionIntersection(
7520 transceiver->direction(),
7521 RtpTransceiverDirectionReversed(offered_direction)))) {
7522 return true;
7523 }
7524 }
7525 }
7526
7527 // If all the preceding checks were performed and true was not returned,
7528 // nothing remains to be negotiated; return false.
7529 return false;
7530}
7531
Henrik Boströme574a312020-08-25 08:20:117532void PeerConnection::OnOperationsChainEmpty() {
7533 RTC_DCHECK_RUN_ON(signaling_thread());
7534 if (IsClosed() || !update_negotiation_needed_on_empty_chain_)
7535 return;
7536 update_negotiation_needed_on_empty_chain_ = false;
7537 // Firing when chain is empty is only supported in Unified Plan to avoid Plan
7538 // B regressions. (In Plan B, onnegotiationneeded is already broken anyway, so
7539 // firing it even more might just be confusing.)
7540 if (IsUnifiedPlan()) {
7541 UpdateNegotiationNeeded();
7542 }
7543}
7544
7545bool PeerConnection::ShouldFireNegotiationNeededEvent(uint32_t event_id) {
7546 RTC_DCHECK_RUN_ON(signaling_thread());
7547 // Plan B? Always fire to conform with useless legacy behavior.
7548 if (!IsUnifiedPlan()) {
7549 return true;
7550 }
7551 // The event ID has been invalidated. Either negotiation is no longer needed
7552 // or a newer negotiation needed event has been generated.
7553 if (event_id != negotiation_needed_event_id_) {
7554 return false;
7555 }
7556 // The chain is no longer empty, update negotiation needed when it becomes
7557 // empty. This should generate a newer negotiation needed event, making this
7558 // one obsolete.
7559 if (!operations_chain_->IsEmpty()) {
7560 // Since we just suppressed an event that would have been fired, if
7561 // negotiation is still needed by the time the chain becomes empty again, we
7562 // must make sure to generate another event if negotiation is needed then.
7563 // This happens when |is_negotiation_needed_| goes from false to true, so we
7564 // set it to false until UpdateNegotiationNeeded() is called.
7565 is_negotiation_needed_ = false;
7566 update_negotiation_needed_on_empty_chain_ = true;
7567 return false;
7568 }
7569 // We must not fire if the signaling state is no longer "stable". If
7570 // negotiation is still needed when we return to "stable", a new negotiation
7571 // needed event will be generated, so this one can safely be suppressed.
7572 if (signaling_state_ != PeerConnectionInterface::kStable) {
7573 return false;
7574 }
7575 // All checks have passed - please fire "negotiationneeded" now!
7576 return true;
7577}
7578
7579void PeerConnection::GenerateNegotiationNeededEvent() {
7580 RTC_DCHECK_RUN_ON(signaling_thread());
7581 ++negotiation_needed_event_id_;
7582 Observer()->OnNegotiationNeededEvent(negotiation_needed_event_id_);
7583}
7584
Eldar Relloead0ec92019-10-21 20:01:317585RTCError PeerConnection::Rollback(SdpType sdp_type) {
Eldar Rello5ab79e62019-10-09 15:29:447586 auto state = signaling_state();
7587 if (state != PeerConnectionInterface::kHaveLocalOffer &&
7588 state != PeerConnectionInterface::kHaveRemoteOffer) {
7589 return RTCError(RTCErrorType::INVALID_STATE,
7590 "Called in wrong signalingState: " +
7591 GetSignalingStateString(signaling_state()));
7592 }
7593 RTC_DCHECK_RUN_ON(signaling_thread());
7594 RTC_DCHECK(IsUnifiedPlan());
Eldar Rello353a7182019-11-25 16:49:447595 std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_added_streams;
7596 std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_removed_streams;
7597 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> removed_receivers;
Eldar Rello5ab79e62019-10-09 15:29:447598
7599 for (auto&& transceivers_stable_state_pair :
7600 transceiver_stable_states_by_transceivers_) {
7601 auto transceiver = transceivers_stable_state_pair.first;
7602 auto state = transceivers_stable_state_pair.second;
Eldar Rello353a7182019-11-25 16:49:447603
7604 if (state.remote_stream_ids()) {
7605 std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
7606 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
7607 SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(),
7608 state.remote_stream_ids().value(),
7609 &added_streams, &removed_streams);
7610 all_added_streams.insert(all_added_streams.end(), added_streams.begin(),
7611 added_streams.end());
7612 all_removed_streams.insert(all_removed_streams.end(),
7613 removed_streams.begin(),
7614 removed_streams.end());
7615 if (!state.has_m_section() && !state.newly_created()) {
7616 continue;
7617 }
7618 }
7619
Eldar Rello5ab79e62019-10-09 15:29:447620 RTC_DCHECK(transceiver->internal()->mid().has_value());
Eldar Rello5ab79e62019-10-09 15:29:447621 DestroyTransceiverChannel(transceiver);
7622
Eldar Relloead0ec92019-10-21 20:01:317623 if (signaling_state() == PeerConnectionInterface::kHaveRemoteOffer &&
7624 transceiver->receiver()) {
Eldar Rello353a7182019-11-25 16:49:447625 removed_receivers.push_back(transceiver->receiver());
Eldar Relloead0ec92019-10-21 20:01:317626 }
Eldar Rello5ab79e62019-10-09 15:29:447627 if (state.newly_created()) {
Eldar Rello353a7182019-11-25 16:49:447628 if (transceiver->internal()->reused_for_addtrack()) {
Eldar Rello5ab79e62019-10-09 15:29:447629 transceiver->internal()->set_created_by_addtrack(true);
7630 } else {
7631 int remaining_transceiver_count = 0;
7632 for (auto&& t : transceivers_) {
7633 if (t != transceiver) {
7634 transceivers_[remaining_transceiver_count++] = t;
7635 }
7636 }
7637 transceivers_.resize(remaining_transceiver_count);
7638 }
7639 }
7640 transceiver->internal()->sender_internal()->set_transport(nullptr);
7641 transceiver->internal()->receiver_internal()->set_transport(nullptr);
Eldar Rello5ab79e62019-10-09 15:29:447642 transceiver->internal()->set_mid(state.mid());
7643 transceiver->internal()->set_mline_index(state.mline_index());
7644 }
Eldar Rellod85ea752020-02-19 18:41:077645 transport_controller_->RollbackTransports();
Eldar Rellod9ebe012020-03-18 18:41:457646 if (have_pending_rtp_data_channel_) {
7647 DestroyDataChannelTransport();
7648 have_pending_rtp_data_channel_ = false;
7649 }
Eldar Rello5ab79e62019-10-09 15:29:447650 transceiver_stable_states_by_transceivers_.clear();
7651 pending_local_description_.reset();
7652 pending_remote_description_.reset();
7653 ChangeSignalingState(PeerConnectionInterface::kStable);
Eldar Relloead0ec92019-10-21 20:01:317654
Eldar Rello353a7182019-11-25 16:49:447655 // Once all processing has finished, fire off callbacks.
7656 for (const auto& receiver : removed_receivers) {
7657 Observer()->OnRemoveTrack(receiver);
7658 }
7659 for (const auto& stream : all_added_streams) {
7660 Observer()->OnAddStream(stream);
7661 }
7662 for (const auto& stream : all_removed_streams) {
7663 Observer()->OnRemoveStream(stream);
7664 }
7665
Eldar Relloead0ec92019-10-21 20:01:317666 // The assumption is that in case of implicit rollback UpdateNegotiationNeeded
7667 // gets called in SetRemoteDescription.
7668 if (sdp_type == SdpType::kRollback) {
7669 UpdateNegotiationNeeded();
7670 if (is_negotiation_needed_) {
Henrik Boströme574a312020-08-25 08:20:117671 // Legacy version.
Eldar Relloead0ec92019-10-21 20:01:317672 Observer()->OnRenegotiationNeeded();
Henrik Boströme574a312020-08-25 08:20:117673 // Spec-compliant version; the event may get invalidated before firing.
7674 GenerateNegotiationNeededEvent();
Eldar Relloead0ec92019-10-21 20:01:317675 }
7676 }
Eldar Rello5ab79e62019-10-09 15:29:447677 return RTCError::OK();
7678}
7679
henrike@webrtc.org28e20752013-07-10 00:45:367680} // namespace webrtc