1 /*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "pc/peer_connection.h"
12
13 #include <algorithm>
14 #include <limits>
15 #include <memory>
16 #include <queue>
17 #include <set>
18 #include <utility>
19 #include <vector>
20
21 #include "absl/algorithm/container.h"
22 #include "absl/strings/match.h"
23 #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"
27 #include "api/rtc_error.h"
28 #include "api/rtc_event_log/rtc_event_log.h"
29 #include "api/rtc_event_log_output_file.h"
30 #include "api/rtp_parameters.h"
31 #include "api/uma_metrics.h"
32 #include "api/video/builtin_video_bitrate_allocator_factory.h"
33 #include "call/call.h"
34 #include "logging/rtc_event_log/ice_logger.h"
35 #include "media/base/rid_description.h"
36 #include "media/sctp/sctp_transport.h"
37 #include "pc/audio_rtp_receiver.h"
38 #include "pc/audio_track.h"
39 #include "pc/channel.h"
40 #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"
48 #include "pc/sctp_transport.h"
49 #include "pc/sctp_utils.h"
50 #include "pc/sdp_utils.h"
51 #include "pc/stream_collection.h"
52 #include "pc/video_rtp_receiver.h"
53 #include "pc/video_track.h"
54 #include "rtc_base/bind.h"
55 #include "rtc_base/checks.h"
56 #include "rtc_base/logging.h"
57 #include "rtc_base/string_encode.h"
58 #include "rtc_base/strings/string_builder.h"
59 #include "rtc_base/trace_event.h"
60 #include "system_wrappers/include/clock.h"
61 #include "system_wrappers/include/field_trial.h"
62 #include "system_wrappers/include/metrics.h"
63
64 using cricket::ContentInfo;
65 using cricket::ContentInfos;
66 using cricket::MediaContentDescription;
67 using cricket::MediaProtocolType;
68 using cricket::RidDescription;
69 using cricket::RidDirection;
70 using cricket::SessionDescription;
71 using cricket::SimulcastDescription;
72 using cricket::SimulcastLayer;
73 using cricket::SimulcastLayerList;
74 using cricket::StreamParams;
75 using cricket::TransportInfo;
76
77 using cricket::LOCAL_PORT_TYPE;
78 using cricket::PRFLX_PORT_TYPE;
79 using cricket::RELAY_PORT_TYPE;
80 using cricket::STUN_PORT_TYPE;
81
82 namespace webrtc {
83
84 // Error messages
85 const char kBundleWithoutRtcpMux[] =
86 "rtcp-mux must be enabled when BUNDLE "
87 "is enabled.";
88 const char kInvalidCandidates[] = "Description contains invalid candidates.";
89 const char kInvalidSdp[] = "Invalid session description.";
90 const char kMlineMismatchInAnswer[] =
91 "The order of m-lines in answer doesn't match order in offer. Rejecting "
92 "answer.";
93 const char kMlineMismatchInSubsequentOffer[] =
94 "The order of m-lines in subsequent offer doesn't match order from "
95 "previous offer/answer.";
96 const char kSdpWithoutDtlsFingerprint[] =
97 "Called with SDP without DTLS fingerprint.";
98 const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto.";
99 const char kSdpWithoutIceUfragPwd[] =
100 "Called with SDP without ice-ufrag and ice-pwd.";
101 const char kSessionError[] = "Session error code: ";
102 const char kSessionErrorDesc[] = "Session error description: ";
103 const char kDtlsSrtpSetupFailureRtp[] =
104 "Couldn't set up DTLS-SRTP on RTP channel.";
105 const char kDtlsSrtpSetupFailureRtcp[] =
106 "Couldn't set up DTLS-SRTP on RTCP channel.";
107
108 namespace {
109
110 // UMA metric names.
111 const char kSimulcastVersionApplyLocalDescription[] =
112 "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription";
113 const char kSimulcastVersionApplyRemoteDescription[] =
114 "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription";
115 const char kSimulcastNumberOfEncodings[] =
116 "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings";
117 const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled";
118
119 static const char kDefaultStreamId[] = "default";
120 static const char kDefaultAudioSenderId[] = "defaulta0";
121 static const char kDefaultVideoSenderId[] = "defaultv0";
122
123 // The length of RTCP CNAMEs.
124 static const int kRtcpCnameLength = 16;
125
126 enum {
127 MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0,
128 MSG_SET_SESSIONDESCRIPTION_FAILED,
129 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
130 MSG_GETSTATS,
131 MSG_REPORT_USAGE_PATTERN,
132 };
133
134 static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000;
135
136 struct SetSessionDescriptionMsg : public rtc::MessageData {
SetSessionDescriptionMsgwebrtc::__anon1df2b4840111::SetSessionDescriptionMsg137 explicit SetSessionDescriptionMsg(
138 webrtc::SetSessionDescriptionObserver* observer)
139 : observer(observer) {}
140
141 rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer;
142 RTCError error;
143 };
144
145 struct CreateSessionDescriptionMsg : public rtc::MessageData {
CreateSessionDescriptionMsgwebrtc::__anon1df2b4840111::CreateSessionDescriptionMsg146 explicit CreateSessionDescriptionMsg(
147 webrtc::CreateSessionDescriptionObserver* observer)
148 : observer(observer) {}
149
150 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
151 RTCError error;
152 };
153
154 struct GetStatsMsg : public rtc::MessageData {
GetStatsMsgwebrtc::__anon1df2b4840111::GetStatsMsg155 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
162 // Check if we can send |new_stream| on a PeerConnection.
CanAddLocalMediaStream(webrtc::StreamCollectionInterface * current_streams,webrtc::MediaStreamInterface * new_stream)163 bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams,
164 webrtc::MediaStreamInterface* new_stream) {
165 if (!new_stream || !current_streams) {
166 return false;
167 }
168 if (current_streams->find(new_stream->id()) != nullptr) {
169 RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id()
170 << " is already added.";
171 return false;
172 }
173 return true;
174 }
175
176 // 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
GetActiveStreams(const cricket::MediaContentDescription * desc)179 std::vector<cricket::StreamParams> GetActiveStreams(
180 const cricket::MediaContentDescription* desc) {
181 return RtpTransceiverDirectionHasSend(desc->direction())
182 ? desc->streams()
183 : std::vector<cricket::StreamParams>();
184 }
185
IsValidOfferToReceiveMedia(int value)186 bool IsValidOfferToReceiveMedia(int value) {
187 typedef PeerConnectionInterface::RTCOfferAnswerOptions Options;
188 return (value >= Options::kUndefined) &&
189 (value <= Options::kMaxOfferToReceiveMedia);
190 }
191
192 // Add options to |[audio/video]_media_description_options| from |senders|.
AddPlanBRtpSenderOptions(const std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> & senders,cricket::MediaDescriptionOptions * audio_media_description_options,cricket::MediaDescriptionOptions * video_media_description_options,int num_sim_layers)193 void AddPlanBRtpSenderOptions(
194 const std::vector<rtc::scoped_refptr<
195 RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders,
196 cricket::MediaDescriptionOptions* audio_media_description_options,
197 cricket::MediaDescriptionOptions* video_media_description_options,
198 int num_sim_layers) {
199 for (const auto& sender : senders) {
200 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
201 if (audio_media_description_options) {
202 audio_media_description_options->AddAudioSender(
203 sender->id(), sender->internal()->stream_ids());
204 }
205 } else {
206 RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO);
207 if (video_media_description_options) {
208 video_media_description_options->AddVideoSender(
209 sender->id(), sender->internal()->stream_ids(), {},
210 SimulcastLayerList(), num_sim_layers);
211 }
212 }
213 }
214 }
215
216 // Add options to |session_options| from |rtp_data_channels|.
AddRtpDataChannelOptions(const std::map<std::string,rtc::scoped_refptr<RtpDataChannel>> & rtp_data_channels,cricket::MediaDescriptionOptions * data_media_description_options)217 void AddRtpDataChannelOptions(
218 const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>&
219 rtp_data_channels,
220 cricket::MediaDescriptionOptions* data_media_description_options) {
221 if (!data_media_description_options) {
222 return;
223 }
224 // Check for data channels.
225 for (const auto& kv : rtp_data_channels) {
226 const RtpDataChannel* channel = kv.second;
227 if (channel->state() == RtpDataChannel::kConnecting ||
228 channel->state() == RtpDataChannel::kOpen) {
229 // 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());
233 }
234 }
235 }
236
ConvertIceTransportTypeToCandidateFilter(PeerConnectionInterface::IceTransportsType type)237 uint32_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:
249 RTC_NOTREACHED();
250 }
251 return cricket::CF_NONE;
252 }
253
GetSignalingStateString(PeerConnectionInterface::SignalingState state)254 std::string GetSignalingStateString(
255 PeerConnectionInterface::SignalingState state) {
256 switch (state) {
257 case PeerConnectionInterface::kStable:
258 return "kStable";
259 case PeerConnectionInterface::kHaveLocalOffer:
260 return "kHaveLocalOffer";
261 case PeerConnectionInterface::kHaveLocalPrAnswer:
262 return "kHavePrAnswer";
263 case PeerConnectionInterface::kHaveRemoteOffer:
264 return "kHaveRemoteOffer";
265 case PeerConnectionInterface::kHaveRemotePrAnswer:
266 return "kHaveRemotePrAnswer";
267 case PeerConnectionInterface::kClosed:
268 return "kClosed";
269 }
270 RTC_NOTREACHED();
271 return "";
272 }
273
GetIceCandidatePairCounter(const cricket::Candidate & local,const cricket::Candidate & remote)274 IceCandidatePairType GetIceCandidatePairCounter(
275 const cricket::Candidate& local,
276 const cricket::Candidate& remote) {
277 const auto& l = local.type();
278 const auto& r = remote.type();
279 const auto& host = LOCAL_PORT_TYPE;
280 const auto& srflx = STUN_PORT_TYPE;
281 const auto& relay = RELAY_PORT_TYPE;
282 const auto& prflx = PRFLX_PORT_TYPE;
283 if (l == host && r == host) {
284 bool local_hostname =
285 !local.address().hostname().empty() && local.address().IsUnresolvedIP();
286 bool remote_hostname = !remote.address().hostname().empty() &&
287 remote.address().IsUnresolvedIP();
288 bool local_private = IPIsPrivate(local.address().ipaddr());
289 bool remote_private = IPIsPrivate(remote.address().ipaddr());
290 if (local_hostname) {
291 if (remote_hostname) {
292 return kIceCandidatePairHostNameHostName;
293 } else if (remote_private) {
294 return kIceCandidatePairHostNameHostPrivate;
295 } else {
296 return kIceCandidatePairHostNameHostPublic;
297 }
298 } else if (local_private) {
299 if (remote_hostname) {
300 return kIceCandidatePairHostPrivateHostName;
301 } else if (remote_private) {
302 return kIceCandidatePairHostPrivateHostPrivate;
303 } else {
304 return kIceCandidatePairHostPrivateHostPublic;
305 }
306 } else {
307 if (remote_hostname) {
308 return kIceCandidatePairHostPublicHostName;
309 } else if (remote_private) {
310 return kIceCandidatePairHostPublicHostPrivate;
311 } else {
312 return kIceCandidatePairHostPublicHostPublic;
313 }
314 }
315 }
316 if (l == host && r == srflx)
317 return kIceCandidatePairHostSrflx;
318 if (l == host && r == relay)
319 return kIceCandidatePairHostRelay;
320 if (l == host && r == prflx)
321 return kIceCandidatePairHostPrflx;
322 if (l == srflx && r == host)
323 return kIceCandidatePairSrflxHost;
324 if (l == srflx && r == srflx)
325 return kIceCandidatePairSrflxSrflx;
326 if (l == srflx && r == relay)
327 return kIceCandidatePairSrflxRelay;
328 if (l == srflx && r == prflx)
329 return kIceCandidatePairSrflxPrflx;
330 if (l == relay && r == host)
331 return kIceCandidatePairRelayHost;
332 if (l == relay && r == srflx)
333 return kIceCandidatePairRelaySrflx;
334 if (l == relay && r == relay)
335 return kIceCandidatePairRelayRelay;
336 if (l == relay && r == prflx)
337 return kIceCandidatePairRelayPrflx;
338 if (l == prflx && r == host)
339 return kIceCandidatePairPrflxHost;
340 if (l == prflx && r == srflx)
341 return kIceCandidatePairPrflxSrflx;
342 if (l == prflx && r == relay)
343 return kIceCandidatePairPrflxRelay;
344 return kIceCandidatePairMax;
345 }
346
347 // Logic to decide if an m= section can be recycled. This means that the new
348 // m= section is not rejected, but the old local or remote m= section is
349 // rejected. |old_content_one| and |old_content_two| refer to the m= section
350 // of the old remote and old local descriptions in no particular order.
351 // We need to check both the old local and remote because either
352 // could be the most current from the latest negotation.
IsMediaSectionBeingRecycled(SdpType type,const ContentInfo & content,const ContentInfo * old_content_one,const ContentInfo * old_content_two)353 bool IsMediaSectionBeingRecycled(SdpType type,
354 const ContentInfo& content,
355 const ContentInfo* old_content_one,
356 const ContentInfo* old_content_two) {
357 return type == SdpType::kOffer && !content.rejected &&
358 ((old_content_one && old_content_one->rejected) ||
359 (old_content_two && old_content_two->rejected));
360 }
361
362 // Verify that the order of media sections in |new_desc| matches
363 // |current_desc|. The number of m= sections in |new_desc| should be no
364 // less than |current_desc|. In the case of checking an answer's
365 // |new_desc|, the |current_desc| is the last offer that was set as the
366 // local or remote. In the case of checking an offer's |new_desc| we
367 // check against the local and remote descriptions stored from the last
368 // negotiation, because either of these could be the most up to date for
369 // possible rejected m sections. These are the |current_desc| and
370 // |secondary_current_desc|.
MediaSectionsInSameOrder(const SessionDescription & current_desc,const SessionDescription * secondary_current_desc,const SessionDescription & new_desc,const SdpType type)371 bool MediaSectionsInSameOrder(const SessionDescription& current_desc,
372 const SessionDescription* secondary_current_desc,
373 const SessionDescription& new_desc,
374 const SdpType type) {
375 if (current_desc.contents().size() > new_desc.contents().size()) {
376 return false;
377 }
378
379 for (size_t i = 0; i < current_desc.contents().size(); ++i) {
380 const cricket::ContentInfo* secondary_content_info = nullptr;
381 if (secondary_current_desc &&
382 i < secondary_current_desc->contents().size()) {
383 secondary_content_info = &secondary_current_desc->contents()[i];
384 }
385 if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i],
386 ¤t_desc.contents()[i],
387 secondary_content_info)) {
388 // For new offer descriptions, if the media section can be recycled, it's
389 // valid for the MID and media type to change.
390 continue;
391 }
392 if (new_desc.contents()[i].name != current_desc.contents()[i].name) {
393 return false;
394 }
395 const MediaContentDescription* new_desc_mdesc =
396 new_desc.contents()[i].media_description();
397 const MediaContentDescription* current_desc_mdesc =
398 current_desc.contents()[i].media_description();
399 if (new_desc_mdesc->type() != current_desc_mdesc->type()) {
400 return false;
401 }
402 }
403 return true;
404 }
405
MediaSectionsHaveSameCount(const SessionDescription & desc1,const SessionDescription & desc2)406 bool MediaSectionsHaveSameCount(const SessionDescription& desc1,
407 const SessionDescription& desc2) {
408 return desc1.contents().size() == desc2.contents().size();
409 }
410
NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type,cricket::MediaType media_type)411 void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type,
412 cricket::MediaType media_type) {
413 // Array of structs needed to map {KeyExchangeProtocolType,
414 // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in
415 // order to avoid -Wglobal-constructors and -Wexit-time-destructors.
416 static constexpr struct {
417 KeyExchangeProtocolType protocol_type;
418 cricket::MediaType media_type;
419 KeyExchangeProtocolMedia protocol_media;
420 } kEnumCounterKeyProtocolMediaMap[] = {
421 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO,
422 kEnumCounterKeyProtocolMediaTypeDtlsAudio},
423 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO,
424 kEnumCounterKeyProtocolMediaTypeDtlsVideo},
425 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA,
426 kEnumCounterKeyProtocolMediaTypeDtlsData},
427 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO,
428 kEnumCounterKeyProtocolMediaTypeSdesAudio},
429 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO,
430 kEnumCounterKeyProtocolMediaTypeSdesVideo},
431 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA,
432 kEnumCounterKeyProtocolMediaTypeSdesData},
433 };
434
435 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type,
436 kEnumCounterKeyProtocolMax);
437
438 for (const auto& i : kEnumCounterKeyProtocolMediaMap) {
439 if (i.protocol_type == protocol_type && i.media_type == media_type) {
440 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia",
441 i.protocol_media,
442 kEnumCounterKeyProtocolMediaTypeMax);
443 }
444 }
445 }
446
NoteAddIceCandidateResult(int result)447 void NoteAddIceCandidateResult(int result) {
448 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result,
449 kAddIceCandidateMax);
450 }
451
452 // Checks that each non-rejected content has SDES crypto keys or a DTLS
453 // fingerprint, unless it's in a BUNDLE group, in which case only the
454 // BUNDLE-tag section (first media section/description in the BUNDLE group)
455 // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
456 // to SDES keys, will be caught in JsepTransport negotiation, and backstopped
457 // by Channel's |srtp_required| check.
VerifyCrypto(const SessionDescription * desc,bool dtls_enabled)458 RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) {
459 const cricket::ContentGroup* bundle =
460 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
461 for (const cricket::ContentInfo& content_info : desc->contents()) {
462 if (content_info.rejected) {
463 continue;
464 }
465 // Note what media is used with each crypto protocol, for all sections.
466 NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls
467 : webrtc::kEnumCounterKeyProtocolSdes,
468 content_info.media_description()->type());
469 const std::string& mid = content_info.name;
470 if (bundle && bundle->HasContentName(mid) &&
471 mid != *(bundle->FirstContentName())) {
472 // This isn't the first media section in the BUNDLE group, so it's not
473 // required to have crypto attributes, since only the crypto attributes
474 // from the first section actually get used.
475 continue;
476 }
477
478 // If the content isn't rejected or bundled into another m= section, crypto
479 // must be present.
480 const MediaContentDescription* media = content_info.media_description();
481 const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
482 if (!media || !tinfo) {
483 // Something is not right.
484 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
485 }
486 if (dtls_enabled) {
487 if (!tinfo->description.identity_fingerprint) {
488 RTC_LOG(LS_WARNING)
489 << "Session description must have DTLS fingerprint if "
490 "DTLS enabled.";
491 return RTCError(RTCErrorType::INVALID_PARAMETER,
492 kSdpWithoutDtlsFingerprint);
493 }
494 } else {
495 if (media->cryptos().empty()) {
496 RTC_LOG(LS_WARNING)
497 << "Session description must have SDES when DTLS disabled.";
498 return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto);
499 }
500 }
501 }
502 return RTCError::OK();
503 }
504
505 // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
506 // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
507 // media section/description in the BUNDLE group) needs a ufrag and pwd.
VerifyIceUfragPwdPresent(const SessionDescription * desc)508 bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
509 const cricket::ContentGroup* bundle =
510 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
511 for (const cricket::ContentInfo& content_info : desc->contents()) {
512 if (content_info.rejected) {
513 continue;
514 }
515 const std::string& mid = content_info.name;
516 if (bundle && bundle->HasContentName(mid) &&
517 mid != *(bundle->FirstContentName())) {
518 // This isn't the first media section in the BUNDLE group, so it's not
519 // required to have ufrag/password, since only the ufrag/password from
520 // the first section actually get used.
521 continue;
522 }
523
524 // If the content isn't rejected or bundled into another m= section,
525 // ice-ufrag and ice-pwd must be present.
526 const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
527 if (!tinfo) {
528 // Something is not right.
529 RTC_LOG(LS_ERROR) << kInvalidSdp;
530 return false;
531 }
532 if (tinfo->description.ice_ufrag.empty() ||
533 tinfo->description.ice_pwd.empty()) {
534 RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
535 return false;
536 }
537 }
538 return true;
539 }
540
541 // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
CheckForRemoteIceRestart(const SessionDescriptionInterface * old_desc,const SessionDescriptionInterface * new_desc,const std::string & content_name)542 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
543 const SessionDescriptionInterface* new_desc,
544 const std::string& content_name) {
545 if (!old_desc) {
546 return false;
547 }
548 const SessionDescription* new_sd = new_desc->description();
549 const SessionDescription* old_sd = old_desc->description();
550 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
551 if (!cinfo || cinfo->rejected) {
552 return false;
553 }
554 // If the content isn't rejected, check if ufrag and password has changed.
555 const cricket::TransportDescription* new_transport_desc =
556 new_sd->GetTransportDescriptionByName(content_name);
557 const cricket::TransportDescription* old_transport_desc =
558 old_sd->GetTransportDescriptionByName(content_name);
559 if (!new_transport_desc || !old_transport_desc) {
560 // No transport description exists. This is not an ICE restart.
561 return false;
562 }
563 if (cricket::IceCredentialsChanged(
564 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
565 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
566 RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
567 << ".";
568 return true;
569 }
570 return false;
571 }
572
573 // Generates a string error message for SetLocalDescription/SetRemoteDescription
574 // from an RTCError.
GetSetDescriptionErrorMessage(cricket::ContentSource source,SdpType type,const RTCError & error)575 std::string GetSetDescriptionErrorMessage(cricket::ContentSource source,
576 SdpType type,
577 const RTCError& error) {
578 rtc::StringBuilder oss;
579 oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote")
580 << " " << SdpTypeToString(type) << " sdp: " << error.message();
581 return oss.Release();
582 }
583
GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids)584 std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) {
585 std::string output = "streams=[";
586 const char* separator = "";
587 for (const auto& stream_id : stream_ids) {
588 output.append(separator).append(stream_id);
589 separator = ", ";
590 }
591 output.append("]");
592 return output;
593 }
594
RTCConfigurationToIceConfigOptionalInt(int rtc_configuration_parameter)595 absl::optional<int> RTCConfigurationToIceConfigOptionalInt(
596 int rtc_configuration_parameter) {
597 if (rtc_configuration_parameter ==
598 webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) {
599 return absl::nullopt;
600 }
601 return rtc_configuration_parameter;
602 }
603
ReportSimulcastApiVersion(const char * name,const SessionDescription & session)604 void ReportSimulcastApiVersion(const char* name,
605 const SessionDescription& session) {
606 bool has_legacy = false;
607 bool has_spec_compliant = false;
608 for (const ContentInfo& content : session.contents()) {
609 if (!content.media_description()) {
610 continue;
611 }
612 has_spec_compliant |= content.media_description()->HasSimulcast();
613 for (const StreamParams& sp : content.media_description()->streams()) {
614 has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics);
615 }
616 }
617
618 if (has_legacy) {
619 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy,
620 kSimulcastApiVersionMax);
621 }
622 if (has_spec_compliant) {
623 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant,
624 kSimulcastApiVersionMax);
625 }
626 if (!has_legacy && !has_spec_compliant) {
627 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone,
628 kSimulcastApiVersionMax);
629 }
630 }
631
FindTransceiverMSection(RtpTransceiverProxyWithInternal<RtpTransceiver> * transceiver,const SessionDescriptionInterface * session_description)632 const ContentInfo* FindTransceiverMSection(
633 RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver,
634 const SessionDescriptionInterface* session_description) {
635 return transceiver->mid()
636 ? session_description->description()->GetContentByName(
637 *transceiver->mid())
638 : nullptr;
639 }
640
641 // Wraps a CreateSessionDescriptionObserver and an OperationsChain operation
642 // complete callback. When the observer is invoked, the wrapped observer is
643 // invoked followed by invoking the completion callback.
644 class CreateSessionDescriptionObserverOperationWrapper
645 : public CreateSessionDescriptionObserver {
646 public:
CreateSessionDescriptionObserverOperationWrapper(rtc::scoped_refptr<CreateSessionDescriptionObserver> observer,std::function<void ()> operation_complete_callback)647 CreateSessionDescriptionObserverOperationWrapper(
648 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer,
649 std::function<void()> operation_complete_callback)
650 : observer_(std::move(observer)),
651 operation_complete_callback_(std::move(operation_complete_callback)) {
652 RTC_DCHECK(observer_);
653 }
~CreateSessionDescriptionObserverOperationWrapper()654 ~CreateSessionDescriptionObserverOperationWrapper() override {
655 RTC_DCHECK(was_called_);
656 }
657
OnSuccess(SessionDescriptionInterface * desc)658 void OnSuccess(SessionDescriptionInterface* desc) override {
659 RTC_DCHECK(!was_called_);
660 #ifdef RTC_DCHECK_IS_ON
661 was_called_ = true;
662 #endif // RTC_DCHECK_IS_ON
663 // Completing the operation before invoking the observer allows the observer
664 // to execute SetLocalDescription() without delay.
665 operation_complete_callback_();
666 observer_->OnSuccess(desc);
667 }
668
OnFailure(RTCError error)669 void OnFailure(RTCError error) override {
670 RTC_DCHECK(!was_called_);
671 #ifdef RTC_DCHECK_IS_ON
672 was_called_ = true;
673 #endif // RTC_DCHECK_IS_ON
674 operation_complete_callback_();
675 observer_->OnFailure(std::move(error));
676 }
677
678 private:
679 #ifdef RTC_DCHECK_IS_ON
680 bool was_called_ = false;
681 #endif // RTC_DCHECK_IS_ON
682 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer_;
683 std::function<void()> operation_complete_callback_;
684 };
685
686 // Check if the changes of IceTransportsType motives an ice restart.
NeedIceRestart(bool surface_ice_candidates_on_ice_transport_type_changed,PeerConnectionInterface::IceTransportsType current,PeerConnectionInterface::IceTransportsType modified)687 bool NeedIceRestart(bool surface_ice_candidates_on_ice_transport_type_changed,
688 PeerConnectionInterface::IceTransportsType current,
689 PeerConnectionInterface::IceTransportsType modified) {
690 if (current == modified) {
691 return false;
692 }
693
694 if (!surface_ice_candidates_on_ice_transport_type_changed) {
695 return true;
696 }
697
698 auto current_filter = ConvertIceTransportTypeToCandidateFilter(current);
699 auto modified_filter = ConvertIceTransportTypeToCandidateFilter(modified);
700
701 // If surface_ice_candidates_on_ice_transport_type_changed is true and we
702 // extend the filter, then no ice restart is needed.
703 return (current_filter & modified_filter) != current_filter;
704 }
705
706 } // namespace
707
708 // Used by parameterless SetLocalDescription() to create an offer or answer.
709 // Upon completion of creating the session description, SetLocalDescription() is
710 // invoked with the result.
711 // For consistency with DoSetLocalDescription(), if the PeerConnection is
712 // destroyed midst operation, we DO NOT inform the
713 // |set_local_description_observer| that the operation failed.
714 // TODO(hbos): If/when we process SLD messages in ~PeerConnection, the
715 // consistent thing would be to inform the observer here.
716 class PeerConnection::ImplicitCreateSessionDescriptionObserver
717 : public CreateSessionDescriptionObserver {
718 public:
ImplicitCreateSessionDescriptionObserver(rtc::WeakPtr<PeerConnection> pc,rtc::scoped_refptr<SetSessionDescriptionObserver> set_local_description_observer)719 ImplicitCreateSessionDescriptionObserver(
720 rtc::WeakPtr<PeerConnection> pc,
721 rtc::scoped_refptr<SetSessionDescriptionObserver>
722 set_local_description_observer)
723 : pc_(std::move(pc)),
724 set_local_description_observer_(
725 std::move(set_local_description_observer)) {}
~ImplicitCreateSessionDescriptionObserver()726 ~ImplicitCreateSessionDescriptionObserver() override {
727 RTC_DCHECK(was_called_);
728 }
729
SetOperationCompleteCallback(std::function<void ()> operation_complete_callback)730 void SetOperationCompleteCallback(
731 std::function<void()> operation_complete_callback) {
732 operation_complete_callback_ = std::move(operation_complete_callback);
733 }
734
was_called() const735 bool was_called() const { return was_called_; }
736
OnSuccess(SessionDescriptionInterface * desc_ptr)737 void OnSuccess(SessionDescriptionInterface* desc_ptr) override {
738 RTC_DCHECK(!was_called_);
739 std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr);
740 was_called_ = true;
741
742 // Abort early if |pc_| is no longer valid.
743 if (!pc_) {
744 operation_complete_callback_();
745 return;
746 }
747 // DoSetLocalDescription() is currently implemented as a synchronous
748 // operation but where the |set_local_description_observer_|'s callbacks are
749 // invoked asynchronously in a post to PeerConnection::OnMessage().
750 pc_->DoSetLocalDescription(std::move(desc),
751 std::move(set_local_description_observer_));
752 // For backwards-compatability reasons, we declare the operation as
753 // completed here (rather than in PeerConnection::OnMessage()). This ensures
754 // that subsequent offer/answer operations can start immediately (without
755 // waiting for OnMessage()).
756 operation_complete_callback_();
757 }
758
OnFailure(RTCError error)759 void OnFailure(RTCError error) override {
760 RTC_DCHECK(!was_called_);
761 was_called_ = true;
762
763 // Abort early if |pc_| is no longer valid.
764 if (!pc_) {
765 operation_complete_callback_();
766 return;
767 }
768 // DoSetLocalDescription() reports its failures in a post. We do the
769 // same thing here for consistency.
770 pc_->PostSetSessionDescriptionFailure(
771 set_local_description_observer_,
772 RTCError(error.type(),
773 std::string("SetLocalDescription failed to create "
774 "session description - ") +
775 error.message()));
776 operation_complete_callback_();
777 }
778
779 private:
780 bool was_called_ = false;
781 rtc::WeakPtr<PeerConnection> pc_;
782 rtc::scoped_refptr<SetSessionDescriptionObserver>
783 set_local_description_observer_;
784 std::function<void()> operation_complete_callback_;
785 };
786
787 class PeerConnection::LocalIceCredentialsToReplace {
788 public:
789 // Sets the ICE credentials that need restarting to the ICE credentials of
790 // the current and pending descriptions.
SetIceCredentialsFromLocalDescriptions(const SessionDescriptionInterface * current_local_description,const SessionDescriptionInterface * pending_local_description)791 void SetIceCredentialsFromLocalDescriptions(
792 const SessionDescriptionInterface* current_local_description,
793 const SessionDescriptionInterface* pending_local_description) {
794 ice_credentials_.clear();
795 if (current_local_description) {
796 AppendIceCredentialsFromSessionDescription(*current_local_description);
797 }
798 if (pending_local_description) {
799 AppendIceCredentialsFromSessionDescription(*pending_local_description);
800 }
801 }
802
ClearIceCredentials()803 void ClearIceCredentials() { ice_credentials_.clear(); }
804
805 // Returns true if we have ICE credentials that need restarting.
HasIceCredentials() const806 bool HasIceCredentials() const { return !ice_credentials_.empty(); }
807
808 // Returns true if |local_description| shares no ICE credentials with the
809 // ICE credentials that need restarting.
SatisfiesIceRestart(const SessionDescriptionInterface & local_description) const810 bool SatisfiesIceRestart(
811 const SessionDescriptionInterface& local_description) const {
812 for (const auto& transport_info :
813 local_description.description()->transport_infos()) {
814 if (ice_credentials_.find(std::make_pair(
815 transport_info.description.ice_ufrag,
816 transport_info.description.ice_pwd)) != ice_credentials_.end()) {
817 return false;
818 }
819 }
820 return true;
821 }
822
823 private:
AppendIceCredentialsFromSessionDescription(const SessionDescriptionInterface & desc)824 void AppendIceCredentialsFromSessionDescription(
825 const SessionDescriptionInterface& desc) {
826 for (const auto& transport_info : desc.description()->transport_infos()) {
827 ice_credentials_.insert(
828 std::make_pair(transport_info.description.ice_ufrag,
829 transport_info.description.ice_pwd));
830 }
831 }
832
833 std::set<std::pair<std::string, std::string>> ice_credentials_;
834 };
835
836 // Upon completion, posts a task to execute the callback of the
837 // SetSessionDescriptionObserver asynchronously on the same thread. At this
838 // point, the state of the peer connection might no longer reflect the effects
839 // of the SetRemoteDescription operation, as the peer connection could have been
840 // modified during the post.
841 // TODO(hbos): Remove this class once we remove the version of
842 // PeerConnectionInterface::SetRemoteDescription() that takes a
843 // SetSessionDescriptionObserver as an argument.
844 class PeerConnection::SetRemoteDescriptionObserverAdapter
845 : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> {
846 public:
SetRemoteDescriptionObserverAdapter(rtc::scoped_refptr<PeerConnection> pc,rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper)847 SetRemoteDescriptionObserverAdapter(
848 rtc::scoped_refptr<PeerConnection> pc,
849 rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper)
850 : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {}
851
852 // SetRemoteDescriptionObserverInterface implementation.
OnSetRemoteDescriptionComplete(RTCError error)853 void OnSetRemoteDescriptionComplete(RTCError error) override {
854 if (error.ok())
855 pc_->PostSetSessionDescriptionSuccess(wrapper_);
856 else
857 pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error));
858 }
859
860 private:
861 rtc::scoped_refptr<PeerConnection> pc_;
862 rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_;
863 };
864
operator ==(const PeerConnectionInterface::RTCConfiguration & o) const865 bool PeerConnectionInterface::RTCConfiguration::operator==(
866 const PeerConnectionInterface::RTCConfiguration& o) const {
867 // This static_assert prevents us from accidentally breaking operator==.
868 // Note: Order matters! Fields must be ordered the same as RTCConfiguration.
869 struct stuff_being_tested_for_equality {
870 IceServers servers;
871 IceTransportsType type;
872 BundlePolicy bundle_policy;
873 RtcpMuxPolicy rtcp_mux_policy;
874 std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates;
875 int ice_candidate_pool_size;
876 bool disable_ipv6;
877 bool disable_ipv6_on_wifi;
878 int max_ipv6_networks;
879 bool disable_link_local_networks;
880 bool enable_rtp_data_channel;
881 absl::optional<int> screencast_min_bitrate;
882 absl::optional<bool> combined_audio_video_bwe;
883 absl::optional<bool> enable_dtls_srtp;
884 TcpCandidatePolicy tcp_candidate_policy;
885 CandidateNetworkPolicy candidate_network_policy;
886 int audio_jitter_buffer_max_packets;
887 bool audio_jitter_buffer_fast_accelerate;
888 int audio_jitter_buffer_min_delay_ms;
889 bool audio_jitter_buffer_enable_rtx_handling;
890 int ice_connection_receiving_timeout;
891 int ice_backup_candidate_pair_ping_interval;
892 ContinualGatheringPolicy continual_gathering_policy;
893 bool prioritize_most_likely_ice_candidate_pairs;
894 struct cricket::MediaConfig media_config;
895 bool prune_turn_ports;
896 PortPrunePolicy turn_port_prune_policy;
897 bool presume_writable_when_fully_relayed;
898 bool enable_ice_renomination;
899 bool redetermine_role_on_ice_restart;
900 bool surface_ice_candidates_on_ice_transport_type_changed;
901 absl::optional<int> ice_check_interval_strong_connectivity;
902 absl::optional<int> ice_check_interval_weak_connectivity;
903 absl::optional<int> ice_check_min_interval;
904 absl::optional<int> ice_unwritable_timeout;
905 absl::optional<int> ice_unwritable_min_checks;
906 absl::optional<int> ice_inactive_timeout;
907 absl::optional<int> stun_candidate_keepalive_interval;
908 webrtc::TurnCustomizer* turn_customizer;
909 SdpSemantics sdp_semantics;
910 absl::optional<rtc::AdapterType> network_preference;
911 bool active_reset_srtp_params;
912 absl::optional<CryptoOptions> crypto_options;
913 bool offer_extmap_allow_mixed;
914 std::string turn_logging_id;
915 bool enable_implicit_rollback;
916 absl::optional<bool> allow_codec_switching;
917 };
918 static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this),
919 "Did you add something to RTCConfiguration and forget to "
920 "update operator==?");
921 return type == o.type && servers == o.servers &&
922 bundle_policy == o.bundle_policy &&
923 rtcp_mux_policy == o.rtcp_mux_policy &&
924 tcp_candidate_policy == o.tcp_candidate_policy &&
925 candidate_network_policy == o.candidate_network_policy &&
926 audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets &&
927 audio_jitter_buffer_fast_accelerate ==
928 o.audio_jitter_buffer_fast_accelerate &&
929 audio_jitter_buffer_min_delay_ms ==
930 o.audio_jitter_buffer_min_delay_ms &&
931 audio_jitter_buffer_enable_rtx_handling ==
932 o.audio_jitter_buffer_enable_rtx_handling &&
933 ice_connection_receiving_timeout ==
934 o.ice_connection_receiving_timeout &&
935 ice_backup_candidate_pair_ping_interval ==
936 o.ice_backup_candidate_pair_ping_interval &&
937 continual_gathering_policy == o.continual_gathering_policy &&
938 certificates == o.certificates &&
939 prioritize_most_likely_ice_candidate_pairs ==
940 o.prioritize_most_likely_ice_candidate_pairs &&
941 media_config == o.media_config && disable_ipv6 == o.disable_ipv6 &&
942 disable_ipv6_on_wifi == o.disable_ipv6_on_wifi &&
943 max_ipv6_networks == o.max_ipv6_networks &&
944 disable_link_local_networks == o.disable_link_local_networks &&
945 enable_rtp_data_channel == o.enable_rtp_data_channel &&
946 screencast_min_bitrate == o.screencast_min_bitrate &&
947 combined_audio_video_bwe == o.combined_audio_video_bwe &&
948 enable_dtls_srtp == o.enable_dtls_srtp &&
949 ice_candidate_pool_size == o.ice_candidate_pool_size &&
950 prune_turn_ports == o.prune_turn_ports &&
951 turn_port_prune_policy == o.turn_port_prune_policy &&
952 presume_writable_when_fully_relayed ==
953 o.presume_writable_when_fully_relayed &&
954 enable_ice_renomination == o.enable_ice_renomination &&
955 redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart &&
956 surface_ice_candidates_on_ice_transport_type_changed ==
957 o.surface_ice_candidates_on_ice_transport_type_changed &&
958 ice_check_interval_strong_connectivity ==
959 o.ice_check_interval_strong_connectivity &&
960 ice_check_interval_weak_connectivity ==
961 o.ice_check_interval_weak_connectivity &&
962 ice_check_min_interval == o.ice_check_min_interval &&
963 ice_unwritable_timeout == o.ice_unwritable_timeout &&
964 ice_unwritable_min_checks == o.ice_unwritable_min_checks &&
965 ice_inactive_timeout == o.ice_inactive_timeout &&
966 stun_candidate_keepalive_interval ==
967 o.stun_candidate_keepalive_interval &&
968 turn_customizer == o.turn_customizer &&
969 sdp_semantics == o.sdp_semantics &&
970 network_preference == o.network_preference &&
971 active_reset_srtp_params == o.active_reset_srtp_params &&
972 crypto_options == o.crypto_options &&
973 offer_extmap_allow_mixed == o.offer_extmap_allow_mixed &&
974 turn_logging_id == o.turn_logging_id &&
975 enable_implicit_rollback == o.enable_implicit_rollback &&
976 allow_codec_switching == o.allow_codec_switching;
977 }
978
operator !=(const PeerConnectionInterface::RTCConfiguration & o) const979 bool PeerConnectionInterface::RTCConfiguration::operator!=(
980 const PeerConnectionInterface::RTCConfiguration& o) const {
981 return !(*this == o);
982 }
983
set_newly_created()984 void PeerConnection::TransceiverStableState::set_newly_created() {
985 RTC_DCHECK(!has_m_section_);
986 newly_created_ = true;
987 }
988
SetMSectionIfUnset(absl::optional<std::string> mid,absl::optional<size_t> mline_index)989 void PeerConnection::TransceiverStableState::SetMSectionIfUnset(
990 absl::optional<std::string> mid,
991 absl::optional<size_t> mline_index) {
992 if (!has_m_section_) {
993 mid_ = mid;
994 mline_index_ = mline_index;
995 has_m_section_ = true;
996 }
997 }
998
SetRemoteStreamIdsIfUnset(const std::vector<std::string> & ids)999 void PeerConnection::TransceiverStableState::SetRemoteStreamIdsIfUnset(
1000 const std::vector<std::string>& ids) {
1001 if (!remote_stream_ids_.has_value()) {
1002 remote_stream_ids_ = ids;
1003 }
1004 }
1005
1006 // Generate a RTCP CNAME when a PeerConnection is created.
GenerateRtcpCname()1007 std::string GenerateRtcpCname() {
1008 std::string cname;
1009 if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) {
1010 RTC_LOG(LS_ERROR) << "Failed to generate CNAME.";
1011 RTC_NOTREACHED();
1012 }
1013 return cname;
1014 }
1015
ValidateOfferAnswerOptions(const PeerConnectionInterface::RTCOfferAnswerOptions & rtc_options)1016 bool ValidateOfferAnswerOptions(
1017 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) {
1018 return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) &&
1019 IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video);
1020 }
1021
1022 // From |rtc_options|, fill parts of |session_options| shared by all generated
1023 // m= sections (in other words, nothing that involves a map/array).
ExtractSharedMediaSessionOptions(const PeerConnectionInterface::RTCOfferAnswerOptions & rtc_options,cricket::MediaSessionOptions * session_options)1024 void ExtractSharedMediaSessionOptions(
1025 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
1026 cricket::MediaSessionOptions* session_options) {
1027 session_options->vad_enabled = rtc_options.voice_activity_detection;
1028 session_options->bundle_enabled = rtc_options.use_rtp_mux;
1029 session_options->raw_packetization_for_video =
1030 rtc_options.raw_packetization_for_video;
1031 }
1032
PeerConnection(PeerConnectionFactory * factory,std::unique_ptr<RtcEventLog> event_log,std::unique_ptr<Call> call)1033 PeerConnection::PeerConnection(PeerConnectionFactory* factory,
1034 std::unique_ptr<RtcEventLog> event_log,
1035 std::unique_ptr<Call> call)
1036 : factory_(factory),
1037 event_log_(std::move(event_log)),
1038 event_log_ptr_(event_log_.get()),
1039 operations_chain_(rtc::OperationsChain::Create()),
1040 rtcp_cname_(GenerateRtcpCname()),
1041 local_streams_(StreamCollection::Create()),
1042 remote_streams_(StreamCollection::Create()),
1043 call_(std::move(call)),
1044 call_ptr_(call_.get()),
1045 local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()),
1046 data_channel_controller_(this),
1047 weak_ptr_factory_(this) {}
1048
~PeerConnection()1049 PeerConnection::~PeerConnection() {
1050 TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection");
1051 RTC_DCHECK_RUN_ON(signaling_thread());
1052
1053 weak_ptr_factory_.InvalidateWeakPtrs();
1054
1055 // Need to stop transceivers before destroying the stats collector because
1056 // AudioRtpSender has a reference to the StatsCollector it will update when
1057 // stopping.
1058 for (const auto& transceiver : transceivers_) {
1059 transceiver->Stop();
1060 }
1061
1062 stats_.reset(nullptr);
1063 if (stats_collector_) {
1064 stats_collector_->WaitForPendingRequest();
1065 stats_collector_ = nullptr;
1066 }
1067
1068 // Don't destroy BaseChannels until after stats has been cleaned up so that
1069 // the last stats request can still read from the channels.
1070 DestroyAllChannels();
1071
1072 RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed.";
1073
1074 webrtc_session_desc_factory_.reset();
1075 sctp_factory_.reset();
1076 transport_controller_.reset();
1077
1078 // port_allocator_ lives on the network thread and should be destroyed there.
1079 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1080 RTC_DCHECK_RUN_ON(network_thread());
1081 port_allocator_.reset();
1082 });
1083 // call_ and event_log_ must be destroyed on the worker thread.
1084 worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1085 RTC_DCHECK_RUN_ON(worker_thread());
1086 call_.reset();
1087 // The event log must outlive call (and any other object that uses it).
1088 event_log_.reset();
1089 });
1090
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 }
1107 }
1108
DestroyAllChannels()1109 void PeerConnection::DestroyAllChannels() {
1110 // Destroy video channels first since they may have a pointer to a voice
1111 // channel.
1112 for (const auto& transceiver : transceivers_) {
1113 if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
1114 DestroyTransceiverChannel(transceiver);
1115 }
1116 }
1117 for (const auto& transceiver : transceivers_) {
1118 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
1119 DestroyTransceiverChannel(transceiver);
1120 }
1121 }
1122 DestroyDataChannelTransport();
1123 }
1124
Initialize(const PeerConnectionInterface::RTCConfiguration & configuration,PeerConnectionDependencies dependencies)1125 bool PeerConnection::Initialize(
1126 const PeerConnectionInterface::RTCConfiguration& configuration,
1127 PeerConnectionDependencies dependencies) {
1128 RTC_DCHECK_RUN_ON(signaling_thread());
1129 TRACE_EVENT0("webrtc", "PeerConnection::Initialize");
1130
1131 RTCError config_error = ValidateConfiguration(configuration);
1132 if (!config_error.ok()) {
1133 RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message();
1134 return false;
1135 }
1136
1137 if (!dependencies.allocator) {
1138 RTC_LOG(LS_ERROR)
1139 << "PeerConnection initialized without a PortAllocator? "
1140 "This shouldn't happen if using PeerConnectionFactory.";
1141 return false;
1142 }
1143
1144 if (!dependencies.observer) {
1145 // TODO(deadbeef): Why do we do this?
1146 RTC_LOG(LS_ERROR) << "PeerConnection initialized without a "
1147 "PeerConnectionObserver";
1148 return false;
1149 }
1150
1151 observer_ = dependencies.observer;
1152 async_resolver_factory_ = std::move(dependencies.async_resolver_factory);
1153 port_allocator_ = std::move(dependencies.allocator);
1154 packet_socket_factory_ = std::move(dependencies.packet_socket_factory);
1155 ice_transport_factory_ = std::move(dependencies.ice_transport_factory);
1156 tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier);
1157
1158 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
1167 // 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
1172 // The port allocator lives on the network thread and should be initialized
1173 // there.
1174 const auto pa_result =
1175 network_thread()->Invoke<InitializePortAllocatorResult>(
1176 RTC_FROM_HERE,
1177 rtc::Bind(&PeerConnection::InitializePortAllocator_n, this,
1178 stun_servers, turn_servers, configuration));
1179
1180 // 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 }
1188
1189 // Send information about IPv4/IPv6 status.
1190 PeerConnectionAddressFamilyCounter address_family;
1191 if (pa_result.enable_ipv6) {
1192 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
1199 const PeerConnectionFactoryInterface::Options& options = factory_->options();
1200
1201 // 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);
1206 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;
1213 // 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;
1218 config.transport_observer = this;
1219 // 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 };
1238 config.event_log = event_log_ptr_;
1239 #if defined(ENABLE_EXTERNAL_AUTH)
1240 config.enable_external_auth = true;
1241 #endif
1242 config.active_reset_srtp_params = configuration.active_reset_srtp_params;
1243
1244 // 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
1264 sctp_factory_ = factory_->CreateSctpTransportInternalFactory();
1265
1266 if (configuration.enable_rtp_data_channel) {
1267 // Enable creation of RTP data channels if the kEnableRtpDataChannels is
1268 // set. It takes precendence over the disable_sctp_data_channels
1269 // PeerConnectionFactoryInterface::Options.
1270 data_channel_controller_.set_data_channel_type(cricket::DCT_RTP);
1271 } else {
1272 // DTLS has to be enabled to use SCTP.
1273 if (!options.disable_sctp_data_channels && dtls_enabled_) {
1274 data_channel_controller_.set_data_channel_type(cricket::DCT_SCTP);
1275 config.sctp_factory = sctp_factory_.get();
1276 }
1277 }
1278
1279 config.ice_transport_factory = ice_transport_factory_.get();
1280
1281 transport_controller_.reset(new JsepTransportController(
1282 signaling_thread(), network_thread(), port_allocator_.get(),
1283 async_resolver_factory_.get(), config));
1284 transport_controller_->SignalIceConnectionState.connect(
1285 this, &PeerConnection::OnTransportControllerConnectionState);
1286 transport_controller_->SignalStandardizedIceConnectionState.connect(
1287 this, &PeerConnection::SetStandardizedIceConnectionState);
1288 transport_controller_->SignalConnectionState.connect(
1289 this, &PeerConnection::SetConnectionState);
1290 transport_controller_->SignalIceGatheringState.connect(
1291 this, &PeerConnection::OnTransportControllerGatheringState);
1292 transport_controller_->SignalIceCandidatesGathered.connect(
1293 this, &PeerConnection::OnTransportControllerCandidatesGathered);
1294 transport_controller_->SignalIceCandidateError.connect(
1295 this, &PeerConnection::OnTransportControllerCandidateError);
1296 transport_controller_->SignalIceCandidatesRemoved.connect(
1297 this, &PeerConnection::OnTransportControllerCandidatesRemoved);
1298 transport_controller_->SignalDtlsHandshakeError.connect(
1299 this, &PeerConnection::OnTransportControllerDtlsHandshakeError);
1300 transport_controller_->SignalIceCandidatePairChanged.connect(
1301 this, &PeerConnection::OnTransportControllerCandidateChanged);
1302
1303 stats_.reset(new StatsCollector(this));
1304 stats_collector_ = RTCStatsCollector::Create(this);
1305
1306 configuration_ = configuration;
1307
1308 transport_controller_->SetIceConfig(ParseIceConfig(configuration));
1309
1310 video_options_.screencast_min_bitrate_kbps =
1311 configuration.screencast_min_bitrate;
1312 audio_options_.combined_audio_video_bwe =
1313 configuration.combined_audio_video_bwe;
1314
1315 audio_options_.audio_jitter_buffer_max_packets =
1316 configuration.audio_jitter_buffer_max_packets;
1317
1318 audio_options_.audio_jitter_buffer_fast_accelerate =
1319 configuration.audio_jitter_buffer_fast_accelerate;
1320
1321 audio_options_.audio_jitter_buffer_min_delay_ms =
1322 configuration.audio_jitter_buffer_min_delay_ms;
1323
1324 audio_options_.audio_jitter_buffer_enable_rtx_handling =
1325 configuration.audio_jitter_buffer_enable_rtx_handling;
1326
1327 // Whether the certificate generator/certificate is null or not determines
1328 // what PeerConnectionDescriptionFactory will do, so make sure that we give it
1329 // the right instructions by clearing the variables if needed.
1330 if (!dtls_enabled_) {
1331 dependencies.cert_generator.reset();
1332 certificate = nullptr;
1333 } else if (certificate) {
1334 // Favor generated certificate over the certificate generator.
1335 dependencies.cert_generator.reset();
1336 }
1337
1338 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
1339 signaling_thread(), channel_manager(), this, session_id(),
1340 std::move(dependencies.cert_generator), certificate, &ssrc_generator_));
1341 webrtc_session_desc_factory_->SignalCertificateReady.connect(
1342 this, &PeerConnection::OnCertificateReady);
1343
1344 if (options.disable_encryption) {
1345 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
1346 }
1347
1348 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
1349 GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions);
1350 webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan());
1351
1352 // Add default audio/video transceivers for Plan B SDP.
1353 if (!IsUnifiedPlan()) {
1354 transceivers_.push_back(
1355 RtpTransceiverProxyWithInternal<RtpTransceiver>::Create(
1356 signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO)));
1357 transceivers_.push_back(
1358 RtpTransceiverProxyWithInternal<RtpTransceiver>::Create(
1359 signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO)));
1360 }
1361 int delay_ms =
1362 return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS;
1363 signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this,
1364 MSG_REPORT_USAGE_PATTERN, nullptr);
1365
1366 if (dependencies.video_bitrate_allocator_factory) {
1367 video_bitrate_allocator_factory_ =
1368 std::move(dependencies.video_bitrate_allocator_factory);
1369 } else {
1370 video_bitrate_allocator_factory_ =
1371 CreateBuiltinVideoBitrateAllocatorFactory();
1372 }
1373 return true;
1374 }
1375
ValidateConfiguration(const RTCConfiguration & config) const1376 RTCError PeerConnection::ValidateConfiguration(
1377 const RTCConfiguration& config) const {
1378 return cricket::P2PTransportChannel::ValidateIceConfig(
1379 ParseIceConfig(config));
1380 }
1381
local_streams()1382 rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() {
1383 RTC_DCHECK_RUN_ON(signaling_thread());
1384 RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified "
1385 "Plan SdpSemantics. Please use GetSenders "
1386 "instead.";
1387 return local_streams_;
1388 }
1389
remote_streams()1390 rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() {
1391 RTC_DCHECK_RUN_ON(signaling_thread());
1392 RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified "
1393 "Plan SdpSemantics. Please use GetReceivers "
1394 "instead.";
1395 return remote_streams_;
1396 }
1397
AddStream(MediaStreamInterface * local_stream)1398 bool PeerConnection::AddStream(MediaStreamInterface* local_stream) {
1399 RTC_DCHECK_RUN_ON(signaling_thread());
1400 RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan "
1401 "SdpSemantics. Please use AddTrack instead.";
1402 TRACE_EVENT0("webrtc", "PeerConnection::AddStream");
1403 if (IsClosed()) {
1404 return false;
1405 }
1406 if (!CanAddLocalMediaStream(local_streams_, local_stream)) {
1407 return false;
1408 }
1409
1410 local_streams_->AddStream(local_stream);
1411 MediaStreamObserver* observer = new MediaStreamObserver(local_stream);
1412 observer->SignalAudioTrackAdded.connect(this,
1413 &PeerConnection::OnAudioTrackAdded);
1414 observer->SignalAudioTrackRemoved.connect(
1415 this, &PeerConnection::OnAudioTrackRemoved);
1416 observer->SignalVideoTrackAdded.connect(this,
1417 &PeerConnection::OnVideoTrackAdded);
1418 observer->SignalVideoTrackRemoved.connect(
1419 this, &PeerConnection::OnVideoTrackRemoved);
1420 stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer));
1421
1422 for (const auto& track : local_stream->GetAudioTracks()) {
1423 AddAudioTrack(track.get(), local_stream);
1424 }
1425 for (const auto& track : local_stream->GetVideoTracks()) {
1426 AddVideoTrack(track.get(), local_stream);
1427 }
1428
1429 stats_->AddStream(local_stream);
1430 UpdateNegotiationNeeded();
1431 return true;
1432 }
1433
RemoveStream(MediaStreamInterface * local_stream)1434 void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) {
1435 RTC_DCHECK_RUN_ON(signaling_thread());
1436 RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified "
1437 "Plan SdpSemantics. Please use RemoveTrack "
1438 "instead.";
1439 TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream");
1440 if (!IsClosed()) {
1441 for (const auto& track : local_stream->GetAudioTracks()) {
1442 RemoveAudioTrack(track.get(), local_stream);
1443 }
1444 for (const auto& track : local_stream->GetVideoTracks()) {
1445 RemoveVideoTrack(track.get(), local_stream);
1446 }
1447 }
1448 local_streams_->RemoveStream(local_stream);
1449 stream_observers_.erase(
1450 std::remove_if(
1451 stream_observers_.begin(), stream_observers_.end(),
1452 [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) {
1453 return observer->stream()->id().compare(local_stream->id()) == 0;
1454 }),
1455 stream_observers_.end());
1456
1457 if (IsClosed()) {
1458 return;
1459 }
1460 UpdateNegotiationNeeded();
1461 }
1462
AddTrack(rtc::scoped_refptr<MediaStreamTrackInterface> track,const std::vector<std::string> & stream_ids)1463 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack(
1464 rtc::scoped_refptr<MediaStreamTrackInterface> track,
1465 const std::vector<std::string>& stream_ids) {
1466 RTC_DCHECK_RUN_ON(signaling_thread());
1467 TRACE_EVENT0("webrtc", "PeerConnection::AddTrack");
1468 if (!track) {
1469 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null.");
1470 }
1471 if (!(track->kind() == MediaStreamTrackInterface::kAudioKind ||
1472 track->kind() == MediaStreamTrackInterface::kVideoKind)) {
1473 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1474 "Track has invalid kind: " + track->kind());
1475 }
1476 if (IsClosed()) {
1477 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
1478 "PeerConnection is closed.");
1479 }
1480 if (FindSenderForTrack(track)) {
1481 LOG_AND_RETURN_ERROR(
1482 RTCErrorType::INVALID_PARAMETER,
1483 "Sender already exists for track " + track->id() + ".");
1484 }
1485 auto sender_or_error =
1486 (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids)
1487 : AddTrackPlanB(track, stream_ids));
1488 if (sender_or_error.ok()) {
1489 UpdateNegotiationNeeded();
1490 stats_->AddTrack(track);
1491 }
1492 return sender_or_error;
1493 }
1494
1495 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>>
AddTrackPlanB(rtc::scoped_refptr<MediaStreamTrackInterface> track,const std::vector<std::string> & stream_ids)1496 PeerConnection::AddTrackPlanB(
1497 rtc::scoped_refptr<MediaStreamTrackInterface> track,
1498 const std::vector<std::string>& stream_ids) {
1499 if (stream_ids.size() > 1u) {
1500 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION,
1501 "AddTrack with more than one stream is not "
1502 "supported with Plan B semantics.");
1503 }
1504 std::vector<std::string> adjusted_stream_ids = stream_ids;
1505 if (adjusted_stream_ids.empty()) {
1506 adjusted_stream_ids.push_back(rtc::CreateRandomUuid());
1507 }
1508 cricket::MediaType media_type =
1509 (track->kind() == MediaStreamTrackInterface::kAudioKind
1510 ? cricket::MEDIA_TYPE_AUDIO
1511 : cricket::MEDIA_TYPE_VIDEO);
1512 auto new_sender =
1513 CreateSender(media_type, track->id(), track, adjusted_stream_ids, {});
1514 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
1515 new_sender->internal()->SetMediaChannel(voice_media_channel());
1516 GetAudioTransceiver()->internal()->AddSender(new_sender);
1517 const RtpSenderInfo* sender_info =
1518 FindSenderInfo(local_audio_sender_infos_,
1519 new_sender->internal()->stream_ids()[0], track->id());
1520 if (sender_info) {
1521 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
1522 }
1523 } else {
1524 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
1525 new_sender->internal()->SetMediaChannel(video_media_channel());
1526 GetVideoTransceiver()->internal()->AddSender(new_sender);
1527 const RtpSenderInfo* sender_info =
1528 FindSenderInfo(local_video_sender_infos_,
1529 new_sender->internal()->stream_ids()[0], track->id());
1530 if (sender_info) {
1531 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
1532 }
1533 }
1534 return rtc::scoped_refptr<RtpSenderInterface>(new_sender);
1535 }
1536
1537 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>>
AddTrackUnifiedPlan(rtc::scoped_refptr<MediaStreamTrackInterface> track,const std::vector<std::string> & stream_ids)1538 PeerConnection::AddTrackUnifiedPlan(
1539 rtc::scoped_refptr<MediaStreamTrackInterface> track,
1540 const std::vector<std::string>& stream_ids) {
1541 auto transceiver = FindFirstTransceiverForAddedTrack(track);
1542 if (transceiver) {
1543 RTC_LOG(LS_INFO) << "Reusing an existing "
1544 << cricket::MediaTypeToString(transceiver->media_type())
1545 << " transceiver for AddTrack.";
1546 if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) {
1547 transceiver->internal()->set_direction(
1548 RtpTransceiverDirection::kSendRecv);
1549 } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) {
1550 transceiver->internal()->set_direction(
1551 RtpTransceiverDirection::kSendOnly);
1552 }
1553 transceiver->sender()->SetTrack(track);
1554 transceiver->internal()->sender_internal()->set_stream_ids(stream_ids);
1555 transceiver->internal()->set_reused_for_addtrack(true);
1556 } else {
1557 cricket::MediaType media_type =
1558 (track->kind() == MediaStreamTrackInterface::kAudioKind
1559 ? cricket::MEDIA_TYPE_AUDIO
1560 : cricket::MEDIA_TYPE_VIDEO);
1561 RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type)
1562 << " transceiver in response to a call to AddTrack.";
1563 std::string sender_id = track->id();
1564 // Avoid creating a sender with an existing ID by generating a random ID.
1565 // This can happen if this is the second time AddTrack has created a sender
1566 // for this track.
1567 if (FindSenderById(sender_id)) {
1568 sender_id = rtc::CreateRandomUuid();
1569 }
1570 auto sender = CreateSender(media_type, sender_id, track, stream_ids, {});
1571 auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid());
1572 transceiver = CreateAndAddTransceiver(sender, receiver);
1573 transceiver->internal()->set_created_by_addtrack(true);
1574 transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv);
1575 }
1576 return transceiver->sender();
1577 }
1578
1579 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
FindFirstTransceiverForAddedTrack(rtc::scoped_refptr<MediaStreamTrackInterface> track)1580 PeerConnection::FindFirstTransceiverForAddedTrack(
1581 rtc::scoped_refptr<MediaStreamTrackInterface> track) {
1582 RTC_DCHECK(track);
1583 for (auto transceiver : transceivers_) {
1584 if (!transceiver->sender()->track() &&
1585 cricket::MediaTypeToString(transceiver->media_type()) ==
1586 track->kind() &&
1587 !transceiver->internal()->has_ever_been_used_to_send() &&
1588 !transceiver->stopped()) {
1589 return transceiver;
1590 }
1591 }
1592 return nullptr;
1593 }
1594
RemoveTrack(RtpSenderInterface * sender)1595 bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) {
1596 TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack");
1597 return RemoveTrackNew(sender).ok();
1598 }
1599
RemoveTrackNew(rtc::scoped_refptr<RtpSenderInterface> sender)1600 RTCError PeerConnection::RemoveTrackNew(
1601 rtc::scoped_refptr<RtpSenderInterface> sender) {
1602 RTC_DCHECK_RUN_ON(signaling_thread());
1603 if (!sender) {
1604 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null.");
1605 }
1606 if (IsClosed()) {
1607 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
1608 "PeerConnection is closed.");
1609 }
1610 if (IsUnifiedPlan()) {
1611 auto transceiver = FindTransceiverBySender(sender);
1612 if (!transceiver || !sender->track()) {
1613 return RTCError::OK();
1614 }
1615 sender->SetTrack(nullptr);
1616 if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) {
1617 transceiver->internal()->set_direction(
1618 RtpTransceiverDirection::kRecvOnly);
1619 } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) {
1620 transceiver->internal()->set_direction(
1621 RtpTransceiverDirection::kInactive);
1622 }
1623 } else {
1624 bool removed;
1625 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
1626 removed = GetAudioTransceiver()->internal()->RemoveSender(sender);
1627 } else {
1628 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type());
1629 removed = GetVideoTransceiver()->internal()->RemoveSender(sender);
1630 }
1631 if (!removed) {
1632 LOG_AND_RETURN_ERROR(
1633 RTCErrorType::INVALID_PARAMETER,
1634 "Couldn't find sender " + sender->id() + " to remove.");
1635 }
1636 }
1637 UpdateNegotiationNeeded();
1638 return RTCError::OK();
1639 }
1640
1641 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender)1642 PeerConnection::FindTransceiverBySender(
1643 rtc::scoped_refptr<RtpSenderInterface> sender) {
1644 for (auto transceiver : transceivers_) {
1645 if (transceiver->sender() == sender) {
1646 return transceiver;
1647 }
1648 }
1649 return nullptr;
1650 }
1651
1652 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
AddTransceiver(rtc::scoped_refptr<MediaStreamTrackInterface> track)1653 PeerConnection::AddTransceiver(
1654 rtc::scoped_refptr<MediaStreamTrackInterface> track) {
1655 return AddTransceiver(track, RtpTransceiverInit());
1656 }
1657
1658 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
AddTransceiver(rtc::scoped_refptr<MediaStreamTrackInterface> track,const RtpTransceiverInit & init)1659 PeerConnection::AddTransceiver(
1660 rtc::scoped_refptr<MediaStreamTrackInterface> track,
1661 const RtpTransceiverInit& init) {
1662 RTC_DCHECK_RUN_ON(signaling_thread());
1663 RTC_CHECK(IsUnifiedPlan())
1664 << "AddTransceiver is only available with Unified Plan SdpSemantics";
1665 if (!track) {
1666 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null");
1667 }
1668 cricket::MediaType media_type;
1669 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
1670 media_type = cricket::MEDIA_TYPE_AUDIO;
1671 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
1672 media_type = cricket::MEDIA_TYPE_VIDEO;
1673 } else {
1674 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1675 "Track kind is not audio or video");
1676 }
1677 return AddTransceiver(media_type, track, init);
1678 }
1679
1680 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
AddTransceiver(cricket::MediaType media_type)1681 PeerConnection::AddTransceiver(cricket::MediaType media_type) {
1682 return AddTransceiver(media_type, RtpTransceiverInit());
1683 }
1684
1685 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
AddTransceiver(cricket::MediaType media_type,const RtpTransceiverInit & init)1686 PeerConnection::AddTransceiver(cricket::MediaType media_type,
1687 const RtpTransceiverInit& init) {
1688 RTC_DCHECK_RUN_ON(signaling_thread());
1689 RTC_CHECK(IsUnifiedPlan())
1690 << "AddTransceiver is only available with Unified Plan SdpSemantics";
1691 if (!(media_type == cricket::MEDIA_TYPE_AUDIO ||
1692 media_type == cricket::MEDIA_TYPE_VIDEO)) {
1693 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1694 "media type is not audio or video");
1695 }
1696 return AddTransceiver(media_type, nullptr, init);
1697 }
1698
1699 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
AddTransceiver(cricket::MediaType media_type,rtc::scoped_refptr<MediaStreamTrackInterface> track,const RtpTransceiverInit & init,bool update_negotiation_needed)1700 PeerConnection::AddTransceiver(
1701 cricket::MediaType media_type,
1702 rtc::scoped_refptr<MediaStreamTrackInterface> track,
1703 const RtpTransceiverInit& init,
1704 bool update_negotiation_needed) {
1705 RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO ||
1706 media_type == cricket::MEDIA_TYPE_VIDEO));
1707 if (track) {
1708 RTC_DCHECK_EQ(media_type,
1709 (track->kind() == MediaStreamTrackInterface::kAudioKind
1710 ? cricket::MEDIA_TYPE_AUDIO
1711 : cricket::MEDIA_TYPE_VIDEO));
1712 }
1713
1714 RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings,
1715 init.send_encodings.size(), 0, 7, 8);
1716
1717 size_t num_rids = absl::c_count_if(init.send_encodings,
1718 [](const RtpEncodingParameters& encoding) {
1719 return !encoding.rid.empty();
1720 });
1721 if (num_rids > 0 && num_rids != init.send_encodings.size()) {
1722 LOG_AND_RETURN_ERROR(
1723 RTCErrorType::INVALID_PARAMETER,
1724 "RIDs must be provided for either all or none of the send encodings.");
1725 }
1726
1727 if (num_rids > 0 && absl::c_any_of(init.send_encodings,
1728 [](const RtpEncodingParameters& encoding) {
1729 return !IsLegalRsidName(encoding.rid);
1730 })) {
1731 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1732 "Invalid RID value provided.");
1733 }
1734
1735 if (absl::c_any_of(init.send_encodings,
1736 [](const RtpEncodingParameters& encoding) {
1737 return encoding.ssrc.has_value();
1738 })) {
1739 LOG_AND_RETURN_ERROR(
1740 RTCErrorType::UNSUPPORTED_PARAMETER,
1741 "Attempted to set an unimplemented parameter of RtpParameters.");
1742 }
1743
1744 RtpParameters parameters;
1745 parameters.encodings = init.send_encodings;
1746
1747 // Encodings are dropped from the tail if too many are provided.
1748 if (parameters.encodings.size() > kMaxSimulcastStreams) {
1749 parameters.encodings.erase(
1750 parameters.encodings.begin() + kMaxSimulcastStreams,
1751 parameters.encodings.end());
1752 }
1753
1754 // Single RID should be removed.
1755 if (parameters.encodings.size() == 1 &&
1756 !parameters.encodings[0].rid.empty()) {
1757 RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << ".";
1758 parameters.encodings[0].rid.clear();
1759 }
1760
1761 // If RIDs were not provided, they are generated for simulcast scenario.
1762 if (parameters.encodings.size() > 1 && num_rids == 0) {
1763 rtc::UniqueStringGenerator rid_generator;
1764 for (RtpEncodingParameters& encoding : parameters.encodings) {
1765 encoding.rid = rid_generator();
1766 }
1767 }
1768
1769 if (UnimplementedRtpParameterHasValue(parameters)) {
1770 LOG_AND_RETURN_ERROR(
1771 RTCErrorType::UNSUPPORTED_PARAMETER,
1772 "Attempted to set an unimplemented parameter of RtpParameters.");
1773 }
1774
1775 auto result = cricket::CheckRtpParametersValues(parameters);
1776 if (!result.ok()) {
1777 LOG_AND_RETURN_ERROR(result.type(), result.message());
1778 }
1779
1780 RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type)
1781 << " transceiver in response to a call to AddTransceiver.";
1782 // Set the sender ID equal to the track ID if the track is specified unless
1783 // that sender ID is already in use.
1784 std::string sender_id =
1785 (track && !FindSenderById(track->id()) ? track->id()
1786 : rtc::CreateRandomUuid());
1787 auto sender = CreateSender(media_type, sender_id, track, init.stream_ids,
1788 parameters.encodings);
1789 auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid());
1790 auto transceiver = CreateAndAddTransceiver(sender, receiver);
1791 transceiver->internal()->set_direction(init.direction);
1792
1793 if (update_negotiation_needed) {
1794 UpdateNegotiationNeeded();
1795 }
1796
1797 return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver);
1798 }
1799
1800 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
CreateSender(cricket::MediaType media_type,const std::string & id,rtc::scoped_refptr<MediaStreamTrackInterface> track,const std::vector<std::string> & stream_ids,const std::vector<RtpEncodingParameters> & send_encodings)1801 PeerConnection::CreateSender(
1802 cricket::MediaType media_type,
1803 const std::string& id,
1804 rtc::scoped_refptr<MediaStreamTrackInterface> track,
1805 const std::vector<std::string>& stream_ids,
1806 const std::vector<RtpEncodingParameters>& send_encodings) {
1807 RTC_DCHECK_RUN_ON(signaling_thread());
1808 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender;
1809 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1810 RTC_DCHECK(!track ||
1811 (track->kind() == MediaStreamTrackInterface::kAudioKind));
1812 sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1813 signaling_thread(),
1814 AudioRtpSender::Create(worker_thread(), id, stats_.get(), this));
1815 NoteUsageEvent(UsageEvent::AUDIO_ADDED);
1816 } else {
1817 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
1818 RTC_DCHECK(!track ||
1819 (track->kind() == MediaStreamTrackInterface::kVideoKind));
1820 sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1821 signaling_thread(), VideoRtpSender::Create(worker_thread(), id, this));
1822 NoteUsageEvent(UsageEvent::VIDEO_ADDED);
1823 }
1824 bool set_track_succeeded = sender->SetTrack(track);
1825 RTC_DCHECK(set_track_succeeded);
1826 sender->internal()->set_stream_ids(stream_ids);
1827 sender->internal()->set_init_send_encodings(send_encodings);
1828 return sender;
1829 }
1830
1831 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
CreateReceiver(cricket::MediaType media_type,const std::string & receiver_id)1832 PeerConnection::CreateReceiver(cricket::MediaType media_type,
1833 const std::string& receiver_id) {
1834 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1835 receiver;
1836 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1837 receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
1838 signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id,
1839 std::vector<std::string>({})));
1840 NoteUsageEvent(UsageEvent::AUDIO_ADDED);
1841 } else {
1842 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
1843 receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
1844 signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id,
1845 std::vector<std::string>({})));
1846 NoteUsageEvent(UsageEvent::VIDEO_ADDED);
1847 }
1848 return receiver;
1849 }
1850
1851 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
CreateAndAddTransceiver(rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> receiver)1852 PeerConnection::CreateAndAddTransceiver(
1853 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
1854 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1855 receiver) {
1856 // Ensure that the new sender does not have an ID that is already in use by
1857 // another sender.
1858 // Allow receiver IDs to conflict since those come from remote SDP (which
1859 // could be invalid, but should not cause a crash).
1860 RTC_DCHECK(!FindSenderById(sender->id()));
1861 auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create(
1862 signaling_thread(),
1863 new RtpTransceiver(
1864 sender, receiver, channel_manager(),
1865 sender->media_type() == cricket::MEDIA_TYPE_AUDIO
1866 ? channel_manager()->GetSupportedAudioRtpHeaderExtensions()
1867 : channel_manager()->GetSupportedVideoRtpHeaderExtensions()));
1868 transceivers_.push_back(transceiver);
1869 transceiver->internal()->SignalNegotiationNeeded.connect(
1870 this, &PeerConnection::OnNegotiationNeeded);
1871 return transceiver;
1872 }
1873
OnNegotiationNeeded()1874 void PeerConnection::OnNegotiationNeeded() {
1875 RTC_DCHECK_RUN_ON(signaling_thread());
1876 RTC_DCHECK(!IsClosed());
1877 UpdateNegotiationNeeded();
1878 }
1879
CreateSender(const std::string & kind,const std::string & stream_id)1880 rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender(
1881 const std::string& kind,
1882 const std::string& stream_id) {
1883 RTC_DCHECK_RUN_ON(signaling_thread());
1884 RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified "
1885 "Plan SdpSemantics. Please use AddTransceiver "
1886 "instead.";
1887 TRACE_EVENT0("webrtc", "PeerConnection::CreateSender");
1888 if (IsClosed()) {
1889 return nullptr;
1890 }
1891
1892 // Internally we need to have one stream with Plan B semantics, so we
1893 // generate a random stream ID if not specified.
1894 std::vector<std::string> stream_ids;
1895 if (stream_id.empty()) {
1896 stream_ids.push_back(rtc::CreateRandomUuid());
1897 RTC_LOG(LS_INFO)
1898 << "No stream_id specified for sender. Generated stream ID: "
1899 << stream_ids[0];
1900 } else {
1901 stream_ids.push_back(stream_id);
1902 }
1903
1904 // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver.
1905 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender;
1906 if (kind == MediaStreamTrackInterface::kAudioKind) {
1907 auto audio_sender = AudioRtpSender::Create(
1908 worker_thread(), rtc::CreateRandomUuid(), stats_.get(), this);
1909 audio_sender->SetMediaChannel(voice_media_channel());
1910 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1911 signaling_thread(), audio_sender);
1912 GetAudioTransceiver()->internal()->AddSender(new_sender);
1913 } else if (kind == MediaStreamTrackInterface::kVideoKind) {
1914 auto video_sender =
1915 VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), this);
1916 video_sender->SetMediaChannel(video_media_channel());
1917 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1918 signaling_thread(), video_sender);
1919 GetVideoTransceiver()->internal()->AddSender(new_sender);
1920 } else {
1921 RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind;
1922 return nullptr;
1923 }
1924 new_sender->internal()->set_stream_ids(stream_ids);
1925
1926 return new_sender;
1927 }
1928
GetSenders() const1929 std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders()
1930 const {
1931 RTC_DCHECK_RUN_ON(signaling_thread());
1932 std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret;
1933 for (const auto& sender : GetSendersInternal()) {
1934 ret.push_back(sender);
1935 }
1936 return ret;
1937 }
1938
1939 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
GetSendersInternal() const1940 PeerConnection::GetSendersInternal() const {
1941 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
1942 all_senders;
1943 for (const auto& transceiver : transceivers_) {
1944 auto senders = transceiver->internal()->senders();
1945 all_senders.insert(all_senders.end(), senders.begin(), senders.end());
1946 }
1947 return all_senders;
1948 }
1949
1950 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>
GetReceivers() const1951 PeerConnection::GetReceivers() const {
1952 RTC_DCHECK_RUN_ON(signaling_thread());
1953 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret;
1954 for (const auto& receiver : GetReceiversInternal()) {
1955 ret.push_back(receiver);
1956 }
1957 return ret;
1958 }
1959
1960 std::vector<
1961 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
GetReceiversInternal() const1962 PeerConnection::GetReceiversInternal() const {
1963 std::vector<
1964 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
1965 all_receivers;
1966 for (const auto& transceiver : transceivers_) {
1967 auto receivers = transceiver->internal()->receivers();
1968 all_receivers.insert(all_receivers.end(), receivers.begin(),
1969 receivers.end());
1970 }
1971 return all_receivers;
1972 }
1973
1974 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>
GetTransceivers() const1975 PeerConnection::GetTransceivers() const {
1976 RTC_DCHECK_RUN_ON(signaling_thread());
1977 RTC_CHECK(IsUnifiedPlan())
1978 << "GetTransceivers is only supported with Unified Plan SdpSemantics.";
1979 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers;
1980 for (const auto& transceiver : transceivers_) {
1981 all_transceivers.push_back(transceiver);
1982 }
1983 return all_transceivers;
1984 }
1985
GetStats(StatsObserver * observer,MediaStreamTrackInterface * track,StatsOutputLevel level)1986 bool PeerConnection::GetStats(StatsObserver* observer,
1987 MediaStreamTrackInterface* track,
1988 StatsOutputLevel level) {
1989 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
1990 RTC_DCHECK_RUN_ON(signaling_thread());
1991 if (!observer) {
1992 RTC_LOG(LS_ERROR) << "GetStats - observer is NULL.";
1993 return false;
1994 }
1995
1996 stats_->UpdateStats(level);
1997 // The StatsCollector is used to tell if a track is valid because it may
1998 // remember tracks that the PeerConnection previously removed.
1999 if (track && !stats_->IsValidTrack(track->id())) {
2000 RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: "
2001 << track->id();
2002 return false;
2003 }
2004 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS,
2005 new GetStatsMsg(observer, track));
2006 return true;
2007 }
2008
GetStats(RTCStatsCollectorCallback * callback)2009 void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) {
2010 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
2011 RTC_DCHECK_RUN_ON(signaling_thread());
2012 RTC_DCHECK(stats_collector_);
2013 RTC_DCHECK(callback);
2014 stats_collector_->GetStatsReport(callback);
2015 }
2016
GetStats(rtc::scoped_refptr<RtpSenderInterface> selector,rtc::scoped_refptr<RTCStatsCollectorCallback> callback)2017 void PeerConnection::GetStats(
2018 rtc::scoped_refptr<RtpSenderInterface> selector,
2019 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
2020 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
2021 RTC_DCHECK_RUN_ON(signaling_thread());
2022 RTC_DCHECK(callback);
2023 RTC_DCHECK(stats_collector_);
2024 rtc::scoped_refptr<RtpSenderInternal> internal_sender;
2025 if (selector) {
2026 for (const auto& proxy_transceiver : transceivers_) {
2027 for (const auto& proxy_sender :
2028 proxy_transceiver->internal()->senders()) {
2029 if (proxy_sender == selector) {
2030 internal_sender = proxy_sender->internal();
2031 break;
2032 }
2033 }
2034 if (internal_sender)
2035 break;
2036 }
2037 }
2038 // If there is no |internal_sender| then |selector| is either null or does not
2039 // belong to the PeerConnection (in Plan B, senders can be removed from the
2040 // PeerConnection). This means that "all the stats objects representing the
2041 // selector" is an empty set. Invoking GetStatsReport() with a null selector
2042 // produces an empty stats report.
2043 stats_collector_->GetStatsReport(internal_sender, callback);
2044 }
2045
GetStats(rtc::scoped_refptr<RtpReceiverInterface> selector,rtc::scoped_refptr<RTCStatsCollectorCallback> callback)2046 void PeerConnection::GetStats(
2047 rtc::scoped_refptr<RtpReceiverInterface> selector,
2048 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
2049 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
2050 RTC_DCHECK_RUN_ON(signaling_thread());
2051 RTC_DCHECK(callback);
2052 RTC_DCHECK(stats_collector_);
2053 rtc::scoped_refptr<RtpReceiverInternal> internal_receiver;
2054 if (selector) {
2055 for (const auto& proxy_transceiver : transceivers_) {
2056 for (const auto& proxy_receiver :
2057 proxy_transceiver->internal()->receivers()) {
2058 if (proxy_receiver == selector) {
2059 internal_receiver = proxy_receiver->internal();
2060 break;
2061 }
2062 }
2063 if (internal_receiver)
2064 break;
2065 }
2066 }
2067 // If there is no |internal_receiver| then |selector| is either null or does
2068 // not belong to the PeerConnection (in Plan B, receivers can be removed from
2069 // the PeerConnection). This means that "all the stats objects representing
2070 // the selector" is an empty set. Invoking GetStatsReport() with a null
2071 // selector produces an empty stats report.
2072 stats_collector_->GetStatsReport(internal_receiver, callback);
2073 }
2074
signaling_state()2075 PeerConnectionInterface::SignalingState PeerConnection::signaling_state() {
2076 RTC_DCHECK_RUN_ON(signaling_thread());
2077 return signaling_state_;
2078 }
2079
2080 PeerConnectionInterface::IceConnectionState
ice_connection_state()2081 PeerConnection::ice_connection_state() {
2082 RTC_DCHECK_RUN_ON(signaling_thread());
2083 return ice_connection_state_;
2084 }
2085
2086 PeerConnectionInterface::IceConnectionState
standardized_ice_connection_state()2087 PeerConnection::standardized_ice_connection_state() {
2088 RTC_DCHECK_RUN_ON(signaling_thread());
2089 return standardized_ice_connection_state_;
2090 }
2091
2092 PeerConnectionInterface::PeerConnectionState
peer_connection_state()2093 PeerConnection::peer_connection_state() {
2094 RTC_DCHECK_RUN_ON(signaling_thread());
2095 return connection_state_;
2096 }
2097
2098 PeerConnectionInterface::IceGatheringState
ice_gathering_state()2099 PeerConnection::ice_gathering_state() {
2100 RTC_DCHECK_RUN_ON(signaling_thread());
2101 return ice_gathering_state_;
2102 }
2103
can_trickle_ice_candidates()2104 absl::optional<bool> PeerConnection::can_trickle_ice_candidates() {
2105 RTC_DCHECK_RUN_ON(signaling_thread());
2106 SessionDescriptionInterface* description = current_remote_description_.get();
2107 if (!description) {
2108 description = pending_remote_description_.get();
2109 }
2110 if (!description) {
2111 return absl::nullopt;
2112 }
2113 // TODO(bugs.webrtc.org/7443): Change to retrieve from session-level option.
2114 if (description->description()->transport_infos().size() < 1) {
2115 return absl::nullopt;
2116 }
2117 return description->description()->transport_infos()[0].description.HasOption(
2118 "trickle");
2119 }
2120
CreateDataChannel(const std::string & label,const DataChannelInit * config)2121 rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel(
2122 const std::string& label,
2123 const DataChannelInit* config) {
2124 RTC_DCHECK_RUN_ON(signaling_thread());
2125 TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel");
2126
2127 bool first_datachannel = !data_channel_controller_.HasDataChannels();
2128
2129 std::unique_ptr<InternalDataChannelInit> internal_config;
2130 if (config) {
2131 internal_config.reset(new InternalDataChannelInit(*config));
2132 }
2133 rtc::scoped_refptr<DataChannelInterface> channel(
2134 data_channel_controller_.InternalCreateDataChannelWithProxy(
2135 label, internal_config.get()));
2136 if (!channel.get()) {
2137 return nullptr;
2138 }
2139
2140 // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or
2141 // the first SCTP DataChannel.
2142 if (data_channel_type() == cricket::DCT_RTP || first_datachannel) {
2143 UpdateNegotiationNeeded();
2144 }
2145 NoteUsageEvent(UsageEvent::DATA_ADDED);
2146 return channel;
2147 }
2148
RestartIce()2149 void PeerConnection::RestartIce() {
2150 RTC_DCHECK_RUN_ON(signaling_thread());
2151 local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions(
2152 current_local_description_.get(), pending_local_description_.get());
2153 UpdateNegotiationNeeded();
2154 }
2155
CreateOffer(CreateSessionDescriptionObserver * observer,const RTCOfferAnswerOptions & options)2156 void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer,
2157 const RTCOfferAnswerOptions& options) {
2158 RTC_DCHECK_RUN_ON(signaling_thread());
2159 // Chain this operation. If asynchronous operations are pending on the chain,
2160 // this operation will be queued to be invoked, otherwise the contents of the
2161 // lambda will execute immediately.
2162 operations_chain_->ChainOperation(
2163 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2164 observer_refptr =
2165 rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
2166 options](std::function<void()> operations_chain_callback) {
2167 // Abort early if |this_weak_ptr| is no longer valid.
2168 if (!this_weak_ptr) {
2169 observer_refptr->OnFailure(
2170 RTCError(RTCErrorType::INTERNAL_ERROR,
2171 "CreateOffer failed because the session was shut down"));
2172 operations_chain_callback();
2173 return;
2174 }
2175 // The operation completes asynchronously when the wrapper is invoked.
2176 rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
2177 observer_wrapper(new rtc::RefCountedObject<
2178 CreateSessionDescriptionObserverOperationWrapper>(
2179 std::move(observer_refptr),
2180 std::move(operations_chain_callback)));
2181 this_weak_ptr->DoCreateOffer(options, observer_wrapper);
2182 });
2183 }
2184
DoCreateOffer(const RTCOfferAnswerOptions & options,rtc::scoped_refptr<CreateSessionDescriptionObserver> observer)2185 void PeerConnection::DoCreateOffer(
2186 const RTCOfferAnswerOptions& options,
2187 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
2188 RTC_DCHECK_RUN_ON(signaling_thread());
2189 TRACE_EVENT0("webrtc", "PeerConnection::DoCreateOffer");
2190
2191 if (!observer) {
2192 RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
2193 return;
2194 }
2195
2196 if (IsClosed()) {
2197 std::string error = "CreateOffer called when PeerConnection is closed.";
2198 RTC_LOG(LS_ERROR) << error;
2199 PostCreateSessionDescriptionFailure(
2200 observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
2201 return;
2202 }
2203
2204 // If a session error has occurred the PeerConnection is in a possibly
2205 // inconsistent state so fail right away.
2206 if (session_error() != SessionError::kNone) {
2207 std::string error_message = GetSessionErrorMsg();
2208 RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message;
2209 PostCreateSessionDescriptionFailure(
2210 observer,
2211 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2212 return;
2213 }
2214
2215 if (!ValidateOfferAnswerOptions(options)) {
2216 std::string error = "CreateOffer called with invalid options.";
2217 RTC_LOG(LS_ERROR) << error;
2218 PostCreateSessionDescriptionFailure(
2219 observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error)));
2220 return;
2221 }
2222
2223 // Legacy handling for offer_to_receive_audio and offer_to_receive_video.
2224 // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions".
2225 if (IsUnifiedPlan()) {
2226 RTCError error = HandleLegacyOfferOptions(options);
2227 if (!error.ok()) {
2228 PostCreateSessionDescriptionFailure(observer, std::move(error));
2229 return;
2230 }
2231 }
2232
2233 cricket::MediaSessionOptions session_options;
2234 GetOptionsForOffer(options, &session_options);
2235 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
2236 }
2237
HandleLegacyOfferOptions(const RTCOfferAnswerOptions & options)2238 RTCError PeerConnection::HandleLegacyOfferOptions(
2239 const RTCOfferAnswerOptions& options) {
2240 RTC_DCHECK(IsUnifiedPlan());
2241
2242 if (options.offer_to_receive_audio == 0) {
2243 RemoveRecvDirectionFromReceivingTransceiversOfType(
2244 cricket::MEDIA_TYPE_AUDIO);
2245 } else if (options.offer_to_receive_audio == 1) {
2246 AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2247 } else if (options.offer_to_receive_audio > 1) {
2248 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
2249 "offer_to_receive_audio > 1 is not supported.");
2250 }
2251
2252 if (options.offer_to_receive_video == 0) {
2253 RemoveRecvDirectionFromReceivingTransceiversOfType(
2254 cricket::MEDIA_TYPE_VIDEO);
2255 } else if (options.offer_to_receive_video == 1) {
2256 AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2257 } else if (options.offer_to_receive_video > 1) {
2258 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
2259 "offer_to_receive_video > 1 is not supported.");
2260 }
2261
2262 return RTCError::OK();
2263 }
2264
RemoveRecvDirectionFromReceivingTransceiversOfType(cricket::MediaType media_type)2265 void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType(
2266 cricket::MediaType media_type) {
2267 for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) {
2268 RtpTransceiverDirection new_direction =
2269 RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false);
2270 if (new_direction != transceiver->direction()) {
2271 RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type)
2272 << " transceiver (MID="
2273 << transceiver->mid().value_or("<not set>") << ") from "
2274 << RtpTransceiverDirectionToString(
2275 transceiver->direction())
2276 << " to "
2277 << RtpTransceiverDirectionToString(new_direction)
2278 << " since CreateOffer specified offer_to_receive=0";
2279 transceiver->internal()->set_direction(new_direction);
2280 }
2281 }
2282 }
2283
AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type)2284 void PeerConnection::AddUpToOneReceivingTransceiverOfType(
2285 cricket::MediaType media_type) {
2286 RTC_DCHECK_RUN_ON(signaling_thread());
2287 if (GetReceivingTransceiversOfType(media_type).empty()) {
2288 RTC_LOG(LS_INFO)
2289 << "Adding one recvonly " << cricket::MediaTypeToString(media_type)
2290 << " transceiver since CreateOffer specified offer_to_receive=1";
2291 RtpTransceiverInit init;
2292 init.direction = RtpTransceiverDirection::kRecvOnly;
2293 AddTransceiver(media_type, nullptr, init,
2294 /*update_negotiation_needed=*/false);
2295 }
2296 }
2297
2298 std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
GetReceivingTransceiversOfType(cricket::MediaType media_type)2299 PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) {
2300 std::vector<
2301 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
2302 receiving_transceivers;
2303 for (const auto& transceiver : transceivers_) {
2304 if (!transceiver->stopped() && transceiver->media_type() == media_type &&
2305 RtpTransceiverDirectionHasRecv(transceiver->direction())) {
2306 receiving_transceivers.push_back(transceiver);
2307 }
2308 }
2309 return receiving_transceivers;
2310 }
2311
CreateAnswer(CreateSessionDescriptionObserver * observer,const RTCOfferAnswerOptions & options)2312 void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer,
2313 const RTCOfferAnswerOptions& options) {
2314 RTC_DCHECK_RUN_ON(signaling_thread());
2315 // Chain this operation. If asynchronous operations are pending on the chain,
2316 // this operation will be queued to be invoked, otherwise the contents of the
2317 // lambda will execute immediately.
2318 operations_chain_->ChainOperation(
2319 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2320 observer_refptr =
2321 rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
2322 options](std::function<void()> operations_chain_callback) {
2323 // Abort early if |this_weak_ptr| is no longer valid.
2324 if (!this_weak_ptr) {
2325 observer_refptr->OnFailure(RTCError(
2326 RTCErrorType::INTERNAL_ERROR,
2327 "CreateAnswer failed because the session was shut down"));
2328 operations_chain_callback();
2329 return;
2330 }
2331 // The operation completes asynchronously when the wrapper is invoked.
2332 rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
2333 observer_wrapper(new rtc::RefCountedObject<
2334 CreateSessionDescriptionObserverOperationWrapper>(
2335 std::move(observer_refptr),
2336 std::move(operations_chain_callback)));
2337 this_weak_ptr->DoCreateAnswer(options, observer_wrapper);
2338 });
2339 }
2340
DoCreateAnswer(const RTCOfferAnswerOptions & options,rtc::scoped_refptr<CreateSessionDescriptionObserver> observer)2341 void PeerConnection::DoCreateAnswer(
2342 const RTCOfferAnswerOptions& options,
2343 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
2344 RTC_DCHECK_RUN_ON(signaling_thread());
2345 TRACE_EVENT0("webrtc", "PeerConnection::DoCreateAnswer");
2346 if (!observer) {
2347 RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
2348 return;
2349 }
2350
2351 // If a session error has occurred the PeerConnection is in a possibly
2352 // inconsistent state so fail right away.
2353 if (session_error() != SessionError::kNone) {
2354 std::string error_message = GetSessionErrorMsg();
2355 RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message;
2356 PostCreateSessionDescriptionFailure(
2357 observer,
2358 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2359 return;
2360 }
2361
2362 if (!(signaling_state_ == kHaveRemoteOffer ||
2363 signaling_state_ == kHaveLocalPrAnswer)) {
2364 std::string error =
2365 "PeerConnection cannot create an answer in a state other than "
2366 "have-remote-offer or have-local-pranswer.";
2367 RTC_LOG(LS_ERROR) << error;
2368 PostCreateSessionDescriptionFailure(
2369 observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
2370 return;
2371 }
2372
2373 // The remote description should be set if we're in the right state.
2374 RTC_DCHECK(remote_description());
2375
2376 if (IsUnifiedPlan()) {
2377 if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) {
2378 RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not "
2379 "supported with Unified Plan semantics. Use the "
2380 "RtpTransceiver API instead.";
2381 }
2382 if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) {
2383 RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not "
2384 "supported with Unified Plan semantics. Use the "
2385 "RtpTransceiver API instead.";
2386 }
2387 }
2388
2389 cricket::MediaSessionOptions session_options;
2390 GetOptionsForAnswer(options, &session_options);
2391
2392 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
2393 }
2394
SetLocalDescription(SetSessionDescriptionObserver * observer,SessionDescriptionInterface * desc_ptr)2395 void PeerConnection::SetLocalDescription(
2396 SetSessionDescriptionObserver* observer,
2397 SessionDescriptionInterface* desc_ptr) {
2398 RTC_DCHECK_RUN_ON(signaling_thread());
2399 // Chain this operation. If asynchronous operations are pending on the chain,
2400 // this operation will be queued to be invoked, otherwise the contents of the
2401 // lambda will execute immediately.
2402 operations_chain_->ChainOperation(
2403 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2404 observer_refptr =
2405 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
2406 desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
2407 std::function<void()> operations_chain_callback) mutable {
2408 // Abort early if |this_weak_ptr| is no longer valid.
2409 if (!this_weak_ptr) {
2410 // For consistency with DoSetLocalDescription(), we DO NOT inform the
2411 // |observer_refptr| that the operation failed in this case.
2412 // TODO(hbos): If/when we process SLD messages in ~PeerConnection,
2413 // the consistent thing would be to inform the observer here.
2414 operations_chain_callback();
2415 return;
2416 }
2417 this_weak_ptr->DoSetLocalDescription(std::move(desc),
2418 std::move(observer_refptr));
2419 // DoSetLocalDescription() is currently implemented as a synchronous
2420 // operation but where the |observer|'s callbacks are invoked
2421 // asynchronously in a post to OnMessage().
2422 // For backwards-compatability reasons, we declare the operation as
2423 // completed here (rather than in OnMessage()). This ensures that
2424 // subsequent offer/answer operations can start immediately (without
2425 // waiting for OnMessage()).
2426 operations_chain_callback();
2427 });
2428 }
2429
SetLocalDescription(SetSessionDescriptionObserver * observer)2430 void PeerConnection::SetLocalDescription(
2431 SetSessionDescriptionObserver* observer) {
2432 RTC_DCHECK_RUN_ON(signaling_thread());
2433 // The |create_sdp_observer| handles performing DoSetLocalDescription() with
2434 // the resulting description as well as completing the operation.
2435 rtc::scoped_refptr<ImplicitCreateSessionDescriptionObserver>
2436 create_sdp_observer(
2437 new rtc::RefCountedObject<ImplicitCreateSessionDescriptionObserver>(
2438 weak_ptr_factory_.GetWeakPtr(),
2439 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer)));
2440 // Chain this operation. If asynchronous operations are pending on the chain,
2441 // this operation will be queued to be invoked, otherwise the contents of the
2442 // lambda will execute immediately.
2443 operations_chain_->ChainOperation(
2444 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2445 create_sdp_observer](std::function<void()> operations_chain_callback) {
2446 // The |create_sdp_observer| is responsible for completing the
2447 // operation.
2448 create_sdp_observer->SetOperationCompleteCallback(
2449 std::move(operations_chain_callback));
2450 // Abort early if |this_weak_ptr| is no longer valid. This triggers the
2451 // same code path as if DoCreateOffer() or DoCreateAnswer() failed.
2452 if (!this_weak_ptr) {
2453 create_sdp_observer->OnFailure(RTCError(
2454 RTCErrorType::INTERNAL_ERROR,
2455 "SetLocalDescription failed because the session was shut down"));
2456 return;
2457 }
2458 switch (this_weak_ptr->signaling_state()) {
2459 case PeerConnectionInterface::kStable:
2460 case PeerConnectionInterface::kHaveLocalOffer:
2461 case PeerConnectionInterface::kHaveRemotePrAnswer:
2462 // TODO(hbos): If [LastCreatedOffer] exists and still represents the
2463 // current state of the system, use that instead of creating another
2464 // offer.
2465 this_weak_ptr->DoCreateOffer(RTCOfferAnswerOptions(),
2466 create_sdp_observer);
2467 break;
2468 case PeerConnectionInterface::kHaveLocalPrAnswer:
2469 case PeerConnectionInterface::kHaveRemoteOffer:
2470 // TODO(hbos): If [LastCreatedAnswer] exists and still represents
2471 // the current state of the system, use that instead of creating
2472 // another answer.
2473 this_weak_ptr->DoCreateAnswer(RTCOfferAnswerOptions(),
2474 create_sdp_observer);
2475 break;
2476 case PeerConnectionInterface::kClosed:
2477 create_sdp_observer->OnFailure(RTCError(
2478 RTCErrorType::INVALID_STATE,
2479 "SetLocalDescription called when PeerConnection is closed."));
2480 break;
2481 }
2482 });
2483 }
2484
DoSetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,rtc::scoped_refptr<SetSessionDescriptionObserver> observer)2485 void PeerConnection::DoSetLocalDescription(
2486 std::unique_ptr<SessionDescriptionInterface> desc,
2487 rtc::scoped_refptr<SetSessionDescriptionObserver> observer) {
2488 RTC_DCHECK_RUN_ON(signaling_thread());
2489 TRACE_EVENT0("webrtc", "PeerConnection::DoSetLocalDescription");
2490
2491 if (!observer) {
2492 RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL.";
2493 return;
2494 }
2495
2496 if (!desc) {
2497 PostSetSessionDescriptionFailure(
2498 observer,
2499 RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL."));
2500 return;
2501 }
2502
2503 // If a session error has occurred the PeerConnection is in a possibly
2504 // inconsistent state so fail right away.
2505 if (session_error() != SessionError::kNone) {
2506 std::string error_message = GetSessionErrorMsg();
2507 RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message;
2508 PostSetSessionDescriptionFailure(
2509 observer,
2510 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2511 return;
2512 }
2513
2514 // For SLD we support only explicit rollback.
2515 if (desc->GetType() == SdpType::kRollback) {
2516 if (IsUnifiedPlan()) {
2517 RTCError error = Rollback(desc->GetType());
2518 if (error.ok()) {
2519 PostSetSessionDescriptionSuccess(observer);
2520 } else {
2521 PostSetSessionDescriptionFailure(observer, std::move(error));
2522 }
2523 } else {
2524 PostSetSessionDescriptionFailure(
2525 observer, RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
2526 "Rollback not supported in Plan B"));
2527 }
2528 return;
2529 }
2530
2531 RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL);
2532 if (!error.ok()) {
2533 std::string error_message = GetSetDescriptionErrorMessage(
2534 cricket::CS_LOCAL, desc->GetType(), error);
2535 RTC_LOG(LS_ERROR) << error_message;
2536 PostSetSessionDescriptionFailure(
2537 observer,
2538 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2539 return;
2540 }
2541
2542 // Grab the description type before moving ownership to ApplyLocalDescription,
2543 // which may destroy it before returning.
2544 const SdpType type = desc->GetType();
2545
2546 error = ApplyLocalDescription(std::move(desc));
2547 // |desc| may be destroyed at this point.
2548
2549 if (!error.ok()) {
2550 // If ApplyLocalDescription fails, the PeerConnection could be in an
2551 // inconsistent state, so act conservatively here and set the session error
2552 // so that future calls to SetLocalDescription/SetRemoteDescription fail.
2553 SetSessionError(SessionError::kContent, error.message());
2554 std::string error_message =
2555 GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error);
2556 RTC_LOG(LS_ERROR) << error_message;
2557 PostSetSessionDescriptionFailure(
2558 observer,
2559 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2560 return;
2561 }
2562 RTC_DCHECK(local_description());
2563
2564 PostSetSessionDescriptionSuccess(observer);
2565
2566 // MaybeStartGathering needs to be called after posting
2567 // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates
2568 // before signaling that SetLocalDescription completed.
2569 transport_controller_->MaybeStartGathering();
2570
2571 if (local_description()->GetType() == SdpType::kAnswer) {
2572 // TODO(deadbeef): We already had to hop to the network thread for
2573 // MaybeStartGathering...
2574 network_thread()->Invoke<void>(
2575 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
2576 port_allocator_.get()));
2577 // Make UMA notes about what was agreed to.
2578 ReportNegotiatedSdpSemantics(*local_description());
2579 }
2580
2581 if (IsUnifiedPlan()) {
2582 bool was_negotiation_needed = is_negotiation_needed_;
2583 UpdateNegotiationNeeded();
2584 if (signaling_state() == kStable && was_negotiation_needed &&
2585 is_negotiation_needed_) {
2586 Observer()->OnRenegotiationNeeded();
2587 }
2588 }
2589
2590 NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED);
2591 }
2592
ApplyLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc)2593 RTCError PeerConnection::ApplyLocalDescription(
2594 std::unique_ptr<SessionDescriptionInterface> desc) {
2595 RTC_DCHECK_RUN_ON(signaling_thread());
2596 RTC_DCHECK(desc);
2597
2598 // Update stats here so that we have the most recent stats for tracks and
2599 // streams that might be removed by updating the session description.
2600 stats_->UpdateStats(kStatsOutputLevelStandard);
2601
2602 // Take a reference to the old local description since it's used below to
2603 // compare against the new local description. When setting the new local
2604 // description, grab ownership of the replaced session description in case it
2605 // is the same as |old_local_description|, to keep it alive for the duration
2606 // of the method.
2607 const SessionDescriptionInterface* old_local_description =
2608 local_description();
2609 std::unique_ptr<SessionDescriptionInterface> replaced_local_description;
2610 SdpType type = desc->GetType();
2611 if (type == SdpType::kAnswer) {
2612 replaced_local_description = pending_local_description_
2613 ? std::move(pending_local_description_)
2614 : std::move(current_local_description_);
2615 current_local_description_ = std::move(desc);
2616 pending_local_description_ = nullptr;
2617 current_remote_description_ = std::move(pending_remote_description_);
2618 } else {
2619 replaced_local_description = std::move(pending_local_description_);
2620 pending_local_description_ = std::move(desc);
2621 }
2622 // The session description to apply now must be accessed by
2623 // |local_description()|.
2624 RTC_DCHECK(local_description());
2625
2626 // Report statistics about any use of simulcast.
2627 ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription,
2628 *local_description()->description());
2629
2630 if (!is_caller_) {
2631 if (remote_description()) {
2632 // Remote description was applied first, so this PC is the callee.
2633 is_caller_ = false;
2634 } else {
2635 // Local description is applied first, so this PC is the caller.
2636 is_caller_ = true;
2637 }
2638 }
2639
2640 RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type);
2641 if (!error.ok()) {
2642 return error;
2643 }
2644
2645 if (IsUnifiedPlan()) {
2646 RTCError error = UpdateTransceiversAndDataChannels(
2647 cricket::CS_LOCAL, *local_description(), old_local_description,
2648 remote_description());
2649 if (!error.ok()) {
2650 return error;
2651 }
2652 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
2653 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
2654 for (const auto& transceiver : transceivers_) {
2655 // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots.
2656 // Note that code paths that don't set MID won't be able to use
2657 // information about DTLS transports.
2658 if (transceiver->mid()) {
2659 auto dtls_transport =
2660 LookupDtlsTransportByMidInternal(*transceiver->mid());
2661 transceiver->internal()->sender_internal()->set_transport(
2662 dtls_transport);
2663 transceiver->internal()->receiver_internal()->set_transport(
2664 dtls_transport);
2665 }
2666
2667 const ContentInfo* content =
2668 FindMediaSectionForTransceiver(transceiver, local_description());
2669 if (!content) {
2670 continue;
2671 }
2672 const MediaContentDescription* media_desc = content->media_description();
2673 // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run
2674 // the following steps:
2675 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
2676 // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and
2677 // transceiver's [[FiredDirection]] slot is either "sendrecv" or
2678 // "recvonly", process the removal of a remote track for the media
2679 // description, given transceiver, removeList, and muteTracks.
2680 if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
2681 (transceiver->internal()->fired_direction() &&
2682 RtpTransceiverDirectionHasRecv(
2683 *transceiver->internal()->fired_direction()))) {
2684 ProcessRemovalOfRemoteTrack(transceiver, &remove_list,
2685 &removed_streams);
2686 }
2687 // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and
2688 // [[FiredDirection]] slots to direction.
2689 transceiver->internal()->set_current_direction(media_desc->direction());
2690 transceiver->internal()->set_fired_direction(media_desc->direction());
2691 }
2692 }
2693 auto observer = Observer();
2694 for (const auto& transceiver : remove_list) {
2695 observer->OnRemoveTrack(transceiver->receiver());
2696 }
2697 for (const auto& stream : removed_streams) {
2698 observer->OnRemoveStream(stream);
2699 }
2700 } else {
2701 // Media channels will be created only when offer is set. These may use new
2702 // transports just created by PushdownTransportDescription.
2703 if (type == SdpType::kOffer) {
2704 // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local
2705 // description is applied. Restore back to old description.
2706 RTCError error = CreateChannels(*local_description()->description());
2707 if (!error.ok()) {
2708 return error;
2709 }
2710 }
2711 // Remove unused channels if MediaContentDescription is rejected.
2712 RemoveUnusedChannels(local_description()->description());
2713 }
2714
2715 error = UpdateSessionState(type, cricket::CS_LOCAL,
2716 local_description()->description());
2717 if (!error.ok()) {
2718 return error;
2719 }
2720
2721 if (remote_description()) {
2722 // Now that we have a local description, we can push down remote candidates.
2723 UseCandidatesInSessionDescription(remote_description());
2724 }
2725
2726 pending_ice_restarts_.clear();
2727 if (session_error() != SessionError::kNone) {
2728 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
2729 }
2730
2731 // If setting the description decided our SSL role, allocate any necessary
2732 // SCTP sids.
2733 rtc::SSLRole role;
2734 if (IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) {
2735 data_channel_controller_.AllocateSctpSids(role);
2736 }
2737
2738 if (IsUnifiedPlan()) {
2739 for (const auto& transceiver : transceivers_) {
2740 const ContentInfo* content =
2741 FindMediaSectionForTransceiver(transceiver, local_description());
2742 if (!content) {
2743 continue;
2744 }
2745 cricket::ChannelInterface* channel = transceiver->internal()->channel();
2746 if (content->rejected || !channel || channel->local_streams().empty()) {
2747 // 0 is a special value meaning "this sender has no associated send
2748 // stream". Need to call this so the sender won't attempt to configure
2749 // a no longer existing stream and run into DCHECKs in the lower
2750 // layers.
2751 transceiver->internal()->sender_internal()->SetSsrc(0);
2752 } else {
2753 // Get the StreamParams from the channel which could generate SSRCs.
2754 const std::vector<StreamParams>& streams = channel->local_streams();
2755 transceiver->internal()->sender_internal()->set_stream_ids(
2756 streams[0].stream_ids());
2757 transceiver->internal()->sender_internal()->SetSsrc(
2758 streams[0].first_ssrc());
2759 }
2760 }
2761 } else {
2762 // Plan B semantics.
2763
2764 // Update state and SSRC of local MediaStreams and DataChannels based on the
2765 // local session description.
2766 const cricket::ContentInfo* audio_content =
2767 GetFirstAudioContent(local_description()->description());
2768 if (audio_content) {
2769 if (audio_content->rejected) {
2770 RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
2771 } else {
2772 const cricket::AudioContentDescription* audio_desc =
2773 audio_content->media_description()->as_audio();
2774 UpdateLocalSenders(audio_desc->streams(), audio_desc->type());
2775 }
2776 }
2777
2778 const cricket::ContentInfo* video_content =
2779 GetFirstVideoContent(local_description()->description());
2780 if (video_content) {
2781 if (video_content->rejected) {
2782 RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
2783 } else {
2784 const cricket::VideoContentDescription* video_desc =
2785 video_content->media_description()->as_video();
2786 UpdateLocalSenders(video_desc->streams(), video_desc->type());
2787 }
2788 }
2789 }
2790
2791 const cricket::ContentInfo* data_content =
2792 GetFirstDataContent(local_description()->description());
2793 if (data_content) {
2794 const cricket::RtpDataContentDescription* rtp_data_desc =
2795 data_content->media_description()->as_rtp_data();
2796 // rtp_data_desc will be null if this is an SCTP description.
2797 if (rtp_data_desc) {
2798 data_channel_controller_.UpdateLocalRtpDataChannels(
2799 rtp_data_desc->streams());
2800 }
2801 }
2802
2803 if (type == SdpType::kAnswer &&
2804 local_ice_credentials_to_replace_->SatisfiesIceRestart(
2805 *current_local_description_)) {
2806 local_ice_credentials_to_replace_->ClearIceCredentials();
2807 }
2808
2809 return RTCError::OK();
2810 }
2811
2812 // The SDP parser used to populate these values by default for the 'content
2813 // name' if an a=mid line was absent.
GetDefaultMidForPlanB(cricket::MediaType media_type)2814 static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) {
2815 switch (media_type) {
2816 case cricket::MEDIA_TYPE_AUDIO:
2817 return cricket::CN_AUDIO;
2818 case cricket::MEDIA_TYPE_VIDEO:
2819 return cricket::CN_VIDEO;
2820 case cricket::MEDIA_TYPE_DATA:
2821 return cricket::CN_DATA;
2822 }
2823 RTC_NOTREACHED();
2824 return "";
2825 }
2826
FillInMissingRemoteMids(cricket::SessionDescription * new_remote_description)2827 void PeerConnection::FillInMissingRemoteMids(
2828 cricket::SessionDescription* new_remote_description) {
2829 RTC_DCHECK(new_remote_description);
2830 const cricket::ContentInfos no_infos;
2831 const cricket::ContentInfos& local_contents =
2832 (local_description() ? local_description()->description()->contents()
2833 : no_infos);
2834 const cricket::ContentInfos& remote_contents =
2835 (remote_description() ? remote_description()->description()->contents()
2836 : no_infos);
2837 for (size_t i = 0; i < new_remote_description->contents().size(); ++i) {
2838 cricket::ContentInfo& content = new_remote_description->contents()[i];
2839 if (!content.name.empty()) {
2840 continue;
2841 }
2842 std::string new_mid;
2843 absl::string_view source_explanation;
2844 if (IsUnifiedPlan()) {
2845 if (i < local_contents.size()) {
2846 new_mid = local_contents[i].name;
2847 source_explanation = "from the matching local media section";
2848 } else if (i < remote_contents.size()) {
2849 new_mid = remote_contents[i].name;
2850 source_explanation = "from the matching previous remote media section";
2851 } else {
2852 new_mid = mid_generator_();
2853 source_explanation = "generated just now";
2854 }
2855 } else {
2856 new_mid = std::string(
2857 GetDefaultMidForPlanB(content.media_description()->type()));
2858 source_explanation = "to match pre-existing behavior";
2859 }
2860 RTC_DCHECK(!new_mid.empty());
2861 content.name = new_mid;
2862 new_remote_description->transport_infos()[i].content_name = new_mid;
2863 RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i
2864 << " is missing an a=mid line. Filling in the value '"
2865 << new_mid << "' " << source_explanation << ".";
2866 }
2867 }
2868
SetRemoteDescription(SetSessionDescriptionObserver * observer,SessionDescriptionInterface * desc_ptr)2869 void PeerConnection::SetRemoteDescription(
2870 SetSessionDescriptionObserver* observer,
2871 SessionDescriptionInterface* desc_ptr) {
2872 RTC_DCHECK_RUN_ON(signaling_thread());
2873 // Chain this operation. If asynchronous operations are pending on the chain,
2874 // this operation will be queued to be invoked, otherwise the contents of the
2875 // lambda will execute immediately.
2876 operations_chain_->ChainOperation(
2877 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2878 observer_refptr =
2879 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
2880 desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
2881 std::function<void()> operations_chain_callback) mutable {
2882 // Abort early if |this_weak_ptr| is no longer valid.
2883 if (!this_weak_ptr) {
2884 // For consistency with SetRemoteDescriptionObserverAdapter, we DO NOT
2885 // inform the |observer_refptr| that the operation failed in this
2886 // case.
2887 // TODO(hbos): If/when we process SRD messages in ~PeerConnection,
2888 // the consistent thing would be to inform the observer here.
2889 operations_chain_callback();
2890 return;
2891 }
2892 this_weak_ptr->DoSetRemoteDescription(
2893 std::move(desc),
2894 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>(
2895 new SetRemoteDescriptionObserverAdapter(
2896 this_weak_ptr.get(), std::move(observer_refptr))));
2897 // DoSetRemoteDescription() is currently implemented as a synchronous
2898 // operation but where SetRemoteDescriptionObserverAdapter ensures that
2899 // the |observer|'s callbacks are invoked asynchronously in a post to
2900 // OnMessage().
2901 // For backwards-compatability reasons, we declare the operation as
2902 // completed here (rather than in OnMessage()). This ensures that
2903 // subsequent offer/answer operations can start immediately (without
2904 // waiting for OnMessage()).
2905 operations_chain_callback();
2906 });
2907 }
2908
SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)2909 void PeerConnection::SetRemoteDescription(
2910 std::unique_ptr<SessionDescriptionInterface> desc,
2911 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
2912 RTC_DCHECK_RUN_ON(signaling_thread());
2913 // Chain this operation. If asynchronous operations are pending on the chain,
2914 // this operation will be queued to be invoked, otherwise the contents of the
2915 // lambda will execute immediately.
2916 operations_chain_->ChainOperation(
2917 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer,
2918 desc = std::move(desc)](
2919 std::function<void()> operations_chain_callback) mutable {
2920 // Abort early if |this_weak_ptr| is no longer valid.
2921 if (!this_weak_ptr) {
2922 // For consistency with DoSetRemoteDescription(), we DO inform the
2923 // |observer| that the operation failed in this case.
2924 observer->OnSetRemoteDescriptionComplete(RTCError(
2925 RTCErrorType::INVALID_STATE,
2926 "Failed to set remote offer sdp: failed because the session was "
2927 "shut down"));
2928 operations_chain_callback();
2929 return;
2930 }
2931 this_weak_ptr->DoSetRemoteDescription(std::move(desc),
2932 std::move(observer));
2933 // DoSetRemoteDescription() is currently implemented as a synchronous
2934 // operation. The |observer| will already have been informed that it
2935 // completed, and we can mark this operation as complete without any
2936 // loose ends.
2937 operations_chain_callback();
2938 });
2939 }
2940
DoSetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)2941 void PeerConnection::DoSetRemoteDescription(
2942 std::unique_ptr<SessionDescriptionInterface> desc,
2943 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
2944 RTC_DCHECK_RUN_ON(signaling_thread());
2945 TRACE_EVENT0("webrtc", "PeerConnection::DoSetRemoteDescription");
2946
2947 if (!observer) {
2948 RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL.";
2949 return;
2950 }
2951
2952 if (!desc) {
2953 observer->OnSetRemoteDescriptionComplete(RTCError(
2954 RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL."));
2955 return;
2956 }
2957
2958 // If a session error has occurred the PeerConnection is in a possibly
2959 // inconsistent state so fail right away.
2960 if (session_error() != SessionError::kNone) {
2961 std::string error_message = GetSessionErrorMsg();
2962 RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message;
2963 observer->OnSetRemoteDescriptionComplete(
2964 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2965 return;
2966 }
2967 if (IsUnifiedPlan()) {
2968 if (configuration_.enable_implicit_rollback) {
2969 if (desc->GetType() == SdpType::kOffer &&
2970 signaling_state() == kHaveLocalOffer) {
2971 Rollback(desc->GetType());
2972 }
2973 }
2974 // Explicit rollback.
2975 if (desc->GetType() == SdpType::kRollback) {
2976 observer->OnSetRemoteDescriptionComplete(Rollback(desc->GetType()));
2977 return;
2978 }
2979 } else if (desc->GetType() == SdpType::kRollback) {
2980 observer->OnSetRemoteDescriptionComplete(
2981 RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
2982 "Rollback not supported in Plan B"));
2983 return;
2984 }
2985 if (desc->GetType() == SdpType::kOffer) {
2986 // Report to UMA the format of the received offer.
2987 ReportSdpFormatReceived(*desc);
2988 }
2989
2990 // Handle remote descriptions missing a=mid lines for interop with legacy end
2991 // points.
2992 FillInMissingRemoteMids(desc->description());
2993
2994 RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE);
2995 if (!error.ok()) {
2996 std::string error_message = GetSetDescriptionErrorMessage(
2997 cricket::CS_REMOTE, desc->GetType(), error);
2998 RTC_LOG(LS_ERROR) << error_message;
2999 observer->OnSetRemoteDescriptionComplete(
3000 RTCError(error.type(), std::move(error_message)));
3001 return;
3002 }
3003
3004 // Grab the description type before moving ownership to
3005 // ApplyRemoteDescription, which may destroy it before returning.
3006 const SdpType type = desc->GetType();
3007
3008 error = ApplyRemoteDescription(std::move(desc));
3009 // |desc| may be destroyed at this point.
3010
3011 if (!error.ok()) {
3012 // If ApplyRemoteDescription fails, the PeerConnection could be in an
3013 // inconsistent state, so act conservatively here and set the session error
3014 // so that future calls to SetLocalDescription/SetRemoteDescription fail.
3015 SetSessionError(SessionError::kContent, error.message());
3016 std::string error_message =
3017 GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error);
3018 RTC_LOG(LS_ERROR) << error_message;
3019 observer->OnSetRemoteDescriptionComplete(
3020 RTCError(error.type(), std::move(error_message)));
3021 return;
3022 }
3023 RTC_DCHECK(remote_description());
3024
3025 if (type == SdpType::kAnswer) {
3026 // TODO(deadbeef): We already had to hop to the network thread for
3027 // MaybeStartGathering...
3028 network_thread()->Invoke<void>(
3029 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
3030 port_allocator_.get()));
3031 // Make UMA notes about what was agreed to.
3032 ReportNegotiatedSdpSemantics(*remote_description());
3033 }
3034
3035 if (IsUnifiedPlan()) {
3036 bool was_negotiation_needed = is_negotiation_needed_;
3037 UpdateNegotiationNeeded();
3038 if (signaling_state() == kStable && was_negotiation_needed &&
3039 is_negotiation_needed_) {
3040 Observer()->OnRenegotiationNeeded();
3041 }
3042 }
3043
3044 observer->OnSetRemoteDescriptionComplete(RTCError::OK());
3045 NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED);
3046 }
3047
ApplyRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc)3048 RTCError PeerConnection::ApplyRemoteDescription(
3049 std::unique_ptr<SessionDescriptionInterface> desc) {
3050 RTC_DCHECK_RUN_ON(signaling_thread());
3051 RTC_DCHECK(desc);
3052
3053 // Update stats here so that we have the most recent stats for tracks and
3054 // streams that might be removed by updating the session description.
3055 stats_->UpdateStats(kStatsOutputLevelStandard);
3056
3057 // Take a reference to the old remote description since it's used below to
3058 // compare against the new remote description. When setting the new remote
3059 // description, grab ownership of the replaced session description in case it
3060 // is the same as |old_remote_description|, to keep it alive for the duration
3061 // of the method.
3062 const SessionDescriptionInterface* old_remote_description =
3063 remote_description();
3064 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
3065 SdpType type = desc->GetType();
3066 if (type == SdpType::kAnswer) {
3067 replaced_remote_description = pending_remote_description_
3068 ? std::move(pending_remote_description_)
3069 : std::move(current_remote_description_);
3070 current_remote_description_ = std::move(desc);
3071 pending_remote_description_ = nullptr;
3072 current_local_description_ = std::move(pending_local_description_);
3073 } else {
3074 replaced_remote_description = std::move(pending_remote_description_);
3075 pending_remote_description_ = std::move(desc);
3076 }
3077 // The session description to apply now must be accessed by
3078 // |remote_description()|.
3079 RTC_DCHECK(remote_description());
3080
3081 // Report statistics about any use of simulcast.
3082 ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription,
3083 *remote_description()->description());
3084
3085 RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type);
3086 if (!error.ok()) {
3087 return error;
3088 }
3089 // Transport and Media channels will be created only when offer is set.
3090 if (IsUnifiedPlan()) {
3091 RTCError error = UpdateTransceiversAndDataChannels(
3092 cricket::CS_REMOTE, *remote_description(), local_description(),
3093 old_remote_description);
3094 if (!error.ok()) {
3095 return error;
3096 }
3097 } else {
3098 // Media channels will be created only when offer is set. These may use new
3099 // transports just created by PushdownTransportDescription.
3100 if (type == SdpType::kOffer) {
3101 // TODO(mallinath) - Handle CreateChannel failure, as new local
3102 // description is applied. Restore back to old description.
3103 RTCError error = CreateChannels(*remote_description()->description());
3104 if (!error.ok()) {
3105 return error;
3106 }
3107 }
3108 // Remove unused channels if MediaContentDescription is rejected.
3109 RemoveUnusedChannels(remote_description()->description());
3110 }
3111
3112 // NOTE: Candidates allocation will be initiated only when
3113 // SetLocalDescription is called.
3114 error = UpdateSessionState(type, cricket::CS_REMOTE,
3115 remote_description()->description());
3116 if (!error.ok()) {
3117 return error;
3118 }
3119
3120 if (local_description() &&
3121 !UseCandidatesInSessionDescription(remote_description())) {
3122 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates);
3123 }
3124
3125 if (old_remote_description) {
3126 for (const cricket::ContentInfo& content :
3127 old_remote_description->description()->contents()) {
3128 // Check if this new SessionDescription contains new ICE ufrag and
3129 // password that indicates the remote peer requests an ICE restart.
3130 // TODO(deadbeef): When we start storing both the current and pending
3131 // remote description, this should reset pending_ice_restarts and compare
3132 // against the current description.
3133 if (CheckForRemoteIceRestart(old_remote_description, remote_description(),
3134 content.name)) {
3135 if (type == SdpType::kOffer) {
3136 pending_ice_restarts_.insert(content.name);
3137 }
3138 } else {
3139 // We retain all received candidates only if ICE is not restarted.
3140 // When ICE is restarted, all previous candidates belong to an old
3141 // generation and should not be kept.
3142 // TODO(deadbeef): This goes against the W3C spec which says the remote
3143 // description should only contain candidates from the last set remote
3144 // description plus any candidates added since then. We should remove
3145 // this once we're sure it won't break anything.
3146 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
3147 old_remote_description, content.name, mutable_remote_description());
3148 }
3149 }
3150 }
3151
3152 if (session_error() != SessionError::kNone) {
3153 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
3154 }
3155
3156 // Set the the ICE connection state to connecting since the connection may
3157 // become writable with peer reflexive candidates before any remote candidate
3158 // is signaled.
3159 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
3160 // is to have a new signal the indicates a change in checking state from the
3161 // transport and expose a new checking() member from transport that can be
3162 // read to determine the current checking state. The existing SignalConnecting
3163 // actually means "gathering candidates", so cannot be be used here.
3164 if (remote_description()->GetType() != SdpType::kOffer &&
3165 remote_description()->number_of_mediasections() > 0u &&
3166 ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) {
3167 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
3168 }
3169
3170 // If setting the description decided our SSL role, allocate any necessary
3171 // SCTP sids.
3172 rtc::SSLRole role;
3173 if (IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) {
3174 data_channel_controller_.AllocateSctpSids(role);
3175 }
3176
3177 if (IsUnifiedPlan()) {
3178 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>
3179 now_receiving_transceivers;
3180 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
3181 std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
3182 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
3183 for (const auto& transceiver : transceivers_) {
3184 const ContentInfo* content =
3185 FindMediaSectionForTransceiver(transceiver, remote_description());
3186 if (!content) {
3187 continue;
3188 }
3189 const MediaContentDescription* media_desc = content->media_description();
3190 RtpTransceiverDirection local_direction =
3191 RtpTransceiverDirectionReversed(media_desc->direction());
3192 // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the
3193 // RTCSessionDescription: Set the associated remote streams given
3194 // transceiver.[[Receiver]], msids, addList, and removeList".
3195 // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription
3196 if (RtpTransceiverDirectionHasRecv(local_direction)) {
3197 std::vector<std::string> stream_ids;
3198 if (!media_desc->streams().empty()) {
3199 // The remote description has signaled the stream IDs.
3200 stream_ids = media_desc->streams()[0].stream_ids();
3201 }
3202 transceiver_stable_states_by_transceivers_[transceiver]
3203 .SetRemoteStreamIdsIfUnset(transceiver->receiver()->stream_ids());
3204
3205 RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name
3206 << " (" << GetStreamIdsString(stream_ids) << ").";
3207 SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(),
3208 stream_ids, &added_streams,
3209 &removed_streams);
3210 // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6
3211 // "Set the RTCSessionDescription: If direction is sendrecv or recvonly,
3212 // and transceiver's current direction is neither sendrecv nor recvonly,
3213 // process the addition of a remote track for the media description.
3214 if (!transceiver->fired_direction() ||
3215 !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) {
3216 RTC_LOG(LS_INFO)
3217 << "Processing the addition of a remote track for MID="
3218 << content->name << ".";
3219 now_receiving_transceivers.push_back(transceiver);
3220 }
3221 }
3222 // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's
3223 // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the
3224 // removal of a remote track for the media description, given transceiver,
3225 // removeList, and muteTracks.
3226 if (!RtpTransceiverDirectionHasRecv(local_direction) &&
3227 (transceiver->fired_direction() &&
3228 RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) {
3229 ProcessRemovalOfRemoteTrack(transceiver, &remove_list,
3230 &removed_streams);
3231 }
3232 // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction.
3233 transceiver->internal()->set_fired_direction(local_direction);
3234 // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run
3235 // the following steps:
3236 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
3237 // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to
3238 // direction.
3239 transceiver->internal()->set_current_direction(local_direction);
3240 // 2.2.8.1.11.[3-6]: Set the transport internal slots.
3241 if (transceiver->mid()) {
3242 auto dtls_transport =
3243 LookupDtlsTransportByMidInternal(*transceiver->mid());
3244 transceiver->internal()->sender_internal()->set_transport(
3245 dtls_transport);
3246 transceiver->internal()->receiver_internal()->set_transport(
3247 dtls_transport);
3248 }
3249 }
3250 // 2.2.8.1.12: If the media description is rejected, and transceiver is
3251 // not already stopped, stop the RTCRtpTransceiver transceiver.
3252 if (content->rejected && !transceiver->stopped()) {
3253 RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name
3254 << " since the media section was rejected.";
3255 transceiver->Stop();
3256 }
3257 if (!content->rejected &&
3258 RtpTransceiverDirectionHasRecv(local_direction)) {
3259 if (!media_desc->streams().empty() &&
3260 media_desc->streams()[0].has_ssrcs()) {
3261 uint32_t ssrc = media_desc->streams()[0].first_ssrc();
3262 transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc);
3263 } else {
3264 transceiver->internal()
3265 ->receiver_internal()
3266 ->SetupUnsignaledMediaChannel();
3267 }
3268 }
3269 }
3270 // Once all processing has finished, fire off callbacks.
3271 auto observer = Observer();
3272 for (const auto& transceiver : now_receiving_transceivers) {
3273 stats_->AddTrack(transceiver->receiver()->track());
3274 observer->OnTrack(transceiver);
3275 observer->OnAddTrack(transceiver->receiver(),
3276 transceiver->receiver()->streams());
3277 }
3278 for (const auto& stream : added_streams) {
3279 observer->OnAddStream(stream);
3280 }
3281 for (const auto& transceiver : remove_list) {
3282 observer->OnRemoveTrack(transceiver->receiver());
3283 }
3284 for (const auto& stream : removed_streams) {
3285 observer->OnRemoveStream(stream);
3286 }
3287 }
3288
3289 const cricket::ContentInfo* audio_content =
3290 GetFirstAudioContent(remote_description()->description());
3291 const cricket::ContentInfo* video_content =
3292 GetFirstVideoContent(remote_description()->description());
3293 const cricket::AudioContentDescription* audio_desc =
3294 GetFirstAudioContentDescription(remote_description()->description());
3295 const cricket::VideoContentDescription* video_desc =
3296 GetFirstVideoContentDescription(remote_description()->description());
3297 const cricket::RtpDataContentDescription* rtp_data_desc =
3298 GetFirstRtpDataContentDescription(remote_description()->description());
3299
3300 // Check if the descriptions include streams, just in case the peer supports
3301 // MSID, but doesn't indicate so with "a=msid-semantic".
3302 if (remote_description()->description()->msid_supported() ||
3303 (audio_desc && !audio_desc->streams().empty()) ||
3304 (video_desc && !video_desc->streams().empty())) {
3305 remote_peer_supports_msid_ = true;
3306 }
3307
3308 // We wait to signal new streams until we finish processing the description,
3309 // since only at that point will new streams have all their tracks.
3310 rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create());
3311
3312 if (!IsUnifiedPlan()) {
3313 // TODO(steveanton): When removing RTP senders/receivers in response to a
3314 // rejected media section, there is some cleanup logic that expects the
3315 // voice/ video channel to still be set. But in this method the voice/video
3316 // channel would have been destroyed by the SetRemoteDescription caller
3317 // above so the cleanup that relies on them fails to run. The RemoveSenders
3318 // calls should be moved to right before the DestroyChannel calls to fix
3319 // this.
3320
3321 // Find all audio rtp streams and create corresponding remote AudioTracks
3322 // and MediaStreams.
3323 if (audio_content) {
3324 if (audio_content->rejected) {
3325 RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
3326 } else {
3327 bool default_audio_track_needed =
3328 !remote_peer_supports_msid_ &&
3329 RtpTransceiverDirectionHasSend(audio_desc->direction());
3330 UpdateRemoteSendersList(GetActiveStreams(audio_desc),
3331 default_audio_track_needed, audio_desc->type(),
3332 new_streams);
3333 }
3334 }
3335
3336 // Find all video rtp streams and create corresponding remote VideoTracks
3337 // and MediaStreams.
3338 if (video_content) {
3339 if (video_content->rejected) {
3340 RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
3341 } else {
3342 bool default_video_track_needed =
3343 !remote_peer_supports_msid_ &&
3344 RtpTransceiverDirectionHasSend(video_desc->direction());
3345 UpdateRemoteSendersList(GetActiveStreams(video_desc),
3346 default_video_track_needed, video_desc->type(),
3347 new_streams);
3348 }
3349 }
3350
3351 // If this is an RTP data transport, update the DataChannels with the
3352 // information from the remote peer.
3353 if (rtp_data_desc) {
3354 data_channel_controller_.UpdateRemoteRtpDataChannels(
3355 GetActiveStreams(rtp_data_desc));
3356 }
3357
3358 // Iterate new_streams and notify the observer about new MediaStreams.
3359 auto observer = Observer();
3360 for (size_t i = 0; i < new_streams->count(); ++i) {
3361 MediaStreamInterface* new_stream = new_streams->at(i);
3362 stats_->AddStream(new_stream);
3363 observer->OnAddStream(
3364 rtc::scoped_refptr<MediaStreamInterface>(new_stream));
3365 }
3366
3367 UpdateEndedRemoteMediaStreams();
3368 }
3369
3370 if (type == SdpType::kAnswer &&
3371 local_ice_credentials_to_replace_->SatisfiesIceRestart(
3372 *current_local_description_)) {
3373 local_ice_credentials_to_replace_->ClearIceCredentials();
3374 }
3375
3376 return RTCError::OK();
3377 }
3378
SetAssociatedRemoteStreams(rtc::scoped_refptr<RtpReceiverInternal> receiver,const std::vector<std::string> & stream_ids,std::vector<rtc::scoped_refptr<MediaStreamInterface>> * added_streams,std::vector<rtc::scoped_refptr<MediaStreamInterface>> * removed_streams)3379 void PeerConnection::SetAssociatedRemoteStreams(
3380 rtc::scoped_refptr<RtpReceiverInternal> receiver,
3381 const std::vector<std::string>& stream_ids,
3382 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
3383 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
3384 std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams;
3385 for (const std::string& stream_id : stream_ids) {
3386 rtc::scoped_refptr<MediaStreamInterface> stream =
3387 remote_streams_->find(stream_id);
3388 if (!stream) {
3389 stream = MediaStreamProxy::Create(rtc::Thread::Current(),
3390 MediaStream::Create(stream_id));
3391 remote_streams_->AddStream(stream);
3392 added_streams->push_back(stream);
3393 }
3394 media_streams.push_back(stream);
3395 }
3396 // Special case: "a=msid" missing, use random stream ID.
3397 if (media_streams.empty() &&
3398 !(remote_description()->description()->msid_signaling() &
3399 cricket::kMsidSignalingMediaSection)) {
3400 if (!missing_msid_default_stream_) {
3401 missing_msid_default_stream_ = MediaStreamProxy::Create(
3402 rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid()));
3403 added_streams->push_back(missing_msid_default_stream_);
3404 }
3405 media_streams.push_back(missing_msid_default_stream_);
3406 }
3407 std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
3408 receiver->streams();
3409 // SetStreams() will add/remove the receiver's track to/from the streams. This
3410 // differs from the spec - the spec uses an "addList" and "removeList" to
3411 // update the stream-track relationships in a later step. We do this earlier,
3412 // changing the order of things, but the end-result is the same.
3413 // TODO(hbos): When we remove remote_streams(), use set_stream_ids()
3414 // instead. https://crbug.com/webrtc/9480
3415 receiver->SetStreams(media_streams);
3416 RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
3417 }
3418
ProcessRemovalOfRemoteTrack(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver,std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> * remove_list,std::vector<rtc::scoped_refptr<MediaStreamInterface>> * removed_streams)3419 void PeerConnection::ProcessRemovalOfRemoteTrack(
3420 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3421 transceiver,
3422 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
3423 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
3424 RTC_DCHECK(transceiver->mid());
3425 RTC_LOG(LS_INFO) << "Processing the removal of a track for MID="
3426 << *transceiver->mid();
3427 std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
3428 transceiver->internal()->receiver_internal()->streams();
3429 // This will remove the remote track from the streams.
3430 transceiver->internal()->receiver_internal()->set_stream_ids({});
3431 remove_list->push_back(transceiver);
3432 RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
3433 }
3434
RemoveRemoteStreamsIfEmpty(const std::vector<rtc::scoped_refptr<MediaStreamInterface>> & remote_streams,std::vector<rtc::scoped_refptr<MediaStreamInterface>> * removed_streams)3435 void PeerConnection::RemoveRemoteStreamsIfEmpty(
3436 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams,
3437 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
3438 // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of
3439 // streams, see if the stream was removed by checking if this was the last
3440 // receiver with that stream ID.
3441 for (const auto& remote_stream : remote_streams) {
3442 if (remote_stream->GetAudioTracks().empty() &&
3443 remote_stream->GetVideoTracks().empty()) {
3444 remote_streams_->RemoveStream(remote_stream);
3445 removed_streams->push_back(remote_stream);
3446 }
3447 }
3448 }
3449
UpdateTransceiversAndDataChannels(cricket::ContentSource source,const SessionDescriptionInterface & new_session,const SessionDescriptionInterface * old_local_description,const SessionDescriptionInterface * old_remote_description)3450 RTCError PeerConnection::UpdateTransceiversAndDataChannels(
3451 cricket::ContentSource source,
3452 const SessionDescriptionInterface& new_session,
3453 const SessionDescriptionInterface* old_local_description,
3454 const SessionDescriptionInterface* old_remote_description) {
3455 RTC_DCHECK(IsUnifiedPlan());
3456
3457 const cricket::ContentGroup* bundle_group = nullptr;
3458 if (new_session.GetType() == SdpType::kOffer) {
3459 auto bundle_group_or_error =
3460 GetEarlyBundleGroup(*new_session.description());
3461 if (!bundle_group_or_error.ok()) {
3462 return bundle_group_or_error.MoveError();
3463 }
3464 bundle_group = bundle_group_or_error.MoveValue();
3465 }
3466
3467 const ContentInfos& new_contents = new_session.description()->contents();
3468 for (size_t i = 0; i < new_contents.size(); ++i) {
3469 const cricket::ContentInfo& new_content = new_contents[i];
3470 cricket::MediaType media_type = new_content.media_description()->type();
3471 mid_generator_.AddKnownId(new_content.name);
3472 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
3473 media_type == cricket::MEDIA_TYPE_VIDEO) {
3474 const cricket::ContentInfo* old_local_content = nullptr;
3475 if (old_local_description &&
3476 i < old_local_description->description()->contents().size()) {
3477 old_local_content =
3478 &old_local_description->description()->contents()[i];
3479 }
3480 const cricket::ContentInfo* old_remote_content = nullptr;
3481 if (old_remote_description &&
3482 i < old_remote_description->description()->contents().size()) {
3483 old_remote_content =
3484 &old_remote_description->description()->contents()[i];
3485 }
3486 auto transceiver_or_error =
3487 AssociateTransceiver(source, new_session.GetType(), i, new_content,
3488 old_local_content, old_remote_content);
3489 if (!transceiver_or_error.ok()) {
3490 return transceiver_or_error.MoveError();
3491 }
3492 auto transceiver = transceiver_or_error.MoveValue();
3493 RTCError error =
3494 UpdateTransceiverChannel(transceiver, new_content, bundle_group);
3495 if (!error.ok()) {
3496 return error;
3497 }
3498 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3499 if (GetDataMid() && new_content.name != *GetDataMid()) {
3500 // Ignore all but the first data section.
3501 RTC_LOG(LS_INFO) << "Ignoring data media section with MID="
3502 << new_content.name;
3503 continue;
3504 }
3505 RTCError error = UpdateDataChannel(source, new_content, bundle_group);
3506 if (!error.ok()) {
3507 return error;
3508 }
3509 } else {
3510 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3511 "Unknown section type.");
3512 }
3513 }
3514
3515 return RTCError::OK();
3516 }
3517
UpdateTransceiverChannel(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver,const cricket::ContentInfo & content,const cricket::ContentGroup * bundle_group)3518 RTCError PeerConnection::UpdateTransceiverChannel(
3519 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3520 transceiver,
3521 const cricket::ContentInfo& content,
3522 const cricket::ContentGroup* bundle_group) {
3523 RTC_DCHECK(IsUnifiedPlan());
3524 RTC_DCHECK(transceiver);
3525 cricket::ChannelInterface* channel = transceiver->internal()->channel();
3526 if (content.rejected) {
3527 if (channel) {
3528 transceiver->internal()->SetChannel(nullptr);
3529 DestroyChannelInterface(channel);
3530 }
3531 } else {
3532 if (!channel) {
3533 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
3534 channel = CreateVoiceChannel(content.name);
3535 } else {
3536 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type());
3537 channel = CreateVideoChannel(content.name);
3538 }
3539 if (!channel) {
3540 LOG_AND_RETURN_ERROR(
3541 RTCErrorType::INTERNAL_ERROR,
3542 "Failed to create channel for mid=" + content.name);
3543 }
3544 transceiver->internal()->SetChannel(channel);
3545 }
3546 }
3547 return RTCError::OK();
3548 }
3549
UpdateDataChannel(cricket::ContentSource source,const cricket::ContentInfo & content,const cricket::ContentGroup * bundle_group)3550 RTCError PeerConnection::UpdateDataChannel(
3551 cricket::ContentSource source,
3552 const cricket::ContentInfo& content,
3553 const cricket::ContentGroup* bundle_group) {
3554 if (data_channel_type() == cricket::DCT_NONE) {
3555 // If data channels are disabled, ignore this media section. CreateAnswer
3556 // will take care of rejecting it.
3557 return RTCError::OK();
3558 }
3559 if (content.rejected) {
3560 RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid();
3561 DestroyDataChannelTransport();
3562 } else {
3563 if (!data_channel_controller_.rtp_data_channel() &&
3564 !data_channel_controller_.data_channel_transport()) {
3565 RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid();
3566 if (!CreateDataChannel(content.name)) {
3567 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3568 "Failed to create data channel.");
3569 }
3570 }
3571 if (source == cricket::CS_REMOTE) {
3572 const MediaContentDescription* data_desc = content.media_description();
3573 if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) {
3574 data_channel_controller_.UpdateRemoteRtpDataChannels(
3575 GetActiveStreams(data_desc));
3576 }
3577 }
3578 }
3579 return RTCError::OK();
3580 }
3581
3582 // This method will extract any send encodings that were sent by the remote
3583 // connection. This is currently only relevant for Simulcast scenario (where
3584 // the number of layers may be communicated by the server).
GetSendEncodingsFromRemoteDescription(const MediaContentDescription & desc)3585 static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription(
3586 const MediaContentDescription& desc) {
3587 if (!desc.HasSimulcast()) {
3588 return {};
3589 }
3590 std::vector<RtpEncodingParameters> result;
3591 const SimulcastDescription& simulcast = desc.simulcast_description();
3592
3593 // This is a remote description, the parameters we are after should appear
3594 // as receive streams.
3595 for (const auto& alternatives : simulcast.receive_layers()) {
3596 RTC_DCHECK(!alternatives.empty());
3597 // There is currently no way to specify or choose from alternatives.
3598 // We will always use the first alternative, which is the most preferred.
3599 const SimulcastLayer& layer = alternatives[0];
3600 RtpEncodingParameters parameters;
3601 parameters.rid = layer.rid;
3602 parameters.active = !layer.is_paused;
3603 result.push_back(parameters);
3604 }
3605
3606 return result;
3607 }
3608
UpdateSimulcastLayerStatusInSender(const std::vector<SimulcastLayer> & layers,rtc::scoped_refptr<RtpSenderInternal> sender)3609 static RTCError UpdateSimulcastLayerStatusInSender(
3610 const std::vector<SimulcastLayer>& layers,
3611 rtc::scoped_refptr<RtpSenderInternal> sender) {
3612 RTC_DCHECK(sender);
3613 RtpParameters parameters = sender->GetParametersInternal();
3614 std::vector<std::string> disabled_layers;
3615
3616 // The simulcast envelope cannot be changed, only the status of the streams.
3617 // So we will iterate over the send encodings rather than the layers.
3618 for (RtpEncodingParameters& encoding : parameters.encodings) {
3619 auto iter = std::find_if(layers.begin(), layers.end(),
3620 [&encoding](const SimulcastLayer& layer) {
3621 return layer.rid == encoding.rid;
3622 });
3623 // A layer that cannot be found may have been removed by the remote party.
3624 if (iter == layers.end()) {
3625 disabled_layers.push_back(encoding.rid);
3626 continue;
3627 }
3628
3629 encoding.active = !iter->is_paused;
3630 }
3631
3632 RTCError result = sender->SetParametersInternal(parameters);
3633 if (result.ok()) {
3634 result = sender->DisableEncodingLayers(disabled_layers);
3635 }
3636
3637 return result;
3638 }
3639
SimulcastIsRejected(const ContentInfo * local_content,const MediaContentDescription & answer_media_desc)3640 static bool SimulcastIsRejected(
3641 const ContentInfo* local_content,
3642 const MediaContentDescription& answer_media_desc) {
3643 bool simulcast_offered = local_content &&
3644 local_content->media_description() &&
3645 local_content->media_description()->HasSimulcast();
3646 bool simulcast_answered = answer_media_desc.HasSimulcast();
3647 bool rids_supported = RtpExtension::FindHeaderExtensionByUri(
3648 answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri);
3649 return simulcast_offered && (!simulcast_answered || !rids_supported);
3650 }
3651
DisableSimulcastInSender(rtc::scoped_refptr<RtpSenderInternal> sender)3652 static RTCError DisableSimulcastInSender(
3653 rtc::scoped_refptr<RtpSenderInternal> sender) {
3654 RTC_DCHECK(sender);
3655 RtpParameters parameters = sender->GetParametersInternal();
3656 if (parameters.encodings.size() <= 1) {
3657 return RTCError::OK();
3658 }
3659
3660 std::vector<std::string> disabled_layers;
3661 std::transform(
3662 parameters.encodings.begin() + 1, parameters.encodings.end(),
3663 std::back_inserter(disabled_layers),
3664 [](const RtpEncodingParameters& encoding) { return encoding.rid; });
3665 return sender->DisableEncodingLayers(disabled_layers);
3666 }
3667
3668 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
AssociateTransceiver(cricket::ContentSource source,SdpType type,size_t mline_index,const ContentInfo & content,const ContentInfo * old_local_content,const ContentInfo * old_remote_content)3669 PeerConnection::AssociateTransceiver(cricket::ContentSource source,
3670 SdpType type,
3671 size_t mline_index,
3672 const ContentInfo& content,
3673 const ContentInfo* old_local_content,
3674 const ContentInfo* old_remote_content) {
3675 RTC_DCHECK(IsUnifiedPlan());
3676 // If this is an offer then the m= section might be recycled. If the m=
3677 // section is being recycled (defined as: rejected in the current local or
3678 // remote description and not rejected in new description), dissociate the
3679 // currently associated RtpTransceiver by setting its mid property to null,
3680 // and discard the mapping between the transceiver and its m= section index.
3681 if (IsMediaSectionBeingRecycled(type, content, old_local_content,
3682 old_remote_content)) {
3683 // We want to dissociate the transceiver that has the rejected mid.
3684 const std::string& old_mid =
3685 (old_local_content && old_local_content->rejected)
3686 ? old_local_content->name
3687 : old_remote_content->name;
3688 auto old_transceiver = GetAssociatedTransceiver(old_mid);
3689 if (old_transceiver) {
3690 RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid
3691 << " since the media section is being recycled.";
3692 old_transceiver->internal()->set_mid(absl::nullopt);
3693 old_transceiver->internal()->set_mline_index(absl::nullopt);
3694 }
3695 }
3696 const MediaContentDescription* media_desc = content.media_description();
3697 auto transceiver = GetAssociatedTransceiver(content.name);
3698 if (source == cricket::CS_LOCAL) {
3699 // Find the RtpTransceiver that corresponds to this m= section, using the
3700 // mapping between transceivers and m= section indices established when
3701 // creating the offer.
3702 if (!transceiver) {
3703 transceiver = GetTransceiverByMLineIndex(mline_index);
3704 }
3705 if (!transceiver) {
3706 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3707 "Unknown transceiver");
3708 }
3709 } else {
3710 RTC_DCHECK_EQ(source, cricket::CS_REMOTE);
3711 // If the m= section is sendrecv or recvonly, and there are RtpTransceivers
3712 // of the same type...
3713 // When simulcast is requested, a transceiver cannot be associated because
3714 // AddTrack cannot be called to initialize it.
3715 if (!transceiver &&
3716 RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
3717 !media_desc->HasSimulcast()) {
3718 transceiver = FindAvailableTransceiverToReceive(media_desc->type());
3719 }
3720 // If no RtpTransceiver was found in the previous step, create one with a
3721 // recvonly direction.
3722 if (!transceiver) {
3723 RTC_LOG(LS_INFO) << "Adding "
3724 << cricket::MediaTypeToString(media_desc->type())
3725 << " transceiver for MID=" << content.name
3726 << " at i=" << mline_index
3727 << " in response to the remote description.";
3728 std::string sender_id = rtc::CreateRandomUuid();
3729 std::vector<RtpEncodingParameters> send_encodings =
3730 GetSendEncodingsFromRemoteDescription(*media_desc);
3731 auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {},
3732 send_encodings);
3733 std::string receiver_id;
3734 if (!media_desc->streams().empty()) {
3735 receiver_id = media_desc->streams()[0].id;
3736 } else {
3737 receiver_id = rtc::CreateRandomUuid();
3738 }
3739 auto receiver = CreateReceiver(media_desc->type(), receiver_id);
3740 transceiver = CreateAndAddTransceiver(sender, receiver);
3741 transceiver->internal()->set_direction(
3742 RtpTransceiverDirection::kRecvOnly);
3743 if (type == SdpType::kOffer) {
3744 transceiver_stable_states_by_transceivers_[transceiver]
3745 .set_newly_created();
3746 }
3747 }
3748 // Check if the offer indicated simulcast but the answer rejected it.
3749 // This can happen when simulcast is not supported on the remote party.
3750 if (SimulcastIsRejected(old_local_content, *media_desc)) {
3751 RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true);
3752 RTCError error =
3753 DisableSimulcastInSender(transceiver->internal()->sender_internal());
3754 if (!error.ok()) {
3755 RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast.";
3756 return std::move(error);
3757 }
3758 }
3759 }
3760 RTC_DCHECK(transceiver);
3761 if (transceiver->media_type() != media_desc->type()) {
3762 LOG_AND_RETURN_ERROR(
3763 RTCErrorType::INVALID_PARAMETER,
3764 "Transceiver type does not match media description type.");
3765 }
3766 if (media_desc->HasSimulcast()) {
3767 std::vector<SimulcastLayer> layers =
3768 source == cricket::CS_LOCAL
3769 ? media_desc->simulcast_description().send_layers().GetAllLayers()
3770 : media_desc->simulcast_description()
3771 .receive_layers()
3772 .GetAllLayers();
3773 RTCError error = UpdateSimulcastLayerStatusInSender(
3774 layers, transceiver->internal()->sender_internal());
3775 if (!error.ok()) {
3776 RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers.";
3777 return std::move(error);
3778 }
3779 }
3780 if (type == SdpType::kOffer) {
3781 bool state_changes = transceiver->internal()->mid() != content.name ||
3782 transceiver->internal()->mline_index() != mline_index;
3783 if (state_changes) {
3784 transceiver_stable_states_by_transceivers_[transceiver]
3785 .SetMSectionIfUnset(transceiver->internal()->mid(),
3786 transceiver->internal()->mline_index());
3787 }
3788 }
3789 // Associate the found or created RtpTransceiver with the m= section by
3790 // setting the value of the RtpTransceiver's mid property to the MID of the m=
3791 // section, and establish a mapping between the transceiver and the index of
3792 // the m= section.
3793 transceiver->internal()->set_mid(content.name);
3794 transceiver->internal()->set_mline_index(mline_index);
3795 return std::move(transceiver);
3796 }
3797
3798 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
GetAssociatedTransceiver(const std::string & mid) const3799 PeerConnection::GetAssociatedTransceiver(const std::string& mid) const {
3800 RTC_DCHECK(IsUnifiedPlan());
3801 for (auto transceiver : transceivers_) {
3802 if (transceiver->mid() == mid) {
3803 return transceiver;
3804 }
3805 }
3806 return nullptr;
3807 }
3808
3809 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
GetTransceiverByMLineIndex(size_t mline_index) const3810 PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const {
3811 RTC_DCHECK(IsUnifiedPlan());
3812 for (auto transceiver : transceivers_) {
3813 if (transceiver->internal()->mline_index() == mline_index) {
3814 return transceiver;
3815 }
3816 }
3817 return nullptr;
3818 }
3819
3820 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
FindAvailableTransceiverToReceive(cricket::MediaType media_type) const3821 PeerConnection::FindAvailableTransceiverToReceive(
3822 cricket::MediaType media_type) const {
3823 RTC_DCHECK(IsUnifiedPlan());
3824 // From JSEP section 5.10 (Applying a Remote Description):
3825 // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of
3826 // the same type that were added to the PeerConnection by addTrack and are not
3827 // associated with any m= section and are not stopped, find the first such
3828 // RtpTransceiver.
3829 for (auto transceiver : transceivers_) {
3830 if (transceiver->media_type() == media_type &&
3831 transceiver->internal()->created_by_addtrack() && !transceiver->mid() &&
3832 !transceiver->stopped()) {
3833 return transceiver;
3834 }
3835 }
3836 return nullptr;
3837 }
3838
FindMediaSectionForTransceiver(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver,const SessionDescriptionInterface * sdesc) const3839 const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver(
3840 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3841 transceiver,
3842 const SessionDescriptionInterface* sdesc) const {
3843 RTC_DCHECK(transceiver);
3844 RTC_DCHECK(sdesc);
3845 if (IsUnifiedPlan()) {
3846 if (!transceiver->internal()->mid()) {
3847 // This transceiver is not associated with a media section yet.
3848 return nullptr;
3849 }
3850 return sdesc->description()->GetContentByName(
3851 *transceiver->internal()->mid());
3852 } else {
3853 // Plan B only allows at most one audio and one video section, so use the
3854 // first media section of that type.
3855 return cricket::GetFirstMediaContent(sdesc->description()->contents(),
3856 transceiver->media_type());
3857 }
3858 }
3859
GetConfiguration()3860 PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() {
3861 RTC_DCHECK_RUN_ON(signaling_thread());
3862 return configuration_;
3863 }
3864
SetConfiguration(const RTCConfiguration & configuration)3865 RTCError PeerConnection::SetConfiguration(
3866 const RTCConfiguration& configuration) {
3867 RTC_DCHECK_RUN_ON(signaling_thread());
3868 TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration");
3869 if (IsClosed()) {
3870 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
3871 "SetConfiguration: PeerConnection is closed.");
3872 }
3873
3874 // According to JSEP, after setLocalDescription, changing the candidate pool
3875 // size is not allowed, and changing the set of ICE servers will not result
3876 // in new candidates being gathered.
3877 if (local_description() && configuration.ice_candidate_pool_size !=
3878 configuration_.ice_candidate_pool_size) {
3879 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
3880 "Can't change candidate pool size after calling "
3881 "SetLocalDescription.");
3882 }
3883
3884 if (local_description() &&
3885 configuration.crypto_options != configuration_.crypto_options) {
3886 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
3887 "Can't change crypto_options after calling "
3888 "SetLocalDescription.");
3889 }
3890
3891 // The simplest (and most future-compatible) way to tell if the config was
3892 // modified in an invalid way is to copy each property we do support
3893 // modifying, then use operator==. There are far more properties we don't
3894 // support modifying than those we do, and more could be added.
3895 RTCConfiguration modified_config = configuration_;
3896 modified_config.servers = configuration.servers;
3897 modified_config.type = configuration.type;
3898 modified_config.ice_candidate_pool_size =
3899 configuration.ice_candidate_pool_size;
3900 modified_config.prune_turn_ports = configuration.prune_turn_ports;
3901 modified_config.turn_port_prune_policy = configuration.turn_port_prune_policy;
3902 modified_config.surface_ice_candidates_on_ice_transport_type_changed =
3903 configuration.surface_ice_candidates_on_ice_transport_type_changed;
3904 modified_config.ice_check_min_interval = configuration.ice_check_min_interval;
3905 modified_config.ice_check_interval_strong_connectivity =
3906 configuration.ice_check_interval_strong_connectivity;
3907 modified_config.ice_check_interval_weak_connectivity =
3908 configuration.ice_check_interval_weak_connectivity;
3909 modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout;
3910 modified_config.ice_unwritable_min_checks =
3911 configuration.ice_unwritable_min_checks;
3912 modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout;
3913 modified_config.stun_candidate_keepalive_interval =
3914 configuration.stun_candidate_keepalive_interval;
3915 modified_config.turn_customizer = configuration.turn_customizer;
3916 modified_config.network_preference = configuration.network_preference;
3917 modified_config.active_reset_srtp_params =
3918 configuration.active_reset_srtp_params;
3919 modified_config.turn_logging_id = configuration.turn_logging_id;
3920 modified_config.allow_codec_switching = configuration.allow_codec_switching;
3921 if (configuration != modified_config) {
3922 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
3923 "Modifying the configuration in an unsupported way.");
3924 }
3925
3926 // Validate the modified configuration.
3927 RTCError validate_error = ValidateConfiguration(modified_config);
3928 if (!validate_error.ok()) {
3929 return validate_error;
3930 }
3931
3932 // Note that this isn't possible through chromium, since it's an unsigned
3933 // short in WebIDL.
3934 if (configuration.ice_candidate_pool_size < 0 ||
3935 configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) {
3936 return RTCError(RTCErrorType::INVALID_RANGE);
3937 }
3938
3939 // Parse ICE servers before hopping to network thread.
3940 cricket::ServerAddresses stun_servers;
3941 std::vector<cricket::RelayServerConfig> turn_servers;
3942 RTCErrorType parse_error =
3943 ParseIceServers(configuration.servers, &stun_servers, &turn_servers);
3944 if (parse_error != RTCErrorType::NONE) {
3945 return RTCError(parse_error);
3946 }
3947 // Add the turn logging id to all turn servers
3948 for (cricket::RelayServerConfig& turn_server : turn_servers) {
3949 turn_server.turn_logging_id = configuration.turn_logging_id;
3950 }
3951
3952 // Note if STUN or TURN servers were supplied.
3953 if (!stun_servers.empty()) {
3954 NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED);
3955 }
3956 if (!turn_servers.empty()) {
3957 NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED);
3958 }
3959
3960 // In theory this shouldn't fail.
3961 if (!network_thread()->Invoke<bool>(
3962 RTC_FROM_HERE,
3963 rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this,
3964 stun_servers, turn_servers, modified_config.type,
3965 modified_config.ice_candidate_pool_size,
3966 modified_config.GetTurnPortPrunePolicy(),
3967 modified_config.turn_customizer,
3968 modified_config.stun_candidate_keepalive_interval,
3969 static_cast<bool>(local_description())))) {
3970 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3971 "Failed to apply configuration to PortAllocator.");
3972 }
3973
3974 // As described in JSEP, calling setConfiguration with new ICE servers or
3975 // candidate policy must set a "needs-ice-restart" bit so that the next offer
3976 // triggers an ICE restart which will pick up the changes.
3977 if (modified_config.servers != configuration_.servers ||
3978 NeedIceRestart(
3979 configuration_.surface_ice_candidates_on_ice_transport_type_changed,
3980 configuration_.type, modified_config.type) ||
3981 modified_config.GetTurnPortPrunePolicy() !=
3982 configuration_.GetTurnPortPrunePolicy()) {
3983 transport_controller_->SetNeedsIceRestartFlag();
3984 }
3985
3986 transport_controller_->SetIceConfig(ParseIceConfig(modified_config));
3987
3988 if (configuration_.active_reset_srtp_params !=
3989 modified_config.active_reset_srtp_params) {
3990 transport_controller_->SetActiveResetSrtpParams(
3991 modified_config.active_reset_srtp_params);
3992 }
3993
3994 if (modified_config.allow_codec_switching.has_value()) {
3995 cricket::VideoMediaChannel* video_channel = video_media_channel();
3996 if (video_channel) {
3997 video_channel->SetVideoCodecSwitchingEnabled(
3998 *modified_config.allow_codec_switching);
3999 }
4000 }
4001
4002 configuration_ = modified_config;
4003 return RTCError::OK();
4004 }
4005
AddIceCandidate(const IceCandidateInterface * ice_candidate)4006 bool PeerConnection::AddIceCandidate(
4007 const IceCandidateInterface* ice_candidate) {
4008 RTC_DCHECK_RUN_ON(signaling_thread());
4009 TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate");
4010 if (IsClosed()) {
4011 RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed.";
4012 NoteAddIceCandidateResult(kAddIceCandidateFailClosed);
4013 return false;
4014 }
4015
4016 if (!remote_description()) {
4017 RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added "
4018 "without any remote session description.";
4019 NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription);
4020 return false;
4021 }
4022
4023 if (!ice_candidate) {
4024 RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null.";
4025 NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate);
4026 return false;
4027 }
4028
4029 bool valid = false;
4030 bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid);
4031 if (!valid) {
4032 NoteAddIceCandidateResult(kAddIceCandidateFailNotValid);
4033 return false;
4034 }
4035
4036 // Add this candidate to the remote session description.
4037 if (!mutable_remote_description()->AddCandidate(ice_candidate)) {
4038 RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used.";
4039 NoteAddIceCandidateResult(kAddIceCandidateFailInAddition);
4040 return false;
4041 }
4042
4043 if (ready) {
4044 bool result = UseCandidate(ice_candidate);
4045 if (result) {
4046 NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED);
4047 NoteAddIceCandidateResult(kAddIceCandidateSuccess);
4048 } else {
4049 NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable);
4050 }
4051 return result;
4052 } else {
4053 RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate.";
4054 NoteAddIceCandidateResult(kAddIceCandidateFailNotReady);
4055 return true;
4056 }
4057 }
4058
AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,std::function<void (RTCError)> callback)4059 void PeerConnection::AddIceCandidate(
4060 std::unique_ptr<IceCandidateInterface> candidate,
4061 std::function<void(RTCError)> callback) {
4062 RTC_DCHECK_RUN_ON(signaling_thread());
4063 // Chain this operation. If asynchronous operations are pending on the chain,
4064 // this operation will be queued to be invoked, otherwise the contents of the
4065 // lambda will execute immediately.
4066 operations_chain_->ChainOperation(
4067 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
4068 candidate = std::move(candidate), callback = std::move(callback)](
4069 std::function<void()> operations_chain_callback) {
4070 if (!this_weak_ptr) {
4071 operations_chain_callback();
4072 callback(RTCError(
4073 RTCErrorType::INVALID_STATE,
4074 "AddIceCandidate failed because the session was shut down"));
4075 return;
4076 }
4077 if (!this_weak_ptr->AddIceCandidate(candidate.get())) {
4078 operations_chain_callback();
4079 // Fail with an error type and message consistent with Chromium.
4080 // TODO(hbos): Fail with error types according to spec.
4081 callback(RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
4082 "Error processing ICE candidate"));
4083 return;
4084 }
4085 operations_chain_callback();
4086 callback(RTCError::OK());
4087 });
4088 }
4089
RemoveIceCandidates(const std::vector<cricket::Candidate> & candidates)4090 bool PeerConnection::RemoveIceCandidates(
4091 const std::vector<cricket::Candidate>& candidates) {
4092 TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates");
4093 RTC_DCHECK_RUN_ON(signaling_thread());
4094 if (IsClosed()) {
4095 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed.";
4096 return false;
4097 }
4098
4099 if (!remote_description()) {
4100 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed "
4101 "without any remote session description.";
4102 return false;
4103 }
4104
4105 if (candidates.empty()) {
4106 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty.";
4107 return false;
4108 }
4109
4110 size_t number_removed =
4111 mutable_remote_description()->RemoveCandidates(candidates);
4112 if (number_removed != candidates.size()) {
4113 RTC_LOG(LS_ERROR)
4114 << "RemoveIceCandidates: Failed to remove candidates. Requested "
4115 << candidates.size() << " but only " << number_removed
4116 << " are removed.";
4117 }
4118
4119 // Remove the candidates from the transport controller.
4120 RTCError error = transport_controller_->RemoveRemoteCandidates(candidates);
4121 if (!error.ok()) {
4122 RTC_LOG(LS_ERROR)
4123 << "RemoveIceCandidates: Error when removing remote candidates: "
4124 << error.message();
4125 }
4126 return true;
4127 }
4128
SetBitrate(const BitrateSettings & bitrate)4129 RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) {
4130 if (!worker_thread()->IsCurrent()) {
4131 return worker_thread()->Invoke<RTCError>(
4132 RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); });
4133 }
4134 RTC_DCHECK_RUN_ON(worker_thread());
4135
4136 const bool has_min = bitrate.min_bitrate_bps.has_value();
4137 const bool has_start = bitrate.start_bitrate_bps.has_value();
4138 const bool has_max = bitrate.max_bitrate_bps.has_value();
4139 if (has_min && *bitrate.min_bitrate_bps < 0) {
4140 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4141 "min_bitrate_bps <= 0");
4142 }
4143 if (has_start) {
4144 if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) {
4145 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4146 "start_bitrate_bps < min_bitrate_bps");
4147 } else if (*bitrate.start_bitrate_bps < 0) {
4148 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4149 "curent_bitrate_bps < 0");
4150 }
4151 }
4152 if (has_max) {
4153 if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) {
4154 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4155 "max_bitrate_bps < start_bitrate_bps");
4156 } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) {
4157 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4158 "max_bitrate_bps < min_bitrate_bps");
4159 } else if (*bitrate.max_bitrate_bps < 0) {
4160 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4161 "max_bitrate_bps < 0");
4162 }
4163 }
4164
4165 RTC_DCHECK(call_.get());
4166 call_->SetClientBitratePreferences(bitrate);
4167
4168 return RTCError::OK();
4169 }
4170
SetAudioPlayout(bool playout)4171 void PeerConnection::SetAudioPlayout(bool playout) {
4172 if (!worker_thread()->IsCurrent()) {
4173 worker_thread()->Invoke<void>(
4174 RTC_FROM_HERE,
4175 rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout));
4176 return;
4177 }
4178 auto audio_state =
4179 factory_->channel_manager()->media_engine()->voice().GetAudioState();
4180 audio_state->SetPlayout(playout);
4181 }
4182
SetAudioRecording(bool recording)4183 void PeerConnection::SetAudioRecording(bool recording) {
4184 if (!worker_thread()->IsCurrent()) {
4185 worker_thread()->Invoke<void>(
4186 RTC_FROM_HERE,
4187 rtc::Bind(&PeerConnection::SetAudioRecording, this, recording));
4188 return;
4189 }
4190 auto audio_state =
4191 factory_->channel_manager()->media_engine()->voice().GetAudioState();
4192 audio_state->SetRecording(recording);
4193 }
4194
4195 std::unique_ptr<rtc::SSLCertificate>
GetRemoteAudioSSLCertificate()4196 PeerConnection::GetRemoteAudioSSLCertificate() {
4197 std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain();
4198 if (!chain || !chain->GetSize()) {
4199 return nullptr;
4200 }
4201 return chain->Get(0).Clone();
4202 }
4203
4204 std::unique_ptr<rtc::SSLCertChain>
GetRemoteAudioSSLCertChain()4205 PeerConnection::GetRemoteAudioSSLCertChain() {
4206 RTC_DCHECK_RUN_ON(signaling_thread());
4207 auto audio_transceiver = GetFirstAudioTransceiver();
4208 if (!audio_transceiver || !audio_transceiver->internal()->channel()) {
4209 return nullptr;
4210 }
4211 return transport_controller_->GetRemoteSSLCertChain(
4212 audio_transceiver->internal()->channel()->transport_name());
4213 }
4214
4215 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
GetFirstAudioTransceiver() const4216 PeerConnection::GetFirstAudioTransceiver() const {
4217 for (auto transceiver : transceivers_) {
4218 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
4219 return transceiver;
4220 }
4221 }
4222 return nullptr;
4223 }
4224
AddAdaptationResource(rtc::scoped_refptr<Resource> resource)4225 void PeerConnection::AddAdaptationResource(
4226 rtc::scoped_refptr<Resource> resource) {
4227 if (!worker_thread()->IsCurrent()) {
4228 return worker_thread()->Invoke<void>(RTC_FROM_HERE, [this, resource]() {
4229 return AddAdaptationResource(resource);
4230 });
4231 }
4232 RTC_DCHECK_RUN_ON(worker_thread());
4233 if (!call_) {
4234 // The PeerConnection has been closed.
4235 return;
4236 }
4237 call_->AddAdaptationResource(resource);
4238 }
4239
StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,int64_t output_period_ms)4240 bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
4241 int64_t output_period_ms) {
4242 return worker_thread()->Invoke<bool>(
4243 RTC_FROM_HERE,
4244 [this, output = std::move(output), output_period_ms]() mutable {
4245 return StartRtcEventLog_w(std::move(output), output_period_ms);
4246 });
4247 }
4248
StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output)4249 bool PeerConnection::StartRtcEventLog(
4250 std::unique_ptr<RtcEventLogOutput> output) {
4251 int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput;
4252 if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) {
4253 output_period_ms = 5000;
4254 }
4255 return StartRtcEventLog(std::move(output), output_period_ms);
4256 }
4257
StopRtcEventLog()4258 void PeerConnection::StopRtcEventLog() {
4259 worker_thread()->Invoke<void>(
4260 RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this));
4261 }
4262
4263 rtc::scoped_refptr<DtlsTransportInterface>
LookupDtlsTransportByMid(const std::string & mid)4264 PeerConnection::LookupDtlsTransportByMid(const std::string& mid) {
4265 RTC_DCHECK_RUN_ON(signaling_thread());
4266 return transport_controller_->LookupDtlsTransportByMid(mid);
4267 }
4268
4269 rtc::scoped_refptr<DtlsTransport>
LookupDtlsTransportByMidInternal(const std::string & mid)4270 PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) {
4271 RTC_DCHECK_RUN_ON(signaling_thread());
4272 return transport_controller_->LookupDtlsTransportByMid(mid);
4273 }
4274
GetSctpTransport() const4275 rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport()
4276 const {
4277 RTC_DCHECK_RUN_ON(signaling_thread());
4278 if (!sctp_mid_s_) {
4279 return nullptr;
4280 }
4281 return transport_controller_->GetSctpTransport(*sctp_mid_s_);
4282 }
4283
local_description() const4284 const SessionDescriptionInterface* PeerConnection::local_description() const {
4285 RTC_DCHECK_RUN_ON(signaling_thread());
4286 return pending_local_description_ ? pending_local_description_.get()
4287 : current_local_description_.get();
4288 }
4289
remote_description() const4290 const SessionDescriptionInterface* PeerConnection::remote_description() const {
4291 RTC_DCHECK_RUN_ON(signaling_thread());
4292 return pending_remote_description_ ? pending_remote_description_.get()
4293 : current_remote_description_.get();
4294 }
4295
current_local_description() const4296 const SessionDescriptionInterface* PeerConnection::current_local_description()
4297 const {
4298 RTC_DCHECK_RUN_ON(signaling_thread());
4299 return current_local_description_.get();
4300 }
4301
current_remote_description() const4302 const SessionDescriptionInterface* PeerConnection::current_remote_description()
4303 const {
4304 RTC_DCHECK_RUN_ON(signaling_thread());
4305 return current_remote_description_.get();
4306 }
4307
pending_local_description() const4308 const SessionDescriptionInterface* PeerConnection::pending_local_description()
4309 const {
4310 RTC_DCHECK_RUN_ON(signaling_thread());
4311 return pending_local_description_.get();
4312 }
4313
pending_remote_description() const4314 const SessionDescriptionInterface* PeerConnection::pending_remote_description()
4315 const {
4316 RTC_DCHECK_RUN_ON(signaling_thread());
4317 return pending_remote_description_.get();
4318 }
4319
Close()4320 void PeerConnection::Close() {
4321 RTC_DCHECK_RUN_ON(signaling_thread());
4322 TRACE_EVENT0("webrtc", "PeerConnection::Close");
4323 // Update stats here so that we have the most recent stats for tracks and
4324 // streams before the channels are closed.
4325 stats_->UpdateStats(kStatsOutputLevelStandard);
4326
4327 ChangeSignalingState(PeerConnectionInterface::kClosed);
4328 NoteUsageEvent(UsageEvent::CLOSE_CALLED);
4329
4330 for (const auto& transceiver : transceivers_) {
4331 transceiver->Stop();
4332 }
4333
4334 // Ensure that all asynchronous stats requests are completed before destroying
4335 // the transport controller below.
4336 if (stats_collector_) {
4337 stats_collector_->WaitForPendingRequest();
4338 }
4339
4340 // Don't destroy BaseChannels until after stats has been cleaned up so that
4341 // the last stats request can still read from the channels.
4342 DestroyAllChannels();
4343
4344 // The event log is used in the transport controller, which must be outlived
4345 // by the former. CreateOffer by the peer connection is implemented
4346 // asynchronously and if the peer connection is closed without resetting the
4347 // WebRTC session description factory, the session description factory would
4348 // call the transport controller.
4349 webrtc_session_desc_factory_.reset();
4350 transport_controller_.reset();
4351
4352 network_thread()->Invoke<void>(
4353 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
4354 port_allocator_.get()));
4355
4356 worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
4357 RTC_DCHECK_RUN_ON(worker_thread());
4358 call_.reset();
4359 // The event log must outlive call (and any other object that uses it).
4360 event_log_.reset();
4361 });
4362 ReportUsagePattern();
4363 // The .h file says that observer can be discarded after close() returns.
4364 // Make sure this is true.
4365 observer_ = nullptr;
4366 }
4367
OnMessage(rtc::Message * msg)4368 void PeerConnection::OnMessage(rtc::Message* msg) {
4369 RTC_DCHECK_RUN_ON(signaling_thread());
4370 switch (msg->message_id) {
4371 case MSG_SET_SESSIONDESCRIPTION_SUCCESS: {
4372 SetSessionDescriptionMsg* param =
4373 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
4374 param->observer->OnSuccess();
4375 delete param;
4376 break;
4377 }
4378 case MSG_SET_SESSIONDESCRIPTION_FAILED: {
4379 SetSessionDescriptionMsg* param =
4380 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
4381 param->observer->OnFailure(std::move(param->error));
4382 delete param;
4383 break;
4384 }
4385 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
4386 CreateSessionDescriptionMsg* param =
4387 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
4388 param->observer->OnFailure(std::move(param->error));
4389 delete param;
4390 break;
4391 }
4392 case MSG_GETSTATS: {
4393 GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata);
4394 StatsReports reports;
4395 stats_->GetStats(param->track, &reports);
4396 param->observer->OnComplete(reports);
4397 delete param;
4398 break;
4399 }
4400 case MSG_REPORT_USAGE_PATTERN: {
4401 ReportUsagePattern();
4402 break;
4403 }
4404 default:
4405 RTC_NOTREACHED() << "Not implemented";
4406 break;
4407 }
4408 }
4409
voice_media_channel() const4410 cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const {
4411 RTC_DCHECK(!IsUnifiedPlan());
4412 auto* voice_channel = static_cast<cricket::VoiceChannel*>(
4413 GetAudioTransceiver()->internal()->channel());
4414 if (voice_channel) {
4415 return voice_channel->media_channel();
4416 } else {
4417 return nullptr;
4418 }
4419 }
4420
video_media_channel() const4421 cricket::VideoMediaChannel* PeerConnection::video_media_channel() const {
4422 RTC_DCHECK(!IsUnifiedPlan());
4423 auto* video_channel = static_cast<cricket::VideoChannel*>(
4424 GetVideoTransceiver()->internal()->channel());
4425 if (video_channel) {
4426 return video_channel->media_channel();
4427 } else {
4428 return nullptr;
4429 }
4430 }
4431
CreateAudioReceiver(MediaStreamInterface * stream,const RtpSenderInfo & remote_sender_info)4432 void PeerConnection::CreateAudioReceiver(
4433 MediaStreamInterface* stream,
4434 const RtpSenderInfo& remote_sender_info) {
4435 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
4436 streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
4437 // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use
4438 // the constructor taking stream IDs instead.
4439 auto* audio_receiver = new AudioRtpReceiver(
4440 worker_thread(), remote_sender_info.sender_id, streams);
4441 audio_receiver->SetMediaChannel(voice_media_channel());
4442 if (remote_sender_info.sender_id == kDefaultAudioSenderId) {
4443 audio_receiver->SetupUnsignaledMediaChannel();
4444 } else {
4445 audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc);
4446 }
4447 auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
4448 signaling_thread(), audio_receiver);
4449 GetAudioTransceiver()->internal()->AddReceiver(receiver);
4450 Observer()->OnAddTrack(receiver, streams);
4451 NoteUsageEvent(UsageEvent::AUDIO_ADDED);
4452 }
4453
CreateVideoReceiver(MediaStreamInterface * stream,const RtpSenderInfo & remote_sender_info)4454 void PeerConnection::CreateVideoReceiver(
4455 MediaStreamInterface* stream,
4456 const RtpSenderInfo& remote_sender_info) {
4457 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
4458 streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
4459 // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use
4460 // the constructor taking stream IDs instead.
4461 auto* video_receiver = new VideoRtpReceiver(
4462 worker_thread(), remote_sender_info.sender_id, streams);
4463 video_receiver->SetMediaChannel(video_media_channel());
4464 if (remote_sender_info.sender_id == kDefaultVideoSenderId) {
4465 video_receiver->SetupUnsignaledMediaChannel();
4466 } else {
4467 video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc);
4468 }
4469 auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
4470 signaling_thread(), video_receiver);
4471 GetVideoTransceiver()->internal()->AddReceiver(receiver);
4472 Observer()->OnAddTrack(receiver, streams);
4473 NoteUsageEvent(UsageEvent::VIDEO_ADDED);
4474 }
4475
4476 // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote
4477 // description.
RemoveAndStopReceiver(const RtpSenderInfo & remote_sender_info)4478 rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver(
4479 const RtpSenderInfo& remote_sender_info) {
4480 auto receiver = FindReceiverById(remote_sender_info.sender_id);
4481 if (!receiver) {
4482 RTC_LOG(LS_WARNING) << "RtpReceiver for track with id "
4483 << remote_sender_info.sender_id << " doesn't exist.";
4484 return nullptr;
4485 }
4486 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
4487 GetAudioTransceiver()->internal()->RemoveReceiver(receiver);
4488 } else {
4489 GetVideoTransceiver()->internal()->RemoveReceiver(receiver);
4490 }
4491 return receiver;
4492 }
4493
AddAudioTrack(AudioTrackInterface * track,MediaStreamInterface * stream)4494 void PeerConnection::AddAudioTrack(AudioTrackInterface* track,
4495 MediaStreamInterface* stream) {
4496 RTC_DCHECK(!IsClosed());
4497 RTC_DCHECK(track);
4498 RTC_DCHECK(stream);
4499 auto sender = FindSenderForTrack(track);
4500 if (sender) {
4501 // We already have a sender for this track, so just change the stream_id
4502 // so that it's correct in the next call to CreateOffer.
4503 sender->internal()->set_stream_ids({stream->id()});
4504 return;
4505 }
4506
4507 // Normal case; we've never seen this track before.
4508 auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track,
4509 {stream->id()}, {});
4510 new_sender->internal()->SetMediaChannel(voice_media_channel());
4511 GetAudioTransceiver()->internal()->AddSender(new_sender);
4512 // If the sender has already been configured in SDP, we call SetSsrc,
4513 // which will connect the sender to the underlying transport. This can
4514 // occur if a local session description that contains the ID of the sender
4515 // is set before AddStream is called. It can also occur if the local
4516 // session description is not changed and RemoveStream is called, and
4517 // later AddStream is called again with the same stream.
4518 const RtpSenderInfo* sender_info =
4519 FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id());
4520 if (sender_info) {
4521 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
4522 }
4523 }
4524
4525 // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around
4526 // indefinitely, when we have unified plan SDP.
RemoveAudioTrack(AudioTrackInterface * track,MediaStreamInterface * stream)4527 void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track,
4528 MediaStreamInterface* stream) {
4529 RTC_DCHECK(!IsClosed());
4530 auto sender = FindSenderForTrack(track);
4531 if (!sender) {
4532 RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
4533 << " doesn't exist.";
4534 return;
4535 }
4536 GetAudioTransceiver()->internal()->RemoveSender(sender);
4537 }
4538
AddVideoTrack(VideoTrackInterface * track,MediaStreamInterface * stream)4539 void PeerConnection::AddVideoTrack(VideoTrackInterface* track,
4540 MediaStreamInterface* stream) {
4541 RTC_DCHECK(!IsClosed());
4542 RTC_DCHECK(track);
4543 RTC_DCHECK(stream);
4544 auto sender = FindSenderForTrack(track);
4545 if (sender) {
4546 // We already have a sender for this track, so just change the stream_id
4547 // so that it's correct in the next call to CreateOffer.
4548 sender->internal()->set_stream_ids({stream->id()});
4549 return;
4550 }
4551
4552 // Normal case; we've never seen this track before.
4553 auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track,
4554 {stream->id()}, {});
4555 new_sender->internal()->SetMediaChannel(video_media_channel());
4556 GetVideoTransceiver()->internal()->AddSender(new_sender);
4557 const RtpSenderInfo* sender_info =
4558 FindSenderInfo(local_video_sender_infos_, stream->id(), track->id());
4559 if (sender_info) {
4560 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
4561 }
4562 }
4563
RemoveVideoTrack(VideoTrackInterface * track,MediaStreamInterface * stream)4564 void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track,
4565 MediaStreamInterface* stream) {
4566 RTC_DCHECK(!IsClosed());
4567 auto sender = FindSenderForTrack(track);
4568 if (!sender) {
4569 RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
4570 << " doesn't exist.";
4571 return;
4572 }
4573 GetVideoTransceiver()->internal()->RemoveSender(sender);
4574 }
4575
SetIceConnectionState(IceConnectionState new_state)4576 void PeerConnection::SetIceConnectionState(IceConnectionState new_state) {
4577 if (ice_connection_state_ == new_state) {
4578 return;
4579 }
4580
4581 // After transitioning to "closed", ignore any additional states from
4582 // TransportController (such as "disconnected").
4583 if (IsClosed()) {
4584 return;
4585 }
4586
4587 RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
4588 << " => " << new_state;
4589 RTC_DCHECK(ice_connection_state_ !=
4590 PeerConnectionInterface::kIceConnectionClosed);
4591
4592 ice_connection_state_ = new_state;
4593 Observer()->OnIceConnectionChange(ice_connection_state_);
4594 }
4595
SetStandardizedIceConnectionState(PeerConnectionInterface::IceConnectionState new_state)4596 void PeerConnection::SetStandardizedIceConnectionState(
4597 PeerConnectionInterface::IceConnectionState new_state) {
4598 if (standardized_ice_connection_state_ == new_state) {
4599 return;
4600 }
4601
4602 if (IsClosed()) {
4603 return;
4604 }
4605
4606 RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState "
4607 << standardized_ice_connection_state_ << " => " << new_state;
4608
4609 standardized_ice_connection_state_ = new_state;
4610 Observer()->OnStandardizedIceConnectionChange(new_state);
4611 }
4612
SetConnectionState(PeerConnectionInterface::PeerConnectionState new_state)4613 void PeerConnection::SetConnectionState(
4614 PeerConnectionInterface::PeerConnectionState new_state) {
4615 if (connection_state_ == new_state)
4616 return;
4617 if (IsClosed())
4618 return;
4619 connection_state_ = new_state;
4620 Observer()->OnConnectionChange(new_state);
4621 }
4622
OnIceGatheringChange(PeerConnectionInterface::IceGatheringState new_state)4623 void PeerConnection::OnIceGatheringChange(
4624 PeerConnectionInterface::IceGatheringState new_state) {
4625 if (IsClosed()) {
4626 return;
4627 }
4628 ice_gathering_state_ = new_state;
4629 Observer()->OnIceGatheringChange(ice_gathering_state_);
4630 }
4631
OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate)4632 void PeerConnection::OnIceCandidate(
4633 std::unique_ptr<IceCandidateInterface> candidate) {
4634 if (IsClosed()) {
4635 return;
4636 }
4637 ReportIceCandidateCollected(candidate->candidate());
4638 Observer()->OnIceCandidate(candidate.get());
4639 }
4640
OnIceCandidateError(const std::string & address,int port,const std::string & url,int error_code,const std::string & error_text)4641 void PeerConnection::OnIceCandidateError(const std::string& address,
4642 int port,
4643 const std::string& url,
4644 int error_code,
4645 const std::string& error_text) {
4646 if (IsClosed()) {
4647 return;
4648 }
4649 Observer()->OnIceCandidateError(address, port, url, error_code, error_text);
4650 // Leftover not to break wpt test during migration to the new API.
4651 Observer()->OnIceCandidateError(address + ":", url, error_code, error_text);
4652 }
4653
OnIceCandidatesRemoved(const std::vector<cricket::Candidate> & candidates)4654 void PeerConnection::OnIceCandidatesRemoved(
4655 const std::vector<cricket::Candidate>& candidates) {
4656 if (IsClosed()) {
4657 return;
4658 }
4659 Observer()->OnIceCandidatesRemoved(candidates);
4660 }
4661
OnSelectedCandidatePairChanged(const cricket::CandidatePairChangeEvent & event)4662 void PeerConnection::OnSelectedCandidatePairChanged(
4663 const cricket::CandidatePairChangeEvent& event) {
4664 if (IsClosed()) {
4665 return;
4666 }
4667
4668 if (event.selected_candidate_pair.local_candidate().type() ==
4669 LOCAL_PORT_TYPE &&
4670 event.selected_candidate_pair.remote_candidate().type() ==
4671 LOCAL_PORT_TYPE) {
4672 NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED);
4673 }
4674
4675 Observer()->OnIceSelectedCandidatePairChanged(event);
4676 }
4677
ChangeSignalingState(PeerConnectionInterface::SignalingState signaling_state)4678 void PeerConnection::ChangeSignalingState(
4679 PeerConnectionInterface::SignalingState signaling_state) {
4680 if (signaling_state_ == signaling_state) {
4681 return;
4682 }
4683 RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: "
4684 << GetSignalingStateString(signaling_state_)
4685 << " New state: "
4686 << GetSignalingStateString(signaling_state);
4687 signaling_state_ = signaling_state;
4688 if (signaling_state == kClosed) {
4689 ice_connection_state_ = kIceConnectionClosed;
4690 Observer()->OnIceConnectionChange(ice_connection_state_);
4691 standardized_ice_connection_state_ =
4692 PeerConnectionInterface::IceConnectionState::kIceConnectionClosed;
4693 connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed;
4694 Observer()->OnConnectionChange(connection_state_);
4695 }
4696 Observer()->OnSignalingChange(signaling_state_);
4697 }
4698
OnAudioTrackAdded(AudioTrackInterface * track,MediaStreamInterface * stream)4699 void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track,
4700 MediaStreamInterface* stream) {
4701 if (IsClosed()) {
4702 return;
4703 }
4704 AddAudioTrack(track, stream);
4705 UpdateNegotiationNeeded();
4706 }
4707
OnAudioTrackRemoved(AudioTrackInterface * track,MediaStreamInterface * stream)4708 void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track,
4709 MediaStreamInterface* stream) {
4710 if (IsClosed()) {
4711 return;
4712 }
4713 RemoveAudioTrack(track, stream);
4714 UpdateNegotiationNeeded();
4715 }
4716
OnVideoTrackAdded(VideoTrackInterface * track,MediaStreamInterface * stream)4717 void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track,
4718 MediaStreamInterface* stream) {
4719 if (IsClosed()) {
4720 return;
4721 }
4722 AddVideoTrack(track, stream);
4723 UpdateNegotiationNeeded();
4724 }
4725
OnVideoTrackRemoved(VideoTrackInterface * track,MediaStreamInterface * stream)4726 void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track,
4727 MediaStreamInterface* stream) {
4728 if (IsClosed()) {
4729 return;
4730 }
4731 RemoveVideoTrack(track, stream);
4732 UpdateNegotiationNeeded();
4733 }
4734
PostSetSessionDescriptionSuccess(SetSessionDescriptionObserver * observer)4735 void PeerConnection::PostSetSessionDescriptionSuccess(
4736 SetSessionDescriptionObserver* observer) {
4737 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
4738 signaling_thread()->Post(RTC_FROM_HERE, this,
4739 MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
4740 }
4741
PostSetSessionDescriptionFailure(SetSessionDescriptionObserver * observer,RTCError && error)4742 void PeerConnection::PostSetSessionDescriptionFailure(
4743 SetSessionDescriptionObserver* observer,
4744 RTCError&& error) {
4745 RTC_DCHECK(!error.ok());
4746 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
4747 msg->error = std::move(error);
4748 signaling_thread()->Post(RTC_FROM_HERE, this,
4749 MSG_SET_SESSIONDESCRIPTION_FAILED, msg);
4750 }
4751
PostCreateSessionDescriptionFailure(CreateSessionDescriptionObserver * observer,RTCError error)4752 void PeerConnection::PostCreateSessionDescriptionFailure(
4753 CreateSessionDescriptionObserver* observer,
4754 RTCError error) {
4755 RTC_DCHECK(!error.ok());
4756 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
4757 msg->error = std::move(error);
4758 signaling_thread()->Post(RTC_FROM_HERE, this,
4759 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
4760 }
4761
GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)4762 void PeerConnection::GetOptionsForOffer(
4763 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
4764 cricket::MediaSessionOptions* session_options) {
4765 ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
4766
4767 if (IsUnifiedPlan()) {
4768 GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options);
4769 } else {
4770 GetOptionsForPlanBOffer(offer_answer_options, session_options);
4771 }
4772
4773 // Intentionally unset the data channel type for RTP data channel with the
4774 // second condition. Otherwise the RTP data channels would be successfully
4775 // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail
4776 // when building with chromium. We want to leave RTP data channels broken, so
4777 // people won't try to use them.
4778 if (data_channel_controller_.HasRtpDataChannels() ||
4779 data_channel_type() != cricket::DCT_RTP) {
4780 session_options->data_channel_type = data_channel_type();
4781 }
4782
4783 // Apply ICE restart flag and renomination flag.
4784 bool ice_restart = offer_answer_options.ice_restart ||
4785 local_ice_credentials_to_replace_->HasIceCredentials();
4786 for (auto& options : session_options->media_description_options) {
4787 options.transport_options.ice_restart = ice_restart;
4788 options.transport_options.enable_ice_renomination =
4789 configuration_.enable_ice_renomination;
4790 }
4791
4792 session_options->rtcp_cname = rtcp_cname_;
4793 session_options->crypto_options = GetCryptoOptions();
4794 session_options->pooled_ice_credentials =
4795 network_thread()->Invoke<std::vector<cricket::IceParameters>>(
4796 RTC_FROM_HERE,
4797 rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials,
4798 port_allocator_.get()));
4799 session_options->offer_extmap_allow_mixed =
4800 configuration_.offer_extmap_allow_mixed;
4801
4802 // Allow fallback for using obsolete SCTP syntax.
4803 // Note that the default in |session_options| is true, while
4804 // the default in |options| is false.
4805 session_options->use_obsolete_sctp_sdp =
4806 offer_answer_options.use_obsolete_sctp_sdp;
4807 }
4808
GetOptionsForPlanBOffer(const PeerConnectionInterface::RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)4809 void PeerConnection::GetOptionsForPlanBOffer(
4810 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
4811 cricket::MediaSessionOptions* session_options) {
4812 // Figure out transceiver directional preferences.
4813 bool send_audio = !GetAudioTransceiver()->internal()->senders().empty();
4814 bool send_video = !GetVideoTransceiver()->internal()->senders().empty();
4815
4816 // By default, generate sendrecv/recvonly m= sections.
4817 bool recv_audio = true;
4818 bool recv_video = true;
4819
4820 // By default, only offer a new m= section if we have media to send with it.
4821 bool offer_new_audio_description = send_audio;
4822 bool offer_new_video_description = send_video;
4823 bool offer_new_data_description = data_channel_controller_.HasDataChannels();
4824
4825 // The "offer_to_receive_X" options allow those defaults to be overridden.
4826 if (offer_answer_options.offer_to_receive_audio !=
4827 RTCOfferAnswerOptions::kUndefined) {
4828 recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
4829 offer_new_audio_description =
4830 offer_new_audio_description ||
4831 (offer_answer_options.offer_to_receive_audio > 0);
4832 }
4833 if (offer_answer_options.offer_to_receive_video !=
4834 RTCOfferAnswerOptions::kUndefined) {
4835 recv_video = (offer_answer_options.offer_to_receive_video > 0);
4836 offer_new_video_description =
4837 offer_new_video_description ||
4838 (offer_answer_options.offer_to_receive_video > 0);
4839 }
4840
4841 absl::optional<size_t> audio_index;
4842 absl::optional<size_t> video_index;
4843 absl::optional<size_t> data_index;
4844 // If a current description exists, generate m= sections in the same order,
4845 // using the first audio/video/data section that appears and rejecting
4846 // extraneous ones.
4847 if (local_description()) {
4848 GenerateMediaDescriptionOptions(
4849 local_description(),
4850 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
4851 RtpTransceiverDirectionFromSendRecv(send_video, recv_video),
4852 &audio_index, &video_index, &data_index, session_options);
4853 }
4854
4855 // Add audio/video/data m= sections to the end if needed.
4856 if (!audio_index && offer_new_audio_description) {
4857 cricket::MediaDescriptionOptions options(
4858 cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO,
4859 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), false);
4860 options.header_extensions =
4861 channel_manager()->GetSupportedAudioRtpHeaderExtensions();
4862 session_options->media_description_options.push_back(options);
4863 audio_index = session_options->media_description_options.size() - 1;
4864 }
4865 if (!video_index && offer_new_video_description) {
4866 cricket::MediaDescriptionOptions options(
4867 cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO,
4868 RtpTransceiverDirectionFromSendRecv(send_video, recv_video), false);
4869 options.header_extensions =
4870 channel_manager()->GetSupportedVideoRtpHeaderExtensions();
4871 session_options->media_description_options.push_back(options);
4872 video_index = session_options->media_description_options.size() - 1;
4873 }
4874 if (!data_index && offer_new_data_description) {
4875 session_options->media_description_options.push_back(
4876 GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA));
4877 data_index = session_options->media_description_options.size() - 1;
4878 }
4879
4880 cricket::MediaDescriptionOptions* audio_media_description_options =
4881 !audio_index ? nullptr
4882 : &session_options->media_description_options[*audio_index];
4883 cricket::MediaDescriptionOptions* video_media_description_options =
4884 !video_index ? nullptr
4885 : &session_options->media_description_options[*video_index];
4886
4887 AddPlanBRtpSenderOptions(GetSendersInternal(),
4888 audio_media_description_options,
4889 video_media_description_options,
4890 offer_answer_options.num_simulcast_layers);
4891 }
4892
4893 static cricket::MediaDescriptionOptions
GetMediaDescriptionOptionsForTransceiver(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver,const std::string & mid)4894 GetMediaDescriptionOptionsForTransceiver(
4895 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
4896 transceiver,
4897 const std::string& mid) {
4898 cricket::MediaDescriptionOptions media_description_options(
4899 transceiver->media_type(), mid, transceiver->direction(),
4900 transceiver->stopped());
4901 media_description_options.codec_preferences =
4902 transceiver->codec_preferences();
4903 media_description_options.header_extensions =
4904 transceiver->HeaderExtensionsToOffer();
4905 // This behavior is specified in JSEP. The gist is that:
4906 // 1. The MSID is included if the RtpTransceiver's direction is sendonly or
4907 // sendrecv.
4908 // 2. If the MSID is included, then it must be included in any subsequent
4909 // offer/answer exactly the same until the RtpTransceiver is stopped.
4910 if (transceiver->stopped() ||
4911 (!RtpTransceiverDirectionHasSend(transceiver->direction()) &&
4912 !transceiver->internal()->has_ever_been_used_to_send())) {
4913 return media_description_options;
4914 }
4915
4916 cricket::SenderOptions sender_options;
4917 sender_options.track_id = transceiver->sender()->id();
4918 sender_options.stream_ids = transceiver->sender()->stream_ids();
4919
4920 // The following sets up RIDs and Simulcast.
4921 // RIDs are included if Simulcast is requested or if any RID was specified.
4922 RtpParameters send_parameters =
4923 transceiver->internal()->sender_internal()->GetParametersInternal();
4924 bool has_rids = std::any_of(send_parameters.encodings.begin(),
4925 send_parameters.encodings.end(),
4926 [](const RtpEncodingParameters& encoding) {
4927 return !encoding.rid.empty();
4928 });
4929
4930 std::vector<RidDescription> send_rids;
4931 SimulcastLayerList send_layers;
4932 for (const RtpEncodingParameters& encoding : send_parameters.encodings) {
4933 if (encoding.rid.empty()) {
4934 continue;
4935 }
4936 send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend));
4937 send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active));
4938 }
4939
4940 if (has_rids) {
4941 sender_options.rids = send_rids;
4942 }
4943
4944 sender_options.simulcast_layers = send_layers;
4945 // When RIDs are configured, we must set num_sim_layers to 0 to.
4946 // Otherwise, num_sim_layers must be 1 because either there is no
4947 // simulcast, or simulcast is acheived by munging the SDP.
4948 sender_options.num_sim_layers = has_rids ? 0 : 1;
4949 media_description_options.sender_options.push_back(sender_options);
4950
4951 return media_description_options;
4952 }
4953
4954 // Returns the ContentInfo at mline index |i|, or null if none exists.
GetContentByIndex(const SessionDescriptionInterface * sdesc,size_t i)4955 static const ContentInfo* GetContentByIndex(
4956 const SessionDescriptionInterface* sdesc,
4957 size_t i) {
4958 if (!sdesc) {
4959 return nullptr;
4960 }
4961 const ContentInfos& contents = sdesc->description()->contents();
4962 return (i < contents.size() ? &contents[i] : nullptr);
4963 }
4964
GetOptionsForUnifiedPlanOffer(const RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)4965 void PeerConnection::GetOptionsForUnifiedPlanOffer(
4966 const RTCOfferAnswerOptions& offer_answer_options,
4967 cricket::MediaSessionOptions* session_options) {
4968 // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial
4969 // Offers) and 5.2.2 (Subsequent Offers).
4970 RTC_DCHECK_EQ(session_options->media_description_options.size(), 0);
4971 const ContentInfos no_infos;
4972 const ContentInfos& local_contents =
4973 (local_description() ? local_description()->description()->contents()
4974 : no_infos);
4975 const ContentInfos& remote_contents =
4976 (remote_description() ? remote_description()->description()->contents()
4977 : no_infos);
4978 // The mline indices that can be recycled. New transceivers should reuse these
4979 // slots first.
4980 std::queue<size_t> recycleable_mline_indices;
4981 // First, go through each media section that exists in either the local or
4982 // remote description and generate a media section in this offer for the
4983 // associated transceiver. If a media section can be recycled, generate a
4984 // default, rejected media section here that can be later overwritten.
4985 for (size_t i = 0;
4986 i < std::max(local_contents.size(), remote_contents.size()); ++i) {
4987 // Either |local_content| or |remote_content| is non-null.
4988 const ContentInfo* local_content =
4989 (i < local_contents.size() ? &local_contents[i] : nullptr);
4990 const ContentInfo* current_local_content =
4991 GetContentByIndex(current_local_description(), i);
4992 const ContentInfo* remote_content =
4993 (i < remote_contents.size() ? &remote_contents[i] : nullptr);
4994 const ContentInfo* current_remote_content =
4995 GetContentByIndex(current_remote_description(), i);
4996 bool had_been_rejected =
4997 (current_local_content && current_local_content->rejected) ||
4998 (current_remote_content && current_remote_content->rejected);
4999 const std::string& mid =
5000 (local_content ? local_content->name : remote_content->name);
5001 cricket::MediaType media_type =
5002 (local_content ? local_content->media_description()->type()
5003 : remote_content->media_description()->type());
5004 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
5005 media_type == cricket::MEDIA_TYPE_VIDEO) {
5006 auto transceiver = GetAssociatedTransceiver(mid);
5007 RTC_CHECK(transceiver);
5008 // A media section is considered eligible for recycling if it is marked as
5009 // rejected in either the current local or current remote description.
5010 if (had_been_rejected && transceiver->stopped()) {
5011 session_options->media_description_options.push_back(
5012 cricket::MediaDescriptionOptions(transceiver->media_type(), mid,
5013 RtpTransceiverDirection::kInactive,
5014 /*stopped=*/true));
5015 recycleable_mline_indices.push(i);
5016 } else {
5017 session_options->media_description_options.push_back(
5018 GetMediaDescriptionOptionsForTransceiver(transceiver, mid));
5019 // CreateOffer shouldn't really cause any state changes in
5020 // PeerConnection, but we need a way to match new transceivers to new
5021 // media sections in SetLocalDescription and JSEP specifies this is done
5022 // by recording the index of the media section generated for the
5023 // transceiver in the offer.
5024 transceiver->internal()->set_mline_index(i);
5025 }
5026 } else {
5027 RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
5028 if (had_been_rejected) {
5029 session_options->media_description_options.push_back(
5030 GetMediaDescriptionOptionsForRejectedData(mid));
5031 } else {
5032 RTC_CHECK(GetDataMid());
5033 if (mid == *GetDataMid()) {
5034 session_options->media_description_options.push_back(
5035 GetMediaDescriptionOptionsForActiveData(mid));
5036 } else {
5037 session_options->media_description_options.push_back(
5038 GetMediaDescriptionOptionsForRejectedData(mid));
5039 }
5040 }
5041 }
5042 }
5043
5044 // Next, look for transceivers that are newly added (that is, are not stopped
5045 // and not associated). Reuse media sections marked as recyclable first,
5046 // otherwise append to the end of the offer. New media sections should be
5047 // added in the order they were added to the PeerConnection.
5048 for (const auto& transceiver : transceivers_) {
5049 if (transceiver->mid() || transceiver->stopped()) {
5050 continue;
5051 }
5052 size_t mline_index;
5053 if (!recycleable_mline_indices.empty()) {
5054 mline_index = recycleable_mline_indices.front();
5055 recycleable_mline_indices.pop();
5056 session_options->media_description_options[mline_index] =
5057 GetMediaDescriptionOptionsForTransceiver(transceiver,
5058 mid_generator_());
5059 } else {
5060 mline_index = session_options->media_description_options.size();
5061 session_options->media_description_options.push_back(
5062 GetMediaDescriptionOptionsForTransceiver(transceiver,
5063 mid_generator_()));
5064 }
5065 // See comment above for why CreateOffer changes the transceiver's state.
5066 transceiver->internal()->set_mline_index(mline_index);
5067 }
5068 // Lastly, add a m-section if we have local data channels and an m section
5069 // does not already exist.
5070 if (!GetDataMid() && data_channel_controller_.HasDataChannels()) {
5071 session_options->media_description_options.push_back(
5072 GetMediaDescriptionOptionsForActiveData(mid_generator_()));
5073 }
5074 }
5075
GetOptionsForAnswer(const RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)5076 void PeerConnection::GetOptionsForAnswer(
5077 const RTCOfferAnswerOptions& offer_answer_options,
5078 cricket::MediaSessionOptions* session_options) {
5079 ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
5080
5081 if (IsUnifiedPlan()) {
5082 GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options);
5083 } else {
5084 GetOptionsForPlanBAnswer(offer_answer_options, session_options);
5085 }
5086
5087 // Intentionally unset the data channel type for RTP data channel. Otherwise
5088 // the RTP data channels would be successfully negotiated by default and the
5089 // unit tests in WebRtcDataBrowserTest will fail when building with chromium.
5090 // We want to leave RTP data channels broken, so people won't try to use them.
5091 if (data_channel_controller_.HasRtpDataChannels() ||
5092 data_channel_type() != cricket::DCT_RTP) {
5093 session_options->data_channel_type = data_channel_type();
5094 }
5095
5096 // Apply ICE renomination flag.
5097 for (auto& options : session_options->media_description_options) {
5098 options.transport_options.enable_ice_renomination =
5099 configuration_.enable_ice_renomination;
5100 }
5101
5102 session_options->rtcp_cname = rtcp_cname_;
5103 session_options->crypto_options = GetCryptoOptions();
5104 session_options->pooled_ice_credentials =
5105 network_thread()->Invoke<std::vector<cricket::IceParameters>>(
5106 RTC_FROM_HERE,
5107 rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials,
5108 port_allocator_.get()));
5109 }
5110
GetOptionsForPlanBAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)5111 void PeerConnection::GetOptionsForPlanBAnswer(
5112 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
5113 cricket::MediaSessionOptions* session_options) {
5114 // Figure out transceiver directional preferences.
5115 bool send_audio = !GetAudioTransceiver()->internal()->senders().empty();
5116 bool send_video = !GetVideoTransceiver()->internal()->senders().empty();
5117
5118 // By default, generate sendrecv/recvonly m= sections. The direction is also
5119 // restricted by the direction in the offer.
5120 bool recv_audio = true;
5121 bool recv_video = true;
5122
5123 // The "offer_to_receive_X" options allow those defaults to be overridden.
5124 if (offer_answer_options.offer_to_receive_audio !=
5125 RTCOfferAnswerOptions::kUndefined) {
5126 recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
5127 }
5128 if (offer_answer_options.offer_to_receive_video !=
5129 RTCOfferAnswerOptions::kUndefined) {
5130 recv_video = (offer_answer_options.offer_to_receive_video > 0);
5131 }
5132
5133 absl::optional<size_t> audio_index;
5134 absl::optional<size_t> video_index;
5135 absl::optional<size_t> data_index;
5136
5137 // Generate m= sections that match those in the offer.
5138 // Note that mediasession.cc will handle intersection our preferred
5139 // direction with the offered direction.
5140 GenerateMediaDescriptionOptions(
5141 remote_description(),
5142 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
5143 RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index,
5144 &video_index, &data_index, session_options);
5145
5146 cricket::MediaDescriptionOptions* audio_media_description_options =
5147 !audio_index ? nullptr
5148 : &session_options->media_description_options[*audio_index];
5149 cricket::MediaDescriptionOptions* video_media_description_options =
5150 !video_index ? nullptr
5151 : &session_options->media_description_options[*video_index];
5152
5153 AddPlanBRtpSenderOptions(GetSendersInternal(),
5154 audio_media_description_options,
5155 video_media_description_options,
5156 offer_answer_options.num_simulcast_layers);
5157 }
5158
GetOptionsForUnifiedPlanAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)5159 void PeerConnection::GetOptionsForUnifiedPlanAnswer(
5160 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
5161 cricket::MediaSessionOptions* session_options) {
5162 // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial
5163 // Answers) and 5.3.2 (Subsequent Answers).
5164 RTC_DCHECK(remote_description());
5165 RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer);
5166 for (const ContentInfo& content :
5167 remote_description()->description()->contents()) {
5168 cricket::MediaType media_type = content.media_description()->type();
5169 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
5170 media_type == cricket::MEDIA_TYPE_VIDEO) {
5171 auto transceiver = GetAssociatedTransceiver(content.name);
5172 RTC_CHECK(transceiver);
5173 session_options->media_description_options.push_back(
5174 GetMediaDescriptionOptionsForTransceiver(transceiver, content.name));
5175 } else {
5176 RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
5177 // Reject all data sections if data channels are disabled.
5178 // Reject a data section if it has already been rejected.
5179 // Reject all data sections except for the first one.
5180 if (data_channel_type() == cricket::DCT_NONE || content.rejected ||
5181 content.name != *GetDataMid()) {
5182 session_options->media_description_options.push_back(
5183 GetMediaDescriptionOptionsForRejectedData(content.name));
5184 } else {
5185 session_options->media_description_options.push_back(
5186 GetMediaDescriptionOptionsForActiveData(content.name));
5187 }
5188 }
5189 }
5190 }
5191
GenerateMediaDescriptionOptions(const SessionDescriptionInterface * session_desc,RtpTransceiverDirection audio_direction,RtpTransceiverDirection video_direction,absl::optional<size_t> * audio_index,absl::optional<size_t> * video_index,absl::optional<size_t> * data_index,cricket::MediaSessionOptions * session_options)5192 void PeerConnection::GenerateMediaDescriptionOptions(
5193 const SessionDescriptionInterface* session_desc,
5194 RtpTransceiverDirection audio_direction,
5195 RtpTransceiverDirection video_direction,
5196 absl::optional<size_t>* audio_index,
5197 absl::optional<size_t>* video_index,
5198 absl::optional<size_t>* data_index,
5199 cricket::MediaSessionOptions* session_options) {
5200 for (const cricket::ContentInfo& content :
5201 session_desc->description()->contents()) {
5202 if (IsAudioContent(&content)) {
5203 // If we already have an audio m= section, reject this extra one.
5204 if (*audio_index) {
5205 session_options->media_description_options.push_back(
5206 cricket::MediaDescriptionOptions(
5207 cricket::MEDIA_TYPE_AUDIO, content.name,
5208 RtpTransceiverDirection::kInactive, /*stopped=*/true));
5209 } else {
5210 bool stopped = (audio_direction == RtpTransceiverDirection::kInactive);
5211 session_options->media_description_options.push_back(
5212 cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO,
5213 content.name, audio_direction,
5214 stopped));
5215 *audio_index = session_options->media_description_options.size() - 1;
5216 }
5217 session_options->media_description_options.back().header_extensions =
5218 channel_manager()->GetSupportedAudioRtpHeaderExtensions();
5219 } else if (IsVideoContent(&content)) {
5220 // If we already have an video m= section, reject this extra one.
5221 if (*video_index) {
5222 session_options->media_description_options.push_back(
5223 cricket::MediaDescriptionOptions(
5224 cricket::MEDIA_TYPE_VIDEO, content.name,
5225 RtpTransceiverDirection::kInactive, /*stopped=*/true));
5226 } else {
5227 bool stopped = (video_direction == RtpTransceiverDirection::kInactive);
5228 session_options->media_description_options.push_back(
5229 cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO,
5230 content.name, video_direction,
5231 stopped));
5232 *video_index = session_options->media_description_options.size() - 1;
5233 }
5234 session_options->media_description_options.back().header_extensions =
5235 channel_manager()->GetSupportedVideoRtpHeaderExtensions();
5236 } else {
5237 RTC_DCHECK(IsDataContent(&content));
5238 // If we already have an data m= section, reject this extra one.
5239 if (*data_index) {
5240 session_options->media_description_options.push_back(
5241 GetMediaDescriptionOptionsForRejectedData(content.name));
5242 } else {
5243 session_options->media_description_options.push_back(
5244 GetMediaDescriptionOptionsForActiveData(content.name));
5245 *data_index = session_options->media_description_options.size() - 1;
5246 }
5247 }
5248 }
5249 }
5250
5251 cricket::MediaDescriptionOptions
GetMediaDescriptionOptionsForActiveData(const std::string & mid) const5252 PeerConnection::GetMediaDescriptionOptionsForActiveData(
5253 const std::string& mid) const {
5254 // Direction for data sections is meaningless, but legacy endpoints might
5255 // expect sendrecv.
5256 cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
5257 RtpTransceiverDirection::kSendRecv,
5258 /*stopped=*/false);
5259 AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(),
5260 &options);
5261 return options;
5262 }
5263
5264 cricket::MediaDescriptionOptions
GetMediaDescriptionOptionsForRejectedData(const std::string & mid) const5265 PeerConnection::GetMediaDescriptionOptionsForRejectedData(
5266 const std::string& mid) const {
5267 cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
5268 RtpTransceiverDirection::kInactive,
5269 /*stopped=*/true);
5270 AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(),
5271 &options);
5272 return options;
5273 }
5274
GetDataMid() const5275 absl::optional<std::string> PeerConnection::GetDataMid() const {
5276 switch (data_channel_type()) {
5277 case cricket::DCT_RTP:
5278 if (!data_channel_controller_.rtp_data_channel()) {
5279 return absl::nullopt;
5280 }
5281 return data_channel_controller_.rtp_data_channel()->content_name();
5282 case cricket::DCT_SCTP:
5283 return sctp_mid_s_;
5284 default:
5285 return absl::nullopt;
5286 }
5287 }
5288
RemoveSenders(cricket::MediaType media_type)5289 void PeerConnection::RemoveSenders(cricket::MediaType media_type) {
5290 UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type);
5291 UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false,
5292 media_type, nullptr);
5293 }
5294
UpdateRemoteSendersList(const cricket::StreamParamsVec & streams,bool default_sender_needed,cricket::MediaType media_type,StreamCollection * new_streams)5295 void PeerConnection::UpdateRemoteSendersList(
5296 const cricket::StreamParamsVec& streams,
5297 bool default_sender_needed,
5298 cricket::MediaType media_type,
5299 StreamCollection* new_streams) {
5300 RTC_DCHECK(!IsUnifiedPlan());
5301
5302 std::vector<RtpSenderInfo>* current_senders =
5303 GetRemoteSenderInfos(media_type);
5304
5305 // Find removed senders. I.e., senders where the sender id or ssrc don't match
5306 // the new StreamParam.
5307 for (auto sender_it = current_senders->begin();
5308 sender_it != current_senders->end();
5309 /* incremented manually */) {
5310 const RtpSenderInfo& info = *sender_it;
5311 const cricket::StreamParams* params =
5312 cricket::GetStreamBySsrc(streams, info.first_ssrc);
5313 std::string params_stream_id;
5314 if (params) {
5315 params_stream_id =
5316 (!params->first_stream_id().empty() ? params->first_stream_id()
5317 : kDefaultStreamId);
5318 }
5319 bool sender_exists = params && params->id == info.sender_id &&
5320 params_stream_id == info.stream_id;
5321 // If this is a default track, and we still need it, don't remove it.
5322 if ((info.stream_id == kDefaultStreamId && default_sender_needed) ||
5323 sender_exists) {
5324 ++sender_it;
5325 } else {
5326 OnRemoteSenderRemoved(info, media_type);
5327 sender_it = current_senders->erase(sender_it);
5328 }
5329 }
5330
5331 // Find new and active senders.
5332 for (const cricket::StreamParams& params : streams) {
5333 if (!params.has_ssrcs()) {
5334 // The remote endpoint has streams, but didn't signal ssrcs. For an active
5335 // sender, this means it is coming from a Unified Plan endpoint,so we just
5336 // create a default.
5337 default_sender_needed = true;
5338 break;
5339 }
5340
5341 // |params.id| is the sender id and the stream id uses the first of
5342 // |params.stream_ids|. The remote description could come from a Unified
5343 // Plan endpoint, with multiple or no stream_ids() signaled. Since this is
5344 // not supported in Plan B, we just take the first here and create the
5345 // default stream ID if none is specified.
5346 const std::string& stream_id =
5347 (!params.first_stream_id().empty() ? params.first_stream_id()
5348 : kDefaultStreamId);
5349 const std::string& sender_id = params.id;
5350 uint32_t ssrc = params.first_ssrc();
5351
5352 rtc::scoped_refptr<MediaStreamInterface> stream =
5353 remote_streams_->find(stream_id);
5354 if (!stream) {
5355 // This is a new MediaStream. Create a new remote MediaStream.
5356 stream = MediaStreamProxy::Create(rtc::Thread::Current(),
5357 MediaStream::Create(stream_id));
5358 remote_streams_->AddStream(stream);
5359 new_streams->AddStream(stream);
5360 }
5361
5362 const RtpSenderInfo* sender_info =
5363 FindSenderInfo(*current_senders, stream_id, sender_id);
5364 if (!sender_info) {
5365 current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
5366 OnRemoteSenderAdded(current_senders->back(), media_type);
5367 }
5368 }
5369
5370 // Add default sender if necessary.
5371 if (default_sender_needed) {
5372 rtc::scoped_refptr<MediaStreamInterface> default_stream =
5373 remote_streams_->find(kDefaultStreamId);
5374 if (!default_stream) {
5375 // Create the new default MediaStream.
5376 default_stream = MediaStreamProxy::Create(
5377 rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId));
5378 remote_streams_->AddStream(default_stream);
5379 new_streams->AddStream(default_stream);
5380 }
5381 std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO)
5382 ? kDefaultAudioSenderId
5383 : kDefaultVideoSenderId;
5384 const RtpSenderInfo* default_sender_info =
5385 FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id);
5386 if (!default_sender_info) {
5387 current_senders->push_back(
5388 RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0));
5389 OnRemoteSenderAdded(current_senders->back(), media_type);
5390 }
5391 }
5392 }
5393
OnRemoteSenderAdded(const RtpSenderInfo & sender_info,cricket::MediaType media_type)5394 void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
5395 cricket::MediaType media_type) {
5396 RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type)
5397 << " receiver for track_id=" << sender_info.sender_id
5398 << " and stream_id=" << sender_info.stream_id;
5399
5400 MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id);
5401 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
5402 CreateAudioReceiver(stream, sender_info);
5403 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
5404 CreateVideoReceiver(stream, sender_info);
5405 } else {
5406 RTC_NOTREACHED() << "Invalid media type";
5407 }
5408 }
5409
OnRemoteSenderRemoved(const RtpSenderInfo & sender_info,cricket::MediaType media_type)5410 void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
5411 cricket::MediaType media_type) {
5412 RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type)
5413 << " receiver for track_id=" << sender_info.sender_id
5414 << " and stream_id=" << sender_info.stream_id;
5415
5416 MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id);
5417
5418 rtc::scoped_refptr<RtpReceiverInterface> receiver;
5419 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
5420 // When the MediaEngine audio channel is destroyed, the RemoteAudioSource
5421 // will be notified which will end the AudioRtpReceiver::track().
5422 receiver = RemoveAndStopReceiver(sender_info);
5423 rtc::scoped_refptr<AudioTrackInterface> audio_track =
5424 stream->FindAudioTrack(sender_info.sender_id);
5425 if (audio_track) {
5426 stream->RemoveTrack(audio_track);
5427 }
5428 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
5429 // Stopping or destroying a VideoRtpReceiver will end the
5430 // VideoRtpReceiver::track().
5431 receiver = RemoveAndStopReceiver(sender_info);
5432 rtc::scoped_refptr<VideoTrackInterface> video_track =
5433 stream->FindVideoTrack(sender_info.sender_id);
5434 if (video_track) {
5435 // There's no guarantee the track is still available, e.g. the track may
5436 // have been removed from the stream by an application.
5437 stream->RemoveTrack(video_track);
5438 }
5439 } else {
5440 RTC_NOTREACHED() << "Invalid media type";
5441 }
5442 if (receiver) {
5443 Observer()->OnRemoveTrack(receiver);
5444 }
5445 }
5446
UpdateEndedRemoteMediaStreams()5447 void PeerConnection::UpdateEndedRemoteMediaStreams() {
5448 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove;
5449 for (size_t i = 0; i < remote_streams_->count(); ++i) {
5450 MediaStreamInterface* stream = remote_streams_->at(i);
5451 if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) {
5452 streams_to_remove.push_back(stream);
5453 }
5454 }
5455
5456 for (auto& stream : streams_to_remove) {
5457 remote_streams_->RemoveStream(stream);
5458 Observer()->OnRemoveStream(std::move(stream));
5459 }
5460 }
5461
UpdateLocalSenders(const std::vector<cricket::StreamParams> & streams,cricket::MediaType media_type)5462 void PeerConnection::UpdateLocalSenders(
5463 const std::vector<cricket::StreamParams>& streams,
5464 cricket::MediaType media_type) {
5465 std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type);
5466
5467 // Find removed tracks. I.e., tracks where the track id, stream id or ssrc
5468 // don't match the new StreamParam.
5469 for (auto sender_it = current_senders->begin();
5470 sender_it != current_senders->end();
5471 /* incremented manually */) {
5472 const RtpSenderInfo& info = *sender_it;
5473 const cricket::StreamParams* params =
5474 cricket::GetStreamBySsrc(streams, info.first_ssrc);
5475 if (!params || params->id != info.sender_id ||
5476 params->first_stream_id() != info.stream_id) {
5477 OnLocalSenderRemoved(info, media_type);
5478 sender_it = current_senders->erase(sender_it);
5479 } else {
5480 ++sender_it;
5481 }
5482 }
5483
5484 // Find new and active senders.
5485 for (const cricket::StreamParams& params : streams) {
5486 // The sync_label is the MediaStream label and the |stream.id| is the
5487 // sender id.
5488 const std::string& stream_id = params.first_stream_id();
5489 const std::string& sender_id = params.id;
5490 uint32_t ssrc = params.first_ssrc();
5491 const RtpSenderInfo* sender_info =
5492 FindSenderInfo(*current_senders, stream_id, sender_id);
5493 if (!sender_info) {
5494 current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
5495 OnLocalSenderAdded(current_senders->back(), media_type);
5496 }
5497 }
5498 }
5499
OnLocalSenderAdded(const RtpSenderInfo & sender_info,cricket::MediaType media_type)5500 void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info,
5501 cricket::MediaType media_type) {
5502 RTC_DCHECK(!IsUnifiedPlan());
5503 auto sender = FindSenderById(sender_info.sender_id);
5504 if (!sender) {
5505 RTC_LOG(LS_WARNING) << "An unknown RtpSender with id "
5506 << sender_info.sender_id
5507 << " has been configured in the local description.";
5508 return;
5509 }
5510
5511 if (sender->media_type() != media_type) {
5512 RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local"
5513 " description with an unexpected media type.";
5514 return;
5515 }
5516
5517 sender->internal()->set_stream_ids({sender_info.stream_id});
5518 sender->internal()->SetSsrc(sender_info.first_ssrc);
5519 }
5520
OnLocalSenderRemoved(const RtpSenderInfo & sender_info,cricket::MediaType media_type)5521 void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
5522 cricket::MediaType media_type) {
5523 auto sender = FindSenderById(sender_info.sender_id);
5524 if (!sender) {
5525 // This is the normal case. I.e., RemoveStream has been called and the
5526 // SessionDescriptions has been renegotiated.
5527 return;
5528 }
5529
5530 // A sender has been removed from the SessionDescription but it's still
5531 // associated with the PeerConnection. This only occurs if the SDP doesn't
5532 // match with the calls to CreateSender, AddStream and RemoveStream.
5533 if (sender->media_type() != media_type) {
5534 RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local"
5535 " description with an unexpected media type.";
5536 return;
5537 }
5538
5539 sender->internal()->SetSsrc(0);
5540 }
5541
OnSctpDataChannelClosed(DataChannelInterface * channel)5542 void PeerConnection::OnSctpDataChannelClosed(DataChannelInterface* channel) {
5543 // Since data_channel_controller doesn't do signals, this
5544 // signal is relayed here.
5545 data_channel_controller_.OnSctpDataChannelClosed(
5546 static_cast<SctpDataChannel*>(channel));
5547 }
5548
5549 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
GetAudioTransceiver() const5550 PeerConnection::GetAudioTransceiver() const {
5551 // This method only works with Plan B SDP, where there is a single
5552 // audio/video transceiver.
5553 RTC_DCHECK(!IsUnifiedPlan());
5554 for (auto transceiver : transceivers_) {
5555 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
5556 return transceiver;
5557 }
5558 }
5559 RTC_NOTREACHED();
5560 return nullptr;
5561 }
5562
5563 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
GetVideoTransceiver() const5564 PeerConnection::GetVideoTransceiver() const {
5565 // This method only works with Plan B SDP, where there is a single
5566 // audio/video transceiver.
5567 RTC_DCHECK(!IsUnifiedPlan());
5568 for (auto transceiver : transceivers_) {
5569 if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
5570 return transceiver;
5571 }
5572 }
5573 RTC_NOTREACHED();
5574 return nullptr;
5575 }
5576
5577 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
FindSenderForTrack(MediaStreamTrackInterface * track) const5578 PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const {
5579 for (const auto& transceiver : transceivers_) {
5580 for (auto sender : transceiver->internal()->senders()) {
5581 if (sender->track() == track) {
5582 return sender;
5583 }
5584 }
5585 }
5586 return nullptr;
5587 }
5588
5589 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
FindSenderById(const std::string & sender_id) const5590 PeerConnection::FindSenderById(const std::string& sender_id) const {
5591 for (const auto& transceiver : transceivers_) {
5592 for (auto sender : transceiver->internal()->senders()) {
5593 if (sender->id() == sender_id) {
5594 return sender;
5595 }
5596 }
5597 }
5598 return nullptr;
5599 }
5600
5601 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
FindReceiverById(const std::string & receiver_id) const5602 PeerConnection::FindReceiverById(const std::string& receiver_id) const {
5603 for (const auto& transceiver : transceivers_) {
5604 for (auto receiver : transceiver->internal()->receivers()) {
5605 if (receiver->id() == receiver_id) {
5606 return receiver;
5607 }
5608 }
5609 }
5610 return nullptr;
5611 }
5612
5613 std::vector<PeerConnection::RtpSenderInfo>*
GetRemoteSenderInfos(cricket::MediaType media_type)5614 PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) {
5615 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
5616 media_type == cricket::MEDIA_TYPE_VIDEO);
5617 return (media_type == cricket::MEDIA_TYPE_AUDIO)
5618 ? &remote_audio_sender_infos_
5619 : &remote_video_sender_infos_;
5620 }
5621
GetLocalSenderInfos(cricket::MediaType media_type)5622 std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos(
5623 cricket::MediaType media_type) {
5624 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
5625 media_type == cricket::MEDIA_TYPE_VIDEO);
5626 return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_
5627 : &local_video_sender_infos_;
5628 }
5629
FindSenderInfo(const std::vector<PeerConnection::RtpSenderInfo> & infos,const std::string & stream_id,const std::string sender_id) const5630 const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo(
5631 const std::vector<PeerConnection::RtpSenderInfo>& infos,
5632 const std::string& stream_id,
5633 const std::string sender_id) const {
5634 for (const RtpSenderInfo& sender_info : infos) {
5635 if (sender_info.stream_id == stream_id &&
5636 sender_info.sender_id == sender_id) {
5637 return &sender_info;
5638 }
5639 }
5640 return nullptr;
5641 }
5642
FindDataChannelBySid(int sid) const5643 SctpDataChannel* PeerConnection::FindDataChannelBySid(int sid) const {
5644 return data_channel_controller_.FindDataChannelBySid(sid);
5645 }
5646
5647 PeerConnection::InitializePortAllocatorResult
InitializePortAllocator_n(const cricket::ServerAddresses & stun_servers,const std::vector<cricket::RelayServerConfig> & turn_servers,const RTCConfiguration & configuration)5648 PeerConnection::InitializePortAllocator_n(
5649 const cricket::ServerAddresses& stun_servers,
5650 const std::vector<cricket::RelayServerConfig>& turn_servers,
5651 const RTCConfiguration& configuration) {
5652 RTC_DCHECK_RUN_ON(network_thread());
5653
5654 port_allocator_->Initialize();
5655 // To handle both internal and externally created port allocator, we will
5656 // enable BUNDLE here.
5657 int port_allocator_flags = port_allocator_->flags();
5658 port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
5659 cricket::PORTALLOCATOR_ENABLE_IPV6 |
5660 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI;
5661 // If the disable-IPv6 flag was specified, we'll not override it
5662 // by experiment.
5663 if (configuration.disable_ipv6) {
5664 port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
5665 } else if (absl::StartsWith(
5666 webrtc::field_trial::FindFullName("WebRTC-IPv6Default"),
5667 "Disabled")) {
5668 port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
5669 }
5670
5671 if (configuration.disable_ipv6_on_wifi) {
5672 port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
5673 RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled.";
5674 }
5675
5676 if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) {
5677 port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
5678 RTC_LOG(LS_INFO) << "TCP candidates are disabled.";
5679 }
5680
5681 if (configuration.candidate_network_policy ==
5682 kCandidateNetworkPolicyLowCost) {
5683 port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS;
5684 RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks";
5685 }
5686
5687 if (configuration.disable_link_local_networks) {
5688 port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS;
5689 RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces.";
5690 }
5691
5692 port_allocator_->set_flags(port_allocator_flags);
5693 // No step delay is used while allocating ports.
5694 port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
5695 port_allocator_->SetCandidateFilter(
5696 ConvertIceTransportTypeToCandidateFilter(configuration.type));
5697 port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks);
5698
5699 auto turn_servers_copy = turn_servers;
5700 for (auto& turn_server : turn_servers_copy) {
5701 turn_server.tls_cert_verifier = tls_cert_verifier_.get();
5702 }
5703 // Call this last since it may create pooled allocator sessions using the
5704 // properties set above.
5705 port_allocator_->SetConfiguration(
5706 stun_servers, std::move(turn_servers_copy),
5707 configuration.ice_candidate_pool_size,
5708 configuration.GetTurnPortPrunePolicy(), configuration.turn_customizer,
5709 configuration.stun_candidate_keepalive_interval);
5710
5711 InitializePortAllocatorResult res;
5712 res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6;
5713 return res;
5714 }
5715
ReconfigurePortAllocator_n(const cricket::ServerAddresses & stun_servers,const std::vector<cricket::RelayServerConfig> & turn_servers,IceTransportsType type,int candidate_pool_size,PortPrunePolicy turn_port_prune_policy,webrtc::TurnCustomizer * turn_customizer,absl::optional<int> stun_candidate_keepalive_interval,bool have_local_description)5716 bool PeerConnection::ReconfigurePortAllocator_n(
5717 const cricket::ServerAddresses& stun_servers,
5718 const std::vector<cricket::RelayServerConfig>& turn_servers,
5719 IceTransportsType type,
5720 int candidate_pool_size,
5721 PortPrunePolicy turn_port_prune_policy,
5722 webrtc::TurnCustomizer* turn_customizer,
5723 absl::optional<int> stun_candidate_keepalive_interval,
5724 bool have_local_description) {
5725 port_allocator_->SetCandidateFilter(
5726 ConvertIceTransportTypeToCandidateFilter(type));
5727 // According to JSEP, after setLocalDescription, changing the candidate pool
5728 // size is not allowed, and changing the set of ICE servers will not result
5729 // in new candidates being gathered.
5730 if (have_local_description) {
5731 port_allocator_->FreezeCandidatePool();
5732 }
5733 // Add the custom tls turn servers if they exist.
5734 auto turn_servers_copy = turn_servers;
5735 for (auto& turn_server : turn_servers_copy) {
5736 turn_server.tls_cert_verifier = tls_cert_verifier_.get();
5737 }
5738 // Call this last since it may create pooled allocator sessions using the
5739 // candidate filter set above.
5740 return port_allocator_->SetConfiguration(
5741 stun_servers, std::move(turn_servers_copy), candidate_pool_size,
5742 turn_port_prune_policy, turn_customizer,
5743 stun_candidate_keepalive_interval);
5744 }
5745
channel_manager() const5746 cricket::ChannelManager* PeerConnection::channel_manager() const {
5747 return factory_->channel_manager();
5748 }
5749
StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,int64_t output_period_ms)5750 bool PeerConnection::StartRtcEventLog_w(
5751 std::unique_ptr<RtcEventLogOutput> output,
5752 int64_t output_period_ms) {
5753 RTC_DCHECK_RUN_ON(worker_thread());
5754 if (!event_log_) {
5755 return false;
5756 }
5757 return event_log_->StartLogging(std::move(output), output_period_ms);
5758 }
5759
StopRtcEventLog_w()5760 void PeerConnection::StopRtcEventLog_w() {
5761 RTC_DCHECK_RUN_ON(worker_thread());
5762 if (event_log_) {
5763 event_log_->StopLogging();
5764 }
5765 }
5766
GetChannel(const std::string & content_name)5767 cricket::ChannelInterface* PeerConnection::GetChannel(
5768 const std::string& content_name) {
5769 for (const auto& transceiver : transceivers_) {
5770 cricket::ChannelInterface* channel = transceiver->internal()->channel();
5771 if (channel && channel->content_name() == content_name) {
5772 return channel;
5773 }
5774 }
5775 if (rtp_data_channel() &&
5776 rtp_data_channel()->content_name() == content_name) {
5777 return rtp_data_channel();
5778 }
5779 return nullptr;
5780 }
5781
GetSctpSslRole(rtc::SSLRole * role)5782 bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) {
5783 RTC_DCHECK_RUN_ON(signaling_thread());
5784 if (!local_description() || !remote_description()) {
5785 RTC_LOG(LS_VERBOSE)
5786 << "Local and Remote descriptions must be applied to get the "
5787 "SSL Role of the SCTP transport.";
5788 return false;
5789 }
5790 if (!data_channel_controller_.data_channel_transport()) {
5791 RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
5792 "SSL Role of the SCTP transport.";
5793 return false;
5794 }
5795
5796 absl::optional<rtc::SSLRole> dtls_role;
5797 if (sctp_mid_s_) {
5798 dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_s_);
5799 if (!dtls_role && is_caller_.has_value()) {
5800 dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT;
5801 }
5802 *role = *dtls_role;
5803 return true;
5804 }
5805 return false;
5806 }
5807
GetSslRole(const std::string & content_name,rtc::SSLRole * role)5808 bool PeerConnection::GetSslRole(const std::string& content_name,
5809 rtc::SSLRole* role) {
5810 RTC_DCHECK_RUN_ON(signaling_thread());
5811 if (!local_description() || !remote_description()) {
5812 RTC_LOG(LS_INFO)
5813 << "Local and Remote descriptions must be applied to get the "
5814 "SSL Role of the session.";
5815 return false;
5816 }
5817
5818 auto dtls_role = transport_controller_->GetDtlsRole(content_name);
5819 if (dtls_role) {
5820 *role = *dtls_role;
5821 return true;
5822 }
5823 return false;
5824 }
5825
SetSessionError(SessionError error,const std::string & error_desc)5826 void PeerConnection::SetSessionError(SessionError error,
5827 const std::string& error_desc) {
5828 RTC_DCHECK_RUN_ON(signaling_thread());
5829 if (error != session_error_) {
5830 session_error_ = error;
5831 session_error_desc_ = error_desc;
5832 }
5833 }
5834
UpdateSessionState(SdpType type,cricket::ContentSource source,const cricket::SessionDescription * description)5835 RTCError PeerConnection::UpdateSessionState(
5836 SdpType type,
5837 cricket::ContentSource source,
5838 const cricket::SessionDescription* description) {
5839 RTC_DCHECK_RUN_ON(signaling_thread());
5840
5841 // If there's already a pending error then no state transition should happen.
5842 // But all call-sites should be verifying this before calling us!
5843 RTC_DCHECK(session_error() == SessionError::kNone);
5844
5845 // If this is answer-ish we're ready to let media flow.
5846 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
5847 EnableSending();
5848 }
5849
5850 // Update the signaling state according to the specified state machine (see
5851 // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum).
5852 if (type == SdpType::kOffer) {
5853 ChangeSignalingState(source == cricket::CS_LOCAL
5854 ? PeerConnectionInterface::kHaveLocalOffer
5855 : PeerConnectionInterface::kHaveRemoteOffer);
5856 } else if (type == SdpType::kPrAnswer) {
5857 ChangeSignalingState(source == cricket::CS_LOCAL
5858 ? PeerConnectionInterface::kHaveLocalPrAnswer
5859 : PeerConnectionInterface::kHaveRemotePrAnswer);
5860 } else {
5861 RTC_DCHECK(type == SdpType::kAnswer);
5862 ChangeSignalingState(PeerConnectionInterface::kStable);
5863 transceiver_stable_states_by_transceivers_.clear();
5864 have_pending_rtp_data_channel_ = false;
5865 }
5866
5867 // Update internal objects according to the session description's media
5868 // descriptions.
5869 RTCError error = PushdownMediaDescription(type, source);
5870 if (!error.ok()) {
5871 return error;
5872 }
5873
5874 return RTCError::OK();
5875 }
5876
PushdownMediaDescription(SdpType type,cricket::ContentSource source)5877 RTCError PeerConnection::PushdownMediaDescription(
5878 SdpType type,
5879 cricket::ContentSource source) {
5880 const SessionDescriptionInterface* sdesc =
5881 (source == cricket::CS_LOCAL ? local_description()
5882 : remote_description());
5883 RTC_DCHECK(sdesc);
5884
5885 // Push down the new SDP media section for each audio/video transceiver.
5886 for (const auto& transceiver : transceivers_) {
5887 const ContentInfo* content_info =
5888 FindMediaSectionForTransceiver(transceiver, sdesc);
5889 cricket::ChannelInterface* channel = transceiver->internal()->channel();
5890 if (!channel || !content_info || content_info->rejected) {
5891 continue;
5892 }
5893 const MediaContentDescription* content_desc =
5894 content_info->media_description();
5895 if (!content_desc) {
5896 continue;
5897 }
5898 std::string error;
5899 bool success = (source == cricket::CS_LOCAL)
5900 ? channel->SetLocalContent(content_desc, type, &error)
5901 : channel->SetRemoteContent(content_desc, type, &error);
5902 if (!success) {
5903 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error);
5904 }
5905 }
5906
5907 // If using the RtpDataChannel, push down the new SDP section for it too.
5908 if (data_channel_controller_.rtp_data_channel()) {
5909 const ContentInfo* data_content =
5910 cricket::GetFirstDataContent(sdesc->description());
5911 if (data_content && !data_content->rejected) {
5912 const MediaContentDescription* data_desc =
5913 data_content->media_description();
5914 if (data_desc) {
5915 std::string error;
5916 bool success =
5917 (source == cricket::CS_LOCAL)
5918 ? data_channel_controller_.rtp_data_channel()->SetLocalContent(
5919 data_desc, type, &error)
5920 : data_channel_controller_.rtp_data_channel()->SetRemoteContent(
5921 data_desc, type, &error);
5922 if (!success) {
5923 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error);
5924 }
5925 }
5926 }
5927 }
5928
5929 // Need complete offer/answer with an SCTP m= section before starting SCTP,
5930 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
5931 if (sctp_mid_s_ && local_description() && remote_description()) {
5932 rtc::scoped_refptr<SctpTransport> sctp_transport =
5933 transport_controller_->GetSctpTransport(*sctp_mid_s_);
5934 auto local_sctp_description = cricket::GetFirstSctpDataContentDescription(
5935 local_description()->description());
5936 auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription(
5937 remote_description()->description());
5938 if (sctp_transport && local_sctp_description && remote_sctp_description) {
5939 int max_message_size;
5940 // A remote max message size of zero means "any size supported".
5941 // We configure the connection with our own max message size.
5942 if (remote_sctp_description->max_message_size() == 0) {
5943 max_message_size = local_sctp_description->max_message_size();
5944 } else {
5945 max_message_size =
5946 std::min(local_sctp_description->max_message_size(),
5947 remote_sctp_description->max_message_size());
5948 }
5949 sctp_transport->Start(local_sctp_description->port(),
5950 remote_sctp_description->port(), max_message_size);
5951 }
5952 }
5953
5954 return RTCError::OK();
5955 }
5956
PushdownTransportDescription(cricket::ContentSource source,SdpType type)5957 RTCError PeerConnection::PushdownTransportDescription(
5958 cricket::ContentSource source,
5959 SdpType type) {
5960 RTC_DCHECK_RUN_ON(signaling_thread());
5961
5962 if (source == cricket::CS_LOCAL) {
5963 const SessionDescriptionInterface* sdesc = local_description();
5964 RTC_DCHECK(sdesc);
5965 return transport_controller_->SetLocalDescription(type,
5966 sdesc->description());
5967 } else {
5968 const SessionDescriptionInterface* sdesc = remote_description();
5969 RTC_DCHECK(sdesc);
5970 return transport_controller_->SetRemoteDescription(type,
5971 sdesc->description());
5972 }
5973 }
5974
GetTransportDescription(const SessionDescription * description,const std::string & content_name,cricket::TransportDescription * tdesc)5975 bool PeerConnection::GetTransportDescription(
5976 const SessionDescription* description,
5977 const std::string& content_name,
5978 cricket::TransportDescription* tdesc) {
5979 if (!description || !tdesc) {
5980 return false;
5981 }
5982 const TransportInfo* transport_info =
5983 description->GetTransportInfoByName(content_name);
5984 if (!transport_info) {
5985 return false;
5986 }
5987 *tdesc = transport_info->description;
5988 return true;
5989 }
5990
ParseIceConfig(const PeerConnectionInterface::RTCConfiguration & config) const5991 cricket::IceConfig PeerConnection::ParseIceConfig(
5992 const PeerConnectionInterface::RTCConfiguration& config) const {
5993 cricket::ContinualGatheringPolicy gathering_policy;
5994 switch (config.continual_gathering_policy) {
5995 case PeerConnectionInterface::GATHER_ONCE:
5996 gathering_policy = cricket::GATHER_ONCE;
5997 break;
5998 case PeerConnectionInterface::GATHER_CONTINUALLY:
5999 gathering_policy = cricket::GATHER_CONTINUALLY;
6000 break;
6001 default:
6002 RTC_NOTREACHED();
6003 gathering_policy = cricket::GATHER_ONCE;
6004 }
6005
6006 cricket::IceConfig ice_config;
6007 ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt(
6008 config.ice_connection_receiving_timeout);
6009 ice_config.prioritize_most_likely_candidate_pairs =
6010 config.prioritize_most_likely_ice_candidate_pairs;
6011 ice_config.backup_connection_ping_interval =
6012 RTCConfigurationToIceConfigOptionalInt(
6013 config.ice_backup_candidate_pair_ping_interval);
6014 ice_config.continual_gathering_policy = gathering_policy;
6015 ice_config.presume_writable_when_fully_relayed =
6016 config.presume_writable_when_fully_relayed;
6017 ice_config.surface_ice_candidates_on_ice_transport_type_changed =
6018 config.surface_ice_candidates_on_ice_transport_type_changed;
6019 ice_config.ice_check_interval_strong_connectivity =
6020 config.ice_check_interval_strong_connectivity;
6021 ice_config.ice_check_interval_weak_connectivity =
6022 config.ice_check_interval_weak_connectivity;
6023 ice_config.ice_check_min_interval = config.ice_check_min_interval;
6024 ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout;
6025 ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks;
6026 ice_config.ice_inactive_timeout = config.ice_inactive_timeout;
6027 ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval;
6028 ice_config.network_preference = config.network_preference;
6029 return ice_config;
6030 }
6031
GetDataChannelStats() const6032 std::vector<DataChannelStats> PeerConnection::GetDataChannelStats() const {
6033 RTC_DCHECK_RUN_ON(signaling_thread());
6034 return data_channel_controller_.GetDataChannelStats();
6035 }
6036
sctp_transport_name() const6037 absl::optional<std::string> PeerConnection::sctp_transport_name() const {
6038 RTC_DCHECK_RUN_ON(signaling_thread());
6039 if (sctp_mid_s_ && transport_controller_) {
6040 auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_s_);
6041 if (dtls_transport) {
6042 return dtls_transport->transport_name();
6043 }
6044 return absl::optional<std::string>();
6045 }
6046 return absl::optional<std::string>();
6047 }
6048
GetPooledCandidateStats() const6049 cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const {
6050 cricket::CandidateStatsList candidate_states_list;
6051 network_thread()->Invoke<void>(
6052 RTC_FROM_HERE,
6053 rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions,
6054 port_allocator_.get(), &candidate_states_list));
6055 return candidate_states_list;
6056 }
6057
GetTransportNamesByMid() const6058 std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid()
6059 const {
6060 RTC_DCHECK_RUN_ON(signaling_thread());
6061 std::map<std::string, std::string> transport_names_by_mid;
6062 for (const auto& transceiver : transceivers_) {
6063 cricket::ChannelInterface* channel = transceiver->internal()->channel();
6064 if (channel) {
6065 transport_names_by_mid[channel->content_name()] =
6066 channel->transport_name();
6067 }
6068 }
6069 if (data_channel_controller_.rtp_data_channel()) {
6070 transport_names_by_mid[data_channel_controller_.rtp_data_channel()
6071 ->content_name()] =
6072 data_channel_controller_.rtp_data_channel()->transport_name();
6073 }
6074 if (data_channel_controller_.data_channel_transport()) {
6075 absl::optional<std::string> transport_name = sctp_transport_name();
6076 RTC_DCHECK(transport_name);
6077 transport_names_by_mid[*sctp_mid_s_] = *transport_name;
6078 }
6079 return transport_names_by_mid;
6080 }
6081
6082 std::map<std::string, cricket::TransportStats>
GetTransportStatsByNames(const std::set<std::string> & transport_names)6083 PeerConnection::GetTransportStatsByNames(
6084 const std::set<std::string>& transport_names) {
6085 if (!network_thread()->IsCurrent()) {
6086 return network_thread()
6087 ->Invoke<std::map<std::string, cricket::TransportStats>>(
6088 RTC_FROM_HERE,
6089 [&] { return GetTransportStatsByNames(transport_names); });
6090 }
6091 RTC_DCHECK_RUN_ON(network_thread());
6092 std::map<std::string, cricket::TransportStats> transport_stats_by_name;
6093 for (const std::string& transport_name : transport_names) {
6094 cricket::TransportStats transport_stats;
6095 bool success =
6096 transport_controller_->GetStats(transport_name, &transport_stats);
6097 if (success) {
6098 transport_stats_by_name[transport_name] = std::move(transport_stats);
6099 } else {
6100 RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name="
6101 << transport_name;
6102 }
6103 }
6104 return transport_stats_by_name;
6105 }
6106
GetLocalCertificate(const std::string & transport_name,rtc::scoped_refptr<rtc::RTCCertificate> * certificate)6107 bool PeerConnection::GetLocalCertificate(
6108 const std::string& transport_name,
6109 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
6110 if (!certificate) {
6111 return false;
6112 }
6113 *certificate = transport_controller_->GetLocalCertificate(transport_name);
6114 return *certificate != nullptr;
6115 }
6116
GetRemoteSSLCertChain(const std::string & transport_name)6117 std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain(
6118 const std::string& transport_name) {
6119 return transport_controller_->GetRemoteSSLCertChain(transport_name);
6120 }
6121
data_channel_type() const6122 cricket::DataChannelType PeerConnection::data_channel_type() const {
6123 return data_channel_controller_.data_channel_type();
6124 }
6125
IceRestartPending(const std::string & content_name) const6126 bool PeerConnection::IceRestartPending(const std::string& content_name) const {
6127 RTC_DCHECK_RUN_ON(signaling_thread());
6128 return pending_ice_restarts_.find(content_name) !=
6129 pending_ice_restarts_.end();
6130 }
6131
NeedsIceRestart(const std::string & content_name) const6132 bool PeerConnection::NeedsIceRestart(const std::string& content_name) const {
6133 return transport_controller_->NeedsIceRestart(content_name);
6134 }
6135
OnCertificateReady(const rtc::scoped_refptr<rtc::RTCCertificate> & certificate)6136 void PeerConnection::OnCertificateReady(
6137 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
6138 transport_controller_->SetLocalCertificate(certificate);
6139 }
6140
OnDtlsSrtpSetupFailure(cricket::BaseChannel *,bool rtcp)6141 void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
6142 SetSessionError(SessionError::kTransport,
6143 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
6144 }
6145
OnTransportControllerConnectionState(cricket::IceConnectionState state)6146 void PeerConnection::OnTransportControllerConnectionState(
6147 cricket::IceConnectionState state) {
6148 switch (state) {
6149 case cricket::kIceConnectionConnecting:
6150 // If the current state is Connected or Completed, then there were
6151 // writable channels but now there are not, so the next state must
6152 // be Disconnected.
6153 // kIceConnectionConnecting is currently used as the default,
6154 // un-connected state by the TransportController, so its only use is
6155 // detecting disconnections.
6156 if (ice_connection_state_ ==
6157 PeerConnectionInterface::kIceConnectionConnected ||
6158 ice_connection_state_ ==
6159 PeerConnectionInterface::kIceConnectionCompleted) {
6160 SetIceConnectionState(
6161 PeerConnectionInterface::kIceConnectionDisconnected);
6162 }
6163 break;
6164 case cricket::kIceConnectionFailed:
6165 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
6166 break;
6167 case cricket::kIceConnectionConnected:
6168 RTC_LOG(LS_INFO) << "Changing to ICE connected state because "
6169 "all transports are writable.";
6170 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
6171 NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED);
6172 break;
6173 case cricket::kIceConnectionCompleted:
6174 RTC_LOG(LS_INFO) << "Changing to ICE completed state because "
6175 "all transports are complete.";
6176 if (ice_connection_state_ !=
6177 PeerConnectionInterface::kIceConnectionConnected) {
6178 // If jumping directly from "checking" to "connected",
6179 // signal "connected" first.
6180 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
6181 }
6182 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
6183 NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED);
6184 ReportTransportStats();
6185 break;
6186 default:
6187 RTC_NOTREACHED();
6188 }
6189 }
6190
OnTransportControllerCandidatesGathered(const std::string & transport_name,const cricket::Candidates & candidates)6191 void PeerConnection::OnTransportControllerCandidatesGathered(
6192 const std::string& transport_name,
6193 const cricket::Candidates& candidates) {
6194 int sdp_mline_index;
6195 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
6196 RTC_LOG(LS_ERROR)
6197 << "OnTransportControllerCandidatesGathered: content name "
6198 << transport_name << " not found";
6199 return;
6200 }
6201
6202 for (cricket::Candidates::const_iterator citer = candidates.begin();
6203 citer != candidates.end(); ++citer) {
6204 // Use transport_name as the candidate media id.
6205 std::unique_ptr<JsepIceCandidate> candidate(
6206 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
6207 if (local_description()) {
6208 mutable_local_description()->AddCandidate(candidate.get());
6209 }
6210 OnIceCandidate(std::move(candidate));
6211 }
6212 }
6213
OnTransportControllerCandidateError(const cricket::IceCandidateErrorEvent & event)6214 void PeerConnection::OnTransportControllerCandidateError(
6215 const cricket::IceCandidateErrorEvent& event) {
6216 OnIceCandidateError(event.address, event.port, event.url, event.error_code,
6217 event.error_text);
6218 }
6219
OnTransportControllerCandidatesRemoved(const std::vector<cricket::Candidate> & candidates)6220 void PeerConnection::OnTransportControllerCandidatesRemoved(
6221 const std::vector<cricket::Candidate>& candidates) {
6222 // Sanity check.
6223 for (const cricket::Candidate& candidate : candidates) {
6224 if (candidate.transport_name().empty()) {
6225 RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
6226 "empty content name in candidate "
6227 << candidate.ToString();
6228 return;
6229 }
6230 }
6231
6232 if (local_description()) {
6233 mutable_local_description()->RemoveCandidates(candidates);
6234 }
6235 OnIceCandidatesRemoved(candidates);
6236 }
6237
OnTransportControllerCandidateChanged(const cricket::CandidatePairChangeEvent & event)6238 void PeerConnection::OnTransportControllerCandidateChanged(
6239 const cricket::CandidatePairChangeEvent& event) {
6240 OnSelectedCandidatePairChanged(event);
6241 }
6242
OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error)6243 void PeerConnection::OnTransportControllerDtlsHandshakeError(
6244 rtc::SSLHandshakeError error) {
6245 RTC_HISTOGRAM_ENUMERATION(
6246 "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error),
6247 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
6248 }
6249
EnableSending()6250 void PeerConnection::EnableSending() {
6251 for (const auto& transceiver : transceivers_) {
6252 cricket::ChannelInterface* channel = transceiver->internal()->channel();
6253 if (channel && !channel->enabled()) {
6254 channel->Enable(true);
6255 }
6256 }
6257
6258 if (data_channel_controller_.rtp_data_channel() &&
6259 !data_channel_controller_.rtp_data_channel()->enabled()) {
6260 data_channel_controller_.rtp_data_channel()->Enable(true);
6261 }
6262 }
6263
6264 // Returns the media index for a local ice candidate given the content name.
GetLocalCandidateMediaIndex(const std::string & content_name,int * sdp_mline_index)6265 bool PeerConnection::GetLocalCandidateMediaIndex(
6266 const std::string& content_name,
6267 int* sdp_mline_index) {
6268 if (!local_description() || !sdp_mline_index) {
6269 return false;
6270 }
6271
6272 bool content_found = false;
6273 const ContentInfos& contents = local_description()->description()->contents();
6274 for (size_t index = 0; index < contents.size(); ++index) {
6275 if (contents[index].name == content_name) {
6276 *sdp_mline_index = static_cast<int>(index);
6277 content_found = true;
6278 break;
6279 }
6280 }
6281 return content_found;
6282 }
6283
UseCandidatesInSessionDescription(const SessionDescriptionInterface * remote_desc)6284 bool PeerConnection::UseCandidatesInSessionDescription(
6285 const SessionDescriptionInterface* remote_desc) {
6286 if (!remote_desc) {
6287 return true;
6288 }
6289 bool ret = true;
6290
6291 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
6292 const IceCandidateCollection* candidates = remote_desc->candidates(m);
6293 for (size_t n = 0; n < candidates->count(); ++n) {
6294 const IceCandidateInterface* candidate = candidates->at(n);
6295 bool valid = false;
6296 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
6297 if (valid) {
6298 RTC_LOG(LS_INFO)
6299 << "UseCandidatesInSessionDescription: Not ready to use "
6300 "candidate.";
6301 }
6302 continue;
6303 }
6304 ret = UseCandidate(candidate);
6305 if (!ret) {
6306 break;
6307 }
6308 }
6309 }
6310 return ret;
6311 }
6312
UseCandidate(const IceCandidateInterface * candidate)6313 bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) {
6314 RTCErrorOr<const cricket::ContentInfo*> result =
6315 FindContentInfo(remote_description(), candidate);
6316 if (!result.ok()) {
6317 RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. "
6318 << result.error().message();
6319 return false;
6320 }
6321 std::vector<cricket::Candidate> candidates;
6322 candidates.push_back(candidate->candidate());
6323 // Invoking BaseSession method to handle remote candidates.
6324 RTCError error = transport_controller_->AddRemoteCandidates(
6325 result.value()->name, candidates);
6326 if (error.ok()) {
6327 ReportRemoteIceCandidateAdded(candidate->candidate());
6328 // Candidates successfully submitted for checking.
6329 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
6330 ice_connection_state_ ==
6331 PeerConnectionInterface::kIceConnectionDisconnected) {
6332 // If state is New, then the session has just gotten its first remote ICE
6333 // candidates, so go to Checking.
6334 // If state is Disconnected, the session is re-using old candidates or
6335 // receiving additional ones, so go to Checking.
6336 // If state is Connected, stay Connected.
6337 // TODO(bemasc): If state is Connected, and the new candidates are for a
6338 // newly added transport, then the state actually _should_ move to
6339 // checking. Add a way to distinguish that case.
6340 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
6341 }
6342 // TODO(bemasc): If state is Completed, go back to Connected.
6343 } else {
6344 RTC_LOG(LS_WARNING) << error.message();
6345 }
6346 return true;
6347 }
6348
FindContentInfo(const SessionDescriptionInterface * description,const IceCandidateInterface * candidate)6349 RTCErrorOr<const cricket::ContentInfo*> PeerConnection::FindContentInfo(
6350 const SessionDescriptionInterface* description,
6351 const IceCandidateInterface* candidate) {
6352 if (candidate->sdp_mline_index() >= 0) {
6353 size_t mediacontent_index =
6354 static_cast<size_t>(candidate->sdp_mline_index());
6355 size_t content_size = description->description()->contents().size();
6356 if (mediacontent_index < content_size) {
6357 return &description->description()->contents()[mediacontent_index];
6358 } else {
6359 return RTCError(RTCErrorType::INVALID_RANGE,
6360 "Media line index (" +
6361 rtc::ToString(candidate->sdp_mline_index()) +
6362 ") out of range (number of mlines: " +
6363 rtc::ToString(content_size) + ").");
6364 }
6365 } else if (!candidate->sdp_mid().empty()) {
6366 auto& contents = description->description()->contents();
6367 auto it = absl::c_find_if(
6368 contents, [candidate](const cricket::ContentInfo& content_info) {
6369 return content_info.mid() == candidate->sdp_mid();
6370 });
6371 if (it == contents.end()) {
6372 return RTCError(
6373 RTCErrorType::INVALID_PARAMETER,
6374 "Mid " + candidate->sdp_mid() +
6375 " specified but no media section with that mid found.");
6376 } else {
6377 return &*it;
6378 }
6379 }
6380
6381 return RTCError(RTCErrorType::INVALID_PARAMETER,
6382 "Neither sdp_mline_index nor sdp_mid specified.");
6383 }
6384
RemoveUnusedChannels(const SessionDescription * desc)6385 void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) {
6386 // Destroy video channel first since it may have a pointer to the
6387 // voice channel.
6388 const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc);
6389 if (!video_info || video_info->rejected) {
6390 DestroyTransceiverChannel(GetVideoTransceiver());
6391 }
6392
6393 const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc);
6394 if (!audio_info || audio_info->rejected) {
6395 DestroyTransceiverChannel(GetAudioTransceiver());
6396 }
6397
6398 const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc);
6399 if (!data_info || data_info->rejected) {
6400 DestroyDataChannelTransport();
6401 }
6402 }
6403
GetEarlyBundleGroup(const SessionDescription & desc) const6404 RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup(
6405 const SessionDescription& desc) const {
6406 const cricket::ContentGroup* bundle_group = nullptr;
6407 if (configuration_.bundle_policy ==
6408 PeerConnectionInterface::kBundlePolicyMaxBundle) {
6409 bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
6410 if (!bundle_group) {
6411 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6412 "max-bundle configured but session description "
6413 "has no BUNDLE group");
6414 }
6415 }
6416 return bundle_group;
6417 }
6418
CreateChannels(const SessionDescription & desc)6419 RTCError PeerConnection::CreateChannels(const SessionDescription& desc) {
6420 // Creating the media channels. Transports should already have been created
6421 // at this point.
6422 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc);
6423 if (voice && !voice->rejected &&
6424 !GetAudioTransceiver()->internal()->channel()) {
6425 cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name);
6426 if (!voice_channel) {
6427 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
6428 "Failed to create voice channel.");
6429 }
6430 GetAudioTransceiver()->internal()->SetChannel(voice_channel);
6431 }
6432
6433 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc);
6434 if (video && !video->rejected &&
6435 !GetVideoTransceiver()->internal()->channel()) {
6436 cricket::VideoChannel* video_channel = CreateVideoChannel(video->name);
6437 if (!video_channel) {
6438 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
6439 "Failed to create video channel.");
6440 }
6441 GetVideoTransceiver()->internal()->SetChannel(video_channel);
6442 }
6443
6444 const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc);
6445 if (data_channel_type() != cricket::DCT_NONE && data && !data->rejected &&
6446 !data_channel_controller_.rtp_data_channel() &&
6447 !data_channel_controller_.data_channel_transport()) {
6448 if (!CreateDataChannel(data->name)) {
6449 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
6450 "Failed to create data channel.");
6451 }
6452 }
6453
6454 return RTCError::OK();
6455 }
6456
6457 // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
CreateVoiceChannel(const std::string & mid)6458 cricket::VoiceChannel* PeerConnection::CreateVoiceChannel(
6459 const std::string& mid) {
6460 RtpTransportInternal* rtp_transport = GetRtpTransport(mid);
6461
6462 cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel(
6463 call_ptr_, configuration_.media_config, rtp_transport, signaling_thread(),
6464 mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_,
6465 audio_options_);
6466 if (!voice_channel) {
6467 return nullptr;
6468 }
6469 voice_channel->SignalDtlsSrtpSetupFailure.connect(
6470 this, &PeerConnection::OnDtlsSrtpSetupFailure);
6471 voice_channel->SignalSentPacket.connect(this,
6472 &PeerConnection::OnSentPacket_w);
6473 voice_channel->SetRtpTransport(rtp_transport);
6474
6475 return voice_channel;
6476 }
6477
6478 // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
CreateVideoChannel(const std::string & mid)6479 cricket::VideoChannel* PeerConnection::CreateVideoChannel(
6480 const std::string& mid) {
6481 RtpTransportInternal* rtp_transport = GetRtpTransport(mid);
6482
6483 cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel(
6484 call_ptr_, configuration_.media_config, rtp_transport, signaling_thread(),
6485 mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_, video_options_,
6486 video_bitrate_allocator_factory_.get());
6487 if (!video_channel) {
6488 return nullptr;
6489 }
6490 video_channel->SignalDtlsSrtpSetupFailure.connect(
6491 this, &PeerConnection::OnDtlsSrtpSetupFailure);
6492 video_channel->SignalSentPacket.connect(this,
6493 &PeerConnection::OnSentPacket_w);
6494 video_channel->SetRtpTransport(rtp_transport);
6495
6496 return video_channel;
6497 }
6498
CreateDataChannel(const std::string & mid)6499 bool PeerConnection::CreateDataChannel(const std::string& mid) {
6500 switch (data_channel_type()) {
6501 case cricket::DCT_SCTP:
6502 if (network_thread()->Invoke<bool>(
6503 RTC_FROM_HERE,
6504 rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, this,
6505 mid))) {
6506 sctp_mid_s_ = mid;
6507 } else {
6508 return false;
6509 }
6510 return true;
6511 case cricket::DCT_RTP:
6512 default:
6513 RtpTransportInternal* rtp_transport = GetRtpTransport(mid);
6514 // TODO(bugs.webrtc.org/9987): set_rtp_data_channel() should be called on
6515 // the network thread like set_data_channel_transport is.
6516 data_channel_controller_.set_rtp_data_channel(
6517 channel_manager()->CreateRtpDataChannel(
6518 configuration_.media_config, rtp_transport, signaling_thread(),
6519 mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_));
6520 if (!data_channel_controller_.rtp_data_channel()) {
6521 return false;
6522 }
6523 data_channel_controller_.rtp_data_channel()
6524 ->SignalDtlsSrtpSetupFailure.connect(
6525 this, &PeerConnection::OnDtlsSrtpSetupFailure);
6526 data_channel_controller_.rtp_data_channel()->SignalSentPacket.connect(
6527 this, &PeerConnection::OnSentPacket_w);
6528 data_channel_controller_.rtp_data_channel()->SetRtpTransport(
6529 rtp_transport);
6530 have_pending_rtp_data_channel_ = true;
6531 return true;
6532 }
6533 return false;
6534 }
6535
GetCallStats()6536 Call::Stats PeerConnection::GetCallStats() {
6537 if (!worker_thread()->IsCurrent()) {
6538 return worker_thread()->Invoke<Call::Stats>(
6539 RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this));
6540 }
6541 RTC_DCHECK_RUN_ON(worker_thread());
6542 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
6543 if (call_) {
6544 return call_->GetStats();
6545 } else {
6546 return Call::Stats();
6547 }
6548 }
6549
SetupDataChannelTransport_n(const std::string & mid)6550 bool PeerConnection::SetupDataChannelTransport_n(const std::string& mid) {
6551 DataChannelTransportInterface* transport =
6552 transport_controller_->GetDataChannelTransport(mid);
6553 if (!transport) {
6554 RTC_LOG(LS_ERROR)
6555 << "Data channel transport is not available for data channels, mid="
6556 << mid;
6557 return false;
6558 }
6559 RTC_LOG(LS_INFO) << "Setting up data channel transport for mid=" << mid;
6560
6561 data_channel_controller_.set_data_channel_transport(transport);
6562 data_channel_controller_.SetupDataChannelTransport_n();
6563 sctp_mid_n_ = mid;
6564
6565 // Note: setting the data sink and checking initial state must be done last,
6566 // after setting up the data channel. Setting the data sink may trigger
6567 // callbacks to PeerConnection which require the transport to be completely
6568 // set up (eg. OnReadyToSend()).
6569 transport->SetDataSink(&data_channel_controller_);
6570 return true;
6571 }
6572
TeardownDataChannelTransport_n()6573 void PeerConnection::TeardownDataChannelTransport_n() {
6574 if (!sctp_mid_n_ && !data_channel_controller_.data_channel_transport()) {
6575 return;
6576 }
6577 RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid="
6578 << *sctp_mid_n_;
6579
6580 // |sctp_mid_| may still be active through an SCTP transport. If not, unset
6581 // it.
6582 sctp_mid_n_.reset();
6583 data_channel_controller_.TeardownDataChannelTransport_n();
6584 }
6585
6586 // Returns false if bundle is enabled and rtcp_mux is disabled.
ValidateBundleSettings(const SessionDescription * desc)6587 bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) {
6588 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
6589 if (!bundle_enabled)
6590 return true;
6591
6592 const cricket::ContentGroup* bundle_group =
6593 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
6594 RTC_DCHECK(bundle_group != NULL);
6595
6596 const cricket::ContentInfos& contents = desc->contents();
6597 for (cricket::ContentInfos::const_iterator citer = contents.begin();
6598 citer != contents.end(); ++citer) {
6599 const cricket::ContentInfo* content = (&*citer);
6600 RTC_DCHECK(content != NULL);
6601 if (bundle_group->HasContentName(content->name) && !content->rejected &&
6602 content->type == MediaProtocolType::kRtp) {
6603 if (!HasRtcpMuxEnabled(content))
6604 return false;
6605 }
6606 }
6607 // RTCP-MUX is enabled in all the contents.
6608 return true;
6609 }
6610
HasRtcpMuxEnabled(const cricket::ContentInfo * content)6611 bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) {
6612 return content->media_description()->rtcp_mux();
6613 }
6614
ValidateMids(const cricket::SessionDescription & description)6615 static RTCError ValidateMids(const cricket::SessionDescription& description) {
6616 std::set<std::string> mids;
6617 for (const cricket::ContentInfo& content : description.contents()) {
6618 if (content.name.empty()) {
6619 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6620 "A media section is missing a MID attribute.");
6621 }
6622 if (!mids.insert(content.name).second) {
6623 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6624 "Duplicate a=mid value '" + content.name + "'.");
6625 }
6626 }
6627 return RTCError::OK();
6628 }
6629
ValidateSessionDescription(const SessionDescriptionInterface * sdesc,cricket::ContentSource source)6630 RTCError PeerConnection::ValidateSessionDescription(
6631 const SessionDescriptionInterface* sdesc,
6632 cricket::ContentSource source) {
6633 if (session_error() != SessionError::kNone) {
6634 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
6635 }
6636
6637 if (!sdesc || !sdesc->description()) {
6638 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
6639 }
6640
6641 SdpType type = sdesc->GetType();
6642 if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) ||
6643 (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) {
6644 LOG_AND_RETURN_ERROR(
6645 RTCErrorType::INVALID_STATE,
6646 "Called in wrong state: " + GetSignalingStateString(signaling_state()));
6647 }
6648
6649 RTCError error = ValidateMids(*sdesc->description());
6650 if (!error.ok()) {
6651 return error;
6652 }
6653
6654 // Verify crypto settings.
6655 std::string crypto_error;
6656 if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
6657 dtls_enabled_) {
6658 RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_);
6659 if (!crypto_error.ok()) {
6660 return crypto_error;
6661 }
6662 }
6663
6664 // Verify ice-ufrag and ice-pwd.
6665 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
6666 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6667 kSdpWithoutIceUfragPwd);
6668 }
6669
6670 if (!ValidateBundleSettings(sdesc->description())) {
6671 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6672 kBundleWithoutRtcpMux);
6673 }
6674
6675 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
6676 // m-lines that do not rtcp-mux enabled.
6677
6678 // Verify m-lines in Answer when compared against Offer.
6679 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
6680 // With an answer we want to compare the new answer session description with
6681 // the offer's session description from the current negotiation.
6682 const cricket::SessionDescription* offer_desc =
6683 (source == cricket::CS_LOCAL) ? remote_description()->description()
6684 : local_description()->description();
6685 if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) ||
6686 !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(),
6687 type)) {
6688 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6689 kMlineMismatchInAnswer);
6690 }
6691 } else {
6692 // The re-offers should respect the order of m= sections in current
6693 // description. See RFC3264 Section 8 paragraph 4 for more details.
6694 // With a re-offer, either the current local or current remote descriptions
6695 // could be the most up to date, so we would like to check against both of
6696 // them if they exist. It could be the case that one of them has a 0 port
6697 // for a media section, but the other does not. This is important to check
6698 // against in the case that we are recycling an m= section.
6699 const cricket::SessionDescription* current_desc = nullptr;
6700 const cricket::SessionDescription* secondary_current_desc = nullptr;
6701 if (local_description()) {
6702 current_desc = local_description()->description();
6703 if (remote_description()) {
6704 secondary_current_desc = remote_description()->description();
6705 }
6706 } else if (remote_description()) {
6707 current_desc = remote_description()->description();
6708 }
6709 if (current_desc &&
6710 !MediaSectionsInSameOrder(*current_desc, secondary_current_desc,
6711 *sdesc->description(), type)) {
6712 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6713 kMlineMismatchInSubsequentOffer);
6714 }
6715 }
6716
6717 if (IsUnifiedPlan()) {
6718 // Ensure that each audio and video media section has at most one
6719 // "StreamParams". This will return an error if receiving a session
6720 // description from a "Plan B" endpoint which adds multiple tracks of the
6721 // same type. With Unified Plan, there can only be at most one track per
6722 // media section.
6723 for (const ContentInfo& content : sdesc->description()->contents()) {
6724 const MediaContentDescription& desc = *content.media_description();
6725 if ((desc.type() == cricket::MEDIA_TYPE_AUDIO ||
6726 desc.type() == cricket::MEDIA_TYPE_VIDEO) &&
6727 desc.streams().size() > 1u) {
6728 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6729 "Media section has more than one track specified "
6730 "with a=ssrc lines which is not supported with "
6731 "Unified Plan.");
6732 }
6733 }
6734 }
6735
6736 return RTCError::OK();
6737 }
6738
ExpectSetLocalDescription(SdpType type)6739 bool PeerConnection::ExpectSetLocalDescription(SdpType type) {
6740 PeerConnectionInterface::SignalingState state = signaling_state();
6741 if (type == SdpType::kOffer) {
6742 return (state == PeerConnectionInterface::kStable) ||
6743 (state == PeerConnectionInterface::kHaveLocalOffer);
6744 } else {
6745 RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
6746 return (state == PeerConnectionInterface::kHaveRemoteOffer) ||
6747 (state == PeerConnectionInterface::kHaveLocalPrAnswer);
6748 }
6749 }
6750
ExpectSetRemoteDescription(SdpType type)6751 bool PeerConnection::ExpectSetRemoteDescription(SdpType type) {
6752 PeerConnectionInterface::SignalingState state = signaling_state();
6753 if (type == SdpType::kOffer) {
6754 return (state == PeerConnectionInterface::kStable) ||
6755 (state == PeerConnectionInterface::kHaveRemoteOffer);
6756 } else {
6757 RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
6758 return (state == PeerConnectionInterface::kHaveLocalOffer) ||
6759 (state == PeerConnectionInterface::kHaveRemotePrAnswer);
6760 }
6761 }
6762
SessionErrorToString(SessionError error) const6763 const char* PeerConnection::SessionErrorToString(SessionError error) const {
6764 switch (error) {
6765 case SessionError::kNone:
6766 return "ERROR_NONE";
6767 case SessionError::kContent:
6768 return "ERROR_CONTENT";
6769 case SessionError::kTransport:
6770 return "ERROR_TRANSPORT";
6771 }
6772 RTC_NOTREACHED();
6773 return "";
6774 }
6775
GetSessionErrorMsg()6776 std::string PeerConnection::GetSessionErrorMsg() {
6777 rtc::StringBuilder desc;
6778 desc << kSessionError << SessionErrorToString(session_error()) << ". ";
6779 desc << kSessionErrorDesc << session_error_desc() << ".";
6780 return desc.Release();
6781 }
6782
ReportSdpFormatReceived(const SessionDescriptionInterface & remote_offer)6783 void PeerConnection::ReportSdpFormatReceived(
6784 const SessionDescriptionInterface& remote_offer) {
6785 int num_audio_mlines = 0;
6786 int num_video_mlines = 0;
6787 int num_audio_tracks = 0;
6788 int num_video_tracks = 0;
6789 for (const ContentInfo& content : remote_offer.description()->contents()) {
6790 cricket::MediaType media_type = content.media_description()->type();
6791 int num_tracks = std::max(
6792 1, static_cast<int>(content.media_description()->streams().size()));
6793 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
6794 num_audio_mlines += 1;
6795 num_audio_tracks += num_tracks;
6796 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
6797 num_video_mlines += 1;
6798 num_video_tracks += num_tracks;
6799 }
6800 }
6801 SdpFormatReceived format = kSdpFormatReceivedNoTracks;
6802 if (num_audio_mlines > 1 || num_video_mlines > 1) {
6803 format = kSdpFormatReceivedComplexUnifiedPlan;
6804 } else if (num_audio_tracks > 1 || num_video_tracks > 1) {
6805 format = kSdpFormatReceivedComplexPlanB;
6806 } else if (num_audio_tracks > 0 || num_video_tracks > 0) {
6807 format = kSdpFormatReceivedSimple;
6808 }
6809 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format,
6810 kSdpFormatReceivedMax);
6811 }
6812
ReportIceCandidateCollected(const cricket::Candidate & candidate)6813 void PeerConnection::ReportIceCandidateCollected(
6814 const cricket::Candidate& candidate) {
6815 NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED);
6816 if (candidate.address().IsPrivateIP()) {
6817 NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED);
6818 }
6819 if (candidate.address().IsUnresolvedIP()) {
6820 NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED);
6821 }
6822 if (candidate.address().family() == AF_INET6) {
6823 NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED);
6824 }
6825 }
6826
ReportRemoteIceCandidateAdded(const cricket::Candidate & candidate)6827 void PeerConnection::ReportRemoteIceCandidateAdded(
6828 const cricket::Candidate& candidate) {
6829 NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED);
6830 if (candidate.address().IsPrivateIP()) {
6831 NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED);
6832 }
6833 if (candidate.address().IsUnresolvedIP()) {
6834 NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED);
6835 }
6836 if (candidate.address().family() == AF_INET6) {
6837 NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED);
6838 }
6839 }
6840
NoteUsageEvent(UsageEvent event)6841 void PeerConnection::NoteUsageEvent(UsageEvent event) {
6842 RTC_DCHECK_RUN_ON(signaling_thread());
6843 usage_event_accumulator_ |= static_cast<int>(event);
6844 }
6845
ReportUsagePattern() const6846 void PeerConnection::ReportUsagePattern() const {
6847 RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_;
6848 RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern",
6849 usage_event_accumulator_,
6850 static_cast<int>(UsageEvent::MAX_VALUE));
6851 const int bad_bits =
6852 static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED) |
6853 static_cast<int>(UsageEvent::CANDIDATE_COLLECTED);
6854 const int good_bits =
6855 static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED) |
6856 static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) |
6857 static_cast<int>(UsageEvent::ICE_STATE_CONNECTED);
6858 if ((usage_event_accumulator_ & bad_bits) == bad_bits &&
6859 (usage_event_accumulator_ & good_bits) == 0) {
6860 // If called after close(), we can't report, because observer may have
6861 // been deallocated, and therefore pointer is null. Write to log instead.
6862 if (observer_) {
6863 Observer()->OnInterestingUsage(usage_event_accumulator_);
6864 } else {
6865 RTC_LOG(LS_INFO) << "Interesting usage signature "
6866 << usage_event_accumulator_
6867 << " observed after observer shutdown";
6868 }
6869 }
6870 }
6871
ReportNegotiatedSdpSemantics(const SessionDescriptionInterface & answer)6872 void PeerConnection::ReportNegotiatedSdpSemantics(
6873 const SessionDescriptionInterface& answer) {
6874 SdpSemanticNegotiated semantics_negotiated;
6875 switch (answer.description()->msid_signaling()) {
6876 case 0:
6877 semantics_negotiated = kSdpSemanticNegotiatedNone;
6878 break;
6879 case cricket::kMsidSignalingMediaSection:
6880 semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan;
6881 break;
6882 case cricket::kMsidSignalingSsrcAttribute:
6883 semantics_negotiated = kSdpSemanticNegotiatedPlanB;
6884 break;
6885 case cricket::kMsidSignalingMediaSection |
6886 cricket::kMsidSignalingSsrcAttribute:
6887 semantics_negotiated = kSdpSemanticNegotiatedMixed;
6888 break;
6889 default:
6890 RTC_NOTREACHED();
6891 }
6892 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated",
6893 semantics_negotiated, kSdpSemanticNegotiatedMax);
6894 }
6895
6896 // We need to check the local/remote description for the Transport instead of
6897 // the session, because a new Transport added during renegotiation may have
6898 // them unset while the session has them set from the previous negotiation.
6899 // Not doing so may trigger the auto generation of transport description and
6900 // mess up DTLS identity information, ICE credential, etc.
ReadyToUseRemoteCandidate(const IceCandidateInterface * candidate,const SessionDescriptionInterface * remote_desc,bool * valid)6901 bool PeerConnection::ReadyToUseRemoteCandidate(
6902 const IceCandidateInterface* candidate,
6903 const SessionDescriptionInterface* remote_desc,
6904 bool* valid) {
6905 *valid = true;
6906
6907 const SessionDescriptionInterface* current_remote_desc =
6908 remote_desc ? remote_desc : remote_description();
6909
6910 if (!current_remote_desc) {
6911 return false;
6912 }
6913
6914 RTCErrorOr<const cricket::ContentInfo*> result =
6915 FindContentInfo(current_remote_desc, candidate);
6916 if (!result.ok()) {
6917 RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. "
6918 << result.error().message();
6919
6920 *valid = false;
6921 return false;
6922 }
6923
6924 std::string transport_name = GetTransportName(result.value()->name);
6925 return !transport_name.empty();
6926 }
6927
SrtpRequired() const6928 bool PeerConnection::SrtpRequired() const {
6929 return (dtls_enabled_ ||
6930 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED);
6931 }
6932
OnTransportControllerGatheringState(cricket::IceGatheringState state)6933 void PeerConnection::OnTransportControllerGatheringState(
6934 cricket::IceGatheringState state) {
6935 RTC_DCHECK(signaling_thread()->IsCurrent());
6936 if (state == cricket::kIceGatheringGathering) {
6937 OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering);
6938 } else if (state == cricket::kIceGatheringComplete) {
6939 OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete);
6940 }
6941 }
6942
ReportTransportStats()6943 void PeerConnection::ReportTransportStats() {
6944 std::map<std::string, std::set<cricket::MediaType>>
6945 media_types_by_transport_name;
6946 for (const auto& transceiver : transceivers_) {
6947 if (transceiver->internal()->channel()) {
6948 const std::string& transport_name =
6949 transceiver->internal()->channel()->transport_name();
6950 media_types_by_transport_name[transport_name].insert(
6951 transceiver->media_type());
6952 }
6953 }
6954 if (rtp_data_channel()) {
6955 media_types_by_transport_name[rtp_data_channel()->transport_name()].insert(
6956 cricket::MEDIA_TYPE_DATA);
6957 }
6958
6959 absl::optional<std::string> transport_name = sctp_transport_name();
6960 if (transport_name) {
6961 media_types_by_transport_name[*transport_name].insert(
6962 cricket::MEDIA_TYPE_DATA);
6963 }
6964
6965 for (const auto& entry : media_types_by_transport_name) {
6966 const std::string& transport_name = entry.first;
6967 const std::set<cricket::MediaType> media_types = entry.second;
6968 cricket::TransportStats stats;
6969 if (transport_controller_->GetStats(transport_name, &stats)) {
6970 ReportBestConnectionState(stats);
6971 ReportNegotiatedCiphers(stats, media_types);
6972 }
6973 }
6974 }
6975 // Walk through the ConnectionInfos to gather best connection usage
6976 // for IPv4 and IPv6.
ReportBestConnectionState(const cricket::TransportStats & stats)6977 void PeerConnection::ReportBestConnectionState(
6978 const cricket::TransportStats& stats) {
6979 for (const cricket::TransportChannelStats& channel_stats :
6980 stats.channel_stats) {
6981 for (const cricket::ConnectionInfo& connection_info :
6982 channel_stats.ice_transport_stats.connection_infos) {
6983 if (!connection_info.best_connection) {
6984 continue;
6985 }
6986
6987 const cricket::Candidate& local = connection_info.local_candidate;
6988 const cricket::Candidate& remote = connection_info.remote_candidate;
6989
6990 // Increment the counter for IceCandidatePairType.
6991 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
6992 (local.type() == RELAY_PORT_TYPE &&
6993 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
6994 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP",
6995 GetIceCandidatePairCounter(local, remote),
6996 kIceCandidatePairMax);
6997 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
6998 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP",
6999 GetIceCandidatePairCounter(local, remote),
7000 kIceCandidatePairMax);
7001 } else {
7002 RTC_CHECK(0);
7003 }
7004
7005 // Increment the counter for IP type.
7006 if (local.address().family() == AF_INET) {
7007 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics",
7008 kBestConnections_IPv4,
7009 kPeerConnectionAddressFamilyCounter_Max);
7010 } else if (local.address().family() == AF_INET6) {
7011 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics",
7012 kBestConnections_IPv6,
7013 kPeerConnectionAddressFamilyCounter_Max);
7014 } else {
7015 RTC_CHECK(!local.address().hostname().empty() &&
7016 local.address().IsUnresolvedIP());
7017 }
7018
7019 return;
7020 }
7021 }
7022 }
7023
ReportNegotiatedCiphers(const cricket::TransportStats & stats,const std::set<cricket::MediaType> & media_types)7024 void PeerConnection::ReportNegotiatedCiphers(
7025 const cricket::TransportStats& stats,
7026 const std::set<cricket::MediaType>& media_types) {
7027 if (!dtls_enabled_ || stats.channel_stats.empty()) {
7028 return;
7029 }
7030
7031 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
7032 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
7033 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
7034 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
7035 return;
7036 }
7037
7038 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
7039 for (cricket::MediaType media_type : media_types) {
7040 switch (media_type) {
7041 case cricket::MEDIA_TYPE_AUDIO:
7042 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7043 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite,
7044 rtc::SRTP_CRYPTO_SUITE_MAX_VALUE);
7045 break;
7046 case cricket::MEDIA_TYPE_VIDEO:
7047 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7048 "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite,
7049 rtc::SRTP_CRYPTO_SUITE_MAX_VALUE);
7050 break;
7051 case cricket::MEDIA_TYPE_DATA:
7052 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7053 "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite,
7054 rtc::SRTP_CRYPTO_SUITE_MAX_VALUE);
7055 break;
7056 default:
7057 RTC_NOTREACHED();
7058 continue;
7059 }
7060 }
7061 }
7062
7063 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
7064 for (cricket::MediaType media_type : media_types) {
7065 switch (media_type) {
7066 case cricket::MEDIA_TYPE_AUDIO:
7067 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7068 "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite,
7069 rtc::SSL_CIPHER_SUITE_MAX_VALUE);
7070 break;
7071 case cricket::MEDIA_TYPE_VIDEO:
7072 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7073 "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite,
7074 rtc::SSL_CIPHER_SUITE_MAX_VALUE);
7075 break;
7076 case cricket::MEDIA_TYPE_DATA:
7077 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7078 "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite,
7079 rtc::SSL_CIPHER_SUITE_MAX_VALUE);
7080 break;
7081 default:
7082 RTC_NOTREACHED();
7083 continue;
7084 }
7085 }
7086 }
7087 }
7088
OnSentPacket_w(const rtc::SentPacket & sent_packet)7089 void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
7090 RTC_DCHECK_RUN_ON(worker_thread());
7091 RTC_DCHECK(call_);
7092 call_->OnSentPacket(sent_packet);
7093 }
7094
GetTransportName(const std::string & content_name)7095 const std::string PeerConnection::GetTransportName(
7096 const std::string& content_name) {
7097 cricket::ChannelInterface* channel = GetChannel(content_name);
7098 if (channel) {
7099 return channel->transport_name();
7100 }
7101 if (data_channel_controller_.data_channel_transport()) {
7102 RTC_DCHECK(sctp_mid_s_);
7103 if (content_name == *sctp_mid_s_) {
7104 return *sctp_transport_name();
7105 }
7106 }
7107 // Return an empty string if failed to retrieve the transport name.
7108 return "";
7109 }
7110
DestroyTransceiverChannel(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver)7111 void PeerConnection::DestroyTransceiverChannel(
7112 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
7113 transceiver) {
7114 RTC_DCHECK(transceiver);
7115
7116 cricket::ChannelInterface* channel = transceiver->internal()->channel();
7117 if (channel) {
7118 transceiver->internal()->SetChannel(nullptr);
7119 DestroyChannelInterface(channel);
7120 }
7121 }
7122
DestroyDataChannelTransport()7123 void PeerConnection::DestroyDataChannelTransport() {
7124 if (data_channel_controller_.rtp_data_channel()) {
7125 data_channel_controller_.OnTransportChannelClosed();
7126 DestroyChannelInterface(data_channel_controller_.rtp_data_channel());
7127 data_channel_controller_.set_rtp_data_channel(nullptr);
7128 }
7129
7130 // Note: Cannot use rtc::Bind to create a functor to invoke because it will
7131 // grab a reference to this PeerConnection. If this is called from the
7132 // PeerConnection destructor, the RefCountedObject vtable will have already
7133 // been destroyed (since it is a subclass of PeerConnection) and using
7134 // rtc::Bind will cause "Pure virtual function called" error to appear.
7135
7136 if (sctp_mid_s_) {
7137 data_channel_controller_.OnTransportChannelClosed();
7138 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
7139 RTC_DCHECK_RUN_ON(network_thread());
7140 TeardownDataChannelTransport_n();
7141 });
7142 sctp_mid_s_.reset();
7143 }
7144 }
7145
DestroyChannelInterface(cricket::ChannelInterface * channel)7146 void PeerConnection::DestroyChannelInterface(
7147 cricket::ChannelInterface* channel) {
7148 RTC_DCHECK(channel);
7149 switch (channel->media_type()) {
7150 case cricket::MEDIA_TYPE_AUDIO:
7151 channel_manager()->DestroyVoiceChannel(
7152 static_cast<cricket::VoiceChannel*>(channel));
7153 break;
7154 case cricket::MEDIA_TYPE_VIDEO:
7155 channel_manager()->DestroyVideoChannel(
7156 static_cast<cricket::VideoChannel*>(channel));
7157 break;
7158 case cricket::MEDIA_TYPE_DATA:
7159 channel_manager()->DestroyRtpDataChannel(
7160 static_cast<cricket::RtpDataChannel*>(channel));
7161 break;
7162 default:
7163 RTC_NOTREACHED() << "Unknown media type: " << channel->media_type();
7164 break;
7165 }
7166 }
7167
OnTransportChanged(const std::string & mid,RtpTransportInternal * rtp_transport,rtc::scoped_refptr<DtlsTransport> dtls_transport,DataChannelTransportInterface * data_channel_transport)7168 bool PeerConnection::OnTransportChanged(
7169 const std::string& mid,
7170 RtpTransportInternal* rtp_transport,
7171 rtc::scoped_refptr<DtlsTransport> dtls_transport,
7172 DataChannelTransportInterface* data_channel_transport) {
7173 RTC_DCHECK_RUN_ON(network_thread());
7174 bool ret = true;
7175 auto base_channel = GetChannel(mid);
7176 if (base_channel) {
7177 ret = base_channel->SetRtpTransport(rtp_transport);
7178 }
7179 if (mid == sctp_mid_n_) {
7180 data_channel_controller_.OnTransportChanged(data_channel_transport);
7181 }
7182 return ret;
7183 }
7184
OnSetStreams()7185 void PeerConnection::OnSetStreams() {
7186 RTC_DCHECK_RUN_ON(signaling_thread());
7187 if (IsUnifiedPlan())
7188 UpdateNegotiationNeeded();
7189 }
7190
Observer() const7191 PeerConnectionObserver* PeerConnection::Observer() const {
7192 RTC_DCHECK_RUN_ON(signaling_thread());
7193 RTC_DCHECK(observer_);
7194 return observer_;
7195 }
7196
GetCryptoOptions()7197 CryptoOptions PeerConnection::GetCryptoOptions() {
7198 // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions
7199 // after it has been removed.
7200 return configuration_.crypto_options.has_value()
7201 ? *configuration_.crypto_options
7202 : factory_->options().crypto_options;
7203 }
7204
ClearStatsCache()7205 void PeerConnection::ClearStatsCache() {
7206 RTC_DCHECK_RUN_ON(signaling_thread());
7207 if (stats_collector_) {
7208 stats_collector_->ClearCachedStatsReport();
7209 }
7210 }
7211
RequestUsagePatternReportForTesting()7212 void PeerConnection::RequestUsagePatternReportForTesting() {
7213 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN,
7214 nullptr);
7215 }
7216
UpdateNegotiationNeeded()7217 void PeerConnection::UpdateNegotiationNeeded() {
7218 RTC_DCHECK_RUN_ON(signaling_thread());
7219 if (!IsUnifiedPlan()) {
7220 Observer()->OnRenegotiationNeeded();
7221 return;
7222 }
7223
7224 // If connection's [[IsClosed]] slot is true, abort these steps.
7225 if (IsClosed())
7226 return;
7227
7228 // If connection's signaling state is not "stable", abort these steps.
7229 if (signaling_state() != kStable)
7230 return;
7231
7232 // NOTE
7233 // The negotiation-needed flag will be updated once the state transitions to
7234 // "stable", as part of the steps for setting an RTCSessionDescription.
7235
7236 // If the result of checking if negotiation is needed is false, clear the
7237 // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot
7238 // to false, and abort these steps.
7239 bool is_negotiation_needed = CheckIfNegotiationIsNeeded();
7240 if (!is_negotiation_needed) {
7241 is_negotiation_needed_ = false;
7242 return;
7243 }
7244
7245 // If connection's [[NegotiationNeeded]] slot is already true, abort these
7246 // steps.
7247 if (is_negotiation_needed_)
7248 return;
7249
7250 // Set connection's [[NegotiationNeeded]] slot to true.
7251 is_negotiation_needed_ = true;
7252
7253 // Queue a task that runs the following steps:
7254 // If connection's [[IsClosed]] slot is true, abort these steps.
7255 // If connection's [[NegotiationNeeded]] slot is false, abort these steps.
7256 // Fire an event named negotiationneeded at connection.
7257 Observer()->OnRenegotiationNeeded();
7258 }
7259
CheckIfNegotiationIsNeeded()7260 bool PeerConnection::CheckIfNegotiationIsNeeded() {
7261 RTC_DCHECK_RUN_ON(signaling_thread());
7262 // 1. If any implementation-specific negotiation is required, as described at
7263 // the start of this section, return true.
7264
7265 // 2. If connection's [[RestartIce]] internal slot is true, return true.
7266 if (local_ice_credentials_to_replace_->HasIceCredentials()) {
7267 return true;
7268 }
7269
7270 // 3. Let description be connection.[[CurrentLocalDescription]].
7271 const SessionDescriptionInterface* description = current_local_description();
7272 if (!description)
7273 return true;
7274
7275 // 4. If connection has created any RTCDataChannels, and no m= section in
7276 // description has been negotiated yet for data, return true.
7277 if (data_channel_controller_.HasSctpDataChannels()) {
7278 if (!cricket::GetFirstDataContent(description->description()->contents()))
7279 return true;
7280 }
7281
7282 // 5. For each transceiver in connection's set of transceivers, perform the
7283 // following checks:
7284 for (const auto& transceiver : transceivers_) {
7285 const ContentInfo* current_local_msection =
7286 FindTransceiverMSection(transceiver.get(), description);
7287
7288 const ContentInfo* current_remote_msection = FindTransceiverMSection(
7289 transceiver.get(), current_remote_description());
7290
7291 // 5.3 If transceiver is stopped and is associated with an m= section,
7292 // but the associated m= section is not yet rejected in
7293 // connection.[[CurrentLocalDescription]] or
7294 // connection.[[CurrentRemoteDescription]], return true.
7295 if (transceiver->stopped()) {
7296 if (current_local_msection && !current_local_msection->rejected &&
7297 ((current_remote_msection && !current_remote_msection->rejected) ||
7298 !current_remote_msection)) {
7299 return true;
7300 }
7301 continue;
7302 }
7303
7304 // 5.1 If transceiver isn't stopped and isn't yet associated with an m=
7305 // section in description, return true.
7306 if (!current_local_msection)
7307 return true;
7308
7309 const MediaContentDescription* current_local_media_description =
7310 current_local_msection->media_description();
7311 // 5.2 If transceiver isn't stopped and is associated with an m= section
7312 // in description then perform the following checks:
7313
7314 // 5.2.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the
7315 // associated m= section in description either doesn't contain a single
7316 // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this
7317 // m= section, or the MSID values themselves, differ from what is in
7318 // transceiver.sender.[[AssociatedMediaStreamIds]], return true.
7319 if (RtpTransceiverDirectionHasSend(transceiver->direction())) {
7320 if (current_local_media_description->streams().size() == 0)
7321 return true;
7322
7323 std::vector<std::string> msection_msids;
7324 for (const auto& stream : current_local_media_description->streams()) {
7325 for (const std::string& msid : stream.stream_ids())
7326 msection_msids.push_back(msid);
7327 }
7328
7329 std::vector<std::string> transceiver_msids =
7330 transceiver->sender()->stream_ids();
7331 if (msection_msids.size() != transceiver_msids.size())
7332 return true;
7333
7334 absl::c_sort(transceiver_msids);
7335 absl::c_sort(msection_msids);
7336 if (transceiver_msids != msection_msids)
7337 return true;
7338 }
7339
7340 // 5.2.2 If description is of type "offer", and the direction of the
7341 // associated m= section in neither connection.[[CurrentLocalDescription]]
7342 // nor connection.[[CurrentRemoteDescription]] matches
7343 // transceiver.[[Direction]], return true.
7344 if (description->GetType() == SdpType::kOffer) {
7345 if (!current_remote_description())
7346 return true;
7347
7348 if (!current_remote_msection)
7349 return true;
7350
7351 RtpTransceiverDirection current_local_direction =
7352 current_local_media_description->direction();
7353 RtpTransceiverDirection current_remote_direction =
7354 current_remote_msection->media_description()->direction();
7355 if (transceiver->direction() != current_local_direction &&
7356 transceiver->direction() !=
7357 RtpTransceiverDirectionReversed(current_remote_direction)) {
7358 return true;
7359 }
7360 }
7361
7362 // 5.2.3 If description is of type "answer", and the direction of the
7363 // associated m= section in the description does not match
7364 // transceiver.[[Direction]] intersected with the offered direction (as
7365 // described in [JSEP] (section 5.3.1.)), return true.
7366 if (description->GetType() == SdpType::kAnswer) {
7367 if (!remote_description())
7368 return true;
7369
7370 const ContentInfo* offered_remote_msection =
7371 FindTransceiverMSection(transceiver.get(), remote_description());
7372
7373 RtpTransceiverDirection offered_direction =
7374 offered_remote_msection
7375 ? offered_remote_msection->media_description()->direction()
7376 : RtpTransceiverDirection::kInactive;
7377
7378 if (current_local_media_description->direction() !=
7379 (RtpTransceiverDirectionIntersection(
7380 transceiver->direction(),
7381 RtpTransceiverDirectionReversed(offered_direction)))) {
7382 return true;
7383 }
7384 }
7385 }
7386
7387 // If all the preceding checks were performed and true was not returned,
7388 // nothing remains to be negotiated; return false.
7389 return false;
7390 }
7391
Rollback(SdpType sdp_type)7392 RTCError PeerConnection::Rollback(SdpType sdp_type) {
7393 auto state = signaling_state();
7394 if (state != PeerConnectionInterface::kHaveLocalOffer &&
7395 state != PeerConnectionInterface::kHaveRemoteOffer) {
7396 return RTCError(RTCErrorType::INVALID_STATE,
7397 "Called in wrong signalingState: " +
7398 GetSignalingStateString(signaling_state()));
7399 }
7400 RTC_DCHECK_RUN_ON(signaling_thread());
7401 RTC_DCHECK(IsUnifiedPlan());
7402 std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_added_streams;
7403 std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_removed_streams;
7404 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> removed_receivers;
7405
7406 for (auto&& transceivers_stable_state_pair :
7407 transceiver_stable_states_by_transceivers_) {
7408 auto transceiver = transceivers_stable_state_pair.first;
7409 auto state = transceivers_stable_state_pair.second;
7410
7411 if (state.remote_stream_ids()) {
7412 std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
7413 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
7414 SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(),
7415 state.remote_stream_ids().value(),
7416 &added_streams, &removed_streams);
7417 all_added_streams.insert(all_added_streams.end(), added_streams.begin(),
7418 added_streams.end());
7419 all_removed_streams.insert(all_removed_streams.end(),
7420 removed_streams.begin(),
7421 removed_streams.end());
7422 if (!state.has_m_section() && !state.newly_created()) {
7423 continue;
7424 }
7425 }
7426
7427 RTC_DCHECK(transceiver->internal()->mid().has_value());
7428 DestroyTransceiverChannel(transceiver);
7429
7430 if (signaling_state() == PeerConnectionInterface::kHaveRemoteOffer &&
7431 transceiver->receiver()) {
7432 removed_receivers.push_back(transceiver->receiver());
7433 }
7434 if (state.newly_created()) {
7435 if (transceiver->internal()->reused_for_addtrack()) {
7436 transceiver->internal()->set_created_by_addtrack(true);
7437 } else {
7438 int remaining_transceiver_count = 0;
7439 for (auto&& t : transceivers_) {
7440 if (t != transceiver) {
7441 transceivers_[remaining_transceiver_count++] = t;
7442 }
7443 }
7444 transceivers_.resize(remaining_transceiver_count);
7445 }
7446 }
7447 transceiver->internal()->sender_internal()->set_transport(nullptr);
7448 transceiver->internal()->receiver_internal()->set_transport(nullptr);
7449 transceiver->internal()->set_mid(state.mid());
7450 transceiver->internal()->set_mline_index(state.mline_index());
7451 }
7452 transport_controller_->RollbackTransports();
7453 if (have_pending_rtp_data_channel_) {
7454 DestroyDataChannelTransport();
7455 have_pending_rtp_data_channel_ = false;
7456 }
7457 transceiver_stable_states_by_transceivers_.clear();
7458 pending_local_description_.reset();
7459 pending_remote_description_.reset();
7460 ChangeSignalingState(PeerConnectionInterface::kStable);
7461
7462 // Once all processing has finished, fire off callbacks.
7463 for (const auto& receiver : removed_receivers) {
7464 Observer()->OnRemoveTrack(receiver);
7465 }
7466 for (const auto& stream : all_added_streams) {
7467 Observer()->OnAddStream(stream);
7468 }
7469 for (const auto& stream : all_removed_streams) {
7470 Observer()->OnRemoveStream(stream);
7471 }
7472
7473 // The assumption is that in case of implicit rollback UpdateNegotiationNeeded
7474 // gets called in SetRemoteDescription.
7475 if (sdp_type == SdpType::kRollback) {
7476 UpdateNegotiationNeeded();
7477 if (is_negotiation_needed_) {
7478 Observer()->OnRenegotiationNeeded();
7479 }
7480 }
7481 return RTCError::OK();
7482 }
7483
7484 } // namespace webrtc
7485