blob: 05e7b95591e0c322286b720d4ed96bf56e2ddeb4 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/peer_connection.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
deadbeefeb459812015-12-15 19:24:43 -080013#include <algorithm>
Harald Alvestrand8ebba742018-05-31 14:00:34 +020014#include <limits>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
Steve Antondcc3c022017-12-22 16:02:54 -080016#include <queue>
Steve Anton75737c02017-11-06 10:37:17 -080017#include <set>
kwiberg0eb15ed2015-12-17 03:04:15 -080018#include <utility>
19#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
Steve Anton64b626b2019-01-28 17:25:26 -080021#include "absl/algorithm/container.h"
Fredrik Solenberg41f3a432018-12-17 21:02:22 +010022#include "absl/strings/match.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/jsep_ice_candidate.h"
24#include "api/jsep_session_description.h"
25#include "api/media_stream_proxy.h"
26#include "api/media_stream_track_proxy.h"
Amit Hilbuchf4770402019-04-08 14:11:57 -070027#include "api/rtc_error.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020028#include "api/rtc_event_log/rtc_event_log.h"
Niels Möllerd8b9ed72019-05-08 13:53:51 +020029#include "api/rtc_event_log_output_file.h"
Amit Hilbuchf4770402019-04-08 14:11:57 -070030#include "api/rtp_parameters.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "api/uma_metrics.h"
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +020032#include "api/video/builtin_video_bitrate_allocator_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "call/call.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "logging/rtc_event_log/ice_logger.h"
Amit Hilbuchf4770402019-04-08 14:11:57 -070035#include "media/base/rid_description.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "media/sctp/sctp_transport.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010037#include "pc/audio_rtp_receiver.h"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "pc/audio_track.h"
Steve Anton75737c02017-11-06 10:37:17 -080039#include "pc/channel.h"
Steve Anton10542f22019-01-11 09:11:00 -080040#include "pc/channel_manager.h"
41#include "pc/dtmf_sender.h"
42#include "pc/media_stream.h"
43#include "pc/media_stream_observer.h"
44#include "pc/remote_audio_source.h"
45#include "pc/rtp_media_utils.h"
46#include "pc/rtp_receiver.h"
47#include "pc/rtp_sender.h"
Harald Alvestrandc85328f2019-02-28 07:51:00 +010048#include "pc/sctp_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "pc/sctp_utils.h"
50#include "pc/sdp_utils.h"
51#include "pc/stream_collection.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010052#include "pc/video_rtp_receiver.h"
Steve Anton10542f22019-01-11 09:11:00 -080053#include "pc/video_track.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/bind.h"
55#include "rtc_base/checks.h"
56#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080057#include "rtc_base/string_encode.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020058#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/trace_event.h"
60#include "system_wrappers/include/clock.h"
61#include "system_wrappers/include/field_trial.h"
Qingsi Wang7fc821d2018-07-12 12:54:53 -070062#include "system_wrappers/include/metrics.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
Steve Anton75737c02017-11-06 10:37:17 -080064using cricket::ContentInfo;
65using cricket::ContentInfos;
66using cricket::MediaContentDescription;
Steve Anton5adfafd2017-12-20 16:34:00 -080067using cricket::MediaProtocolType;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -080068using cricket::RidDescription;
69using cricket::RidDirection;
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080070using cricket::SessionDescription;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -080071using cricket::SimulcastDescription;
72using cricket::SimulcastLayer;
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080073using cricket::SimulcastLayerList;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -080074using cricket::StreamParams;
Steve Anton75737c02017-11-06 10:37:17 -080075using cricket::TransportInfo;
76
77using cricket::LOCAL_PORT_TYPE;
Steve Anton75737c02017-11-06 10:37:17 -080078using cricket::PRFLX_PORT_TYPE;
Jeroen de Borstaf242c82019-04-24 13:13:48 -070079using cricket::RELAY_PORT_TYPE;
80using cricket::STUN_PORT_TYPE;
Steve Anton75737c02017-11-06 10:37:17 -080081
Steve Antonba818672017-11-06 10:21:57 -080082namespace webrtc {
83
Steve Anton75737c02017-11-06 10:37:17 -080084// Error messages
85const char kBundleWithoutRtcpMux[] =
86 "rtcp-mux must be enabled when BUNDLE "
87 "is enabled.";
Steve Anton75737c02017-11-06 10:37:17 -080088const char kInvalidCandidates[] = "Description contains invalid candidates.";
89const char kInvalidSdp[] = "Invalid session description.";
90const char kMlineMismatchInAnswer[] =
91 "The order of m-lines in answer doesn't match order in offer. Rejecting "
92 "answer.";
93const char kMlineMismatchInSubsequentOffer[] =
94 "The order of m-lines in subsequent offer doesn't match order from "
95 "previous offer/answer.";
Steve Anton75737c02017-11-06 10:37:17 -080096const char kSdpWithoutDtlsFingerprint[] =
97 "Called with SDP without DTLS fingerprint.";
98const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto.";
99const char kSdpWithoutIceUfragPwd[] =
100 "Called with SDP without ice-ufrag and ice-pwd.";
101const char kSessionError[] = "Session error code: ";
102const char kSessionErrorDesc[] = "Session error description: ";
103const char kDtlsSrtpSetupFailureRtp[] =
104 "Couldn't set up DTLS-SRTP on RTP channel.";
105const char kDtlsSrtpSetupFailureRtcp[] =
106 "Couldn't set up DTLS-SRTP on RTCP channel.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
Steve Anton75737c02017-11-06 10:37:17 -0800108namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
Bjorn A Mellem5985a042019-06-28 14:19:38 -0700110// Field trials.
111// Controls datagram transport support.
112const char kDatagramTransportFieldTrial[] = "WebRTC-DatagramTransport";
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700113// Controls datagram transport data channel support.
114const char kDatagramTransportDataChannelFieldTrial[] =
115 "WebRTC-DatagramTransportDataChannels";
Bjorn A Mellem5985a042019-06-28 14:19:38 -0700116
Amit Hilbuche2a284d2019-03-05 12:36:31 -0800117// UMA metric names.
118const char kSimulcastVersionApplyLocalDescription[] =
119 "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription";
120const char kSimulcastVersionApplyRemoteDescription[] =
121 "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription";
122const char kSimulcastNumberOfEncodings[] =
123 "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings";
124const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled";
125
Seth Hampson845e8782018-03-02 11:34:10 -0800126static const char kDefaultStreamId[] = "default";
Steve Anton4171afb2017-11-20 10:20:22 -0800127static const char kDefaultAudioSenderId[] = "defaulta0";
128static const char kDefaultVideoSenderId[] = "defaultv0";
deadbeefab9b2d12015-10-14 11:33:11 -0700129
zhihuang8f65cdf2016-05-06 18:40:30 -0700130// The length of RTCP CNAMEs.
131static const int kRtcpCnameLength = 16;
132
Steve Antonad182762018-09-05 20:22:40 +0000133enum {
134 MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0,
135 MSG_SET_SESSIONDESCRIPTION_FAILED,
136 MSG_CREATE_SESSIONDESCRIPTION_FAILED,
137 MSG_GETSTATS,
Steve Antonad182762018-09-05 20:22:40 +0000138 MSG_REPORT_USAGE_PATTERN,
139};
140
Harald Alvestrand19793842018-06-25 12:03:50 +0200141static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000;
142
Steve Antonad182762018-09-05 20:22:40 +0000143struct SetSessionDescriptionMsg : public rtc::MessageData {
144 explicit SetSessionDescriptionMsg(
145 webrtc::SetSessionDescriptionObserver* observer)
146 : observer(observer) {}
147
148 rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer;
149 RTCError error;
150};
151
152struct CreateSessionDescriptionMsg : public rtc::MessageData {
153 explicit CreateSessionDescriptionMsg(
154 webrtc::CreateSessionDescriptionObserver* observer)
155 : observer(observer) {}
156
157 rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
158 RTCError error;
159};
160
161struct GetStatsMsg : public rtc::MessageData {
162 GetStatsMsg(webrtc::StatsObserver* observer,
163 webrtc::MediaStreamTrackInterface* track)
164 : observer(observer), track(track) {}
165 rtc::scoped_refptr<webrtc::StatsObserver> observer;
166 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track;
167};
168
deadbeefab9b2d12015-10-14 11:33:11 -0700169// Check if we can send |new_stream| on a PeerConnection.
170bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams,
171 webrtc::MediaStreamInterface* new_stream) {
172 if (!new_stream || !current_streams) {
173 return false;
174 }
Seth Hampson13b8bad2018-03-13 16:05:28 -0700175 if (current_streams->find(new_stream->id()) != nullptr) {
176 RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id()
Mirko Bonadei675513b2017-11-09 11:09:25 +0100177 << " is already added.";
deadbeefab9b2d12015-10-14 11:33:11 -0700178 return false;
179 }
180 return true;
181}
182
deadbeef5e97fb52015-10-15 12:49:08 -0700183// If the direction is "recvonly" or "inactive", treat the description
184// as containing no streams.
185// See: https://code.google.com/p/webrtc/issues/detail?id=5054
186std::vector<cricket::StreamParams> GetActiveStreams(
187 const cricket::MediaContentDescription* desc) {
Steve Anton4e70a722017-11-28 14:57:10 -0800188 return RtpTransceiverDirectionHasSend(desc->direction())
deadbeef5e97fb52015-10-15 12:49:08 -0700189 ? desc->streams()
190 : std::vector<cricket::StreamParams>();
191}
192
deadbeefab9b2d12015-10-14 11:33:11 -0700193bool IsValidOfferToReceiveMedia(int value) {
194 typedef PeerConnectionInterface::RTCOfferAnswerOptions Options;
195 return (value >= Options::kUndefined) &&
196 (value <= Options::kMaxOfferToReceiveMedia);
197}
198
zhihuang1c378ed2017-08-17 14:10:50 -0700199// Add options to |[audio/video]_media_description_options| from |senders|.
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800200void AddPlanBRtpSenderOptions(
deadbeefa601f5c2016-06-06 14:27:39 -0700201 const std::vector<rtc::scoped_refptr<
202 RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders,
zhihuang1c378ed2017-08-17 14:10:50 -0700203 cricket::MediaDescriptionOptions* audio_media_description_options,
Jonas Orelandfc1acd22018-08-24 10:58:37 +0200204 cricket::MediaDescriptionOptions* video_media_description_options,
205 int num_sim_layers) {
olka3c747662017-08-17 06:50:32 -0700206 for (const auto& sender : senders) {
zhihuang1c378ed2017-08-17 14:10:50 -0700207 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
208 if (audio_media_description_options) {
209 audio_media_description_options->AddAudioSender(
Steve Anton8ffb9c32017-08-31 15:45:38 -0700210 sender->id(), sender->internal()->stream_ids());
zhihuang1c378ed2017-08-17 14:10:50 -0700211 }
212 } else {
213 RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO);
214 if (video_media_description_options) {
215 video_media_description_options->AddVideoSender(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800216 sender->id(), sender->internal()->stream_ids(), {},
217 SimulcastLayerList(), num_sim_layers);
zhihuang1c378ed2017-08-17 14:10:50 -0700218 }
219 }
zhihuanga77e6bb2017-08-14 18:17:48 -0700220 }
zhihuang1c378ed2017-08-17 14:10:50 -0700221}
olka3c747662017-08-17 06:50:32 -0700222
zhihuang1c378ed2017-08-17 14:10:50 -0700223// Add options to |session_options| from |rtp_data_channels|.
224void AddRtpDataChannelOptions(
225 const std::map<std::string, rtc::scoped_refptr<DataChannel>>&
226 rtp_data_channels,
227 cricket::MediaDescriptionOptions* data_media_description_options) {
228 if (!data_media_description_options) {
229 return;
230 }
deadbeefab9b2d12015-10-14 11:33:11 -0700231 // Check for data channels.
232 for (const auto& kv : rtp_data_channels) {
233 const DataChannel* channel = kv.second;
234 if (channel->state() == DataChannel::kConnecting ||
235 channel->state() == DataChannel::kOpen) {
zhihuang1c378ed2017-08-17 14:10:50 -0700236 // Legacy RTP data channels are signaled with the track/stream ID set to
237 // the data channel's label.
238 data_media_description_options->AddRtpDataChannel(channel->label(),
239 channel->label());
deadbeefab9b2d12015-10-14 11:33:11 -0700240 }
241 }
242}
243
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700244uint32_t ConvertIceTransportTypeToCandidateFilter(
245 PeerConnectionInterface::IceTransportsType type) {
246 switch (type) {
247 case PeerConnectionInterface::kNone:
248 return cricket::CF_NONE;
249 case PeerConnectionInterface::kRelay:
250 return cricket::CF_RELAY;
251 case PeerConnectionInterface::kNoHost:
252 return (cricket::CF_ALL & ~cricket::CF_HOST);
253 case PeerConnectionInterface::kAll:
254 return cricket::CF_ALL;
255 default:
nissec80e7412017-01-11 05:56:46 -0800256 RTC_NOTREACHED();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700257 }
258 return cricket::CF_NONE;
259}
260
Steve Antonba818672017-11-06 10:21:57 -0800261std::string GetSignalingStateString(
262 PeerConnectionInterface::SignalingState state) {
263 switch (state) {
264 case PeerConnectionInterface::kStable:
265 return "kStable";
266 case PeerConnectionInterface::kHaveLocalOffer:
267 return "kHaveLocalOffer";
268 case PeerConnectionInterface::kHaveLocalPrAnswer:
269 return "kHavePrAnswer";
270 case PeerConnectionInterface::kHaveRemoteOffer:
271 return "kHaveRemoteOffer";
272 case PeerConnectionInterface::kHaveRemotePrAnswer:
273 return "kHaveRemotePrAnswer";
274 case PeerConnectionInterface::kClosed:
275 return "kClosed";
276 }
277 RTC_NOTREACHED();
278 return "";
279}
deadbeef0a6c4ca2015-10-06 11:38:28 -0700280
Steve Anton75737c02017-11-06 10:37:17 -0800281IceCandidatePairType GetIceCandidatePairCounter(
282 const cricket::Candidate& local,
283 const cricket::Candidate& remote) {
284 const auto& l = local.type();
285 const auto& r = remote.type();
286 const auto& host = LOCAL_PORT_TYPE;
287 const auto& srflx = STUN_PORT_TYPE;
288 const auto& relay = RELAY_PORT_TYPE;
289 const auto& prflx = PRFLX_PORT_TYPE;
290 if (l == host && r == host) {
Jeroen de Borst833979f2018-12-13 08:25:54 -0800291 bool local_hostname =
292 !local.address().hostname().empty() && local.address().IsUnresolvedIP();
293 bool remote_hostname = !remote.address().hostname().empty() &&
294 remote.address().IsUnresolvedIP();
Steve Anton75737c02017-11-06 10:37:17 -0800295 bool local_private = IPIsPrivate(local.address().ipaddr());
296 bool remote_private = IPIsPrivate(remote.address().ipaddr());
Jeroen de Borst833979f2018-12-13 08:25:54 -0800297 if (local_hostname) {
298 if (remote_hostname) {
299 return kIceCandidatePairHostNameHostName;
300 } else if (remote_private) {
301 return kIceCandidatePairHostNameHostPrivate;
302 } else {
303 return kIceCandidatePairHostNameHostPublic;
304 }
305 } else if (local_private) {
306 if (remote_hostname) {
307 return kIceCandidatePairHostPrivateHostName;
308 } else if (remote_private) {
Steve Anton75737c02017-11-06 10:37:17 -0800309 return kIceCandidatePairHostPrivateHostPrivate;
310 } else {
311 return kIceCandidatePairHostPrivateHostPublic;
312 }
313 } else {
Jeroen de Borst833979f2018-12-13 08:25:54 -0800314 if (remote_hostname) {
315 return kIceCandidatePairHostPublicHostName;
316 } else if (remote_private) {
Steve Anton75737c02017-11-06 10:37:17 -0800317 return kIceCandidatePairHostPublicHostPrivate;
318 } else {
319 return kIceCandidatePairHostPublicHostPublic;
320 }
321 }
322 }
323 if (l == host && r == srflx)
324 return kIceCandidatePairHostSrflx;
325 if (l == host && r == relay)
326 return kIceCandidatePairHostRelay;
327 if (l == host && r == prflx)
328 return kIceCandidatePairHostPrflx;
329 if (l == srflx && r == host)
330 return kIceCandidatePairSrflxHost;
331 if (l == srflx && r == srflx)
332 return kIceCandidatePairSrflxSrflx;
333 if (l == srflx && r == relay)
334 return kIceCandidatePairSrflxRelay;
335 if (l == srflx && r == prflx)
336 return kIceCandidatePairSrflxPrflx;
337 if (l == relay && r == host)
338 return kIceCandidatePairRelayHost;
339 if (l == relay && r == srflx)
340 return kIceCandidatePairRelaySrflx;
341 if (l == relay && r == relay)
342 return kIceCandidatePairRelayRelay;
343 if (l == relay && r == prflx)
344 return kIceCandidatePairRelayPrflx;
345 if (l == prflx && r == host)
346 return kIceCandidatePairPrflxHost;
347 if (l == prflx && r == srflx)
348 return kIceCandidatePairPrflxSrflx;
349 if (l == prflx && r == relay)
350 return kIceCandidatePairPrflxRelay;
351 return kIceCandidatePairMax;
352}
353
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800354// Logic to decide if an m= section can be recycled. This means that the new
355// m= section is not rejected, but the old local or remote m= section is
356// rejected. |old_content_one| and |old_content_two| refer to the m= section
357// of the old remote and old local descriptions in no particular order.
358// We need to check both the old local and remote because either
359// could be the most current from the latest negotation.
360bool IsMediaSectionBeingRecycled(SdpType type,
361 const ContentInfo& content,
362 const ContentInfo* old_content_one,
363 const ContentInfo* old_content_two) {
364 return type == SdpType::kOffer && !content.rejected &&
365 ((old_content_one && old_content_one->rejected) ||
366 (old_content_two && old_content_two->rejected));
367}
368
Steve Anton75737c02017-11-06 10:37:17 -0800369// Verify that the order of media sections in |new_desc| matches
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800370// |current_desc|. The number of m= sections in |new_desc| should be no
371// less than |current_desc|. In the case of checking an answer's
372// |new_desc|, the |current_desc| is the last offer that was set as the
373// local or remote. In the case of checking an offer's |new_desc| we
374// check against the local and remote descriptions stored from the last
375// negotiation, because either of these could be the most up to date for
376// possible rejected m sections. These are the |current_desc| and
377// |secondary_current_desc|.
378bool MediaSectionsInSameOrder(const SessionDescription& current_desc,
379 const SessionDescription* secondary_current_desc,
380 const SessionDescription& new_desc,
381 const SdpType type) {
382 if (current_desc.contents().size() > new_desc.contents().size()) {
Steve Anton75737c02017-11-06 10:37:17 -0800383 return false;
384 }
385
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800386 for (size_t i = 0; i < current_desc.contents().size(); ++i) {
387 const cricket::ContentInfo* secondary_content_info = nullptr;
388 if (secondary_current_desc &&
389 i < secondary_current_desc->contents().size()) {
390 secondary_content_info = &secondary_current_desc->contents()[i];
391 }
392 if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i],
393 &current_desc.contents()[i],
394 secondary_content_info)) {
395 // For new offer descriptions, if the media section can be recycled, it's
396 // valid for the MID and media type to change.
Steve Antondcc3c022017-12-22 16:02:54 -0800397 continue;
398 }
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800399 if (new_desc.contents()[i].name != current_desc.contents()[i].name) {
Steve Anton75737c02017-11-06 10:37:17 -0800400 return false;
401 }
402 const MediaContentDescription* new_desc_mdesc =
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800403 new_desc.contents()[i].media_description();
404 const MediaContentDescription* current_desc_mdesc =
405 current_desc.contents()[i].media_description();
406 if (new_desc_mdesc->type() != current_desc_mdesc->type()) {
Steve Anton75737c02017-11-06 10:37:17 -0800407 return false;
408 }
409 }
410 return true;
411}
412
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800413bool MediaSectionsHaveSameCount(const SessionDescription& desc1,
414 const SessionDescription& desc2) {
415 return desc1.contents().size() == desc2.contents().size();
Steve Anton75737c02017-11-06 10:37:17 -0800416}
417
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700418void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type,
419 cricket::MediaType media_type) {
Mirko Bonadeiab499822018-09-11 10:43:20 +0200420 // Array of structs needed to map {KeyExchangeProtocolType,
421 // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in
422 // order to avoid -Wglobal-constructors and -Wexit-time-destructors.
423 static constexpr struct {
424 KeyExchangeProtocolType protocol_type;
425 cricket::MediaType media_type;
426 KeyExchangeProtocolMedia protocol_media;
427 } kEnumCounterKeyProtocolMediaMap[] = {
428 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO,
429 kEnumCounterKeyProtocolMediaTypeDtlsAudio},
430 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO,
431 kEnumCounterKeyProtocolMediaTypeDtlsVideo},
432 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA,
433 kEnumCounterKeyProtocolMediaTypeDtlsData},
434 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO,
435 kEnumCounterKeyProtocolMediaTypeSdesAudio},
436 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO,
437 kEnumCounterKeyProtocolMediaTypeSdesVideo},
438 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA,
439 kEnumCounterKeyProtocolMediaTypeSdesData},
440 };
441
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700442 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type,
443 kEnumCounterKeyProtocolMax);
Harald Alvestrandf9d0f1d2018-03-02 14:15:26 +0100444
Mirko Bonadeiab499822018-09-11 10:43:20 +0200445 for (const auto& i : kEnumCounterKeyProtocolMediaMap) {
446 if (i.protocol_type == protocol_type && i.media_type == media_type) {
447 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia",
448 i.protocol_media,
449 kEnumCounterKeyProtocolMediaTypeMax);
450 }
Harald Alvestrandf9d0f1d2018-03-02 14:15:26 +0100451 }
452}
453
Harald Alvestrand76829d72018-07-18 23:24:36 +0200454void NoteAddIceCandidateResult(int result) {
455 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result,
456 kAddIceCandidateMax);
457}
458
Steve Anton75737c02017-11-06 10:37:17 -0800459// Checks that each non-rejected content has SDES crypto keys or a DTLS
460// fingerprint, unless it's in a BUNDLE group, in which case only the
461// BUNDLE-tag section (first media section/description in the BUNDLE group)
462// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
463// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
464// by Channel's |srtp_required| check.
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700465RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) {
Steve Anton75737c02017-11-06 10:37:17 -0800466 const cricket::ContentGroup* bundle =
467 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
Steve Anton8a006912017-12-04 15:25:56 -0800468 for (const cricket::ContentInfo& content_info : desc->contents()) {
469 if (content_info.rejected) {
Steve Anton75737c02017-11-06 10:37:17 -0800470 continue;
471 }
Harald Alvestrand2e180612018-03-07 10:56:14 +0100472 // Note what media is used with each crypto protocol, for all sections.
473 NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls
474 : webrtc::kEnumCounterKeyProtocolSdes,
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700475 content_info.media_description()->type());
Steve Anton8a006912017-12-04 15:25:56 -0800476 const std::string& mid = content_info.name;
477 if (bundle && bundle->HasContentName(mid) &&
478 mid != *(bundle->FirstContentName())) {
Steve Anton75737c02017-11-06 10:37:17 -0800479 // This isn't the first media section in the BUNDLE group, so it's not
480 // required to have crypto attributes, since only the crypto attributes
481 // from the first section actually get used.
482 continue;
483 }
484
485 // If the content isn't rejected or bundled into another m= section, crypto
486 // must be present.
Steve Antonb1c1de12017-12-21 15:14:30 -0800487 const MediaContentDescription* media = content_info.media_description();
Steve Anton8a006912017-12-04 15:25:56 -0800488 const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
Steve Anton75737c02017-11-06 10:37:17 -0800489 if (!media || !tinfo) {
490 // Something is not right.
Steve Anton8a006912017-12-04 15:25:56 -0800491 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
Steve Anton75737c02017-11-06 10:37:17 -0800492 }
493 if (dtls_enabled) {
494 if (!tinfo->description.identity_fingerprint) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100495 RTC_LOG(LS_WARNING)
496 << "Session description must have DTLS fingerprint if "
497 "DTLS enabled.";
Steve Anton8a006912017-12-04 15:25:56 -0800498 return RTCError(RTCErrorType::INVALID_PARAMETER,
499 kSdpWithoutDtlsFingerprint);
Steve Anton75737c02017-11-06 10:37:17 -0800500 }
501 } else {
502 if (media->cryptos().empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100503 RTC_LOG(LS_WARNING)
Steve Anton75737c02017-11-06 10:37:17 -0800504 << "Session description must have SDES when DTLS disabled.";
Steve Anton8a006912017-12-04 15:25:56 -0800505 return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto);
Steve Anton75737c02017-11-06 10:37:17 -0800506 }
507 }
508 }
Steve Anton8a006912017-12-04 15:25:56 -0800509 return RTCError::OK();
Steve Anton75737c02017-11-06 10:37:17 -0800510}
511
512// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
513// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
514// media section/description in the BUNDLE group) needs a ufrag and pwd.
515bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
516 const cricket::ContentGroup* bundle =
517 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
Steve Anton8a006912017-12-04 15:25:56 -0800518 for (const cricket::ContentInfo& content_info : desc->contents()) {
519 if (content_info.rejected) {
Steve Anton75737c02017-11-06 10:37:17 -0800520 continue;
521 }
Steve Anton8a006912017-12-04 15:25:56 -0800522 const std::string& mid = content_info.name;
523 if (bundle && bundle->HasContentName(mid) &&
524 mid != *(bundle->FirstContentName())) {
Steve Anton75737c02017-11-06 10:37:17 -0800525 // This isn't the first media section in the BUNDLE group, so it's not
526 // required to have ufrag/password, since only the ufrag/password from
527 // the first section actually get used.
528 continue;
529 }
530
531 // If the content isn't rejected or bundled into another m= section,
532 // ice-ufrag and ice-pwd must be present.
Steve Anton8a006912017-12-04 15:25:56 -0800533 const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
Steve Anton75737c02017-11-06 10:37:17 -0800534 if (!tinfo) {
535 // Something is not right.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100536 RTC_LOG(LS_ERROR) << kInvalidSdp;
Steve Anton75737c02017-11-06 10:37:17 -0800537 return false;
538 }
539 if (tinfo->description.ice_ufrag.empty() ||
540 tinfo->description.ice_pwd.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100541 RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
Steve Anton75737c02017-11-06 10:37:17 -0800542 return false;
543 }
544 }
545 return true;
546}
547
Steve Anton75737c02017-11-06 10:37:17 -0800548// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
549bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
550 const SessionDescriptionInterface* new_desc,
551 const std::string& content_name) {
552 if (!old_desc) {
553 return false;
554 }
555 const SessionDescription* new_sd = new_desc->description();
556 const SessionDescription* old_sd = old_desc->description();
557 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
558 if (!cinfo || cinfo->rejected) {
559 return false;
560 }
561 // If the content isn't rejected, check if ufrag and password has changed.
562 const cricket::TransportDescription* new_transport_desc =
563 new_sd->GetTransportDescriptionByName(content_name);
564 const cricket::TransportDescription* old_transport_desc =
565 old_sd->GetTransportDescriptionByName(content_name);
566 if (!new_transport_desc || !old_transport_desc) {
567 // No transport description exists. This is not an ICE restart.
568 return false;
569 }
570 if (cricket::IceCredentialsChanged(
571 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
572 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100573 RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
574 << ".";
Steve Anton75737c02017-11-06 10:37:17 -0800575 return true;
576 }
577 return false;
578}
579
Steve Anton80dd7b52018-02-16 17:08:42 -0800580// Generates a string error message for SetLocalDescription/SetRemoteDescription
581// from an RTCError.
582std::string GetSetDescriptionErrorMessage(cricket::ContentSource source,
583 SdpType type,
584 const RTCError& error) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200585 rtc::StringBuilder oss;
Steve Anton80dd7b52018-02-16 17:08:42 -0800586 oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote")
587 << " " << SdpTypeToString(type) << " sdp: " << error.message();
Jonas Olsson84df1c72018-09-14 16:59:32 +0200588 return oss.Release();
Steve Anton80dd7b52018-02-16 17:08:42 -0800589}
590
Seth Hampson5b4f0752018-04-02 16:31:36 -0700591std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) {
592 std::string output = "streams=[";
593 const char* separator = "";
594 for (const auto& stream_id : stream_ids) {
595 output.append(separator).append(stream_id);
596 separator = ", ";
597 }
598 output.append("]");
599 return output;
600}
601
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200602absl::optional<int> RTCConfigurationToIceConfigOptionalInt(
Qingsi Wang866e08d2018-03-22 17:54:23 -0700603 int rtc_configuration_parameter) {
604 if (rtc_configuration_parameter ==
605 webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200606 return absl::nullopt;
Qingsi Wang866e08d2018-03-22 17:54:23 -0700607 }
608 return rtc_configuration_parameter;
609}
610
Amit Hilbuche2a284d2019-03-05 12:36:31 -0800611void ReportSimulcastApiVersion(const char* name,
612 const SessionDescription& session) {
613 bool has_legacy = false;
614 bool has_spec_compliant = false;
615 for (const ContentInfo& content : session.contents()) {
Harald Alvestrand1716d392019-06-03 20:35:45 +0200616 if (!content.media_description()) {
Amit Hilbuche2a284d2019-03-05 12:36:31 -0800617 continue;
618 }
Harald Alvestrand1716d392019-06-03 20:35:45 +0200619 has_spec_compliant |= content.media_description()->HasSimulcast();
620 for (const StreamParams& sp : content.media_description()->streams()) {
Amit Hilbuche2a284d2019-03-05 12:36:31 -0800621 has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics);
622 }
623 }
624
625 if (has_legacy) {
626 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy,
627 kSimulcastApiVersionMax);
628 }
629 if (has_spec_compliant) {
630 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant,
631 kSimulcastApiVersionMax);
632 }
633 if (!has_legacy && !has_spec_compliant) {
634 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone,
635 kSimulcastApiVersionMax);
636 }
637}
638
Guido Urdaneta70c2db12019-04-16 12:24:14 +0200639const ContentInfo* FindTransceiverMSection(
640 RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver,
641 const SessionDescriptionInterface* session_description) {
642 return transceiver->mid()
643 ? session_description->description()->GetContentByName(
644 *transceiver->mid())
645 : nullptr;
646}
647
Henrik Boströma3728d32019-10-28 12:09:49 +0100648// Wraps a CreateSessionDescriptionObserver and an OperationsChain operation
649// complete callback. When the observer is invoked, the wrapped observer is
650// invoked followed by invoking the completion callback.
651class CreateSessionDescriptionObserverOperationWrapper
652 : public CreateSessionDescriptionObserver {
653 public:
654 CreateSessionDescriptionObserverOperationWrapper(
655 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer,
656 std::function<void()> operation_complete_callback)
657 : observer_(std::move(observer)),
658 operation_complete_callback_(std::move(operation_complete_callback)) {
659 RTC_DCHECK(observer_);
660 }
661 ~CreateSessionDescriptionObserverOperationWrapper() override {
662 RTC_DCHECK(was_called_);
663 }
664
665 void OnSuccess(SessionDescriptionInterface* desc) override {
666 RTC_DCHECK(!was_called_);
667#ifdef RTC_DCHECK_IS_ON
668 was_called_ = true;
669#endif // RTC_DCHECK_IS_ON
670 // Completing the operation before invoking the observer allows the observer
671 // to execute SetLocalDescription() without delay.
672 operation_complete_callback_();
673 observer_->OnSuccess(desc);
674 }
675
676 void OnFailure(RTCError error) override {
677 RTC_DCHECK(!was_called_);
678#ifdef RTC_DCHECK_IS_ON
679 was_called_ = true;
680#endif // RTC_DCHECK_IS_ON
681 operation_complete_callback_();
682 observer_->OnFailure(std::move(error));
683 }
684
685 private:
686#ifdef RTC_DCHECK_IS_ON
687 bool was_called_ = false;
688#endif // RTC_DCHECK_IS_ON
689 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer_;
690 std::function<void()> operation_complete_callback_;
691};
692
Steve Anton75737c02017-11-06 10:37:17 -0800693} // namespace
694
Henrik Boström4e196702019-10-30 10:35:50 +0100695// Used by parameterless SetLocalDescription() to create an offer or answer.
696// Upon completion of creating the session description, SetLocalDescription() is
697// invoked with the result.
698// For consistency with DoSetLocalDescription(), if the PeerConnection is
699// destroyed midst operation, we DO NOT inform the
700// |set_local_description_observer| that the operation failed.
701// TODO(hbos): If/when we process SLD messages in ~PeerConnection, the
702// consistent thing would be to inform the observer here.
703class PeerConnection::ImplicitCreateSessionDescriptionObserver
704 : public CreateSessionDescriptionObserver {
705 public:
706 ImplicitCreateSessionDescriptionObserver(
707 rtc::WeakPtr<PeerConnection> pc,
708 rtc::scoped_refptr<SetSessionDescriptionObserver>
709 set_local_description_observer)
710 : pc_(std::move(pc)),
711 set_local_description_observer_(
712 std::move(set_local_description_observer)) {}
713 ~ImplicitCreateSessionDescriptionObserver() override {
714 RTC_DCHECK(was_called_);
715 }
716
717 void SetOperationCompleteCallback(
718 std::function<void()> operation_complete_callback) {
719 operation_complete_callback_ = std::move(operation_complete_callback);
720 }
721
722 bool was_called() const { return was_called_; }
723
724 void OnSuccess(SessionDescriptionInterface* desc_ptr) override {
725 RTC_DCHECK(!was_called_);
726 std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr);
727 was_called_ = true;
728
729 // Abort early if |pc_| is no longer valid.
730 if (!pc_) {
731 operation_complete_callback_();
732 return;
733 }
734 // DoSetLocalDescription() is currently implemented as a synchronous
735 // operation but where the |set_local_description_observer_|'s callbacks are
736 // invoked asynchronously in a post to PeerConnection::OnMessage().
737 pc_->DoSetLocalDescription(std::move(desc),
738 std::move(set_local_description_observer_));
739 // For backwards-compatability reasons, we declare the operation as
740 // completed here (rather than in PeerConnection::OnMessage()). This ensures
741 // that subsequent offer/answer operations can start immediately (without
742 // waiting for OnMessage()).
743 operation_complete_callback_();
744 }
745
746 void OnFailure(RTCError error) override {
747 RTC_DCHECK(!was_called_);
748 was_called_ = true;
749
750 // Abort early if |pc_| is no longer valid.
751 if (!pc_) {
752 operation_complete_callback_();
753 return;
754 }
755 // DoSetLocalDescription() reports its failures in a post. We do the
756 // same thing here for consistency.
757 pc_->PostSetSessionDescriptionFailure(
758 set_local_description_observer_,
759 RTCError(error.type(),
760 std::string("SetLocalDescription failed to create "
761 "session description - ") +
762 error.message()));
763 operation_complete_callback_();
764 }
765
766 private:
767 bool was_called_ = false;
768 rtc::WeakPtr<PeerConnection> pc_;
769 rtc::scoped_refptr<SetSessionDescriptionObserver>
770 set_local_description_observer_;
771 std::function<void()> operation_complete_callback_;
772};
773
Henrik Boström79b69802019-07-18 11:16:56 +0200774class PeerConnection::LocalIceCredentialsToReplace {
775 public:
776 // Sets the ICE credentials that need restarting to the ICE credentials of
777 // the current and pending descriptions.
778 void SetIceCredentialsFromLocalDescriptions(
779 const SessionDescriptionInterface* current_local_description,
780 const SessionDescriptionInterface* pending_local_description) {
781 ice_credentials_.clear();
782 if (current_local_description) {
783 AppendIceCredentialsFromSessionDescription(*current_local_description);
784 }
785 if (pending_local_description) {
786 AppendIceCredentialsFromSessionDescription(*pending_local_description);
787 }
788 }
789
790 void ClearIceCredentials() { ice_credentials_.clear(); }
791
792 // Returns true if we have ICE credentials that need restarting.
793 bool HasIceCredentials() const { return !ice_credentials_.empty(); }
794
795 // Returns true if |local_description| shares no ICE credentials with the
796 // ICE credentials that need restarting.
797 bool SatisfiesIceRestart(
798 const SessionDescriptionInterface& local_description) const {
799 for (const auto& transport_info :
800 local_description.description()->transport_infos()) {
801 if (ice_credentials_.find(std::make_pair(
802 transport_info.description.ice_ufrag,
803 transport_info.description.ice_pwd)) != ice_credentials_.end()) {
804 return false;
805 }
806 }
807 return true;
808 }
809
810 private:
811 void AppendIceCredentialsFromSessionDescription(
812 const SessionDescriptionInterface& desc) {
813 for (const auto& transport_info : desc.description()->transport_infos()) {
814 ice_credentials_.insert(
815 std::make_pair(transport_info.description.ice_ufrag,
816 transport_info.description.ice_pwd));
817 }
818 }
819
820 std::set<std::pair<std::string, std::string>> ice_credentials_;
821};
822
Henrik Boström31638672017-11-23 17:48:32 +0100823// Upon completion, posts a task to execute the callback of the
824// SetSessionDescriptionObserver asynchronously on the same thread. At this
825// point, the state of the peer connection might no longer reflect the effects
826// of the SetRemoteDescription operation, as the peer connection could have been
827// modified during the post.
828// TODO(hbos): Remove this class once we remove the version of
829// PeerConnectionInterface::SetRemoteDescription() that takes a
830// SetSessionDescriptionObserver as an argument.
831class PeerConnection::SetRemoteDescriptionObserverAdapter
832 : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> {
833 public:
834 SetRemoteDescriptionObserverAdapter(
835 rtc::scoped_refptr<PeerConnection> pc,
836 rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper)
837 : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {}
838
839 // SetRemoteDescriptionObserverInterface implementation.
840 void OnSetRemoteDescriptionComplete(RTCError error) override {
841 if (error.ok())
842 pc_->PostSetSessionDescriptionSuccess(wrapper_);
843 else
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100844 pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error));
Henrik Boström31638672017-11-23 17:48:32 +0100845 }
846
847 private:
848 rtc::scoped_refptr<PeerConnection> pc_;
849 rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_;
850};
851
deadbeef293e9262017-01-11 12:28:30 -0800852bool PeerConnectionInterface::RTCConfiguration::operator==(
853 const PeerConnectionInterface::RTCConfiguration& o) const {
854 // This static_assert prevents us from accidentally breaking operator==.
Steve Anton300bf8e2017-07-14 10:13:10 -0700855 // Note: Order matters! Fields must be ordered the same as RTCConfiguration.
deadbeef293e9262017-01-11 12:28:30 -0800856 struct stuff_being_tested_for_equality {
Magnus Jedvert3beb2072017-07-14 14:23:56 +0000857 IceServers servers;
Steve Anton300bf8e2017-07-14 10:13:10 -0700858 IceTransportsType type;
deadbeef293e9262017-01-11 12:28:30 -0800859 BundlePolicy bundle_policy;
860 RtcpMuxPolicy rtcp_mux_policy;
Steve Anton300bf8e2017-07-14 10:13:10 -0700861 std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates;
862 int ice_candidate_pool_size;
863 bool disable_ipv6;
864 bool disable_ipv6_on_wifi;
deadbeefd21eab32017-07-26 16:50:11 -0700865 int max_ipv6_networks;
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100866 bool disable_link_local_networks;
Steve Anton300bf8e2017-07-14 10:13:10 -0700867 bool enable_rtp_data_channel;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200868 absl::optional<int> screencast_min_bitrate;
869 absl::optional<bool> combined_audio_video_bwe;
870 absl::optional<bool> enable_dtls_srtp;
deadbeef293e9262017-01-11 12:28:30 -0800871 TcpCandidatePolicy tcp_candidate_policy;
872 CandidateNetworkPolicy candidate_network_policy;
873 int audio_jitter_buffer_max_packets;
874 bool audio_jitter_buffer_fast_accelerate;
Jakob Ivarsson10403ae2018-11-27 15:45:20 +0100875 int audio_jitter_buffer_min_delay_ms;
Jakob Ivarsson53eae872019-01-10 15:58:36 +0100876 bool audio_jitter_buffer_enable_rtx_handling;
deadbeef293e9262017-01-11 12:28:30 -0800877 int ice_connection_receiving_timeout;
878 int ice_backup_candidate_pair_ping_interval;
879 ContinualGatheringPolicy continual_gathering_policy;
deadbeef293e9262017-01-11 12:28:30 -0800880 bool prioritize_most_likely_ice_candidate_pairs;
881 struct cricket::MediaConfig media_config;
deadbeef293e9262017-01-11 12:28:30 -0800882 bool prune_turn_ports;
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700883 PortPrunePolicy turn_port_prune_policy;
deadbeef293e9262017-01-11 12:28:30 -0800884 bool presume_writable_when_fully_relayed;
885 bool enable_ice_renomination;
886 bool redetermine_role_on_ice_restart;
Qingsi Wang1fe119f2019-05-31 16:55:33 -0700887 bool surface_ice_candidates_on_ice_transport_type_changed;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200888 absl::optional<int> ice_check_interval_strong_connectivity;
889 absl::optional<int> ice_check_interval_weak_connectivity;
890 absl::optional<int> ice_check_min_interval;
891 absl::optional<int> ice_unwritable_timeout;
892 absl::optional<int> ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800893 absl::optional<int> ice_inactive_timeout;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200894 absl::optional<int> stun_candidate_keepalive_interval;
Jonas Orelandbdcee282017-10-10 14:01:40 +0200895 webrtc::TurnCustomizer* turn_customizer;
Steve Anton79e79602017-11-20 10:25:56 -0800896 SdpSemantics sdp_semantics;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200897 absl::optional<rtc::AdapterType> network_preference;
Zhi Huangb57e1692018-06-12 11:41:11 -0700898 bool active_reset_srtp_params;
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700899 bool use_media_transport;
Bjorn Mellema9bbd862018-11-02 09:07:48 -0700900 bool use_media_transport_for_data_channels;
Bjorn A Mellem5985a042019-06-28 14:19:38 -0700901 absl::optional<bool> use_datagram_transport;
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700902 absl::optional<bool> use_datagram_transport_for_data_channels;
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700903 absl::optional<bool> use_datagram_transport_for_data_channels_receive_only;
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700904 absl::optional<CryptoOptions> crypto_options;
Johannes Kron89f874e2018-11-12 10:25:48 +0100905 bool offer_extmap_allow_mixed;
Jonas Oreland3c028422019-08-22 16:16:35 +0200906 std::string turn_logging_id;
Eldar Rello5ab79e62019-10-09 18:29:44 +0300907 bool enable_implicit_rollback;
philipel16cec3b2019-10-25 12:23:02 +0200908 absl::optional<bool> allow_codec_switching;
deadbeef293e9262017-01-11 12:28:30 -0800909 };
910 static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this),
911 "Did you add something to RTCConfiguration and forget to "
912 "update operator==?");
913 return type == o.type && servers == o.servers &&
914 bundle_policy == o.bundle_policy &&
915 rtcp_mux_policy == o.rtcp_mux_policy &&
916 tcp_candidate_policy == o.tcp_candidate_policy &&
917 candidate_network_policy == o.candidate_network_policy &&
918 audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets &&
919 audio_jitter_buffer_fast_accelerate ==
920 o.audio_jitter_buffer_fast_accelerate &&
Jakob Ivarsson10403ae2018-11-27 15:45:20 +0100921 audio_jitter_buffer_min_delay_ms ==
922 o.audio_jitter_buffer_min_delay_ms &&
Jakob Ivarsson53eae872019-01-10 15:58:36 +0100923 audio_jitter_buffer_enable_rtx_handling ==
924 o.audio_jitter_buffer_enable_rtx_handling &&
deadbeef293e9262017-01-11 12:28:30 -0800925 ice_connection_receiving_timeout ==
926 o.ice_connection_receiving_timeout &&
927 ice_backup_candidate_pair_ping_interval ==
928 o.ice_backup_candidate_pair_ping_interval &&
929 continual_gathering_policy == o.continual_gathering_policy &&
930 certificates == o.certificates &&
931 prioritize_most_likely_ice_candidate_pairs ==
932 o.prioritize_most_likely_ice_candidate_pairs &&
933 media_config == o.media_config && disable_ipv6 == o.disable_ipv6 &&
zhihuangb09b3f92017-03-07 14:40:51 -0800934 disable_ipv6_on_wifi == o.disable_ipv6_on_wifi &&
deadbeefd21eab32017-07-26 16:50:11 -0700935 max_ipv6_networks == o.max_ipv6_networks &&
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100936 disable_link_local_networks == o.disable_link_local_networks &&
deadbeef293e9262017-01-11 12:28:30 -0800937 enable_rtp_data_channel == o.enable_rtp_data_channel &&
deadbeef293e9262017-01-11 12:28:30 -0800938 screencast_min_bitrate == o.screencast_min_bitrate &&
939 combined_audio_video_bwe == o.combined_audio_video_bwe &&
940 enable_dtls_srtp == o.enable_dtls_srtp &&
941 ice_candidate_pool_size == o.ice_candidate_pool_size &&
942 prune_turn_ports == o.prune_turn_ports &&
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700943 turn_port_prune_policy == o.turn_port_prune_policy &&
deadbeef293e9262017-01-11 12:28:30 -0800944 presume_writable_when_fully_relayed ==
945 o.presume_writable_when_fully_relayed &&
946 enable_ice_renomination == o.enable_ice_renomination &&
skvlad51072462017-02-02 11:50:14 -0800947 redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart &&
Qingsi Wang1fe119f2019-05-31 16:55:33 -0700948 surface_ice_candidates_on_ice_transport_type_changed ==
949 o.surface_ice_candidates_on_ice_transport_type_changed &&
Qingsi Wange6826d22018-03-08 14:55:14 -0800950 ice_check_interval_strong_connectivity ==
951 o.ice_check_interval_strong_connectivity &&
952 ice_check_interval_weak_connectivity ==
953 o.ice_check_interval_weak_connectivity &&
Steve Anton300bf8e2017-07-14 10:13:10 -0700954 ice_check_min_interval == o.ice_check_min_interval &&
Qingsi Wang22e623a2018-03-13 10:53:57 -0700955 ice_unwritable_timeout == o.ice_unwritable_timeout &&
956 ice_unwritable_min_checks == o.ice_unwritable_min_checks &&
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800957 ice_inactive_timeout == o.ice_inactive_timeout &&
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800958 stun_candidate_keepalive_interval ==
959 o.stun_candidate_keepalive_interval &&
Steve Anton79e79602017-11-20 10:25:56 -0800960 turn_customizer == o.turn_customizer &&
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800961 sdp_semantics == o.sdp_semantics &&
Zhi Huangb57e1692018-06-12 11:41:11 -0700962 network_preference == o.network_preference &&
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700963 active_reset_srtp_params == o.active_reset_srtp_params &&
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700964 use_media_transport == o.use_media_transport &&
Bjorn Mellema9bbd862018-11-02 09:07:48 -0700965 use_media_transport_for_data_channels ==
966 o.use_media_transport_for_data_channels &&
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700967 use_datagram_transport == o.use_datagram_transport &&
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700968 use_datagram_transport_for_data_channels ==
969 o.use_datagram_transport_for_data_channels &&
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700970 use_datagram_transport_for_data_channels_receive_only ==
971 o.use_datagram_transport_for_data_channels_receive_only &&
Johannes Kron89f874e2018-11-12 10:25:48 +0100972 crypto_options == o.crypto_options &&
Jonas Oreland3c028422019-08-22 16:16:35 +0200973 offer_extmap_allow_mixed == o.offer_extmap_allow_mixed &&
Eldar Rello5ab79e62019-10-09 18:29:44 +0300974 turn_logging_id == o.turn_logging_id &&
philipel16cec3b2019-10-25 12:23:02 +0200975 enable_implicit_rollback == o.enable_implicit_rollback &&
Henrik Boströma0ff50c2020-05-05 15:54:46 +0200976 allow_codec_switching == o.allow_codec_switching &&
977 enable_simulcast_stats == o.enable_simulcast_stats;
deadbeef293e9262017-01-11 12:28:30 -0800978}
979
980bool PeerConnectionInterface::RTCConfiguration::operator!=(
981 const PeerConnectionInterface::RTCConfiguration& o) const {
982 return !(*this == o);
deadbeef3edec7c2016-12-10 11:44:26 -0800983}
984
Eldar Rello353a7182019-11-25 18:49:44 +0200985void PeerConnection::TransceiverStableState::set_newly_created() {
986 RTC_DCHECK(!has_m_section_);
987 newly_created_ = true;
988}
989
990void PeerConnection::TransceiverStableState::SetMSectionIfUnset(
991 absl::optional<std::string> mid,
992 absl::optional<size_t> mline_index) {
993 if (!has_m_section_) {
994 mid_ = mid;
995 mline_index_ = mline_index;
996 has_m_section_ = true;
997 }
998}
999
1000void PeerConnection::TransceiverStableState::SetRemoteStreamIdsIfUnset(
1001 const std::vector<std::string>& ids) {
1002 if (!remote_stream_ids_.has_value()) {
1003 remote_stream_ids_ = ids;
1004 }
1005}
1006
zhihuang8f65cdf2016-05-06 18:40:30 -07001007// Generate a RTCP CNAME when a PeerConnection is created.
1008std::string GenerateRtcpCname() {
1009 std::string cname;
1010 if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001011 RTC_LOG(LS_ERROR) << "Failed to generate CNAME.";
nisseeb4ca4e2017-01-12 02:24:27 -08001012 RTC_NOTREACHED();
zhihuang8f65cdf2016-05-06 18:40:30 -07001013 }
1014 return cname;
1015}
1016
zhihuang1c378ed2017-08-17 14:10:50 -07001017bool ValidateOfferAnswerOptions(
1018 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) {
1019 return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) &&
1020 IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video);
olka3c747662017-08-17 06:50:32 -07001021}
1022
zhihuang1c378ed2017-08-17 14:10:50 -07001023// From |rtc_options|, fill parts of |session_options| shared by all generated
1024// m= sections (in other words, nothing that involves a map/array).
1025void ExtractSharedMediaSessionOptions(
1026 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
1027 cricket::MediaSessionOptions* session_options) {
1028 session_options->vad_enabled = rtc_options.voice_activity_detection;
1029 session_options->bundle_enabled = rtc_options.use_rtp_mux;
Mirta Dvornicic479a3c02019-06-04 15:38:50 +02001030 session_options->raw_packetization_for_video =
1031 rtc_options.raw_packetization_for_video;
zhihuang1c378ed2017-08-17 14:10:50 -07001032}
zhihuanga77e6bb2017-08-14 18:17:48 -07001033
zhihuang38ede132017-06-15 12:52:32 -07001034PeerConnection::PeerConnection(PeerConnectionFactory* factory,
1035 std::unique_ptr<RtcEventLog> event_log,
1036 std::unique_ptr<Call> call)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037 : factory_(factory),
zhihuang38ede132017-06-15 12:52:32 -07001038 event_log_(std::move(event_log)),
Karl Wibergb03ab712019-02-14 11:59:57 +01001039 event_log_ptr_(event_log_.get()),
Henrik Boströma3728d32019-10-28 12:09:49 +01001040 operations_chain_(rtc::OperationsChain::Create()),
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001041 datagram_transport_config_(
1042 field_trial::FindFullName(kDatagramTransportFieldTrial)),
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001043 datagram_transport_data_channel_config_(
1044 field_trial::FindFullName(kDatagramTransportDataChannelFieldTrial)),
zhihuang8f65cdf2016-05-06 18:40:30 -07001045 rtcp_cname_(GenerateRtcpCname()),
deadbeefab9b2d12015-10-14 11:33:11 -07001046 local_streams_(StreamCollection::Create()),
zhihuang38ede132017-06-15 12:52:32 -07001047 remote_streams_(StreamCollection::Create()),
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001048 call_(std::move(call)),
Henrik Boström79b69802019-07-18 11:16:56 +02001049 call_ptr_(call_.get()),
Henrik Boströma3728d32019-10-28 12:09:49 +01001050 local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()),
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01001051 data_channel_controller_(this),
Henrik Boströma3728d32019-10-28 12:09:49 +01001052 weak_ptr_factory_(this) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053
1054PeerConnection::~PeerConnection() {
Peter Boström1a9d6152015-12-08 22:15:17 +01001055 TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection");
Steve Anton4171afb2017-11-20 10:20:22 -08001056 RTC_DCHECK_RUN_ON(signaling_thread());
1057
Henrik Boströma3728d32019-10-28 12:09:49 +01001058 weak_ptr_factory_.InvalidateWeakPtrs();
1059
Steve Anton8af21862017-12-15 11:20:13 -08001060 // Need to stop transceivers before destroying the stats collector because
1061 // AudioRtpSender has a reference to the StatsCollector it will update when
1062 // stopping.
Mirko Bonadei739baf02019-01-27 17:29:42 +01001063 for (const auto& transceiver : transceivers_) {
Steve Anton8af21862017-12-15 11:20:13 -08001064 transceiver->Stop();
1065 }
Steve Anton4171afb2017-11-20 10:20:22 -08001066
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001067 stats_.reset(nullptr);
hbosb78306a2016-12-19 05:06:57 -08001068 if (stats_collector_) {
1069 stats_collector_->WaitForPendingRequest();
1070 stats_collector_ = nullptr;
1071 }
Steve Anton75737c02017-11-06 10:37:17 -08001072
Steve Anton8af21862017-12-15 11:20:13 -08001073 // Don't destroy BaseChannels until after stats has been cleaned up so that
1074 // the last stats request can still read from the channels.
1075 DestroyAllChannels();
1076
Mirko Bonadei675513b2017-11-09 11:09:25 +01001077 RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed.";
Steve Anton75737c02017-11-06 10:37:17 -08001078
1079 webrtc_session_desc_factory_.reset();
Steve Anton75737c02017-11-06 10:37:17 -08001080 sctp_factory_.reset();
1081 transport_controller_.reset();
1082
deadbeef91dd5672016-05-18 16:55:30 -07001083 // port_allocator_ lives on the network thread and should be destroyed there.
Karl Wibergfb3be392019-03-22 14:13:22 +01001084 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1085 RTC_DCHECK_RUN_ON(network_thread());
1086 port_allocator_.reset();
1087 });
eladalon248fd4f2017-09-06 05:18:15 -07001088 // call_ and event_log_ must be destroyed on the worker thread.
Steve Anton978b8762017-09-29 12:15:02 -07001089 worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
Karl Wibergb03ab712019-02-14 11:59:57 +01001090 RTC_DCHECK_RUN_ON(worker_thread());
eladalon248fd4f2017-09-06 05:18:15 -07001091 call_.reset();
Qingsi Wang93a84392018-01-30 17:13:09 -08001092 // The event log must outlive call (and any other object that uses it).
eladalon248fd4f2017-09-06 05:18:15 -07001093 event_log_.reset();
1094 });
Henrik Boströma3728d32019-10-28 12:09:49 +01001095
1096 // Process all pending notifications in the message queue. If we don't do
1097 // this, requests will linger and not know they succeeded or failed.
1098 rtc::MessageList list;
1099 signaling_thread()->Clear(this, rtc::MQID_ANY, &list);
1100 for (auto& msg : list) {
1101 if (msg.message_id == MSG_CREATE_SESSIONDESCRIPTION_FAILED) {
1102 // Processing CreateOffer() and CreateAnswer() messages ensures their
1103 // observers are invoked even if the PeerConnection is destroyed early.
1104 OnMessage(&msg);
1105 } else {
1106 // TODO(hbos): Consider processing all pending messages. This would mean
1107 // that SetLocalDescription() and SetRemoteDescription() observers are
1108 // informed of successes and failures; this is currently NOT the case.
1109 delete msg.pdata;
1110 }
1111 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112}
1113
Steve Anton8af21862017-12-15 11:20:13 -08001114void PeerConnection::DestroyAllChannels() {
Steve Anton3fe1b152017-12-12 10:20:08 -08001115 // Destroy video channels first since they may have a pointer to a voice
1116 // channel.
Mirko Bonadei739baf02019-01-27 17:29:42 +01001117 for (const auto& transceiver : transceivers_) {
Steve Anton69470252018-02-09 11:43:08 -08001118 if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
Steve Anton3fe1b152017-12-12 10:20:08 -08001119 DestroyTransceiverChannel(transceiver);
1120 }
1121 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001122 for (const auto& transceiver : transceivers_) {
Steve Anton69470252018-02-09 11:43:08 -08001123 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton3fe1b152017-12-12 10:20:08 -08001124 DestroyTransceiverChannel(transceiver);
1125 }
1126 }
Harald Alvestrand408cb4b2019-11-16 12:09:08 +01001127 DestroyDataChannelTransport();
Steve Anton3fe1b152017-12-12 10:20:08 -08001128}
1129
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130bool PeerConnection::Initialize(
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001131 const PeerConnectionInterface::RTCConfiguration& configuration,
Benjamin Wrightcab58882018-05-02 15:12:47 -07001132 PeerConnectionDependencies dependencies) {
Karl Wiberg744310f2019-02-14 10:18:56 +01001133 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 22:15:17 +01001134 TRACE_EVENT0("webrtc", "PeerConnection::Initialize");
Steve Anton038834f2017-07-14 15:59:59 -07001135
1136 RTCError config_error = ValidateConfiguration(configuration);
1137 if (!config_error.ok()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001138 RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message();
Steve Anton038834f2017-07-14 15:59:59 -07001139 return false;
1140 }
1141
Benjamin Wrightcab58882018-05-02 15:12:47 -07001142 if (!dependencies.allocator) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001143 RTC_LOG(LS_ERROR)
1144 << "PeerConnection initialized without a PortAllocator? "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001145 "This shouldn't happen if using PeerConnectionFactory.";
deadbeef293e9262017-01-11 12:28:30 -08001146 return false;
1147 }
Jonas Orelandbdcee282017-10-10 14:01:40 +02001148
Benjamin Wrightcab58882018-05-02 15:12:47 -07001149 if (!dependencies.observer) {
deadbeef293e9262017-01-11 12:28:30 -08001150 // TODO(deadbeef): Why do we do this?
Mirko Bonadei675513b2017-11-09 11:09:25 +01001151 RTC_LOG(LS_ERROR) << "PeerConnection initialized without a "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001152 "PeerConnectionObserver";
deadbeef653b8e02015-11-11 12:55:10 -08001153 return false;
1154 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001155
Benjamin Wrightcab58882018-05-02 15:12:47 -07001156 observer_ = dependencies.observer;
Zach Steine20867f2018-08-02 13:20:15 -07001157 async_resolver_factory_ = std::move(dependencies.async_resolver_factory);
Benjamin Wrightcab58882018-05-02 15:12:47 -07001158 port_allocator_ = std::move(dependencies.allocator);
Jorge E. Moreira00b46f72020-03-27 18:01:19 -07001159 packet_socket_factory_ = std::move(dependencies.packet_socket_factory);
Qingsi Wang25ec8882019-11-15 12:33:05 -08001160 ice_transport_factory_ = std::move(dependencies.ice_transport_factory);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001161 tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier);
deadbeef653b8e02015-11-11 12:55:10 -08001162
Harald Alvestrandb2a74782018-06-28 13:54:07 +02001163 cricket::ServerAddresses stun_servers;
1164 std::vector<cricket::RelayServerConfig> turn_servers;
1165
1166 RTCErrorType parse_error =
1167 ParseIceServers(configuration.servers, &stun_servers, &turn_servers);
1168 if (parse_error != RTCErrorType::NONE) {
1169 return false;
1170 }
1171
Jonas Oreland3c028422019-08-22 16:16:35 +02001172 // Add the turn logging id to all turn servers
1173 for (cricket::RelayServerConfig& turn_server : turn_servers) {
1174 turn_server.turn_logging_id = configuration.turn_logging_id;
1175 }
1176
deadbeef91dd5672016-05-18 16:55:30 -07001177 // The port allocator lives on the network thread and should be initialized
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001178 // there.
Karl Wibergfb3be392019-03-22 14:13:22 +01001179 const auto pa_result =
1180 network_thread()->Invoke<InitializePortAllocatorResult>(
Harald Alvestrandb2a74782018-06-28 13:54:07 +02001181 RTC_FROM_HERE,
1182 rtc::Bind(&PeerConnection::InitializePortAllocator_n, this,
Karl Wibergfb3be392019-03-22 14:13:22 +01001183 stun_servers, turn_servers, configuration));
1184
Harald Alvestrandb2a74782018-06-28 13:54:07 +02001185 // If initialization was successful, note if STUN or TURN servers
1186 // were supplied.
1187 if (!stun_servers.empty()) {
1188 NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED);
1189 }
1190 if (!turn_servers.empty()) {
1191 NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED);
1192 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001194 // Send information about IPv4/IPv6 status.
1195 PeerConnectionAddressFamilyCounter address_family;
Karl Wibergfb3be392019-03-22 14:13:22 +01001196 if (pa_result.enable_ipv6) {
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001197 address_family = kPeerConnection_IPv6;
1198 } else {
1199 address_family = kPeerConnection_IPv4;
1200 }
1201 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family,
1202 kPeerConnectionAddressFamilyCounter_Max);
1203
Zhi Huange830e682018-03-30 10:48:35 -07001204 const PeerConnectionFactoryInterface::Options& options = factory_->options();
1205
Steve Anton75737c02017-11-06 10:37:17 -08001206 // RFC 3264: The numeric value of the session id and version in the
1207 // o line MUST be representable with a "64 bit signed integer".
1208 // Due to this constraint session id |session_id_| is max limited to
1209 // LLONG_MAX.
1210 session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX);
Zhi Huange830e682018-03-30 10:48:35 -07001211 JsepTransportController::Config config;
1212 config.redetermine_role_on_ice_restart =
1213 configuration.redetermine_role_on_ice_restart;
1214 config.ssl_max_version = factory_->options().ssl_max_version;
1215 config.disable_encryption = options.disable_encryption;
1216 config.bundle_policy = configuration.bundle_policy;
1217 config.rtcp_mux_policy = configuration.rtcp_mux_policy;
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001218 // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this
1219 // stub.
1220 config.crypto_options = configuration.crypto_options.has_value()
1221 ? *configuration.crypto_options
1222 : options.crypto_options;
Zhi Huang365381f2018-04-13 16:44:34 -07001223 config.transport_observer = this;
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +02001224 // It's safe to pass |this| and using |rtcp_invoker_| and the |call_| pointer
1225 // since the JsepTransportController instance is owned by this PeerConnection
1226 // instance and is destroyed before both |rtcp_invoker_| and the |call_|
1227 // pointer.
1228 config.rtcp_handler = [this](const rtc::CopyOnWriteBuffer& packet,
1229 int64_t packet_time_us) {
1230 RTC_DCHECK_RUN_ON(network_thread());
1231 rtcp_invoker_.AsyncInvoke<void>(
1232 RTC_FROM_HERE, worker_thread(), [this, packet, packet_time_us] {
1233 RTC_DCHECK_RUN_ON(worker_thread());
1234 // |call_| is reset on the worker thread in the PeerConnection
1235 // destructor, so we check that it's still valid before propagating
1236 // the packet.
1237 if (call_) {
1238 call_->Receiver()->DeliverPacket(MediaType::ANY, packet,
1239 packet_time_us);
1240 }
1241 });
1242 };
Karl Wibergb03ab712019-02-14 11:59:57 +01001243 config.event_log = event_log_ptr_;
Zhi Huange830e682018-03-30 10:48:35 -07001244#if defined(ENABLE_EXTERNAL_AUTH)
1245 config.enable_external_auth = true;
1246#endif
Zhi Huangb57e1692018-06-12 11:41:11 -07001247 config.active_reset_srtp_params = configuration.active_reset_srtp_params;
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001248
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001249 use_datagram_transport_ = datagram_transport_config_.enabled &&
Bjorn A Mellem238aab92019-07-02 11:06:00 -07001250 configuration.use_datagram_transport.value_or(
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001251 datagram_transport_config_.default_value);
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001252 use_datagram_transport_for_data_channels_ =
1253 datagram_transport_data_channel_config_.enabled &&
1254 configuration.use_datagram_transport_for_data_channels.value_or(
1255 datagram_transport_data_channel_config_.default_value);
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07001256 use_datagram_transport_for_data_channels_receive_only_ =
1257 configuration.use_datagram_transport_for_data_channels_receive_only
1258 .value_or(datagram_transport_data_channel_config_.receive_only);
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -08001259 if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) {
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001260 if (!factory_->media_transport_factory()) {
1261 RTC_DCHECK(false)
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -08001262 << "PeerConnecton is initialized with use_datagram_transport = true "
1263 "or use_datagram_transport_for_data_channels = true "
Jonas Olssonb2b20312020-01-14 12:11:31 +01001264 "but media transport factory is not set in PeerConnectionFactory";
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001265 return false;
1266 }
1267
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001268 config.use_datagram_transport = use_datagram_transport_;
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001269 config.use_datagram_transport_for_data_channels =
1270 use_datagram_transport_for_data_channels_;
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07001271 config.use_datagram_transport_for_data_channels_receive_only =
1272 use_datagram_transport_for_data_channels_receive_only_;
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001273 config.media_transport_factory = factory_->media_transport_factory();
1274 }
1275
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001276 // Obtain a certificate from RTCConfiguration if any were provided (optional).
1277 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
1278 if (!configuration.certificates.empty()) {
1279 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
1280 // just picking the first one. The decision should be made based on the DTLS
1281 // handshake. The DTLS negotiations need to know about all certificates.
1282 certificate = configuration.certificates[0];
1283 }
1284
1285 if (options.disable_encryption) {
1286 dtls_enabled_ = false;
1287 } else {
1288 // Enable DTLS by default if we have an identity store or a certificate.
1289 dtls_enabled_ = (dependencies.cert_generator || certificate);
1290 // |configuration| can override the default |dtls_enabled_| value.
1291 if (configuration.enable_dtls_srtp) {
1292 dtls_enabled_ = *(configuration.enable_dtls_srtp);
1293 }
1294 }
1295
1296 sctp_factory_ = factory_->CreateSctpTransportInternalFactory();
1297
1298 if (use_datagram_transport_for_data_channels_) {
1299 if (configuration.enable_rtp_data_channel) {
1300 RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and "
1301 "use_datagram_transport_for_data_channels are "
1302 "incompatible and cannot both be set to true";
1303 return false;
1304 }
1305 if (configuration.enable_dtls_srtp && !*configuration.enable_dtls_srtp) {
1306 RTC_LOG(LS_INFO) << "Using data channel transport with no fallback";
Harald Alvestrand05e4d082019-12-03 14:04:21 +01001307 data_channel_controller_.set_data_channel_type(
1308 cricket::DCT_DATA_CHANNEL_TRANSPORT);
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001309 } else {
1310 RTC_LOG(LS_INFO) << "Using data channel transport with fallback to SCTP";
Harald Alvestrand05e4d082019-12-03 14:04:21 +01001311 data_channel_controller_.set_data_channel_type(
1312 cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP);
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001313 config.sctp_factory = sctp_factory_.get();
1314 }
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001315 } else if (configuration.enable_rtp_data_channel) {
1316 // Enable creation of RTP data channels if the kEnableRtpDataChannels is
1317 // set. It takes precendence over the disable_sctp_data_channels
1318 // PeerConnectionFactoryInterface::Options.
Harald Alvestrand05e4d082019-12-03 14:04:21 +01001319 data_channel_controller_.set_data_channel_type(cricket::DCT_RTP);
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001320 } else {
1321 // DTLS has to be enabled to use SCTP.
1322 if (!options.disable_sctp_data_channels && dtls_enabled_) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01001323 data_channel_controller_.set_data_channel_type(cricket::DCT_SCTP);
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001324 config.sctp_factory = sctp_factory_.get();
1325 }
1326 }
1327
Qingsi Wang25ec8882019-11-15 12:33:05 -08001328 config.ice_transport_factory = ice_transport_factory_.get();
1329
Zhi Huange830e682018-03-30 10:48:35 -07001330 transport_controller_.reset(new JsepTransportController(
Zach Steine20867f2018-08-02 13:20:15 -07001331 signaling_thread(), network_thread(), port_allocator_.get(),
1332 async_resolver_factory_.get(), config));
Zhi Huange830e682018-03-30 10:48:35 -07001333 transport_controller_->SignalIceConnectionState.connect(
Alex Loiko9289eda2018-11-23 16:18:59 +00001334 this, &PeerConnection::OnTransportControllerConnectionState);
1335 transport_controller_->SignalStandardizedIceConnectionState.connect(
1336 this, &PeerConnection::SetStandardizedIceConnectionState);
Jonas Olsson635474e2018-10-18 15:58:17 +02001337 transport_controller_->SignalConnectionState.connect(
1338 this, &PeerConnection::SetConnectionState);
Zhi Huange830e682018-03-30 10:48:35 -07001339 transport_controller_->SignalIceGatheringState.connect(
Steve Anton75737c02017-11-06 10:37:17 -08001340 this, &PeerConnection::OnTransportControllerGatheringState);
Zhi Huange830e682018-03-30 10:48:35 -07001341 transport_controller_->SignalIceCandidatesGathered.connect(
Steve Anton75737c02017-11-06 10:37:17 -08001342 this, &PeerConnection::OnTransportControllerCandidatesGathered);
Eldar Relloda13ea22019-06-01 12:23:43 +03001343 transport_controller_->SignalIceCandidateError.connect(
1344 this, &PeerConnection::OnTransportControllerCandidateError);
Zhi Huange830e682018-03-30 10:48:35 -07001345 transport_controller_->SignalIceCandidatesRemoved.connect(
Steve Anton75737c02017-11-06 10:37:17 -08001346 this, &PeerConnection::OnTransportControllerCandidatesRemoved);
1347 transport_controller_->SignalDtlsHandshakeError.connect(
1348 this, &PeerConnection::OnTransportControllerDtlsHandshakeError);
Alex Drake00c7ecf2019-08-06 10:54:47 -07001349 transport_controller_->SignalIceCandidatePairChanged.connect(
1350 this, &PeerConnection::OnTransportControllerCandidateChanged);
Steve Anton75737c02017-11-06 10:37:17 -08001351
deadbeefab9b2d12015-10-14 11:33:11 -07001352 stats_.reset(new StatsCollector(this));
hbos74e1a4f2016-09-15 23:33:01 -07001353 stats_collector_ = RTCStatsCollector::Create(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354
Steve Antonba818672017-11-06 10:21:57 -08001355 configuration_ = configuration;
1356
Steve Antond25da372017-11-06 14:50:29 -08001357 transport_controller_->SetIceConfig(ParseIceConfig(configuration));
Steve Anton75737c02017-11-06 10:37:17 -08001358
Steve Anton75737c02017-11-06 10:37:17 -08001359 video_options_.screencast_min_bitrate_kbps =
1360 configuration.screencast_min_bitrate;
1361 audio_options_.combined_audio_video_bwe =
1362 configuration.combined_audio_video_bwe;
1363
1364 audio_options_.audio_jitter_buffer_max_packets =
Oskar Sundbom9b28a032017-11-16 10:53:30 +01001365 configuration.audio_jitter_buffer_max_packets;
Steve Anton75737c02017-11-06 10:37:17 -08001366
1367 audio_options_.audio_jitter_buffer_fast_accelerate =
Oskar Sundbom9b28a032017-11-16 10:53:30 +01001368 configuration.audio_jitter_buffer_fast_accelerate;
Steve Anton75737c02017-11-06 10:37:17 -08001369
Jakob Ivarsson10403ae2018-11-27 15:45:20 +01001370 audio_options_.audio_jitter_buffer_min_delay_ms =
1371 configuration.audio_jitter_buffer_min_delay_ms;
1372
Jakob Ivarsson53eae872019-01-10 15:58:36 +01001373 audio_options_.audio_jitter_buffer_enable_rtx_handling =
1374 configuration.audio_jitter_buffer_enable_rtx_handling;
1375
Steve Anton75737c02017-11-06 10:37:17 -08001376 // Whether the certificate generator/certificate is null or not determines
1377 // what PeerConnectionDescriptionFactory will do, so make sure that we give it
1378 // the right instructions by clearing the variables if needed.
1379 if (!dtls_enabled_) {
Benjamin Wrightcab58882018-05-02 15:12:47 -07001380 dependencies.cert_generator.reset();
Steve Anton75737c02017-11-06 10:37:17 -08001381 certificate = nullptr;
1382 } else if (certificate) {
1383 // Favor generated certificate over the certificate generator.
Benjamin Wrightcab58882018-05-02 15:12:47 -07001384 dependencies.cert_generator.reset();
Steve Anton75737c02017-11-06 10:37:17 -08001385 }
1386
1387 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
1388 signaling_thread(), channel_manager(), this, session_id(),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001389 std::move(dependencies.cert_generator), certificate, &ssrc_generator_));
Steve Anton75737c02017-11-06 10:37:17 -08001390 webrtc_session_desc_factory_->SignalCertificateReady.connect(
1391 this, &PeerConnection::OnCertificateReady);
1392
1393 if (options.disable_encryption) {
1394 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
1395 }
1396
1397 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001398 GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions);
Steve Anton8f66ddb2018-12-10 16:08:05 -08001399 webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400
Steve Anton4171afb2017-11-20 10:20:22 -08001401 // Add default audio/video transceivers for Plan B SDP.
1402 if (!IsUnifiedPlan()) {
1403 transceivers_.push_back(
1404 RtpTransceiverProxyWithInternal<RtpTransceiver>::Create(
1405 signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO)));
1406 transceivers_.push_back(
1407 RtpTransceiverProxyWithInternal<RtpTransceiver>::Create(
1408 signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO)));
1409 }
Harald Alvestrand183e09d2018-06-28 12:04:41 +02001410 int delay_ms =
1411 return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS;
Steve Antonad182762018-09-05 20:22:40 +00001412 signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this,
1413 MSG_REPORT_USAGE_PATTERN, nullptr);
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +02001414
1415 if (dependencies.video_bitrate_allocator_factory) {
1416 video_bitrate_allocator_factory_ =
1417 std::move(dependencies.video_bitrate_allocator_factory);
1418 } else {
1419 video_bitrate_allocator_factory_ =
1420 CreateBuiltinVideoBitrateAllocatorFactory();
1421 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 return true;
1423}
1424
Steve Anton038834f2017-07-14 15:59:59 -07001425RTCError PeerConnection::ValidateConfiguration(
1426 const RTCConfiguration& config) const {
Steve Antonf4172382020-01-27 15:45:02 -08001427 return cricket::P2PTransportChannel::ValidateIceConfig(
1428 ParseIceConfig(config));
Steve Anton038834f2017-07-14 15:59:59 -07001429}
1430
Yves Gerey665174f2018-06-19 15:03:05 +02001431rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() {
Karl Wiberg5966c502019-02-21 23:55:09 +01001432 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 13:48:58 -08001433 RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified "
1434 "Plan SdpSemantics. Please use GetSenders "
1435 "instead.";
deadbeefab9b2d12015-10-14 11:33:11 -07001436 return local_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437}
1438
Yves Gerey665174f2018-06-19 15:03:05 +02001439rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() {
Karl Wiberg5966c502019-02-21 23:55:09 +01001440 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 13:48:58 -08001441 RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified "
1442 "Plan SdpSemantics. Please use GetReceivers "
1443 "instead.";
deadbeefab9b2d12015-10-14 11:33:11 -07001444 return remote_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445}
1446
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001447bool PeerConnection::AddStream(MediaStreamInterface* local_stream) {
Karl Wiberg744310f2019-02-14 10:18:56 +01001448 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 13:48:58 -08001449 RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan "
1450 "SdpSemantics. Please use AddTrack instead.";
Peter Boström1a9d6152015-12-08 22:15:17 +01001451 TRACE_EVENT0("webrtc", "PeerConnection::AddStream");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452 if (IsClosed()) {
1453 return false;
1454 }
deadbeefab9b2d12015-10-14 11:33:11 -07001455 if (!CanAddLocalMediaStream(local_streams_, local_stream)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 return false;
1457 }
deadbeefab9b2d12015-10-14 11:33:11 -07001458
1459 local_streams_->AddStream(local_stream);
deadbeefeb459812015-12-15 19:24:43 -08001460 MediaStreamObserver* observer = new MediaStreamObserver(local_stream);
1461 observer->SignalAudioTrackAdded.connect(this,
1462 &PeerConnection::OnAudioTrackAdded);
1463 observer->SignalAudioTrackRemoved.connect(
1464 this, &PeerConnection::OnAudioTrackRemoved);
1465 observer->SignalVideoTrackAdded.connect(this,
1466 &PeerConnection::OnVideoTrackAdded);
1467 observer->SignalVideoTrackRemoved.connect(
1468 this, &PeerConnection::OnVideoTrackRemoved);
kwibergd1fe2812016-04-27 06:47:29 -07001469 stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer));
deadbeefab9b2d12015-10-14 11:33:11 -07001470
deadbeefab9b2d12015-10-14 11:33:11 -07001471 for (const auto& track : local_stream->GetAudioTracks()) {
korniltsev.anatolyec390b52017-07-24 17:00:25 -07001472 AddAudioTrack(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -07001473 }
1474 for (const auto& track : local_stream->GetVideoTracks()) {
korniltsev.anatolyec390b52017-07-24 17:00:25 -07001475 AddVideoTrack(track.get(), local_stream);
deadbeefab9b2d12015-10-14 11:33:11 -07001476 }
1477
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001478 stats_->AddStream(local_stream);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001479 UpdateNegotiationNeeded();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480 return true;
1481}
1482
1483void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) {
Karl Wiberg744310f2019-02-14 10:18:56 +01001484 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 13:48:58 -08001485 RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified "
1486 "Plan SdpSemantics. Please use RemoveTrack "
1487 "instead.";
Peter Boström1a9d6152015-12-08 22:15:17 +01001488 TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream");
korniltsev.anatolyec390b52017-07-24 17:00:25 -07001489 if (!IsClosed()) {
1490 for (const auto& track : local_stream->GetAudioTracks()) {
1491 RemoveAudioTrack(track.get(), local_stream);
1492 }
1493 for (const auto& track : local_stream->GetVideoTracks()) {
1494 RemoveVideoTrack(track.get(), local_stream);
1495 }
deadbeefab9b2d12015-10-14 11:33:11 -07001496 }
deadbeefab9b2d12015-10-14 11:33:11 -07001497 local_streams_->RemoveStream(local_stream);
deadbeefeb459812015-12-15 19:24:43 -08001498 stream_observers_.erase(
1499 std::remove_if(
1500 stream_observers_.begin(), stream_observers_.end(),
kwibergd1fe2812016-04-27 06:47:29 -07001501 [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001502 return observer->stream()->id().compare(local_stream->id()) == 0;
deadbeefeb459812015-12-15 19:24:43 -08001503 }),
1504 stream_observers_.end());
deadbeefab9b2d12015-10-14 11:33:11 -07001505
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 if (IsClosed()) {
1507 return;
1508 }
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001509 UpdateNegotiationNeeded();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510}
1511
Steve Anton2d6c76a2018-01-05 17:10:52 -08001512RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -08001513 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -08001514 const std::vector<std::string>& stream_ids) {
Karl Wiberg744310f2019-02-14 10:18:56 +01001515 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton2d6c76a2018-01-05 17:10:52 -08001516 TRACE_EVENT0("webrtc", "PeerConnection::AddTrack");
Steve Antonf9381f02017-12-14 10:23:57 -08001517 if (!track) {
1518 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null.");
1519 }
1520 if (!(track->kind() == MediaStreamTrackInterface::kAudioKind ||
1521 track->kind() == MediaStreamTrackInterface::kVideoKind)) {
1522 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1523 "Track has invalid kind: " + track->kind());
1524 }
Steve Antonf9381f02017-12-14 10:23:57 -08001525 if (IsClosed()) {
1526 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
1527 "PeerConnection is closed.");
deadbeefe1f9d832016-01-14 15:35:42 -08001528 }
Steve Anton4171afb2017-11-20 10:20:22 -08001529 if (FindSenderForTrack(track)) {
Steve Antonf9381f02017-12-14 10:23:57 -08001530 LOG_AND_RETURN_ERROR(
1531 RTCErrorType::INVALID_PARAMETER,
1532 "Sender already exists for track " + track->id() + ".");
deadbeefe1f9d832016-01-14 15:35:42 -08001533 }
Steve Antonf9381f02017-12-14 10:23:57 -08001534 auto sender_or_error =
Seth Hampson5b4f0752018-04-02 16:31:36 -07001535 (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids)
1536 : AddTrackPlanB(track, stream_ids));
Steve Antonf9381f02017-12-14 10:23:57 -08001537 if (sender_or_error.ok()) {
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001538 UpdateNegotiationNeeded();
Steve Anton43a723a2018-01-04 15:48:17 -08001539 stats_->AddTrack(track);
Steve Antonf9381f02017-12-14 10:23:57 -08001540 }
1541 return sender_or_error;
1542}
deadbeefe1f9d832016-01-14 15:35:42 -08001543
Steve Antonf9381f02017-12-14 10:23:57 -08001544RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>>
1545PeerConnection::AddTrackPlanB(
1546 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -08001547 const std::vector<std::string>& stream_ids) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07001548 if (stream_ids.size() > 1u) {
1549 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION,
1550 "AddTrack with more than one stream is not "
1551 "supported with Plan B semantics.");
1552 }
1553 std::vector<std::string> adjusted_stream_ids = stream_ids;
1554 if (adjusted_stream_ids.empty()) {
1555 adjusted_stream_ids.push_back(rtc::CreateRandomUuid());
1556 }
Steve Anton02ee47c2018-01-10 16:26:06 -08001557 cricket::MediaType media_type =
1558 (track->kind() == MediaStreamTrackInterface::kAudioKind
1559 ? cricket::MEDIA_TYPE_AUDIO
1560 : cricket::MEDIA_TYPE_VIDEO);
Steve Anton111fdfd2018-06-25 13:03:36 -07001561 auto new_sender =
Florent Castelli892acf02018-10-01 22:47:20 +02001562 CreateSender(media_type, track->id(), track, adjusted_stream_ids, {});
deadbeefe1f9d832016-01-14 15:35:42 -08001563 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001564 new_sender->internal()->SetMediaChannel(voice_media_channel());
Steve Anton4171afb2017-11-20 10:20:22 -08001565 GetAudioTransceiver()->internal()->AddSender(new_sender);
Steve Anton4171afb2017-11-20 10:20:22 -08001566 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609ea2018-03-27 21:57:18 +00001567 FindSenderInfo(local_audio_sender_infos_,
Seth Hampson5b4f0752018-04-02 16:31:36 -07001568 new_sender->internal()->stream_ids()[0], track->id());
Steve Anton4171afb2017-11-20 10:20:22 -08001569 if (sender_info) {
1570 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
deadbeefe1f9d832016-01-14 15:35:42 -08001571 }
Steve Antonf9381f02017-12-14 10:23:57 -08001572 } else {
1573 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001574 new_sender->internal()->SetMediaChannel(video_media_channel());
Steve Anton4171afb2017-11-20 10:20:22 -08001575 GetVideoTransceiver()->internal()->AddSender(new_sender);
Steve Anton4171afb2017-11-20 10:20:22 -08001576 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609ea2018-03-27 21:57:18 +00001577 FindSenderInfo(local_video_sender_infos_,
Seth Hampson5b4f0752018-04-02 16:31:36 -07001578 new_sender->internal()->stream_ids()[0], track->id());
Steve Anton4171afb2017-11-20 10:20:22 -08001579 if (sender_info) {
1580 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
deadbeefe1f9d832016-01-14 15:35:42 -08001581 }
deadbeefe1f9d832016-01-14 15:35:42 -08001582 }
Steve Anton02ee47c2018-01-10 16:26:06 -08001583 return rtc::scoped_refptr<RtpSenderInterface>(new_sender);
Steve Antonf9381f02017-12-14 10:23:57 -08001584}
deadbeefe1f9d832016-01-14 15:35:42 -08001585
Steve Antonf9381f02017-12-14 10:23:57 -08001586RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>>
1587PeerConnection::AddTrackUnifiedPlan(
1588 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -08001589 const std::vector<std::string>& stream_ids) {
Steve Antonf9381f02017-12-14 10:23:57 -08001590 auto transceiver = FindFirstTransceiverForAddedTrack(track);
1591 if (transceiver) {
Steve Anton3d954a62018-04-02 11:27:23 -07001592 RTC_LOG(LS_INFO) << "Reusing an existing "
1593 << cricket::MediaTypeToString(transceiver->media_type())
1594 << " transceiver for AddTrack.";
Steve Antonf9381f02017-12-14 10:23:57 -08001595 if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) {
Steve Anton52d86772018-02-20 15:48:12 -08001596 transceiver->internal()->set_direction(
1597 RtpTransceiverDirection::kSendRecv);
Steve Antonf9381f02017-12-14 10:23:57 -08001598 } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) {
Steve Anton52d86772018-02-20 15:48:12 -08001599 transceiver->internal()->set_direction(
1600 RtpTransceiverDirection::kSendOnly);
Steve Antonf9381f02017-12-14 10:23:57 -08001601 }
Steve Anton02ee47c2018-01-10 16:26:06 -08001602 transceiver->sender()->SetTrack(track);
Henrik Andreasssoncc189172019-05-20 09:01:38 +00001603 transceiver->internal()->sender_internal()->set_stream_ids(stream_ids);
Eldar Rello353a7182019-11-25 18:49:44 +02001604 transceiver->internal()->set_reused_for_addtrack(true);
Steve Antonf9381f02017-12-14 10:23:57 -08001605 } else {
1606 cricket::MediaType media_type =
1607 (track->kind() == MediaStreamTrackInterface::kAudioKind
1608 ? cricket::MEDIA_TYPE_AUDIO
1609 : cricket::MEDIA_TYPE_VIDEO);
Steve Anton3d954a62018-04-02 11:27:23 -07001610 RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type)
1611 << " transceiver in response to a call to AddTrack.";
Steve Anton07563732018-06-26 11:13:50 -07001612 std::string sender_id = track->id();
1613 // Avoid creating a sender with an existing ID by generating a random ID.
1614 // This can happen if this is the second time AddTrack has created a sender
1615 // for this track.
1616 if (FindSenderById(sender_id)) {
1617 sender_id = rtc::CreateRandomUuid();
1618 }
Florent Castelli892acf02018-10-01 22:47:20 +02001619 auto sender = CreateSender(media_type, sender_id, track, stream_ids, {});
Steve Anton02ee47c2018-01-10 16:26:06 -08001620 auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid());
1621 transceiver = CreateAndAddTransceiver(sender, receiver);
Steve Antonf9381f02017-12-14 10:23:57 -08001622 transceiver->internal()->set_created_by_addtrack(true);
Steve Anton52d86772018-02-20 15:48:12 -08001623 transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv);
Steve Antonf9381f02017-12-14 10:23:57 -08001624 }
Steve Antonf9381f02017-12-14 10:23:57 -08001625 return transceiver->sender();
1626}
1627
1628rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1629PeerConnection::FindFirstTransceiverForAddedTrack(
1630 rtc::scoped_refptr<MediaStreamTrackInterface> track) {
1631 RTC_DCHECK(track);
1632 for (auto transceiver : transceivers_) {
1633 if (!transceiver->sender()->track() &&
Steve Anton69470252018-02-09 11:43:08 -08001634 cricket::MediaTypeToString(transceiver->media_type()) ==
Steve Antonf9381f02017-12-14 10:23:57 -08001635 track->kind() &&
Seth Hampson2f0d7022018-02-20 11:54:42 -08001636 !transceiver->internal()->has_ever_been_used_to_send() &&
1637 !transceiver->stopped()) {
Steve Antonf9381f02017-12-14 10:23:57 -08001638 return transceiver;
1639 }
1640 }
1641 return nullptr;
deadbeefe1f9d832016-01-14 15:35:42 -08001642}
1643
1644bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) {
1645 TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack");
Steve Anton24db5732018-07-23 10:27:33 -07001646 return RemoveTrackNew(sender).ok();
Steve Antonf9381f02017-12-14 10:23:57 -08001647}
1648
Steve Anton24db5732018-07-23 10:27:33 -07001649RTCError PeerConnection::RemoveTrackNew(
Steve Antonf9381f02017-12-14 10:23:57 -08001650 rtc::scoped_refptr<RtpSenderInterface> sender) {
Karl Wiberg744310f2019-02-14 10:18:56 +01001651 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -08001652 if (!sender) {
1653 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null.");
1654 }
deadbeefe1f9d832016-01-14 15:35:42 -08001655 if (IsClosed()) {
Steve Antonf9381f02017-12-14 10:23:57 -08001656 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
1657 "PeerConnection is closed.");
deadbeefe1f9d832016-01-14 15:35:42 -08001658 }
Steve Antonf9381f02017-12-14 10:23:57 -08001659 if (IsUnifiedPlan()) {
1660 auto transceiver = FindTransceiverBySender(sender);
1661 if (!transceiver || !sender->track()) {
1662 return RTCError::OK();
1663 }
1664 sender->SetTrack(nullptr);
1665 if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) {
Steve Anton52d86772018-02-20 15:48:12 -08001666 transceiver->internal()->set_direction(
1667 RtpTransceiverDirection::kRecvOnly);
Steve Antonf9381f02017-12-14 10:23:57 -08001668 } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) {
Steve Anton52d86772018-02-20 15:48:12 -08001669 transceiver->internal()->set_direction(
1670 RtpTransceiverDirection::kInactive);
Steve Antonf9381f02017-12-14 10:23:57 -08001671 }
Steve Anton4171afb2017-11-20 10:20:22 -08001672 } else {
Steve Antonf9381f02017-12-14 10:23:57 -08001673 bool removed;
1674 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
1675 removed = GetAudioTransceiver()->internal()->RemoveSender(sender);
1676 } else {
1677 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type());
1678 removed = GetVideoTransceiver()->internal()->RemoveSender(sender);
1679 }
1680 if (!removed) {
1681 LOG_AND_RETURN_ERROR(
1682 RTCErrorType::INVALID_PARAMETER,
1683 "Couldn't find sender " + sender->id() + " to remove.");
1684 }
Steve Anton4171afb2017-11-20 10:20:22 -08001685 }
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001686 UpdateNegotiationNeeded();
Steve Antonf9381f02017-12-14 10:23:57 -08001687 return RTCError::OK();
1688}
1689
1690rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1691PeerConnection::FindTransceiverBySender(
1692 rtc::scoped_refptr<RtpSenderInterface> sender) {
1693 for (auto transceiver : transceivers_) {
1694 if (transceiver->sender() == sender) {
1695 return transceiver;
1696 }
1697 }
1698 return nullptr;
deadbeefe1f9d832016-01-14 15:35:42 -08001699}
1700
Steve Anton9158ef62017-11-27 13:01:52 -08001701RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1702PeerConnection::AddTransceiver(
1703 rtc::scoped_refptr<MediaStreamTrackInterface> track) {
1704 return AddTransceiver(track, RtpTransceiverInit());
1705}
1706
1707RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1708PeerConnection::AddTransceiver(
1709 rtc::scoped_refptr<MediaStreamTrackInterface> track,
1710 const RtpTransceiverInit& init) {
Karl Wiberg744310f2019-02-14 10:18:56 +01001711 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 13:48:58 -08001712 RTC_CHECK(IsUnifiedPlan())
1713 << "AddTransceiver is only available with Unified Plan SdpSemantics";
Steve Anton9158ef62017-11-27 13:01:52 -08001714 if (!track) {
1715 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null");
1716 }
1717 cricket::MediaType media_type;
1718 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
1719 media_type = cricket::MEDIA_TYPE_AUDIO;
1720 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
1721 media_type = cricket::MEDIA_TYPE_VIDEO;
1722 } else {
1723 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1724 "Track kind is not audio or video");
1725 }
1726 return AddTransceiver(media_type, track, init);
1727}
1728
1729RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1730PeerConnection::AddTransceiver(cricket::MediaType media_type) {
1731 return AddTransceiver(media_type, RtpTransceiverInit());
1732}
1733
1734RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1735PeerConnection::AddTransceiver(cricket::MediaType media_type,
1736 const RtpTransceiverInit& init) {
Karl Wiberg744310f2019-02-14 10:18:56 +01001737 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 13:48:58 -08001738 RTC_CHECK(IsUnifiedPlan())
1739 << "AddTransceiver is only available with Unified Plan SdpSemantics";
Steve Anton9158ef62017-11-27 13:01:52 -08001740 if (!(media_type == cricket::MEDIA_TYPE_AUDIO ||
1741 media_type == cricket::MEDIA_TYPE_VIDEO)) {
1742 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1743 "media type is not audio or video");
1744 }
1745 return AddTransceiver(media_type, nullptr, init);
1746}
1747
1748RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
1749PeerConnection::AddTransceiver(
1750 cricket::MediaType media_type,
1751 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 13:58:07 -08001752 const RtpTransceiverInit& init,
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001753 bool update_negotiation_needed) {
Steve Anton9158ef62017-11-27 13:01:52 -08001754 RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO ||
1755 media_type == cricket::MEDIA_TYPE_VIDEO));
1756 if (track) {
1757 RTC_DCHECK_EQ(media_type,
1758 (track->kind() == MediaStreamTrackInterface::kAudioKind
1759 ? cricket::MEDIA_TYPE_AUDIO
1760 : cricket::MEDIA_TYPE_VIDEO));
1761 }
1762
Amit Hilbuche2a284d2019-03-05 12:36:31 -08001763 RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings,
1764 init.send_encodings.size(), 0, 7, 8);
1765
Amit Hilbuchaa584152019-02-06 17:09:52 -08001766 size_t num_rids = absl::c_count_if(init.send_encodings,
1767 [](const RtpEncodingParameters& encoding) {
1768 return !encoding.rid.empty();
1769 });
1770 if (num_rids > 0 && num_rids != init.send_encodings.size()) {
Amit Hilbuchce470aa2019-02-06 17:09:52 -08001771 LOG_AND_RETURN_ERROR(
Amit Hilbuchaa584152019-02-06 17:09:52 -08001772 RTCErrorType::INVALID_PARAMETER,
1773 "RIDs must be provided for either all or none of the send encodings.");
Emircan Uysaler78323432019-02-08 20:41:39 +00001774 }
1775
Amit Hilbuchf4770402019-04-08 14:11:57 -07001776 if (num_rids > 0 && absl::c_any_of(init.send_encodings,
1777 [](const RtpEncodingParameters& encoding) {
1778 return !IsLegalRsidName(encoding.rid);
1779 })) {
1780 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
1781 "Invalid RID value provided.");
1782 }
1783
Amit Hilbuchaa584152019-02-06 17:09:52 -08001784 if (absl::c_any_of(init.send_encodings,
1785 [](const RtpEncodingParameters& encoding) {
1786 return encoding.ssrc.has_value();
1787 })) {
1788 LOG_AND_RETURN_ERROR(
1789 RTCErrorType::UNSUPPORTED_PARAMETER,
1790 "Attempted to set an unimplemented parameter of RtpParameters.");
Florent Castelli892acf02018-10-01 22:47:20 +02001791 }
1792
1793 RtpParameters parameters;
1794 parameters.encodings = init.send_encodings;
Amit Hilbuchaa584152019-02-06 17:09:52 -08001795
1796 // Encodings are dropped from the tail if too many are provided.
1797 if (parameters.encodings.size() > kMaxSimulcastStreams) {
1798 parameters.encodings.erase(
1799 parameters.encodings.begin() + kMaxSimulcastStreams,
1800 parameters.encodings.end());
1801 }
1802
1803 // Single RID should be removed.
1804 if (parameters.encodings.size() == 1 &&
1805 !parameters.encodings[0].rid.empty()) {
1806 RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << ".";
1807 parameters.encodings[0].rid.clear();
1808 }
1809
1810 // If RIDs were not provided, they are generated for simulcast scenario.
1811 if (parameters.encodings.size() > 1 && num_rids == 0) {
1812 rtc::UniqueStringGenerator rid_generator;
1813 for (RtpEncodingParameters& encoding : parameters.encodings) {
1814 encoding.rid = rid_generator();
1815 }
1816 }
1817
Florent Castelli892acf02018-10-01 22:47:20 +02001818 if (UnimplementedRtpParameterHasValue(parameters)) {
1819 LOG_AND_RETURN_ERROR(
1820 RTCErrorType::UNSUPPORTED_PARAMETER,
1821 "Attempted to set an unimplemented parameter of RtpParameters.");
1822 }
Steve Anton9158ef62017-11-27 13:01:52 -08001823
Florent Castellic1a0bcb2019-01-29 14:26:48 +01001824 auto result = cricket::CheckRtpParametersValues(parameters);
1825 if (!result.ok()) {
1826 LOG_AND_RETURN_ERROR(result.type(), result.message());
1827 }
1828
Steve Anton3d954a62018-04-02 11:27:23 -07001829 RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type)
1830 << " transceiver in response to a call to AddTransceiver.";
Steve Anton07563732018-06-26 11:13:50 -07001831 // Set the sender ID equal to the track ID if the track is specified unless
1832 // that sender ID is already in use.
1833 std::string sender_id =
1834 (track && !FindSenderById(track->id()) ? track->id()
1835 : rtc::CreateRandomUuid());
Florent Castelli892acf02018-10-01 22:47:20 +02001836 auto sender = CreateSender(media_type, sender_id, track, init.stream_ids,
Amit Hilbuchaa584152019-02-06 17:09:52 -08001837 parameters.encodings);
Steve Anton02ee47c2018-01-10 16:26:06 -08001838 auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid());
1839 auto transceiver = CreateAndAddTransceiver(sender, receiver);
1840 transceiver->internal()->set_direction(init.direction);
1841
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001842 if (update_negotiation_needed) {
1843 UpdateNegotiationNeeded();
Steve Anton22da89f2018-01-25 13:58:07 -08001844 }
Steve Antonf9381f02017-12-14 10:23:57 -08001845
1846 return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver);
1847}
1848
Steve Anton02ee47c2018-01-10 16:26:06 -08001849rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
1850PeerConnection::CreateSender(
1851 cricket::MediaType media_type,
Steve Anton111fdfd2018-06-25 13:03:36 -07001852 const std::string& id,
Steve Anton02ee47c2018-01-10 16:26:06 -08001853 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Florent Castelli892acf02018-10-01 22:47:20 +02001854 const std::vector<std::string>& stream_ids,
1855 const std::vector<RtpEncodingParameters>& send_encodings) {
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001856 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton9158ef62017-11-27 13:01:52 -08001857 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender;
Steve Anton02ee47c2018-01-10 16:26:06 -08001858 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1859 RTC_DCHECK(!track ||
1860 (track->kind() == MediaStreamTrackInterface::kAudioKind));
1861 sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1862 signaling_thread(),
Guido Urdaneta1ff16c82019-05-20 19:31:53 +02001863 AudioRtpSender::Create(worker_thread(), id, stats_.get(), this));
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001864 NoteUsageEvent(UsageEvent::AUDIO_ADDED);
Steve Anton02ee47c2018-01-10 16:26:06 -08001865 } else {
1866 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
1867 RTC_DCHECK(!track ||
1868 (track->kind() == MediaStreamTrackInterface::kVideoKind));
1869 sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Guido Urdaneta1ff16c82019-05-20 19:31:53 +02001870 signaling_thread(), VideoRtpSender::Create(worker_thread(), id, this));
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001871 NoteUsageEvent(UsageEvent::VIDEO_ADDED);
Steve Anton02ee47c2018-01-10 16:26:06 -08001872 }
Steve Anton111fdfd2018-06-25 13:03:36 -07001873 bool set_track_succeeded = sender->SetTrack(track);
1874 RTC_DCHECK(set_track_succeeded);
Henrik Andreasssoncc189172019-05-20 09:01:38 +00001875 sender->internal()->set_stream_ids(stream_ids);
Florent Castelli892acf02018-10-01 22:47:20 +02001876 sender->internal()->set_init_send_encodings(send_encodings);
Steve Anton02ee47c2018-01-10 16:26:06 -08001877 return sender;
1878}
1879
1880rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1881PeerConnection::CreateReceiver(cricket::MediaType media_type,
1882 const std::string& receiver_id) {
Steve Anton9158ef62017-11-27 13:01:52 -08001883 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1884 receiver;
Steve Anton9158ef62017-11-27 13:01:52 -08001885 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton9158ef62017-11-27 13:01:52 -08001886 receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
Henrik Boström199e27b2018-07-04 20:51:53 +02001887 signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id,
1888 std::vector<std::string>({})));
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001889 NoteUsageEvent(UsageEvent::AUDIO_ADDED);
Steve Anton9158ef62017-11-27 13:01:52 -08001890 } else {
Steve Anton02ee47c2018-01-10 16:26:06 -08001891 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
Steve Anton9158ef62017-11-27 13:01:52 -08001892 receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
Henrik Boström199e27b2018-07-04 20:51:53 +02001893 signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id,
1894 std::vector<std::string>({})));
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001895 NoteUsageEvent(UsageEvent::VIDEO_ADDED);
Steve Anton9158ef62017-11-27 13:01:52 -08001896 }
Steve Anton02ee47c2018-01-10 16:26:06 -08001897 return receiver;
1898}
1899
1900rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1901PeerConnection::CreateAndAddTransceiver(
1902 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
1903 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
1904 receiver) {
Steve Anton07563732018-06-26 11:13:50 -07001905 // Ensure that the new sender does not have an ID that is already in use by
1906 // another sender.
1907 // Allow receiver IDs to conflict since those come from remote SDP (which
1908 // could be invalid, but should not cause a crash).
1909 RTC_DCHECK(!FindSenderById(sender->id()));
Steve Anton02ee47c2018-01-10 16:26:06 -08001910 auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create(
Florent Castelli2d9d82e2019-04-23 19:25:51 +02001911 signaling_thread(),
Markus Handell0357b3e2020-03-16 13:40:51 +01001912 new RtpTransceiver(
1913 sender, receiver, channel_manager(),
1914 sender->media_type() == cricket::MEDIA_TYPE_AUDIO
1915 ? channel_manager()->GetSupportedAudioRtpHeaderExtensions()
1916 : channel_manager()->GetSupportedVideoRtpHeaderExtensions()));
Steve Anton9158ef62017-11-27 13:01:52 -08001917 transceivers_.push_back(transceiver);
Steve Anton52d86772018-02-20 15:48:12 -08001918 transceiver->internal()->SignalNegotiationNeeded.connect(
1919 this, &PeerConnection::OnNegotiationNeeded);
Steve Antonf9381f02017-12-14 10:23:57 -08001920 return transceiver;
Steve Anton9158ef62017-11-27 13:01:52 -08001921}
1922
Steve Anton52d86772018-02-20 15:48:12 -08001923void PeerConnection::OnNegotiationNeeded() {
1924 RTC_DCHECK_RUN_ON(signaling_thread());
1925 RTC_DCHECK(!IsClosed());
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001926 UpdateNegotiationNeeded();
Steve Anton52d86772018-02-20 15:48:12 -08001927}
1928
deadbeeffac06552015-11-25 11:26:01 -08001929rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -08001930 const std::string& kind,
1931 const std::string& stream_id) {
Karl Wiberg5966c502019-02-21 23:55:09 +01001932 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 13:48:58 -08001933 RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified "
1934 "Plan SdpSemantics. Please use AddTransceiver "
1935 "instead.";
Peter Boström1a9d6152015-12-08 22:15:17 +01001936 TRACE_EVENT0("webrtc", "PeerConnection::CreateSender");
zhihuang29ff8442016-07-27 11:07:25 -07001937 if (IsClosed()) {
1938 return nullptr;
1939 }
Steve Anton4171afb2017-11-20 10:20:22 -08001940
Seth Hampson5b4f0752018-04-02 16:31:36 -07001941 // Internally we need to have one stream with Plan B semantics, so we
1942 // generate a random stream ID if not specified.
Seth Hampson845e8782018-03-02 11:34:10 -08001943 std::vector<std::string> stream_ids;
Seth Hampson5b4f0752018-04-02 16:31:36 -07001944 if (stream_id.empty()) {
1945 stream_ids.push_back(rtc::CreateRandomUuid());
1946 RTC_LOG(LS_INFO)
1947 << "No stream_id specified for sender. Generated stream ID: "
1948 << stream_ids[0];
1949 } else {
Seth Hampson845e8782018-03-02 11:34:10 -08001950 stream_ids.push_back(stream_id);
Steve Anton02ee47c2018-01-10 16:26:06 -08001951 }
1952
Steve Anton4171afb2017-11-20 10:20:22 -08001953 // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver.
deadbeefa601f5c2016-06-06 14:27:39 -07001954 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender;
deadbeeffac06552015-11-25 11:26:01 -08001955 if (kind == MediaStreamTrackInterface::kAudioKind) {
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -08001956 auto audio_sender = AudioRtpSender::Create(
Guido Urdaneta1ff16c82019-05-20 19:31:53 +02001957 worker_thread(), rtc::CreateRandomUuid(), stats_.get(), this);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001958 audio_sender->SetMediaChannel(voice_media_channel());
deadbeefa601f5c2016-06-06 14:27:39 -07001959 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Steve Anton02ee47c2018-01-10 16:26:06 -08001960 signaling_thread(), audio_sender);
Steve Anton4171afb2017-11-20 10:20:22 -08001961 GetAudioTransceiver()->internal()->AddSender(new_sender);
deadbeeffac06552015-11-25 11:26:01 -08001962 } else if (kind == MediaStreamTrackInterface::kVideoKind) {
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -08001963 auto video_sender =
Guido Urdaneta1ff16c82019-05-20 19:31:53 +02001964 VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), this);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001965 video_sender->SetMediaChannel(video_media_channel());
deadbeefa601f5c2016-06-06 14:27:39 -07001966 new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
Steve Anton02ee47c2018-01-10 16:26:06 -08001967 signaling_thread(), video_sender);
Steve Anton4171afb2017-11-20 10:20:22 -08001968 GetVideoTransceiver()->internal()->AddSender(new_sender);
deadbeeffac06552015-11-25 11:26:01 -08001969 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001970 RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind;
Steve Anton4171afb2017-11-20 10:20:22 -08001971 return nullptr;
deadbeeffac06552015-11-25 11:26:01 -08001972 }
Henrik Andreasssoncc189172019-05-20 09:01:38 +00001973 new_sender->internal()->set_stream_ids(stream_ids);
Steve Anton4171afb2017-11-20 10:20:22 -08001974
deadbeefe1f9d832016-01-14 15:35:42 -08001975 return new_sender;
deadbeeffac06552015-11-25 11:26:01 -08001976}
1977
deadbeef70ab1a12015-09-28 16:53:55 -07001978std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders()
1979 const {
Karl Wiberga58e1692019-03-26 13:33:43 +01001980 RTC_DCHECK_RUN_ON(signaling_thread());
deadbeefa601f5c2016-06-06 14:27:39 -07001981 std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001982 for (const auto& sender : GetSendersInternal()) {
Steve Anton4171afb2017-11-20 10:20:22 -08001983 ret.push_back(sender);
deadbeefa601f5c2016-06-06 14:27:39 -07001984 }
1985 return ret;
deadbeef70ab1a12015-09-28 16:53:55 -07001986}
1987
Steve Anton4171afb2017-11-20 10:20:22 -08001988std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
1989PeerConnection::GetSendersInternal() const {
1990 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
1991 all_senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001992 for (const auto& transceiver : transceivers_) {
Steve Anton4171afb2017-11-20 10:20:22 -08001993 auto senders = transceiver->internal()->senders();
1994 all_senders.insert(all_senders.end(), senders.begin(), senders.end());
1995 }
1996 return all_senders;
1997}
1998
deadbeef70ab1a12015-09-28 16:53:55 -07001999std::vector<rtc::scoped_refptr<RtpReceiverInterface>>
2000PeerConnection::GetReceivers() const {
Karl Wiberga58e1692019-03-26 13:33:43 +01002001 RTC_DCHECK_RUN_ON(signaling_thread());
deadbeefa601f5c2016-06-06 14:27:39 -07002002 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret;
Steve Anton4171afb2017-11-20 10:20:22 -08002003 for (const auto& receiver : GetReceiversInternal()) {
2004 ret.push_back(receiver);
deadbeefa601f5c2016-06-06 14:27:39 -07002005 }
2006 return ret;
deadbeef70ab1a12015-09-28 16:53:55 -07002007}
2008
Steve Anton4171afb2017-11-20 10:20:22 -08002009std::vector<
2010 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
2011PeerConnection::GetReceiversInternal() const {
2012 std::vector<
2013 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
2014 all_receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01002015 for (const auto& transceiver : transceivers_) {
Steve Anton4171afb2017-11-20 10:20:22 -08002016 auto receivers = transceiver->internal()->receivers();
2017 all_receivers.insert(all_receivers.end(), receivers.begin(),
2018 receivers.end());
2019 }
2020 return all_receivers;
2021}
2022
Steve Anton9158ef62017-11-27 13:01:52 -08002023std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>
2024PeerConnection::GetTransceivers() const {
Karl Wiberg5966c502019-02-21 23:55:09 +01002025 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonfc853712018-03-01 13:48:58 -08002026 RTC_CHECK(IsUnifiedPlan())
2027 << "GetTransceivers is only supported with Unified Plan SdpSemantics.";
Steve Anton9158ef62017-11-27 13:01:52 -08002028 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01002029 for (const auto& transceiver : transceivers_) {
Steve Anton9158ef62017-11-27 13:01:52 -08002030 all_transceivers.push_back(transceiver);
2031 }
2032 return all_transceivers;
2033}
2034
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035bool PeerConnection::GetStats(StatsObserver* observer,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002036 MediaStreamTrackInterface* track,
2037 StatsOutputLevel level) {
Peter Boström1a9d6152015-12-08 22:15:17 +01002038 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
Karl Wiberg6cab5c82019-03-26 09:57:01 +01002039 RTC_DCHECK_RUN_ON(signaling_thread());
nisse7ce109a2017-01-31 00:57:56 -08002040 if (!observer) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002041 RTC_LOG(LS_ERROR) << "GetStats - observer is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 return false;
2043 }
2044
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00002045 stats_->UpdateStats(level);
zhihuange9e94c32016-11-04 11:38:15 -07002046 // The StatsCollector is used to tell if a track is valid because it may
2047 // remember tracks that the PeerConnection previously removed.
2048 if (track && !stats_->IsValidTrack(track->id())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002049 RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: "
2050 << track->id();
zhihuange9e94c32016-11-04 11:38:15 -07002051 return false;
2052 }
Steve Antonad182762018-09-05 20:22:40 +00002053 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS,
2054 new GetStatsMsg(observer, track));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055 return true;
2056}
2057
hbos74e1a4f2016-09-15 23:33:01 -07002058void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) {
Henrik Boström1df1bf82018-03-20 13:24:20 +01002059 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
Karl Wiberg6cab5c82019-03-26 09:57:01 +01002060 RTC_DCHECK_RUN_ON(signaling_thread());
hbos74e1a4f2016-09-15 23:33:01 -07002061 RTC_DCHECK(stats_collector_);
Henrik Boström1df1bf82018-03-20 13:24:20 +01002062 RTC_DCHECK(callback);
hbos74e1a4f2016-09-15 23:33:01 -07002063 stats_collector_->GetStatsReport(callback);
2064}
2065
Henrik Boström1df1bf82018-03-20 13:24:20 +01002066void PeerConnection::GetStats(
2067 rtc::scoped_refptr<RtpSenderInterface> selector,
2068 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
2069 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
Karl Wiberg6cab5c82019-03-26 09:57:01 +01002070 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boström1df1bf82018-03-20 13:24:20 +01002071 RTC_DCHECK(callback);
2072 RTC_DCHECK(stats_collector_);
2073 rtc::scoped_refptr<RtpSenderInternal> internal_sender;
2074 if (selector) {
2075 for (const auto& proxy_transceiver : transceivers_) {
2076 for (const auto& proxy_sender :
2077 proxy_transceiver->internal()->senders()) {
2078 if (proxy_sender == selector) {
2079 internal_sender = proxy_sender->internal();
2080 break;
2081 }
2082 }
2083 if (internal_sender)
2084 break;
2085 }
2086 }
Sebastian Jansson6eb8a162018-11-16 11:29:55 +01002087 // If there is no |internal_sender| then |selector| is either null or does not
Henrik Boström1df1bf82018-03-20 13:24:20 +01002088 // belong to the PeerConnection (in Plan B, senders can be removed from the
2089 // PeerConnection). This means that "all the stats objects representing the
2090 // selector" is an empty set. Invoking GetStatsReport() with a null selector
2091 // produces an empty stats report.
2092 stats_collector_->GetStatsReport(internal_sender, callback);
2093}
2094
2095void PeerConnection::GetStats(
2096 rtc::scoped_refptr<RtpReceiverInterface> selector,
2097 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
2098 TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
Karl Wiberg6cab5c82019-03-26 09:57:01 +01002099 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boström1df1bf82018-03-20 13:24:20 +01002100 RTC_DCHECK(callback);
2101 RTC_DCHECK(stats_collector_);
2102 rtc::scoped_refptr<RtpReceiverInternal> internal_receiver;
2103 if (selector) {
2104 for (const auto& proxy_transceiver : transceivers_) {
2105 for (const auto& proxy_receiver :
2106 proxy_transceiver->internal()->receivers()) {
2107 if (proxy_receiver == selector) {
2108 internal_receiver = proxy_receiver->internal();
2109 break;
2110 }
2111 }
2112 if (internal_receiver)
2113 break;
2114 }
2115 }
Sebastian Jansson6eb8a162018-11-16 11:29:55 +01002116 // If there is no |internal_receiver| then |selector| is either null or does
Henrik Boström1df1bf82018-03-20 13:24:20 +01002117 // not belong to the PeerConnection (in Plan B, receivers can be removed from
2118 // the PeerConnection). This means that "all the stats objects representing
2119 // the selector" is an empty set. Invoking GetStatsReport() with a null
2120 // selector produces an empty stats report.
2121 stats_collector_->GetStatsReport(internal_receiver, callback);
2122}
2123
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124PeerConnectionInterface::SignalingState PeerConnection::signaling_state() {
Karl Wiberg8d2e2282019-02-17 13:00:07 +01002125 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 return signaling_state_;
2127}
2128
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129PeerConnectionInterface::IceConnectionState
2130PeerConnection::ice_connection_state() {
Karl Wiberg8d2e2282019-02-17 13:00:07 +01002131 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 return ice_connection_state_;
2133}
2134
Alex Loiko9289eda2018-11-23 16:18:59 +00002135PeerConnectionInterface::IceConnectionState
2136PeerConnection::standardized_ice_connection_state() {
Karl Wiberg8d2e2282019-02-17 13:00:07 +01002137 RTC_DCHECK_RUN_ON(signaling_thread());
Alex Loiko9289eda2018-11-23 16:18:59 +00002138 return standardized_ice_connection_state_;
2139}
2140
Jonas Olsson635474e2018-10-18 15:58:17 +02002141PeerConnectionInterface::PeerConnectionState
2142PeerConnection::peer_connection_state() {
Karl Wiberg8d2e2282019-02-17 13:00:07 +01002143 RTC_DCHECK_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +02002144 return connection_state_;
2145}
2146
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147PeerConnectionInterface::IceGatheringState
2148PeerConnection::ice_gathering_state() {
Karl Wiberg8d2e2282019-02-17 13:00:07 +01002149 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150 return ice_gathering_state_;
2151}
2152
Harald Alvestrand61f74d92020-03-02 11:20:00 +01002153absl::optional<bool> PeerConnection::can_trickle_ice_candidates() {
2154 RTC_DCHECK_RUN_ON(signaling_thread());
2155 SessionDescriptionInterface* description = current_remote_description_.get();
2156 if (!description) {
2157 description = pending_remote_description_.get();
2158 }
2159 if (!description) {
2160 return absl::nullopt;
2161 }
2162 // TODO(bugs.webrtc.org/7443): Change to retrieve from session-level option.
2163 if (description->description()->transport_infos().size() < 1) {
2164 return absl::nullopt;
2165 }
2166 return description->description()->transport_infos()[0].description.HasOption(
2167 "trickle");
2168}
2169
Yves Gerey665174f2018-06-19 15:03:05 +02002170rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 const std::string& label,
2172 const DataChannelInit* config) {
Karl Wiberg106d92d2019-02-14 10:17:47 +01002173 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 22:15:17 +01002174 TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel");
zhihuang9763d562016-08-05 11:14:50 -07002175
Harald Alvestrand05e4d082019-12-03 14:04:21 +01002176 bool first_datachannel = !data_channel_controller_.HasDataChannels();
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002177
kwibergd1fe2812016-04-27 06:47:29 -07002178 std::unique_ptr<InternalDataChannelInit> internal_config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002179 if (config) {
2180 internal_config.reset(new InternalDataChannelInit(*config));
2181 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002182 rtc::scoped_refptr<DataChannelInterface> channel(
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01002183 data_channel_controller_.InternalCreateDataChannel(
2184 label, internal_config.get()));
deadbeefab9b2d12015-10-14 11:33:11 -07002185 if (!channel.get()) {
2186 return nullptr;
2187 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002189 // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or
2190 // the first SCTP DataChannel.
Steve Anton75737c02017-11-06 10:37:17 -08002191 if (data_channel_type() == cricket::DCT_RTP || first_datachannel) {
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002192 UpdateNegotiationNeeded();
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002193 }
Harald Alvestrand8ebba742018-05-31 14:00:34 +02002194 NoteUsageEvent(UsageEvent::DATA_ADDED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 return DataChannelProxy::Create(signaling_thread(), channel.get());
2196}
2197
Henrik Boström79b69802019-07-18 11:16:56 +02002198void PeerConnection::RestartIce() {
2199 RTC_DCHECK_RUN_ON(signaling_thread());
2200 local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions(
2201 current_local_description_.get(), pending_local_description_.get());
2202 UpdateNegotiationNeeded();
2203}
2204
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer,
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002206 const RTCOfferAnswerOptions& options) {
Karl Wiberg5966c502019-02-21 23:55:09 +01002207 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boströma3728d32019-10-28 12:09:49 +01002208 // Chain this operation. If asynchronous operations are pending on the chain,
2209 // this operation will be queued to be invoked, otherwise the contents of the
2210 // lambda will execute immediately.
2211 operations_chain_->ChainOperation(
2212 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2213 observer_refptr =
2214 rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
2215 options](std::function<void()> operations_chain_callback) {
2216 // Abort early if |this_weak_ptr| is no longer valid.
2217 if (!this_weak_ptr) {
2218 observer_refptr->OnFailure(
2219 RTCError(RTCErrorType::INTERNAL_ERROR,
2220 "CreateOffer failed because the session was shut down"));
2221 operations_chain_callback();
2222 return;
2223 }
2224 // The operation completes asynchronously when the wrapper is invoked.
2225 rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
2226 observer_wrapper(new rtc::RefCountedObject<
2227 CreateSessionDescriptionObserverOperationWrapper>(
2228 std::move(observer_refptr),
2229 std::move(operations_chain_callback)));
2230 this_weak_ptr->DoCreateOffer(options, observer_wrapper);
2231 });
2232}
2233
2234void PeerConnection::DoCreateOffer(
2235 const RTCOfferAnswerOptions& options,
2236 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
2237 RTC_DCHECK_RUN_ON(signaling_thread());
2238 TRACE_EVENT0("webrtc", "PeerConnection::DoCreateOffer");
Steve Anton8d3444d2017-10-20 15:30:51 -07002239
nisse7ce109a2017-01-31 00:57:56 -08002240 if (!observer) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002241 RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +00002242 return;
2243 }
deadbeefab9b2d12015-10-14 11:33:11 -07002244
Steve Anton8d3444d2017-10-20 15:30:51 -07002245 if (IsClosed()) {
2246 std::string error = "CreateOffer called when PeerConnection is closed.";
Mirko Bonadei675513b2017-11-09 11:09:25 +01002247 RTC_LOG(LS_ERROR) << error;
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01002248 PostCreateSessionDescriptionFailure(
Harald Alvestrand3725d542018-04-13 15:02:10 +02002249 observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
Steve Anton8d3444d2017-10-20 15:30:51 -07002250 return;
2251 }
2252
Steve Anton25ca0ac2019-06-25 10:40:48 -07002253 // If a session error has occurred the PeerConnection is in a possibly
2254 // inconsistent state so fail right away.
2255 if (session_error() != SessionError::kNone) {
2256 std::string error_message = GetSessionErrorMsg();
2257 RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message;
2258 PostCreateSessionDescriptionFailure(
2259 observer,
2260 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2261 return;
2262 }
2263
zhihuang1c378ed2017-08-17 14:10:50 -07002264 if (!ValidateOfferAnswerOptions(options)) {
deadbeefab9b2d12015-10-14 11:33:11 -07002265 std::string error = "CreateOffer called with invalid options.";
Mirko Bonadei675513b2017-11-09 11:09:25 +01002266 RTC_LOG(LS_ERROR) << error;
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01002267 PostCreateSessionDescriptionFailure(
Harald Alvestrand3725d542018-04-13 15:02:10 +02002268 observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error)));
deadbeefab9b2d12015-10-14 11:33:11 -07002269 return;
2270 }
2271
Steve Anton22da89f2018-01-25 13:58:07 -08002272 // Legacy handling for offer_to_receive_audio and offer_to_receive_video.
2273 // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions".
2274 if (IsUnifiedPlan()) {
2275 RTCError error = HandleLegacyOfferOptions(options);
2276 if (!error.ok()) {
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01002277 PostCreateSessionDescriptionFailure(observer, std::move(error));
Steve Anton22da89f2018-01-25 13:58:07 -08002278 return;
2279 }
2280 }
2281
zhihuang1c378ed2017-08-17 14:10:50 -07002282 cricket::MediaSessionOptions session_options;
2283 GetOptionsForOffer(options, &session_options);
Steve Antond25da372017-11-06 14:50:29 -08002284 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285}
2286
Steve Anton22da89f2018-01-25 13:58:07 -08002287RTCError PeerConnection::HandleLegacyOfferOptions(
2288 const RTCOfferAnswerOptions& options) {
2289 RTC_DCHECK(IsUnifiedPlan());
2290
2291 if (options.offer_to_receive_audio == 0) {
2292 RemoveRecvDirectionFromReceivingTransceiversOfType(
2293 cricket::MEDIA_TYPE_AUDIO);
2294 } else if (options.offer_to_receive_audio == 1) {
2295 AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2296 } else if (options.offer_to_receive_audio > 1) {
2297 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
2298 "offer_to_receive_audio > 1 is not supported.");
2299 }
2300
2301 if (options.offer_to_receive_video == 0) {
2302 RemoveRecvDirectionFromReceivingTransceiversOfType(
2303 cricket::MEDIA_TYPE_VIDEO);
2304 } else if (options.offer_to_receive_video == 1) {
2305 AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2306 } else if (options.offer_to_receive_video > 1) {
2307 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
2308 "offer_to_receive_video > 1 is not supported.");
2309 }
2310
2311 return RTCError::OK();
2312}
2313
2314void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType(
2315 cricket::MediaType media_type) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01002316 for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) {
Steve Anton3d954a62018-04-02 11:27:23 -07002317 RtpTransceiverDirection new_direction =
2318 RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false);
2319 if (new_direction != transceiver->direction()) {
2320 RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type)
2321 << " transceiver (MID="
2322 << transceiver->mid().value_or("<not set>") << ") from "
2323 << RtpTransceiverDirectionToString(
2324 transceiver->direction())
2325 << " to "
2326 << RtpTransceiverDirectionToString(new_direction)
2327 << " since CreateOffer specified offer_to_receive=0";
2328 transceiver->internal()->set_direction(new_direction);
2329 }
Steve Anton22da89f2018-01-25 13:58:07 -08002330 }
2331}
2332
2333void PeerConnection::AddUpToOneReceivingTransceiverOfType(
2334 cricket::MediaType media_type) {
Karl Wiberg744310f2019-02-14 10:18:56 +01002335 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -08002336 if (GetReceivingTransceiversOfType(media_type).empty()) {
Steve Anton3d954a62018-04-02 11:27:23 -07002337 RTC_LOG(LS_INFO)
2338 << "Adding one recvonly " << cricket::MediaTypeToString(media_type)
2339 << " transceiver since CreateOffer specified offer_to_receive=1";
Steve Anton22da89f2018-01-25 13:58:07 -08002340 RtpTransceiverInit init;
2341 init.direction = RtpTransceiverDirection::kRecvOnly;
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002342 AddTransceiver(media_type, nullptr, init,
2343 /*update_negotiation_needed=*/false);
Steve Anton22da89f2018-01-25 13:58:07 -08002344 }
2345}
2346
2347std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
2348PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) {
2349 std::vector<
2350 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
2351 receiving_transceivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01002352 for (const auto& transceiver : transceivers_) {
Steve Anton69470252018-02-09 11:43:08 -08002353 if (!transceiver->stopped() && transceiver->media_type() == media_type &&
Steve Anton22da89f2018-01-25 13:58:07 -08002354 RtpTransceiverDirectionHasRecv(transceiver->direction())) {
2355 receiving_transceivers.push_back(transceiver);
2356 }
2357 }
2358 return receiving_transceivers;
2359}
2360
htaa2a49d92016-03-04 02:51:39 -08002361void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer,
2362 const RTCOfferAnswerOptions& options) {
Karl Wiberg8d2e2282019-02-17 13:00:07 +01002363 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boströma3728d32019-10-28 12:09:49 +01002364 // Chain this operation. If asynchronous operations are pending on the chain,
2365 // this operation will be queued to be invoked, otherwise the contents of the
2366 // lambda will execute immediately.
2367 operations_chain_->ChainOperation(
2368 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2369 observer_refptr =
2370 rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
2371 options](std::function<void()> operations_chain_callback) {
2372 // Abort early if |this_weak_ptr| is no longer valid.
2373 if (!this_weak_ptr) {
2374 observer_refptr->OnFailure(RTCError(
2375 RTCErrorType::INTERNAL_ERROR,
2376 "CreateAnswer failed because the session was shut down"));
2377 operations_chain_callback();
2378 return;
2379 }
2380 // The operation completes asynchronously when the wrapper is invoked.
2381 rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
2382 observer_wrapper(new rtc::RefCountedObject<
2383 CreateSessionDescriptionObserverOperationWrapper>(
2384 std::move(observer_refptr),
2385 std::move(operations_chain_callback)));
2386 this_weak_ptr->DoCreateAnswer(options, observer_wrapper);
2387 });
2388}
2389
2390void PeerConnection::DoCreateAnswer(
2391 const RTCOfferAnswerOptions& options,
2392 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
2393 RTC_DCHECK_RUN_ON(signaling_thread());
2394 TRACE_EVENT0("webrtc", "PeerConnection::DoCreateAnswer");
nisse7ce109a2017-01-31 00:57:56 -08002395 if (!observer) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002396 RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
htaa2a49d92016-03-04 02:51:39 -08002397 return;
2398 }
2399
Steve Anton25ca0ac2019-06-25 10:40:48 -07002400 // If a session error has occurred the PeerConnection is in a possibly
2401 // inconsistent state so fail right away.
2402 if (session_error() != SessionError::kNone) {
2403 std::string error_message = GetSessionErrorMsg();
2404 RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message;
2405 PostCreateSessionDescriptionFailure(
2406 observer,
2407 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2408 return;
2409 }
2410
Steve Antondffead82018-02-06 10:31:29 -08002411 if (!(signaling_state_ == kHaveRemoteOffer ||
2412 signaling_state_ == kHaveLocalPrAnswer)) {
2413 std::string error =
2414 "PeerConnection cannot create an answer in a state other than "
2415 "have-remote-offer or have-local-pranswer.";
Mirko Bonadei675513b2017-11-09 11:09:25 +01002416 RTC_LOG(LS_ERROR) << error;
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01002417 PostCreateSessionDescriptionFailure(
Harald Alvestrand3725d542018-04-13 15:02:10 +02002418 observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
Steve Anton8d3444d2017-10-20 15:30:51 -07002419 return;
2420 }
2421
Steve Antondffead82018-02-06 10:31:29 -08002422 // The remote description should be set if we're in the right state.
2423 RTC_DCHECK(remote_description());
Steve Anton8d3444d2017-10-20 15:30:51 -07002424
Steve Anton22da89f2018-01-25 13:58:07 -08002425 if (IsUnifiedPlan()) {
2426 if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) {
2427 RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not "
2428 "supported with Unified Plan semantics. Use the "
2429 "RtpTransceiver API instead.";
2430 }
2431 if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) {
2432 RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not "
2433 "supported with Unified Plan semantics. Use the "
2434 "RtpTransceiver API instead.";
2435 }
2436 }
2437
htaa2a49d92016-03-04 02:51:39 -08002438 cricket::MediaSessionOptions session_options;
zhihuang1c378ed2017-08-17 14:10:50 -07002439 GetOptionsForAnswer(options, &session_options);
htaa2a49d92016-03-04 02:51:39 -08002440
Steve Antond25da372017-11-06 14:50:29 -08002441 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002442}
2443
2444void PeerConnection::SetLocalDescription(
2445 SetSessionDescriptionObserver* observer,
Steve Anton80dd7b52018-02-16 17:08:42 -08002446 SessionDescriptionInterface* desc_ptr) {
Karl Wiberg5966c502019-02-21 23:55:09 +01002447 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boströma3728d32019-10-28 12:09:49 +01002448 // Chain this operation. If asynchronous operations are pending on the chain,
2449 // this operation will be queued to be invoked, otherwise the contents of the
2450 // lambda will execute immediately.
2451 operations_chain_->ChainOperation(
2452 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2453 observer_refptr =
2454 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
2455 desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
2456 std::function<void()> operations_chain_callback) mutable {
2457 // Abort early if |this_weak_ptr| is no longer valid.
2458 if (!this_weak_ptr) {
2459 // For consistency with DoSetLocalDescription(), we DO NOT inform the
2460 // |observer_refptr| that the operation failed in this case.
2461 // TODO(hbos): If/when we process SLD messages in ~PeerConnection,
2462 // the consistent thing would be to inform the observer here.
2463 operations_chain_callback();
2464 return;
2465 }
2466 this_weak_ptr->DoSetLocalDescription(std::move(desc),
2467 std::move(observer_refptr));
2468 // DoSetLocalDescription() is currently implemented as a synchronous
2469 // operation but where the |observer|'s callbacks are invoked
2470 // asynchronously in a post to OnMessage().
2471 // For backwards-compatability reasons, we declare the operation as
Henrik Boström4e196702019-10-30 10:35:50 +01002472 // completed here (rather than in OnMessage()). This ensures that
2473 // subsequent offer/answer operations can start immediately (without
2474 // waiting for OnMessage()).
Henrik Boströma3728d32019-10-28 12:09:49 +01002475 operations_chain_callback();
2476 });
2477}
Steve Anton8a006912017-12-04 15:25:56 -08002478
Henrik Boström4e196702019-10-30 10:35:50 +01002479void PeerConnection::SetLocalDescription(
2480 SetSessionDescriptionObserver* observer) {
2481 RTC_DCHECK_RUN_ON(signaling_thread());
2482 // The |create_sdp_observer| handles performing DoSetLocalDescription() with
2483 // the resulting description as well as completing the operation.
2484 rtc::scoped_refptr<ImplicitCreateSessionDescriptionObserver>
2485 create_sdp_observer(
2486 new rtc::RefCountedObject<ImplicitCreateSessionDescriptionObserver>(
2487 weak_ptr_factory_.GetWeakPtr(),
2488 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer)));
2489 // Chain this operation. If asynchronous operations are pending on the chain,
2490 // this operation will be queued to be invoked, otherwise the contents of the
2491 // lambda will execute immediately.
2492 operations_chain_->ChainOperation(
2493 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2494 create_sdp_observer](std::function<void()> operations_chain_callback) {
2495 // The |create_sdp_observer| is responsible for completing the
2496 // operation.
2497 create_sdp_observer->SetOperationCompleteCallback(
2498 std::move(operations_chain_callback));
2499 // Abort early if |this_weak_ptr| is no longer valid. This triggers the
2500 // same code path as if DoCreateOffer() or DoCreateAnswer() failed.
2501 if (!this_weak_ptr) {
2502 create_sdp_observer->OnFailure(RTCError(
2503 RTCErrorType::INTERNAL_ERROR,
2504 "SetLocalDescription failed because the session was shut down"));
2505 return;
2506 }
2507 switch (this_weak_ptr->signaling_state()) {
2508 case PeerConnectionInterface::kStable:
2509 case PeerConnectionInterface::kHaveLocalOffer:
2510 case PeerConnectionInterface::kHaveRemotePrAnswer:
2511 // TODO(hbos): If [LastCreatedOffer] exists and still represents the
2512 // current state of the system, use that instead of creating another
2513 // offer.
2514 this_weak_ptr->DoCreateOffer(RTCOfferAnswerOptions(),
2515 create_sdp_observer);
2516 break;
2517 case PeerConnectionInterface::kHaveLocalPrAnswer:
2518 case PeerConnectionInterface::kHaveRemoteOffer:
2519 // TODO(hbos): If [LastCreatedAnswer] exists and still represents
2520 // the current state of the system, use that instead of creating
2521 // another answer.
2522 this_weak_ptr->DoCreateAnswer(RTCOfferAnswerOptions(),
2523 create_sdp_observer);
2524 break;
2525 case PeerConnectionInterface::kClosed:
2526 create_sdp_observer->OnFailure(RTCError(
2527 RTCErrorType::INVALID_STATE,
2528 "SetLocalDescription called when PeerConnection is closed."));
2529 break;
2530 }
2531 });
2532}
2533
Henrik Boströma3728d32019-10-28 12:09:49 +01002534void PeerConnection::DoSetLocalDescription(
2535 std::unique_ptr<SessionDescriptionInterface> desc,
2536 rtc::scoped_refptr<SetSessionDescriptionObserver> observer) {
2537 RTC_DCHECK_RUN_ON(signaling_thread());
2538 TRACE_EVENT0("webrtc", "PeerConnection::DoSetLocalDescription");
Steve Anton80dd7b52018-02-16 17:08:42 -08002539
nisse7ce109a2017-01-31 00:57:56 -08002540 if (!observer) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002541 RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002542 return;
2543 }
Steve Anton8a006912017-12-04 15:25:56 -08002544
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002545 if (!desc) {
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01002546 PostSetSessionDescriptionFailure(
2547 observer,
2548 RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL."));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002549 return;
2550 }
Steve Anton8d3444d2017-10-20 15:30:51 -07002551
Steve Anton80dd7b52018-02-16 17:08:42 -08002552 // If a session error has occurred the PeerConnection is in a possibly
2553 // inconsistent state so fail right away.
2554 if (session_error() != SessionError::kNone) {
2555 std::string error_message = GetSessionErrorMsg();
2556 RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message;
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01002557 PostSetSessionDescriptionFailure(
2558 observer,
2559 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
Steve Anton80dd7b52018-02-16 17:08:42 -08002560 return;
2561 }
Steve Anton8d3444d2017-10-20 15:30:51 -07002562
Eldar Rello5ab79e62019-10-09 18:29:44 +03002563 // For SLD we support only explicit rollback.
2564 if (desc->GetType() == SdpType::kRollback) {
2565 if (IsUnifiedPlan()) {
Eldar Relloead0ec92019-10-21 23:01:31 +03002566 RTCError error = Rollback(desc->GetType());
Eldar Rello5ab79e62019-10-09 18:29:44 +03002567 if (error.ok()) {
2568 PostSetSessionDescriptionSuccess(observer);
2569 } else {
2570 PostSetSessionDescriptionFailure(observer, std::move(error));
2571 }
2572 } else {
2573 PostSetSessionDescriptionFailure(
2574 observer, RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
2575 "Rollback not supported in Plan B"));
2576 }
2577 return;
2578 }
2579
Steve Anton80dd7b52018-02-16 17:08:42 -08002580 RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL);
2581 if (!error.ok()) {
2582 std::string error_message = GetSetDescriptionErrorMessage(
2583 cricket::CS_LOCAL, desc->GetType(), error);
2584 RTC_LOG(LS_ERROR) << error_message;
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01002585 PostSetSessionDescriptionFailure(
2586 observer,
2587 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
Steve Anton80dd7b52018-02-16 17:08:42 -08002588 return;
2589 }
2590
2591 // Grab the description type before moving ownership to ApplyLocalDescription,
2592 // which may destroy it before returning.
2593 const SdpType type = desc->GetType();
2594
2595 error = ApplyLocalDescription(std::move(desc));
Steve Anton8a006912017-12-04 15:25:56 -08002596 // |desc| may be destroyed at this point.
2597
2598 if (!error.ok()) {
Steve Anton80dd7b52018-02-16 17:08:42 -08002599 // If ApplyLocalDescription fails, the PeerConnection could be in an
2600 // inconsistent state, so act conservatively here and set the session error
2601 // so that future calls to SetLocalDescription/SetRemoteDescription fail.
2602 SetSessionError(SessionError::kContent, error.message());
2603 std::string error_message =
2604 GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error);
2605 RTC_LOG(LS_ERROR) << error_message;
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01002606 PostSetSessionDescriptionFailure(
2607 observer,
2608 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
Steve Anton8d3444d2017-10-20 15:30:51 -07002609 return;
2610 }
Steve Anton8a006912017-12-04 15:25:56 -08002611 RTC_DCHECK(local_description());
2612
2613 PostSetSessionDescriptionSuccess(observer);
2614
Steve Antonad182762018-09-05 20:22:40 +00002615 // MaybeStartGathering needs to be called after posting
2616 // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates
2617 // before signaling that SetLocalDescription completed.
Steve Anton8a006912017-12-04 15:25:56 -08002618 transport_controller_->MaybeStartGathering();
2619
Steve Antona3a92c22017-12-07 10:27:41 -08002620 if (local_description()->GetType() == SdpType::kAnswer) {
Steve Anton8a006912017-12-04 15:25:56 -08002621 // TODO(deadbeef): We already had to hop to the network thread for
2622 // MaybeStartGathering...
2623 network_thread()->Invoke<void>(
2624 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
2625 port_allocator_.get()));
Steve Anton0ffaaa22018-02-23 10:31:30 -08002626 // Make UMA notes about what was agreed to.
2627 ReportNegotiatedSdpSemantics(*local_description());
Steve Anton8a006912017-12-04 15:25:56 -08002628 }
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002629
2630 if (IsUnifiedPlan()) {
2631 bool was_negotiation_needed = is_negotiation_needed_;
2632 UpdateNegotiationNeeded();
2633 if (signaling_state() == kStable && was_negotiation_needed &&
2634 is_negotiation_needed_) {
2635 Observer()->OnRenegotiationNeeded();
2636 }
2637 }
2638
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07002639 NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED);
Steve Anton8a006912017-12-04 15:25:56 -08002640}
2641
2642RTCError PeerConnection::ApplyLocalDescription(
2643 std::unique_ptr<SessionDescriptionInterface> desc) {
2644 RTC_DCHECK_RUN_ON(signaling_thread());
2645 RTC_DCHECK(desc);
2646
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002647 // Update stats here so that we have the most recent stats for tracks and
2648 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00002649 stats_->UpdateStats(kStatsOutputLevelStandard);
Steve Anton8a006912017-12-04 15:25:56 -08002650
Steve Antondcc3c022017-12-22 16:02:54 -08002651 // Take a reference to the old local description since it's used below to
2652 // compare against the new local description. When setting the new local
2653 // description, grab ownership of the replaced session description in case it
2654 // is the same as |old_local_description|, to keep it alive for the duration
2655 // of the method.
2656 const SessionDescriptionInterface* old_local_description =
2657 local_description();
2658 std::unique_ptr<SessionDescriptionInterface> replaced_local_description;
Zhi Huange830e682018-03-30 10:48:35 -07002659 SdpType type = desc->GetType();
Steve Anton3828c062017-12-06 10:34:51 -08002660 if (type == SdpType::kAnswer) {
Steve Antondcc3c022017-12-22 16:02:54 -08002661 replaced_local_description = pending_local_description_
2662 ? std::move(pending_local_description_)
2663 : std::move(current_local_description_);
Steve Anton8a006912017-12-04 15:25:56 -08002664 current_local_description_ = std::move(desc);
2665 pending_local_description_ = nullptr;
2666 current_remote_description_ = std::move(pending_remote_description_);
2667 } else {
Steve Antondcc3c022017-12-22 16:02:54 -08002668 replaced_local_description = std::move(pending_local_description_);
Steve Anton8a006912017-12-04 15:25:56 -08002669 pending_local_description_ = std::move(desc);
2670 }
2671 // The session description to apply now must be accessed by
2672 // |local_description()|.
Henrik Boströmfdb92012017-11-09 19:55:44 +01002673 RTC_DCHECK(local_description());
deadbeefab9b2d12015-10-14 11:33:11 -07002674
Amit Hilbuche2a284d2019-03-05 12:36:31 -08002675 // Report statistics about any use of simulcast.
2676 ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription,
2677 *local_description()->description());
2678
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08002679 if (!is_caller_) {
2680 if (remote_description()) {
2681 // Remote description was applied first, so this PC is the callee.
2682 is_caller_ = false;
2683 } else {
2684 // Local description is applied first, so this PC is the caller.
2685 is_caller_ = true;
2686 }
2687 }
2688
Zhi Huange830e682018-03-30 10:48:35 -07002689 RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type);
2690 if (!error.ok()) {
2691 return error;
2692 }
2693
Steve Antondcc3c022017-12-22 16:02:54 -08002694 if (IsUnifiedPlan()) {
2695 RTCError error = UpdateTransceiversAndDataChannels(
Seth Hampsonae8a90a2018-02-13 15:33:48 -08002696 cricket::CS_LOCAL, *local_description(), old_local_description,
2697 remote_description());
Steve Anton8a006912017-12-04 15:25:56 -08002698 if (!error.ok()) {
2699 return error;
2700 }
Steve Anton0f5400a2018-07-17 14:25:36 -07002701 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
2702 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
Mirko Bonadei739baf02019-01-27 17:29:42 +01002703 for (const auto& transceiver : transceivers_) {
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +01002704 // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots.
2705 // Note that code paths that don't set MID won't be able to use
2706 // information about DTLS transports.
2707 if (transceiver->mid()) {
2708 auto dtls_transport =
2709 LookupDtlsTransportByMidInternal(*transceiver->mid());
2710 transceiver->internal()->sender_internal()->set_transport(
2711 dtls_transport);
2712 transceiver->internal()->receiver_internal()->set_transport(
2713 dtls_transport);
2714 }
2715
Steve Antondcc3c022017-12-22 16:02:54 -08002716 const ContentInfo* content =
2717 FindMediaSectionForTransceiver(transceiver, local_description());
2718 if (!content) {
2719 continue;
2720 }
2721 const MediaContentDescription* media_desc = content->media_description();
Steve Anton0f5400a2018-07-17 14:25:36 -07002722 // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run
2723 // the following steps:
Steve Antondcc3c022017-12-22 16:02:54 -08002724 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
Steve Anton0f5400a2018-07-17 14:25:36 -07002725 // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and
2726 // transceiver's [[FiredDirection]] slot is either "sendrecv" or
2727 // "recvonly", process the removal of a remote track for the media
2728 // description, given transceiver, removeList, and muteTracks.
2729 if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
2730 (transceiver->internal()->fired_direction() &&
2731 RtpTransceiverDirectionHasRecv(
2732 *transceiver->internal()->fired_direction()))) {
2733 ProcessRemovalOfRemoteTrack(transceiver, &remove_list,
2734 &removed_streams);
2735 }
2736 // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and
2737 // [[FiredDirection]] slots to direction.
Steve Antondcc3c022017-12-22 16:02:54 -08002738 transceiver->internal()->set_current_direction(media_desc->direction());
Steve Anton0f5400a2018-07-17 14:25:36 -07002739 transceiver->internal()->set_fired_direction(media_desc->direction());
Steve Antondcc3c022017-12-22 16:02:54 -08002740 }
Steve Anton0f5400a2018-07-17 14:25:36 -07002741 }
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02002742 auto observer = Observer();
Mirko Bonadei739baf02019-01-27 17:29:42 +01002743 for (const auto& transceiver : remove_list) {
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02002744 observer->OnRemoveTrack(transceiver->receiver());
Steve Anton0f5400a2018-07-17 14:25:36 -07002745 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01002746 for (const auto& stream : removed_streams) {
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02002747 observer->OnRemoveStream(stream);
Steve Antondcc3c022017-12-22 16:02:54 -08002748 }
2749 } else {
Zhi Huange830e682018-03-30 10:48:35 -07002750 // Media channels will be created only when offer is set. These may use new
2751 // transports just created by PushdownTransportDescription.
Steve Antondcc3c022017-12-22 16:02:54 -08002752 if (type == SdpType::kOffer) {
2753 // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local
2754 // description is applied. Restore back to old description.
2755 RTCError error = CreateChannels(*local_description()->description());
2756 if (!error.ok()) {
2757 return error;
2758 }
2759 }
Steve Antondcc3c022017-12-22 16:02:54 -08002760 // Remove unused channels if MediaContentDescription is rejected.
2761 RemoveUnusedChannels(local_description()->description());
2762 }
Steve Anton8a006912017-12-04 15:25:56 -08002763
Zhi Huange830e682018-03-30 10:48:35 -07002764 error = UpdateSessionState(type, cricket::CS_LOCAL,
2765 local_description()->description());
Steve Anton8a006912017-12-04 15:25:56 -08002766 if (!error.ok()) {
2767 return error;
2768 }
Steve Antondcc3c022017-12-22 16:02:54 -08002769
Steve Anton8a006912017-12-04 15:25:56 -08002770 if (remote_description()) {
2771 // Now that we have a local description, we can push down remote candidates.
2772 UseCandidatesInSessionDescription(remote_description());
2773 }
2774
2775 pending_ice_restarts_.clear();
2776 if (session_error() != SessionError::kNone) {
2777 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
2778 }
2779
deadbeefab9b2d12015-10-14 11:33:11 -07002780 // If setting the description decided our SSL role, allocate any necessary
2781 // SCTP sids.
2782 rtc::SSLRole role;
Harald Alvestrand05e4d082019-12-03 14:04:21 +01002783 if (DataChannel::IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01002784 data_channel_controller_.AllocateSctpSids(role);
deadbeefab9b2d12015-10-14 11:33:11 -07002785 }
2786
Steve Antond3679212018-01-17 17:41:02 -08002787 if (IsUnifiedPlan()) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01002788 for (const auto& transceiver : transceivers_) {
Steve Antond3679212018-01-17 17:41:02 -08002789 const ContentInfo* content =
2790 FindMediaSectionForTransceiver(transceiver, local_description());
2791 if (!content) {
2792 continue;
2793 }
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002794 cricket::ChannelInterface* channel = transceiver->internal()->channel();
2795 if (content->rejected || !channel || channel->local_streams().empty()) {
Steve Anton60b6c1d2018-06-13 11:32:27 -07002796 // 0 is a special value meaning "this sender has no associated send
2797 // stream". Need to call this so the sender won't attempt to configure
2798 // a no longer existing stream and run into DCHECKs in the lower
2799 // layers.
2800 transceiver->internal()->sender_internal()->SetSsrc(0);
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002801 } else {
2802 // Get the StreamParams from the channel which could generate SSRCs.
2803 const std::vector<StreamParams>& streams = channel->local_streams();
Henrik Andreasssoncc189172019-05-20 09:01:38 +00002804 transceiver->internal()->sender_internal()->set_stream_ids(
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002805 streams[0].stream_ids());
2806 transceiver->internal()->sender_internal()->SetSsrc(
2807 streams[0].first_ssrc());
Steve Antond3679212018-01-17 17:41:02 -08002808 }
2809 }
2810 } else {
2811 // Plan B semantics.
2812
Steve Antondcc3c022017-12-22 16:02:54 -08002813 // Update state and SSRC of local MediaStreams and DataChannels based on the
2814 // local session description.
2815 const cricket::ContentInfo* audio_content =
2816 GetFirstAudioContent(local_description()->description());
2817 if (audio_content) {
2818 if (audio_content->rejected) {
2819 RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
2820 } else {
2821 const cricket::AudioContentDescription* audio_desc =
2822 audio_content->media_description()->as_audio();
2823 UpdateLocalSenders(audio_desc->streams(), audio_desc->type());
2824 }
deadbeeffaac4972015-11-12 15:33:07 -08002825 }
deadbeefab9b2d12015-10-14 11:33:11 -07002826
Steve Antondcc3c022017-12-22 16:02:54 -08002827 const cricket::ContentInfo* video_content =
2828 GetFirstVideoContent(local_description()->description());
2829 if (video_content) {
2830 if (video_content->rejected) {
2831 RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
2832 } else {
2833 const cricket::VideoContentDescription* video_desc =
2834 video_content->media_description()->as_video();
2835 UpdateLocalSenders(video_desc->streams(), video_desc->type());
2836 }
deadbeeffaac4972015-11-12 15:33:07 -08002837 }
deadbeefab9b2d12015-10-14 11:33:11 -07002838 }
2839
2840 const cricket::ContentInfo* data_content =
Henrik Boströmfdb92012017-11-09 19:55:44 +01002841 GetFirstDataContent(local_description()->description());
deadbeefab9b2d12015-10-14 11:33:11 -07002842 if (data_content) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02002843 const cricket::RtpDataContentDescription* rtp_data_desc =
2844 data_content->media_description()->as_rtp_data();
2845 // rtp_data_desc will be null if this is an SCTP description.
2846 if (rtp_data_desc) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01002847 data_channel_controller_.UpdateLocalRtpDataChannels(
2848 rtp_data_desc->streams());
deadbeefab9b2d12015-10-14 11:33:11 -07002849 }
2850 }
2851
Henrik Boström79b69802019-07-18 11:16:56 +02002852 if (type == SdpType::kAnswer &&
2853 local_ice_credentials_to_replace_->SatisfiesIceRestart(
2854 *current_local_description_)) {
2855 local_ice_credentials_to_replace_->ClearIceCredentials();
2856 }
2857
Steve Anton8a006912017-12-04 15:25:56 -08002858 return RTCError::OK();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002859}
2860
Steve Anton06817cd2018-12-18 15:55:30 -08002861// The SDP parser used to populate these values by default for the 'content
2862// name' if an a=mid line was absent.
2863static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) {
2864 switch (media_type) {
2865 case cricket::MEDIA_TYPE_AUDIO:
2866 return cricket::CN_AUDIO;
2867 case cricket::MEDIA_TYPE_VIDEO:
2868 return cricket::CN_VIDEO;
2869 case cricket::MEDIA_TYPE_DATA:
2870 return cricket::CN_DATA;
2871 }
2872 RTC_NOTREACHED();
2873 return "";
2874}
2875
2876void PeerConnection::FillInMissingRemoteMids(
Steve Antond7180cc2019-02-07 10:44:53 -08002877 cricket::SessionDescription* new_remote_description) {
2878 RTC_DCHECK(new_remote_description);
Harald Alvestrand1716d392019-06-03 20:35:45 +02002879 const cricket::ContentInfos no_infos;
Steve Anton06817cd2018-12-18 15:55:30 -08002880 const cricket::ContentInfos& local_contents =
2881 (local_description() ? local_description()->description()->contents()
Harald Alvestrand1716d392019-06-03 20:35:45 +02002882 : no_infos);
Steve Antond7180cc2019-02-07 10:44:53 -08002883 const cricket::ContentInfos& remote_contents =
2884 (remote_description() ? remote_description()->description()->contents()
Harald Alvestrand1716d392019-06-03 20:35:45 +02002885 : no_infos);
Steve Antond7180cc2019-02-07 10:44:53 -08002886 for (size_t i = 0; i < new_remote_description->contents().size(); ++i) {
2887 cricket::ContentInfo& content = new_remote_description->contents()[i];
Steve Anton06817cd2018-12-18 15:55:30 -08002888 if (!content.name.empty()) {
2889 continue;
2890 }
Amit Hilbuchae3df542019-01-07 12:13:08 -08002891 std::string new_mid;
Steve Anton06817cd2018-12-18 15:55:30 -08002892 absl::string_view source_explanation;
2893 if (IsUnifiedPlan()) {
2894 if (i < local_contents.size()) {
2895 new_mid = local_contents[i].name;
2896 source_explanation = "from the matching local media section";
Steve Antond7180cc2019-02-07 10:44:53 -08002897 } else if (i < remote_contents.size()) {
2898 new_mid = remote_contents[i].name;
2899 source_explanation = "from the matching previous remote media section";
Steve Anton06817cd2018-12-18 15:55:30 -08002900 } else {
Amit Hilbuchae3df542019-01-07 12:13:08 -08002901 new_mid = mid_generator_();
Steve Anton06817cd2018-12-18 15:55:30 -08002902 source_explanation = "generated just now";
2903 }
2904 } else {
Amit Hilbuchae3df542019-01-07 12:13:08 -08002905 new_mid = std::string(
2906 GetDefaultMidForPlanB(content.media_description()->type()));
Steve Anton06817cd2018-12-18 15:55:30 -08002907 source_explanation = "to match pre-existing behavior";
2908 }
Steve Antond7180cc2019-02-07 10:44:53 -08002909 RTC_DCHECK(!new_mid.empty());
Amit Hilbuchae3df542019-01-07 12:13:08 -08002910 content.name = new_mid;
Steve Antond7180cc2019-02-07 10:44:53 -08002911 new_remote_description->transport_infos()[i].content_name = new_mid;
Steve Anton06817cd2018-12-18 15:55:30 -08002912 RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i
2913 << " is missing an a=mid line. Filling in the value '"
2914 << new_mid << "' " << source_explanation << ".";
2915 }
2916}
2917
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002918void PeerConnection::SetRemoteDescription(
Henrik Boströma4ecf552017-11-23 14:17:07 +00002919 SetSessionDescriptionObserver* observer,
Henrik Boströma3728d32019-10-28 12:09:49 +01002920 SessionDescriptionInterface* desc_ptr) {
2921 RTC_DCHECK_RUN_ON(signaling_thread());
2922 // Chain this operation. If asynchronous operations are pending on the chain,
2923 // this operation will be queued to be invoked, otherwise the contents of the
2924 // lambda will execute immediately.
2925 operations_chain_->ChainOperation(
2926 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2927 observer_refptr =
2928 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
2929 desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
2930 std::function<void()> operations_chain_callback) mutable {
2931 // Abort early if |this_weak_ptr| is no longer valid.
2932 if (!this_weak_ptr) {
2933 // For consistency with SetRemoteDescriptionObserverAdapter, we DO NOT
2934 // inform the |observer_refptr| that the operation failed in this
2935 // case.
2936 // TODO(hbos): If/when we process SRD messages in ~PeerConnection,
2937 // the consistent thing would be to inform the observer here.
2938 operations_chain_callback();
2939 return;
2940 }
2941 this_weak_ptr->DoSetRemoteDescription(
2942 std::move(desc),
2943 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>(
2944 new SetRemoteDescriptionObserverAdapter(
2945 this_weak_ptr.get(), std::move(observer_refptr))));
2946 // DoSetRemoteDescription() is currently implemented as a synchronous
2947 // operation but where SetRemoteDescriptionObserverAdapter ensures that
2948 // the |observer|'s callbacks are invoked asynchronously in a post to
2949 // OnMessage().
2950 // For backwards-compatability reasons, we declare the operation as
Henrik Boström4e196702019-10-30 10:35:50 +01002951 // completed here (rather than in OnMessage()). This ensures that
2952 // subsequent offer/answer operations can start immediately (without
2953 // waiting for OnMessage()).
Henrik Boströma3728d32019-10-28 12:09:49 +01002954 operations_chain_callback();
2955 });
Henrik Boström31638672017-11-23 17:48:32 +01002956}
2957
2958void PeerConnection::SetRemoteDescription(
2959 std::unique_ptr<SessionDescriptionInterface> desc,
2960 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
Karl Wiberg5966c502019-02-21 23:55:09 +01002961 RTC_DCHECK_RUN_ON(signaling_thread());
Henrik Boströma3728d32019-10-28 12:09:49 +01002962 // Chain this operation. If asynchronous operations are pending on the chain,
2963 // this operation will be queued to be invoked, otherwise the contents of the
2964 // lambda will execute immediately.
2965 operations_chain_->ChainOperation(
2966 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer,
2967 desc = std::move(desc)](
2968 std::function<void()> operations_chain_callback) mutable {
2969 // Abort early if |this_weak_ptr| is no longer valid.
2970 if (!this_weak_ptr) {
2971 // For consistency with DoSetRemoteDescription(), we DO inform the
2972 // |observer| that the operation failed in this case.
2973 observer->OnSetRemoteDescriptionComplete(RTCError(
2974 RTCErrorType::INVALID_STATE,
2975 "Failed to set remote offer sdp: failed because the session was "
2976 "shut down"));
2977 operations_chain_callback();
2978 return;
2979 }
2980 this_weak_ptr->DoSetRemoteDescription(std::move(desc),
2981 std::move(observer));
2982 // DoSetRemoteDescription() is currently implemented as a synchronous
2983 // operation. The |observer| will already have been informed that it
2984 // completed, and we can mark this operation as complete without any
2985 // loose ends.
2986 operations_chain_callback();
2987 });
2988}
2989
2990void PeerConnection::DoSetRemoteDescription(
2991 std::unique_ptr<SessionDescriptionInterface> desc,
2992 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
2993 RTC_DCHECK_RUN_ON(signaling_thread());
2994 TRACE_EVENT0("webrtc", "PeerConnection::DoSetRemoteDescription");
Steve Anton8a006912017-12-04 15:25:56 -08002995
nisse7ce109a2017-01-31 00:57:56 -08002996 if (!observer) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002997 RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002998 return;
2999 }
Steve Anton8a006912017-12-04 15:25:56 -08003000
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003001 if (!desc) {
Henrik Boström31638672017-11-23 17:48:32 +01003002 observer->OnSetRemoteDescriptionComplete(RTCError(
Steve Anton8a006912017-12-04 15:25:56 -08003003 RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL."));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003004 return;
3005 }
Steve Anton8d3444d2017-10-20 15:30:51 -07003006
Steve Anton80dd7b52018-02-16 17:08:42 -08003007 // If a session error has occurred the PeerConnection is in a possibly
3008 // inconsistent state so fail right away.
3009 if (session_error() != SessionError::kNone) {
3010 std::string error_message = GetSessionErrorMsg();
3011 RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message;
3012 observer->OnSetRemoteDescriptionComplete(
3013 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
3014 return;
3015 }
Eldar Rello5ab79e62019-10-09 18:29:44 +03003016 if (IsUnifiedPlan()) {
3017 if (configuration_.enable_implicit_rollback) {
3018 if (desc->GetType() == SdpType::kOffer &&
3019 signaling_state() == kHaveLocalOffer) {
Eldar Relloead0ec92019-10-21 23:01:31 +03003020 Rollback(desc->GetType());
Eldar Rello5ab79e62019-10-09 18:29:44 +03003021 }
3022 }
3023 // Explicit rollback.
3024 if (desc->GetType() == SdpType::kRollback) {
Eldar Relloead0ec92019-10-21 23:01:31 +03003025 observer->OnSetRemoteDescriptionComplete(Rollback(desc->GetType()));
Eldar Rello5ab79e62019-10-09 18:29:44 +03003026 return;
3027 }
3028 } else if (desc->GetType() == SdpType::kRollback) {
3029 observer->OnSetRemoteDescriptionComplete(
3030 RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
3031 "Rollback not supported in Plan B"));
3032 return;
3033 }
Steve Antonba42e992018-04-09 14:10:01 -07003034 if (desc->GetType() == SdpType::kOffer) {
3035 // Report to UMA the format of the received offer.
3036 ReportSdpFormatReceived(*desc);
3037 }
3038
Steve Anton06817cd2018-12-18 15:55:30 -08003039 // Handle remote descriptions missing a=mid lines for interop with legacy end
3040 // points.
3041 FillInMissingRemoteMids(desc->description());
3042
Steve Anton80dd7b52018-02-16 17:08:42 -08003043 RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE);
Steve Anton71439a62018-02-15 11:53:06 -08003044 if (!error.ok()) {
Steve Anton80dd7b52018-02-16 17:08:42 -08003045 std::string error_message = GetSetDescriptionErrorMessage(
3046 cricket::CS_REMOTE, desc->GetType(), error);
3047 RTC_LOG(LS_ERROR) << error_message;
Steve Anton71439a62018-02-15 11:53:06 -08003048 observer->OnSetRemoteDescriptionComplete(
3049 RTCError(error.type(), std::move(error_message)));
3050 return;
3051 }
Steve Anton71439a62018-02-15 11:53:06 -08003052
Steve Anton80dd7b52018-02-16 17:08:42 -08003053 // Grab the description type before moving ownership to
3054 // ApplyRemoteDescription, which may destroy it before returning.
3055 const SdpType type = desc->GetType();
3056
3057 error = ApplyRemoteDescription(std::move(desc));
3058 // |desc| may be destroyed at this point.
3059
3060 if (!error.ok()) {
3061 // If ApplyRemoteDescription fails, the PeerConnection could be in an
3062 // inconsistent state, so act conservatively here and set the session error
3063 // so that future calls to SetLocalDescription/SetRemoteDescription fail.
3064 SetSessionError(SessionError::kContent, error.message());
3065 std::string error_message =
3066 GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error);
3067 RTC_LOG(LS_ERROR) << error_message;
3068 observer->OnSetRemoteDescriptionComplete(
3069 RTCError(error.type(), std::move(error_message)));
3070 return;
3071 }
3072 RTC_DCHECK(remote_description());
3073
3074 if (type == SdpType::kAnswer) {
Steve Anton8a006912017-12-04 15:25:56 -08003075 // TODO(deadbeef): We already had to hop to the network thread for
3076 // MaybeStartGathering...
3077 network_thread()->Invoke<void>(
3078 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
3079 port_allocator_.get()));
Harald Alvestrand5dbb5862018-02-13 23:48:00 +01003080 // Make UMA notes about what was agreed to.
Steve Anton0ffaaa22018-02-23 10:31:30 -08003081 ReportNegotiatedSdpSemantics(*remote_description());
Steve Anton8a006912017-12-04 15:25:56 -08003082 }
3083
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003084 if (IsUnifiedPlan()) {
3085 bool was_negotiation_needed = is_negotiation_needed_;
3086 UpdateNegotiationNeeded();
3087 if (signaling_state() == kStable && was_negotiation_needed &&
3088 is_negotiation_needed_) {
3089 Observer()->OnRenegotiationNeeded();
3090 }
3091 }
3092
Steve Anton8a006912017-12-04 15:25:56 -08003093 observer->OnSetRemoteDescriptionComplete(RTCError::OK());
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07003094 NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED);
Steve Anton8a006912017-12-04 15:25:56 -08003095}
3096
3097RTCError PeerConnection::ApplyRemoteDescription(
3098 std::unique_ptr<SessionDescriptionInterface> desc) {
3099 RTC_DCHECK_RUN_ON(signaling_thread());
3100 RTC_DCHECK(desc);
3101
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003102 // Update stats here so that we have the most recent stats for tracks and
3103 // streams that might be removed by updating the session description.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00003104 stats_->UpdateStats(kStatsOutputLevelStandard);
Steve Anton8a006912017-12-04 15:25:56 -08003105
Steve Antondcc3c022017-12-22 16:02:54 -08003106 // Take a reference to the old remote description since it's used below to
3107 // compare against the new remote description. When setting the new remote
3108 // description, grab ownership of the replaced session description in case it
3109 // is the same as |old_remote_description|, to keep it alive for the duration
3110 // of the method.
Steve Anton8a006912017-12-04 15:25:56 -08003111 const SessionDescriptionInterface* old_remote_description =
3112 remote_description();
Steve Anton8a006912017-12-04 15:25:56 -08003113 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
Steve Anton3828c062017-12-06 10:34:51 -08003114 SdpType type = desc->GetType();
3115 if (type == SdpType::kAnswer) {
Steve Anton8a006912017-12-04 15:25:56 -08003116 replaced_remote_description = pending_remote_description_
3117 ? std::move(pending_remote_description_)
3118 : std::move(current_remote_description_);
3119 current_remote_description_ = std::move(desc);
3120 pending_remote_description_ = nullptr;
3121 current_local_description_ = std::move(pending_local_description_);
3122 } else {
3123 replaced_remote_description = std::move(pending_remote_description_);
3124 pending_remote_description_ = std::move(desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003125 }
Steve Anton8a006912017-12-04 15:25:56 -08003126 // The session description to apply now must be accessed by
3127 // |remote_description()|.
Henrik Boströmfdb92012017-11-09 19:55:44 +01003128 RTC_DCHECK(remote_description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003129
Amit Hilbuche2a284d2019-03-05 12:36:31 -08003130 // Report statistics about any use of simulcast.
3131 ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription,
3132 *remote_description()->description());
3133
Zhi Huange830e682018-03-30 10:48:35 -07003134 RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type);
3135 if (!error.ok()) {
3136 return error;
3137 }
Steve Anton8a006912017-12-04 15:25:56 -08003138 // Transport and Media channels will be created only when offer is set.
Steve Antondcc3c022017-12-22 16:02:54 -08003139 if (IsUnifiedPlan()) {
3140 RTCError error = UpdateTransceiversAndDataChannels(
Seth Hampsonae8a90a2018-02-13 15:33:48 -08003141 cricket::CS_REMOTE, *remote_description(), local_description(),
3142 old_remote_description);
Steve Anton8a006912017-12-04 15:25:56 -08003143 if (!error.ok()) {
3144 return error;
3145 }
Steve Antondcc3c022017-12-22 16:02:54 -08003146 } else {
Zhi Huange830e682018-03-30 10:48:35 -07003147 // Media channels will be created only when offer is set. These may use new
3148 // transports just created by PushdownTransportDescription.
Steve Antondcc3c022017-12-22 16:02:54 -08003149 if (type == SdpType::kOffer) {
Zhi Huange830e682018-03-30 10:48:35 -07003150 // TODO(mallinath) - Handle CreateChannel failure, as new local
Steve Antondcc3c022017-12-22 16:02:54 -08003151 // description is applied. Restore back to old description.
3152 RTCError error = CreateChannels(*remote_description()->description());
3153 if (!error.ok()) {
3154 return error;
3155 }
3156 }
Steve Antondcc3c022017-12-22 16:02:54 -08003157 // Remove unused channels if MediaContentDescription is rejected.
3158 RemoveUnusedChannels(remote_description()->description());
3159 }
Steve Anton8a006912017-12-04 15:25:56 -08003160
Zhi Huange830e682018-03-30 10:48:35 -07003161 // NOTE: Candidates allocation will be initiated only when
3162 // SetLocalDescription is called.
3163 error = UpdateSessionState(type, cricket::CS_REMOTE,
3164 remote_description()->description());
Steve Anton8a006912017-12-04 15:25:56 -08003165 if (!error.ok()) {
3166 return error;
3167 }
3168
3169 if (local_description() &&
3170 !UseCandidatesInSessionDescription(remote_description())) {
3171 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates);
3172 }
3173
3174 if (old_remote_description) {
3175 for (const cricket::ContentInfo& content :
3176 old_remote_description->description()->contents()) {
3177 // Check if this new SessionDescription contains new ICE ufrag and
3178 // password that indicates the remote peer requests an ICE restart.
3179 // TODO(deadbeef): When we start storing both the current and pending
3180 // remote description, this should reset pending_ice_restarts and compare
3181 // against the current description.
3182 if (CheckForRemoteIceRestart(old_remote_description, remote_description(),
3183 content.name)) {
Steve Anton3828c062017-12-06 10:34:51 -08003184 if (type == SdpType::kOffer) {
Steve Anton8a006912017-12-04 15:25:56 -08003185 pending_ice_restarts_.insert(content.name);
3186 }
3187 } else {
3188 // We retain all received candidates only if ICE is not restarted.
3189 // When ICE is restarted, all previous candidates belong to an old
3190 // generation and should not be kept.
3191 // TODO(deadbeef): This goes against the W3C spec which says the remote
3192 // description should only contain candidates from the last set remote
3193 // description plus any candidates added since then. We should remove
3194 // this once we're sure it won't break anything.
3195 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
3196 old_remote_description, content.name, mutable_remote_description());
3197 }
3198 }
3199 }
3200
3201 if (session_error() != SessionError::kNone) {
3202 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
3203 }
3204
3205 // Set the the ICE connection state to connecting since the connection may
3206 // become writable with peer reflexive candidates before any remote candidate
3207 // is signaled.
3208 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
3209 // is to have a new signal the indicates a change in checking state from the
3210 // transport and expose a new checking() member from transport that can be
3211 // read to determine the current checking state. The existing SignalConnecting
3212 // actually means "gathering candidates", so cannot be be used here.
Steve Antona3a92c22017-12-07 10:27:41 -08003213 if (remote_description()->GetType() != SdpType::kOffer &&
Steve Antonf764cf42018-05-01 14:32:17 -07003214 remote_description()->number_of_mediasections() > 0u &&
Steve Anton8a006912017-12-04 15:25:56 -08003215 ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) {
3216 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
3217 }
3218
deadbeefab9b2d12015-10-14 11:33:11 -07003219 // If setting the description decided our SSL role, allocate any necessary
3220 // SCTP sids.
3221 rtc::SSLRole role;
Harald Alvestrand05e4d082019-12-03 14:04:21 +01003222 if (DataChannel::IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01003223 data_channel_controller_.AllocateSctpSids(role);
deadbeefab9b2d12015-10-14 11:33:11 -07003224 }
3225
Steve Antondcc3c022017-12-22 16:02:54 -08003226 if (IsUnifiedPlan()) {
Steve Anton8b815cd2018-02-16 16:14:42 -08003227 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>
Steve Anton3172c032018-05-03 15:30:18 -07003228 now_receiving_transceivers;
Steve Anton0f5400a2018-07-17 14:25:36 -07003229 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
Steve Antonc49bcd92018-02-14 14:28:13 -08003230 std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
Steve Anton3172c032018-05-03 15:30:18 -07003231 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
Mirko Bonadei739baf02019-01-27 17:29:42 +01003232 for (const auto& transceiver : transceivers_) {
Steve Antondcc3c022017-12-22 16:02:54 -08003233 const ContentInfo* content =
3234 FindMediaSectionForTransceiver(transceiver, remote_description());
3235 if (!content) {
3236 continue;
3237 }
3238 const MediaContentDescription* media_desc = content->media_description();
3239 RtpTransceiverDirection local_direction =
3240 RtpTransceiverDirectionReversed(media_desc->direction());
Henrik Boströmafa07dd2018-12-20 11:06:02 +01003241 // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the
3242 // RTCSessionDescription: Set the associated remote streams given
3243 // transceiver.[[Receiver]], msids, addList, and removeList".
3244 // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription
3245 if (RtpTransceiverDirectionHasRecv(local_direction)) {
3246 std::vector<std::string> stream_ids;
3247 if (!media_desc->streams().empty()) {
3248 // The remote description has signaled the stream IDs.
3249 stream_ids = media_desc->streams()[0].stream_ids();
Steve Antonef65ef12018-01-10 17:15:20 -08003250 }
Eldar Rello353a7182019-11-25 18:49:44 +02003251 transceiver_stable_states_by_transceivers_[transceiver]
3252 .SetRemoteStreamIdsIfUnset(transceiver->receiver()->stream_ids());
3253
Henrik Boströmafa07dd2018-12-20 11:06:02 +01003254 RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name
3255 << " (" << GetStreamIdsString(stream_ids) << ").";
3256 SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(),
3257 stream_ids, &added_streams,
3258 &removed_streams);
3259 // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6
3260 // "Set the RTCSessionDescription: If direction is sendrecv or recvonly,
3261 // and transceiver's current direction is neither sendrecv nor recvonly,
3262 // process the addition of a remote track for the media description.
3263 if (!transceiver->fired_direction() ||
3264 !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) {
3265 RTC_LOG(LS_INFO)
3266 << "Processing the addition of a remote track for MID="
3267 << content->name << ".";
3268 now_receiving_transceivers.push_back(transceiver);
Henrik Boström5b147782018-12-04 11:25:05 +01003269 }
Steve Antondcc3c022017-12-22 16:02:54 -08003270 }
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +01003271 // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's
Steve Anton0f5400a2018-07-17 14:25:36 -07003272 // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the
3273 // removal of a remote track for the media description, given transceiver,
3274 // removeList, and muteTracks.
Steve Antondcc3c022017-12-22 16:02:54 -08003275 if (!RtpTransceiverDirectionHasRecv(local_direction) &&
Steve Anton0f5400a2018-07-17 14:25:36 -07003276 (transceiver->fired_direction() &&
3277 RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) {
3278 ProcessRemovalOfRemoteTrack(transceiver, &remove_list,
3279 &removed_streams);
Steve Antondcc3c022017-12-22 16:02:54 -08003280 }
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +01003281 // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction.
Steve Anton0f5400a2018-07-17 14:25:36 -07003282 transceiver->internal()->set_fired_direction(local_direction);
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +01003283 // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run
Steve Anton0f5400a2018-07-17 14:25:36 -07003284 // the following steps:
Steve Antondcc3c022017-12-22 16:02:54 -08003285 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +01003286 // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to
Steve Anton0f5400a2018-07-17 14:25:36 -07003287 // direction.
Steve Antondcc3c022017-12-22 16:02:54 -08003288 transceiver->internal()->set_current_direction(local_direction);
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +01003289 // 2.2.8.1.11.[3-6]: Set the transport internal slots.
3290 if (transceiver->mid()) {
3291 auto dtls_transport =
3292 LookupDtlsTransportByMidInternal(*transceiver->mid());
3293 transceiver->internal()->sender_internal()->set_transport(
3294 dtls_transport);
3295 transceiver->internal()->receiver_internal()->set_transport(
3296 dtls_transport);
3297 }
Steve Antondcc3c022017-12-22 16:02:54 -08003298 }
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +01003299 // 2.2.8.1.12: If the media description is rejected, and transceiver is
Steve Anton0f5400a2018-07-17 14:25:36 -07003300 // not already stopped, stop the RTCRtpTransceiver transceiver.
Steve Antondcc3c022017-12-22 16:02:54 -08003301 if (content->rejected && !transceiver->stopped()) {
Steve Anton3d954a62018-04-02 11:27:23 -07003302 RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name
3303 << " since the media section was rejected.";
Steve Antondcc3c022017-12-22 16:02:54 -08003304 transceiver->Stop();
3305 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003306 if (!content->rejected &&
3307 RtpTransceiverDirectionHasRecv(local_direction)) {
Seth Hampson5897a6e2018-04-03 11:16:33 -07003308 if (!media_desc->streams().empty() &&
3309 media_desc->streams()[0].has_ssrcs()) {
Saurav Das7262fc22019-09-11 16:23:05 -07003310 uint32_t ssrc = media_desc->streams()[0].first_ssrc();
3311 transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc);
3312 } else {
3313 transceiver->internal()
3314 ->receiver_internal()
3315 ->SetupUnsignaledMediaChannel();
Seth Hampson2f0d7022018-02-20 11:54:42 -08003316 }
Steve Antond3679212018-01-17 17:41:02 -08003317 }
Steve Antondcc3c022017-12-22 16:02:54 -08003318 }
Steve Antonc49bcd92018-02-14 14:28:13 -08003319 // Once all processing has finished, fire off callbacks.
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02003320 auto observer = Observer();
Mirko Bonadei739baf02019-01-27 17:29:42 +01003321 for (const auto& transceiver : now_receiving_transceivers) {
Steve Anton6e221372018-02-20 12:59:16 -08003322 stats_->AddTrack(transceiver->receiver()->track());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02003323 observer->OnTrack(transceiver);
3324 observer->OnAddTrack(transceiver->receiver(),
3325 transceiver->receiver()->streams());
Steve Antonef65ef12018-01-10 17:15:20 -08003326 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01003327 for (const auto& stream : added_streams) {
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02003328 observer->OnAddStream(stream);
Steve Antonc49bcd92018-02-14 14:28:13 -08003329 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01003330 for (const auto& transceiver : remove_list) {
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02003331 observer->OnRemoveTrack(transceiver->receiver());
Steve Anton3172c032018-05-03 15:30:18 -07003332 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01003333 for (const auto& stream : removed_streams) {
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02003334 observer->OnRemoveStream(stream);
Steve Anton3172c032018-05-03 15:30:18 -07003335 }
Steve Antondcc3c022017-12-22 16:02:54 -08003336 }
3337
Henrik Boströmfdb92012017-11-09 19:55:44 +01003338 const cricket::ContentInfo* audio_content =
3339 GetFirstAudioContent(remote_description()->description());
3340 const cricket::ContentInfo* video_content =
3341 GetFirstVideoContent(remote_description()->description());
deadbeefbda7e0b2015-12-08 17:13:40 -08003342 const cricket::AudioContentDescription* audio_desc =
Henrik Boströmfdb92012017-11-09 19:55:44 +01003343 GetFirstAudioContentDescription(remote_description()->description());
deadbeefbda7e0b2015-12-08 17:13:40 -08003344 const cricket::VideoContentDescription* video_desc =
Henrik Boströmfdb92012017-11-09 19:55:44 +01003345 GetFirstVideoContentDescription(remote_description()->description());
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003346 const cricket::RtpDataContentDescription* rtp_data_desc =
3347 GetFirstRtpDataContentDescription(remote_description()->description());
deadbeefbda7e0b2015-12-08 17:13:40 -08003348
3349 // Check if the descriptions include streams, just in case the peer supports
3350 // MSID, but doesn't indicate so with "a=msid-semantic".
Henrik Boströmfdb92012017-11-09 19:55:44 +01003351 if (remote_description()->description()->msid_supported() ||
deadbeefbda7e0b2015-12-08 17:13:40 -08003352 (audio_desc && !audio_desc->streams().empty()) ||
3353 (video_desc && !video_desc->streams().empty())) {
3354 remote_peer_supports_msid_ = true;
3355 }
deadbeefab9b2d12015-10-14 11:33:11 -07003356
3357 // We wait to signal new streams until we finish processing the description,
3358 // since only at that point will new streams have all their tracks.
3359 rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create());
3360
Steve Antondcc3c022017-12-22 16:02:54 -08003361 if (!IsUnifiedPlan()) {
3362 // TODO(steveanton): When removing RTP senders/receivers in response to a
3363 // rejected media section, there is some cleanup logic that expects the
3364 // voice/ video channel to still be set. But in this method the voice/video
3365 // channel would have been destroyed by the SetRemoteDescription caller
3366 // above so the cleanup that relies on them fails to run. The RemoveSenders
3367 // calls should be moved to right before the DestroyChannel calls to fix
3368 // this.
Steve Anton8d3444d2017-10-20 15:30:51 -07003369
Steve Antondcc3c022017-12-22 16:02:54 -08003370 // Find all audio rtp streams and create corresponding remote AudioTracks
3371 // and MediaStreams.
3372 if (audio_content) {
3373 if (audio_content->rejected) {
3374 RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
3375 } else {
3376 bool default_audio_track_needed =
3377 !remote_peer_supports_msid_ &&
3378 RtpTransceiverDirectionHasSend(audio_desc->direction());
3379 UpdateRemoteSendersList(GetActiveStreams(audio_desc),
3380 default_audio_track_needed, audio_desc->type(),
3381 new_streams);
3382 }
deadbeeffaac4972015-11-12 15:33:07 -08003383 }
deadbeefab9b2d12015-10-14 11:33:11 -07003384
Steve Antondcc3c022017-12-22 16:02:54 -08003385 // Find all video rtp streams and create corresponding remote VideoTracks
3386 // and MediaStreams.
3387 if (video_content) {
3388 if (video_content->rejected) {
3389 RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
3390 } else {
3391 bool default_video_track_needed =
3392 !remote_peer_supports_msid_ &&
3393 RtpTransceiverDirectionHasSend(video_desc->direction());
3394 UpdateRemoteSendersList(GetActiveStreams(video_desc),
3395 default_video_track_needed, video_desc->type(),
3396 new_streams);
3397 }
deadbeeffaac4972015-11-12 15:33:07 -08003398 }
deadbeefab9b2d12015-10-14 11:33:11 -07003399
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003400 // If this is an RTP data transport, update the DataChannels with the
3401 // information from the remote peer.
3402 if (rtp_data_desc) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01003403 data_channel_controller_.UpdateRemoteRtpDataChannels(
3404 GetActiveStreams(rtp_data_desc));
deadbeefab9b2d12015-10-14 11:33:11 -07003405 }
deadbeefab9b2d12015-10-14 11:33:11 -07003406
Steve Antondcc3c022017-12-22 16:02:54 -08003407 // Iterate new_streams and notify the observer about new MediaStreams.
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02003408 auto observer = Observer();
Steve Antondcc3c022017-12-22 16:02:54 -08003409 for (size_t i = 0; i < new_streams->count(); ++i) {
3410 MediaStreamInterface* new_stream = new_streams->at(i);
3411 stats_->AddStream(new_stream);
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02003412 observer->OnAddStream(
Steve Antondcc3c022017-12-22 16:02:54 -08003413 rtc::scoped_refptr<MediaStreamInterface>(new_stream));
3414 }
deadbeefab9b2d12015-10-14 11:33:11 -07003415
Steve Antondcc3c022017-12-22 16:02:54 -08003416 UpdateEndedRemoteMediaStreams();
3417 }
deadbeefab9b2d12015-10-14 11:33:11 -07003418
Henrik Boström79b69802019-07-18 11:16:56 +02003419 if (type == SdpType::kAnswer &&
3420 local_ice_credentials_to_replace_->SatisfiesIceRestart(
3421 *current_local_description_)) {
3422 local_ice_credentials_to_replace_->ClearIceCredentials();
3423 }
3424
Steve Anton8a006912017-12-04 15:25:56 -08003425 return RTCError::OK();
deadbeeffc648b62015-10-13 16:42:33 -07003426}
3427
Henrik Boströmafa07dd2018-12-20 11:06:02 +01003428void PeerConnection::SetAssociatedRemoteStreams(
3429 rtc::scoped_refptr<RtpReceiverInternal> receiver,
3430 const std::vector<std::string>& stream_ids,
3431 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
3432 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
3433 std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams;
3434 for (const std::string& stream_id : stream_ids) {
3435 rtc::scoped_refptr<MediaStreamInterface> stream =
3436 remote_streams_->find(stream_id);
3437 if (!stream) {
3438 stream = MediaStreamProxy::Create(rtc::Thread::Current(),
3439 MediaStream::Create(stream_id));
3440 remote_streams_->AddStream(stream);
3441 added_streams->push_back(stream);
3442 }
3443 media_streams.push_back(stream);
3444 }
3445 // Special case: "a=msid" missing, use random stream ID.
3446 if (media_streams.empty() &&
3447 !(remote_description()->description()->msid_signaling() &
3448 cricket::kMsidSignalingMediaSection)) {
3449 if (!missing_msid_default_stream_) {
3450 missing_msid_default_stream_ = MediaStreamProxy::Create(
3451 rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid()));
3452 added_streams->push_back(missing_msid_default_stream_);
3453 }
3454 media_streams.push_back(missing_msid_default_stream_);
3455 }
3456 std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
3457 receiver->streams();
3458 // SetStreams() will add/remove the receiver's track to/from the streams. This
3459 // differs from the spec - the spec uses an "addList" and "removeList" to
3460 // update the stream-track relationships in a later step. We do this earlier,
3461 // changing the order of things, but the end-result is the same.
3462 // TODO(hbos): When we remove remote_streams(), use set_stream_ids()
3463 // instead. https://crbug.com/webrtc/9480
3464 receiver->SetStreams(media_streams);
3465 RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
3466}
3467
Steve Anton0f5400a2018-07-17 14:25:36 -07003468void PeerConnection::ProcessRemovalOfRemoteTrack(
3469 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3470 transceiver,
3471 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
3472 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
3473 RTC_DCHECK(transceiver->mid());
3474 RTC_LOG(LS_INFO) << "Processing the removal of a track for MID="
3475 << *transceiver->mid();
Henrik Boströmafa07dd2018-12-20 11:06:02 +01003476 std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
Steve Anton0f5400a2018-07-17 14:25:36 -07003477 transceiver->internal()->receiver_internal()->streams();
3478 // This will remove the remote track from the streams.
3479 transceiver->internal()->receiver_internal()->set_stream_ids({});
3480 remove_list->push_back(transceiver);
Henrik Boströmafa07dd2018-12-20 11:06:02 +01003481 RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
3482}
3483
3484void PeerConnection::RemoveRemoteStreamsIfEmpty(
3485 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams,
3486 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
3487 // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of
3488 // streams, see if the stream was removed by checking if this was the last
3489 // receiver with that stream ID.
Mirko Bonadei739baf02019-01-27 17:29:42 +01003490 for (const auto& remote_stream : remote_streams) {
Henrik Boströmafa07dd2018-12-20 11:06:02 +01003491 if (remote_stream->GetAudioTracks().empty() &&
3492 remote_stream->GetVideoTracks().empty()) {
3493 remote_streams_->RemoveStream(remote_stream);
3494 removed_streams->push_back(remote_stream);
Steve Anton0f5400a2018-07-17 14:25:36 -07003495 }
3496 }
3497}
3498
Steve Antondcc3c022017-12-22 16:02:54 -08003499RTCError PeerConnection::UpdateTransceiversAndDataChannels(
3500 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -08003501 const SessionDescriptionInterface& new_session,
3502 const SessionDescriptionInterface* old_local_description,
3503 const SessionDescriptionInterface* old_remote_description) {
Steve Antondcc3c022017-12-22 16:02:54 -08003504 RTC_DCHECK(IsUnifiedPlan());
3505
Steve Anton7464fca2018-01-19 11:10:37 -08003506 const cricket::ContentGroup* bundle_group = nullptr;
3507 if (new_session.GetType() == SdpType::kOffer) {
3508 auto bundle_group_or_error =
3509 GetEarlyBundleGroup(*new_session.description());
3510 if (!bundle_group_or_error.ok()) {
3511 return bundle_group_or_error.MoveError();
3512 }
3513 bundle_group = bundle_group_or_error.MoveValue();
Steve Antondcc3c022017-12-22 16:02:54 -08003514 }
Steve Antondcc3c022017-12-22 16:02:54 -08003515
Steve Antondcc3c022017-12-22 16:02:54 -08003516 const ContentInfos& new_contents = new_session.description()->contents();
Steve Antondcc3c022017-12-22 16:02:54 -08003517 for (size_t i = 0; i < new_contents.size(); ++i) {
3518 const cricket::ContentInfo& new_content = new_contents[i];
Steve Antondcc3c022017-12-22 16:02:54 -08003519 cricket::MediaType media_type = new_content.media_description()->type();
Amit Hilbuchae3df542019-01-07 12:13:08 -08003520 mid_generator_.AddKnownId(new_content.name);
Steve Antondcc3c022017-12-22 16:02:54 -08003521 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
3522 media_type == cricket::MEDIA_TYPE_VIDEO) {
Seth Hampsonae8a90a2018-02-13 15:33:48 -08003523 const cricket::ContentInfo* old_local_content = nullptr;
3524 if (old_local_description &&
3525 i < old_local_description->description()->contents().size()) {
3526 old_local_content =
3527 &old_local_description->description()->contents()[i];
3528 }
3529 const cricket::ContentInfo* old_remote_content = nullptr;
3530 if (old_remote_description &&
3531 i < old_remote_description->description()->contents().size()) {
3532 old_remote_content =
3533 &old_remote_description->description()->contents()[i];
3534 }
Steve Antondcc3c022017-12-22 16:02:54 -08003535 auto transceiver_or_error =
Seth Hampsonae8a90a2018-02-13 15:33:48 -08003536 AssociateTransceiver(source, new_session.GetType(), i, new_content,
3537 old_local_content, old_remote_content);
Steve Antondcc3c022017-12-22 16:02:54 -08003538 if (!transceiver_or_error.ok()) {
3539 return transceiver_or_error.MoveError();
3540 }
3541 auto transceiver = transceiver_or_error.MoveValue();
Steve Antondcc3c022017-12-22 16:02:54 -08003542 RTCError error =
3543 UpdateTransceiverChannel(transceiver, new_content, bundle_group);
3544 if (!error.ok()) {
3545 return error;
3546 }
3547 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonfa2260d2017-12-28 16:38:23 -08003548 if (GetDataMid() && new_content.name != *GetDataMid()) {
3549 // Ignore all but the first data section.
Steve Anton3d954a62018-04-02 11:27:23 -07003550 RTC_LOG(LS_INFO) << "Ignoring data media section with MID="
3551 << new_content.name;
Steve Antonfa2260d2017-12-28 16:38:23 -08003552 continue;
3553 }
3554 RTCError error = UpdateDataChannel(source, new_content, bundle_group);
3555 if (!error.ok()) {
3556 return error;
3557 }
Steve Antondcc3c022017-12-22 16:02:54 -08003558 } else {
3559 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3560 "Unknown section type.");
3561 }
3562 }
3563
3564 return RTCError::OK();
3565}
3566
3567RTCError PeerConnection::UpdateTransceiverChannel(
3568 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3569 transceiver,
3570 const cricket::ContentInfo& content,
3571 const cricket::ContentGroup* bundle_group) {
3572 RTC_DCHECK(IsUnifiedPlan());
3573 RTC_DCHECK(transceiver);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08003574 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Antondcc3c022017-12-22 16:02:54 -08003575 if (content.rejected) {
3576 if (channel) {
3577 transceiver->internal()->SetChannel(nullptr);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08003578 DestroyChannelInterface(channel);
Steve Antondcc3c022017-12-22 16:02:54 -08003579 }
3580 } else {
3581 if (!channel) {
Steve Anton69470252018-02-09 11:43:08 -08003582 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Zhi Huange830e682018-03-30 10:48:35 -07003583 channel = CreateVoiceChannel(content.name);
Steve Antondcc3c022017-12-22 16:02:54 -08003584 } else {
Steve Anton69470252018-02-09 11:43:08 -08003585 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type());
Zhi Huange830e682018-03-30 10:48:35 -07003586 channel = CreateVideoChannel(content.name);
Steve Antondcc3c022017-12-22 16:02:54 -08003587 }
3588 if (!channel) {
3589 LOG_AND_RETURN_ERROR(
3590 RTCErrorType::INTERNAL_ERROR,
3591 "Failed to create channel for mid=" + content.name);
3592 }
3593 transceiver->internal()->SetChannel(channel);
3594 }
3595 }
3596 return RTCError::OK();
3597}
3598
Steve Antonfa2260d2017-12-28 16:38:23 -08003599RTCError PeerConnection::UpdateDataChannel(
3600 cricket::ContentSource source,
3601 const cricket::ContentInfo& content,
3602 const cricket::ContentGroup* bundle_group) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01003603 if (data_channel_type() == cricket::DCT_NONE) {
Steve Antondbf9d032018-01-19 15:23:40 -08003604 // If data channels are disabled, ignore this media section. CreateAnswer
3605 // will take care of rejecting it.
3606 return RTCError::OK();
Steve Antonfa2260d2017-12-28 16:38:23 -08003607 }
3608 if (content.rejected) {
Bjorn A Mellemb689af42019-08-21 10:44:59 -07003609 RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid();
Harald Alvestrand408cb4b2019-11-16 12:09:08 +01003610 DestroyDataChannelTransport();
Steve Antonfa2260d2017-12-28 16:38:23 -08003611 } else {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01003612 if (!data_channel_controller_.rtp_data_channel() &&
3613 !data_channel_controller_.data_channel_transport()) {
Bjorn A Mellemb689af42019-08-21 10:44:59 -07003614 RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid();
Zhi Huange830e682018-03-30 10:48:35 -07003615 if (!CreateDataChannel(content.name)) {
Steve Antonfa2260d2017-12-28 16:38:23 -08003616 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3617 "Failed to create data channel.");
3618 }
3619 }
3620 if (source == cricket::CS_REMOTE) {
3621 const MediaContentDescription* data_desc = content.media_description();
3622 if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01003623 data_channel_controller_.UpdateRemoteRtpDataChannels(
3624 GetActiveStreams(data_desc));
Steve Antonfa2260d2017-12-28 16:38:23 -08003625 }
3626 }
3627 }
3628 return RTCError::OK();
3629}
3630
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003631// This method will extract any send encodings that were sent by the remote
3632// connection. This is currently only relevant for Simulcast scenario (where
3633// the number of layers may be communicated by the server).
3634static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription(
3635 const MediaContentDescription& desc) {
3636 if (!desc.HasSimulcast()) {
3637 return {};
3638 }
3639 std::vector<RtpEncodingParameters> result;
3640 const SimulcastDescription& simulcast = desc.simulcast_description();
3641
3642 // This is a remote description, the parameters we are after should appear
3643 // as receive streams.
3644 for (const auto& alternatives : simulcast.receive_layers()) {
3645 RTC_DCHECK(!alternatives.empty());
3646 // There is currently no way to specify or choose from alternatives.
3647 // We will always use the first alternative, which is the most preferred.
3648 const SimulcastLayer& layer = alternatives[0];
3649 RtpEncodingParameters parameters;
3650 parameters.rid = layer.rid;
3651 parameters.active = !layer.is_paused;
3652 result.push_back(parameters);
3653 }
3654
3655 return result;
3656}
3657
3658static RTCError UpdateSimulcastLayerStatusInSender(
3659 const std::vector<SimulcastLayer>& layers,
Amit Hilbuch2297d332019-02-19 12:49:22 -08003660 rtc::scoped_refptr<RtpSenderInternal> sender) {
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003661 RTC_DCHECK(sender);
Amit Hilbuch619b2942019-02-26 15:55:19 -08003662 RtpParameters parameters = sender->GetParametersInternal();
Amit Hilbuch2297d332019-02-19 12:49:22 -08003663 std::vector<std::string> disabled_layers;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003664
3665 // The simulcast envelope cannot be changed, only the status of the streams.
3666 // So we will iterate over the send encodings rather than the layers.
3667 for (RtpEncodingParameters& encoding : parameters.encodings) {
3668 auto iter = std::find_if(layers.begin(), layers.end(),
3669 [&encoding](const SimulcastLayer& layer) {
3670 return layer.rid == encoding.rid;
3671 });
3672 // A layer that cannot be found may have been removed by the remote party.
Amit Hilbuch2297d332019-02-19 12:49:22 -08003673 if (iter == layers.end()) {
3674 disabled_layers.push_back(encoding.rid);
3675 continue;
3676 }
3677
3678 encoding.active = !iter->is_paused;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003679 }
3680
Amit Hilbuch619b2942019-02-26 15:55:19 -08003681 RTCError result = sender->SetParametersInternal(parameters);
Amit Hilbuch2297d332019-02-19 12:49:22 -08003682 if (result.ok()) {
3683 result = sender->DisableEncodingLayers(disabled_layers);
3684 }
3685
3686 return result;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003687}
3688
Amit Hilbuchaabd0362019-03-01 13:14:46 -08003689static bool SimulcastIsRejected(
3690 const ContentInfo* local_content,
3691 const MediaContentDescription& answer_media_desc) {
3692 bool simulcast_offered = local_content &&
3693 local_content->media_description() &&
3694 local_content->media_description()->HasSimulcast();
3695 bool simulcast_answered = answer_media_desc.HasSimulcast();
3696 bool rids_supported = RtpExtension::FindHeaderExtensionByUri(
3697 answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri);
3698 return simulcast_offered && (!simulcast_answered || !rids_supported);
3699}
3700
Amit Hilbuch2297d332019-02-19 12:49:22 -08003701static RTCError DisableSimulcastInSender(
3702 rtc::scoped_refptr<RtpSenderInternal> sender) {
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003703 RTC_DCHECK(sender);
Amit Hilbuch619b2942019-02-26 15:55:19 -08003704 RtpParameters parameters = sender->GetParametersInternal();
Amit Hilbuch2297d332019-02-19 12:49:22 -08003705 if (parameters.encodings.size() <= 1) {
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003706 return RTCError::OK();
3707 }
3708
Amit Hilbuch2297d332019-02-19 12:49:22 -08003709 std::vector<std::string> disabled_layers;
3710 std::transform(
3711 parameters.encodings.begin() + 1, parameters.encodings.end(),
3712 std::back_inserter(disabled_layers),
3713 [](const RtpEncodingParameters& encoding) { return encoding.rid; });
3714 return sender->DisableEncodingLayers(disabled_layers);
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003715}
3716
Steve Antondcc3c022017-12-22 16:02:54 -08003717RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
3718PeerConnection::AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -08003719 SdpType type,
Steve Antondcc3c022017-12-22 16:02:54 -08003720 size_t mline_index,
3721 const ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 15:33:48 -08003722 const ContentInfo* old_local_content,
3723 const ContentInfo* old_remote_content) {
Steve Antondcc3c022017-12-22 16:02:54 -08003724 RTC_DCHECK(IsUnifiedPlan());
Seth Hampsonae8a90a2018-02-13 15:33:48 -08003725 // If this is an offer then the m= section might be recycled. If the m=
3726 // section is being recycled (defined as: rejected in the current local or
3727 // remote description and not rejected in new description), dissociate the
3728 // currently associated RtpTransceiver by setting its mid property to null,
3729 // and discard the mapping between the transceiver and its m= section index.
3730 if (IsMediaSectionBeingRecycled(type, content, old_local_content,
3731 old_remote_content)) {
3732 // We want to dissociate the transceiver that has the rejected mid.
3733 const std::string& old_mid =
3734 (old_local_content && old_local_content->rejected)
3735 ? old_local_content->name
3736 : old_remote_content->name;
3737 auto old_transceiver = GetAssociatedTransceiver(old_mid);
Steve Antondcc3c022017-12-22 16:02:54 -08003738 if (old_transceiver) {
Steve Anton3d954a62018-04-02 11:27:23 -07003739 RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid
3740 << " since the media section is being recycled.";
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02003741 old_transceiver->internal()->set_mid(absl::nullopt);
3742 old_transceiver->internal()->set_mline_index(absl::nullopt);
Steve Antondcc3c022017-12-22 16:02:54 -08003743 }
3744 }
3745 const MediaContentDescription* media_desc = content.media_description();
3746 auto transceiver = GetAssociatedTransceiver(content.name);
3747 if (source == cricket::CS_LOCAL) {
3748 // Find the RtpTransceiver that corresponds to this m= section, using the
3749 // mapping between transceivers and m= section indices established when
3750 // creating the offer.
3751 if (!transceiver) {
3752 transceiver = GetTransceiverByMLineIndex(mline_index);
3753 }
3754 if (!transceiver) {
3755 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3756 "Unknown transceiver");
3757 }
3758 } else {
3759 RTC_DCHECK_EQ(source, cricket::CS_REMOTE);
3760 // If the m= section is sendrecv or recvonly, and there are RtpTransceivers
3761 // of the same type...
Amit Hilbuchaa584152019-02-06 17:09:52 -08003762 // When simulcast is requested, a transceiver cannot be associated because
3763 // AddTrack cannot be called to initialize it.
Steve Antondcc3c022017-12-22 16:02:54 -08003764 if (!transceiver &&
Amit Hilbuchaa584152019-02-06 17:09:52 -08003765 RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
3766 !media_desc->HasSimulcast()) {
Steve Antondcc3c022017-12-22 16:02:54 -08003767 transceiver = FindAvailableTransceiverToReceive(media_desc->type());
3768 }
3769 // If no RtpTransceiver was found in the previous step, create one with a
3770 // recvonly direction.
3771 if (!transceiver) {
Steve Anton3d954a62018-04-02 11:27:23 -07003772 RTC_LOG(LS_INFO) << "Adding "
3773 << cricket::MediaTypeToString(media_desc->type())
3774 << " transceiver for MID=" << content.name
3775 << " at i=" << mline_index
3776 << " in response to the remote description.";
Steve Anton111fdfd2018-06-25 13:03:36 -07003777 std::string sender_id = rtc::CreateRandomUuid();
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003778 std::vector<RtpEncodingParameters> send_encodings =
3779 GetSendEncodingsFromRemoteDescription(*media_desc);
3780 auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {},
3781 send_encodings);
Steve Anton5f94aa22018-02-01 10:58:30 -08003782 std::string receiver_id;
3783 if (!media_desc->streams().empty()) {
3784 receiver_id = media_desc->streams()[0].id;
3785 } else {
3786 receiver_id = rtc::CreateRandomUuid();
3787 }
3788 auto receiver = CreateReceiver(media_desc->type(), receiver_id);
Steve Anton02ee47c2018-01-10 16:26:06 -08003789 transceiver = CreateAndAddTransceiver(sender, receiver);
Steve Antondcc3c022017-12-22 16:02:54 -08003790 transceiver->internal()->set_direction(
3791 RtpTransceiverDirection::kRecvOnly);
Eldar Rello5ab79e62019-10-09 18:29:44 +03003792 if (type == SdpType::kOffer) {
Eldar Rello353a7182019-11-25 18:49:44 +02003793 transceiver_stable_states_by_transceivers_[transceiver]
3794 .set_newly_created();
Eldar Rello5ab79e62019-10-09 18:29:44 +03003795 }
Steve Antondcc3c022017-12-22 16:02:54 -08003796 }
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003797 // Check if the offer indicated simulcast but the answer rejected it.
3798 // This can happen when simulcast is not supported on the remote party.
Amit Hilbuchaabd0362019-03-01 13:14:46 -08003799 if (SimulcastIsRejected(old_local_content, *media_desc)) {
Amit Hilbuche2a284d2019-03-05 12:36:31 -08003800 RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true);
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003801 RTCError error =
Amit Hilbuch2297d332019-02-19 12:49:22 -08003802 DisableSimulcastInSender(transceiver->internal()->sender_internal());
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003803 if (!error.ok()) {
3804 RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast.";
3805 return std::move(error);
3806 }
3807 }
Steve Antondcc3c022017-12-22 16:02:54 -08003808 }
3809 RTC_DCHECK(transceiver);
Steve Anton69470252018-02-09 11:43:08 -08003810 if (transceiver->media_type() != media_desc->type()) {
Steve Antondcc3c022017-12-22 16:02:54 -08003811 LOG_AND_RETURN_ERROR(
3812 RTCErrorType::INVALID_PARAMETER,
3813 "Transceiver type does not match media description type.");
3814 }
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003815 if (media_desc->HasSimulcast()) {
3816 std::vector<SimulcastLayer> layers =
3817 source == cricket::CS_LOCAL
3818 ? media_desc->simulcast_description().send_layers().GetAllLayers()
3819 : media_desc->simulcast_description()
3820 .receive_layers()
3821 .GetAllLayers();
3822 RTCError error = UpdateSimulcastLayerStatusInSender(
Amit Hilbuch2297d332019-02-19 12:49:22 -08003823 layers, transceiver->internal()->sender_internal());
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08003824 if (!error.ok()) {
3825 RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers.";
3826 return std::move(error);
3827 }
3828 }
Eldar Rello5ab79e62019-10-09 18:29:44 +03003829 if (type == SdpType::kOffer) {
Eldar Rello353a7182019-11-25 18:49:44 +02003830 bool state_changes = transceiver->internal()->mid() != content.name ||
3831 transceiver->internal()->mline_index() != mline_index;
3832 if (state_changes) {
3833 transceiver_stable_states_by_transceivers_[transceiver]
3834 .SetMSectionIfUnset(transceiver->internal()->mid(),
3835 transceiver->internal()->mline_index());
Eldar Rello5ab79e62019-10-09 18:29:44 +03003836 }
3837 }
Steve Antondcc3c022017-12-22 16:02:54 -08003838 // Associate the found or created RtpTransceiver with the m= section by
3839 // setting the value of the RtpTransceiver's mid property to the MID of the m=
3840 // section, and establish a mapping between the transceiver and the index of
3841 // the m= section.
3842 transceiver->internal()->set_mid(content.name);
3843 transceiver->internal()->set_mline_index(mline_index);
3844 return std::move(transceiver);
3845}
3846
3847rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3848PeerConnection::GetAssociatedTransceiver(const std::string& mid) const {
3849 RTC_DCHECK(IsUnifiedPlan());
3850 for (auto transceiver : transceivers_) {
3851 if (transceiver->mid() == mid) {
3852 return transceiver;
3853 }
3854 }
3855 return nullptr;
3856}
3857
3858rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3859PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const {
3860 RTC_DCHECK(IsUnifiedPlan());
3861 for (auto transceiver : transceivers_) {
3862 if (transceiver->internal()->mline_index() == mline_index) {
3863 return transceiver;
3864 }
3865 }
3866 return nullptr;
3867}
3868
3869rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3870PeerConnection::FindAvailableTransceiverToReceive(
3871 cricket::MediaType media_type) const {
3872 RTC_DCHECK(IsUnifiedPlan());
3873 // From JSEP section 5.10 (Applying a Remote Description):
3874 // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of
3875 // the same type that were added to the PeerConnection by addTrack and are not
3876 // associated with any m= section and are not stopped, find the first such
3877 // RtpTransceiver.
3878 for (auto transceiver : transceivers_) {
Steve Anton69470252018-02-09 11:43:08 -08003879 if (transceiver->media_type() == media_type &&
Steve Antondcc3c022017-12-22 16:02:54 -08003880 transceiver->internal()->created_by_addtrack() && !transceiver->mid() &&
3881 !transceiver->stopped()) {
3882 return transceiver;
3883 }
3884 }
3885 return nullptr;
3886}
3887
Steve Antoned10bd92017-12-05 10:52:59 -08003888const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver(
3889 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3890 transceiver,
3891 const SessionDescriptionInterface* sdesc) const {
3892 RTC_DCHECK(transceiver);
3893 RTC_DCHECK(sdesc);
3894 if (IsUnifiedPlan()) {
3895 if (!transceiver->internal()->mid()) {
3896 // This transceiver is not associated with a media section yet.
3897 return nullptr;
3898 }
3899 return sdesc->description()->GetContentByName(
3900 *transceiver->internal()->mid());
3901 } else {
3902 // Plan B only allows at most one audio and one video section, so use the
3903 // first media section of that type.
3904 return cricket::GetFirstMediaContent(sdesc->description()->contents(),
Steve Anton69470252018-02-09 11:43:08 -08003905 transceiver->media_type());
Steve Antoned10bd92017-12-05 10:52:59 -08003906 }
3907}
3908
deadbeef46c73892016-11-16 19:42:04 -08003909PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() {
Karl Wiberg5966c502019-02-21 23:55:09 +01003910 RTC_DCHECK_RUN_ON(signaling_thread());
deadbeef46c73892016-11-16 19:42:04 -08003911 return configuration_;
3912}
3913
Niels Möller2579f0c2019-08-19 09:58:17 +02003914RTCError PeerConnection::SetConfiguration(
3915 const RTCConfiguration& configuration) {
Karl Wiberg5966c502019-02-21 23:55:09 +01003916 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 22:15:17 +01003917 TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration");
Steve Antonc79268f2018-04-24 09:54:10 -07003918 if (IsClosed()) {
Niels Möller2579f0c2019-08-19 09:58:17 +02003919 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
3920 "SetConfiguration: PeerConnection is closed.");
Steve Antonc79268f2018-04-24 09:54:10 -07003921 }
3922
Qingsi Wanga2d60672018-04-11 16:57:45 -07003923 // According to JSEP, after setLocalDescription, changing the candidate pool
3924 // size is not allowed, and changing the set of ICE servers will not result
3925 // in new candidates being gathered.
Steve Anton75737c02017-11-06 10:37:17 -08003926 if (local_description() && configuration.ice_candidate_pool_size !=
3927 configuration_.ice_candidate_pool_size) {
Niels Möller2579f0c2019-08-19 09:58:17 +02003928 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
3929 "Can't change candidate pool size after calling "
3930 "SetLocalDescription.");
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00003931 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -07003932
Piotr (Peter) Slatalaaa1e7c22018-10-16 10:04:45 -07003933 if (local_description() &&
Benjamin Wright8c27cca2018-10-25 10:16:44 -07003934 configuration.crypto_options != configuration_.crypto_options) {
Niels Möller2579f0c2019-08-19 09:58:17 +02003935 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
3936 "Can't change crypto_options after calling "
3937 "SetLocalDescription.");
Benjamin Wright8c27cca2018-10-25 10:16:44 -07003938 }
3939
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07003940 if (local_description() && configuration.use_datagram_transport !=
3941 configuration_.use_datagram_transport) {
Niels Möller2579f0c2019-08-19 09:58:17 +02003942 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
3943 "Can't change use_datagram_transport "
3944 "after calling SetLocalDescription.");
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07003945 }
3946
3947 if (remote_description() && configuration.use_datagram_transport !=
3948 configuration_.use_datagram_transport) {
Niels Möller2579f0c2019-08-19 09:58:17 +02003949 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
3950 "Can't change use_datagram_transport "
3951 "after calling SetRemoteDescription.");
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07003952 }
3953
Bjorn A Mellemb689af42019-08-21 10:44:59 -07003954 if (local_description() &&
3955 configuration.use_datagram_transport_for_data_channels !=
3956 configuration_.use_datagram_transport_for_data_channels) {
3957 LOG_AND_RETURN_ERROR(
3958 RTCErrorType::INVALID_MODIFICATION,
3959 "Can't change use_datagram_transport_for_data_channels "
3960 "after calling SetLocalDescription.");
3961 }
3962
3963 if (remote_description() &&
3964 configuration.use_datagram_transport_for_data_channels !=
3965 configuration_.use_datagram_transport_for_data_channels) {
3966 LOG_AND_RETURN_ERROR(
3967 RTCErrorType::INVALID_MODIFICATION,
3968 "Can't change use_datagram_transport_for_data_channels "
3969 "after calling SetRemoteDescription.");
3970 }
3971
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07003972 if (local_description() &&
3973 configuration.use_datagram_transport_for_data_channels_receive_only !=
3974 configuration_
3975 .use_datagram_transport_for_data_channels_receive_only) {
3976 LOG_AND_RETURN_ERROR(
3977 RTCErrorType::INVALID_MODIFICATION,
3978 "Can't change use_datagram_transport_for_data_channels_receive_only "
3979 "after calling SetLocalDescription.");
3980 }
3981
3982 if (remote_description() &&
3983 configuration.use_datagram_transport_for_data_channels_receive_only !=
3984 configuration_
3985 .use_datagram_transport_for_data_channels_receive_only) {
3986 LOG_AND_RETURN_ERROR(
3987 RTCErrorType::INVALID_MODIFICATION,
3988 "Can't change use_datagram_transport_for_data_channels_receive_only "
3989 "after calling SetRemoteDescription.");
3990 }
3991
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -08003992 if ((configuration.use_datagram_transport &&
Bjorn A Mellemb689af42019-08-21 10:44:59 -07003993 *configuration.use_datagram_transport) ||
3994 (configuration.use_datagram_transport_for_data_channels &&
3995 *configuration.use_datagram_transport_for_data_channels)) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08003996 RTC_CHECK(configuration.bundle_policy == kBundlePolicyMaxBundle)
3997 << "Media transport requires MaxBundle policy.";
3998 }
3999
deadbeef293e9262017-01-11 12:28:30 -08004000 // The simplest (and most future-compatible) way to tell if the config was
4001 // modified in an invalid way is to copy each property we do support
4002 // modifying, then use operator==. There are far more properties we don't
4003 // support modifying than those we do, and more could be added.
4004 RTCConfiguration modified_config = configuration_;
4005 modified_config.servers = configuration.servers;
4006 modified_config.type = configuration.type;
4007 modified_config.ice_candidate_pool_size =
4008 configuration.ice_candidate_pool_size;
4009 modified_config.prune_turn_ports = configuration.prune_turn_ports;
Honghai Zhangf8998cf2019-10-14 11:27:50 -07004010 modified_config.turn_port_prune_policy = configuration.turn_port_prune_policy;
Qingsi Wangbca14852019-06-26 14:56:02 -07004011 modified_config.surface_ice_candidates_on_ice_transport_type_changed =
4012 configuration.surface_ice_candidates_on_ice_transport_type_changed;
skvladd1f5fda2017-02-03 16:54:05 -08004013 modified_config.ice_check_min_interval = configuration.ice_check_min_interval;
Qingsi Wange6826d22018-03-08 14:55:14 -08004014 modified_config.ice_check_interval_strong_connectivity =
4015 configuration.ice_check_interval_strong_connectivity;
4016 modified_config.ice_check_interval_weak_connectivity =
4017 configuration.ice_check_interval_weak_connectivity;
Qingsi Wang22e623a2018-03-13 10:53:57 -07004018 modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout;
4019 modified_config.ice_unwritable_min_checks =
4020 configuration.ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -08004021 modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -08004022 modified_config.stun_candidate_keepalive_interval =
4023 configuration.stun_candidate_keepalive_interval;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004024 modified_config.turn_customizer = configuration.turn_customizer;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -08004025 modified_config.network_preference = configuration.network_preference;
Zhi Huangb57e1692018-06-12 11:41:11 -07004026 modified_config.active_reset_srtp_params =
4027 configuration.active_reset_srtp_params;
Bjorn A Mellem5985a042019-06-28 14:19:38 -07004028 modified_config.use_datagram_transport = configuration.use_datagram_transport;
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004029 modified_config.use_datagram_transport_for_data_channels =
4030 configuration.use_datagram_transport_for_data_channels;
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07004031 modified_config.use_datagram_transport_for_data_channels_receive_only =
4032 configuration.use_datagram_transport_for_data_channels_receive_only;
Jonas Oreland3c028422019-08-22 16:16:35 +02004033 modified_config.turn_logging_id = configuration.turn_logging_id;
philipel16cec3b2019-10-25 12:23:02 +02004034 modified_config.allow_codec_switching = configuration.allow_codec_switching;
deadbeef293e9262017-01-11 12:28:30 -08004035 if (configuration != modified_config) {
Niels Möller2579f0c2019-08-19 09:58:17 +02004036 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
4037 "Modifying the configuration in an unsupported way.");
deadbeef293e9262017-01-11 12:28:30 -08004038 }
4039
Steve Anton038834f2017-07-14 15:59:59 -07004040 // Validate the modified configuration.
4041 RTCError validate_error = ValidateConfiguration(modified_config);
4042 if (!validate_error.ok()) {
Niels Möller2579f0c2019-08-19 09:58:17 +02004043 return validate_error;
Steve Anton038834f2017-07-14 15:59:59 -07004044 }
4045
deadbeef293e9262017-01-11 12:28:30 -08004046 // Note that this isn't possible through chromium, since it's an unsigned
4047 // short in WebIDL.
4048 if (configuration.ice_candidate_pool_size < 0 ||
Wez939eb802018-05-03 03:34:17 -07004049 configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) {
Niels Möller2579f0c2019-08-19 09:58:17 +02004050 return RTCError(RTCErrorType::INVALID_RANGE);
deadbeef293e9262017-01-11 12:28:30 -08004051 }
4052
4053 // Parse ICE servers before hopping to network thread.
4054 cricket::ServerAddresses stun_servers;
4055 std::vector<cricket::RelayServerConfig> turn_servers;
4056 RTCErrorType parse_error =
4057 ParseIceServers(configuration.servers, &stun_servers, &turn_servers);
4058 if (parse_error != RTCErrorType::NONE) {
Niels Möller2579f0c2019-08-19 09:58:17 +02004059 return RTCError(parse_error);
deadbeef293e9262017-01-11 12:28:30 -08004060 }
Jonas Oreland3c028422019-08-22 16:16:35 +02004061 // Add the turn logging id to all turn servers
4062 for (cricket::RelayServerConfig& turn_server : turn_servers) {
4063 turn_server.turn_logging_id = configuration.turn_logging_id;
4064 }
4065
Harald Alvestrand8ebba742018-05-31 14:00:34 +02004066 // Note if STUN or TURN servers were supplied.
4067 if (!stun_servers.empty()) {
4068 NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED);
4069 }
4070 if (!turn_servers.empty()) {
4071 NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED);
4072 }
deadbeef293e9262017-01-11 12:28:30 -08004073
4074 // In theory this shouldn't fail.
4075 if (!network_thread()->Invoke<bool>(
4076 RTC_FROM_HERE,
4077 rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this,
4078 stun_servers, turn_servers, modified_config.type,
4079 modified_config.ice_candidate_pool_size,
Honghai Zhangf8998cf2019-10-14 11:27:50 -07004080 modified_config.GetTurnPortPrunePolicy(),
Qingsi Wangdb53f8e2018-02-20 14:45:49 -08004081 modified_config.turn_customizer,
Karl Wiberg739506e2019-04-03 11:37:28 +02004082 modified_config.stun_candidate_keepalive_interval,
4083 static_cast<bool>(local_description())))) {
Niels Möller2579f0c2019-08-19 09:58:17 +02004084 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4085 "Failed to apply configuration to PortAllocator.");
deadbeef293e9262017-01-11 12:28:30 -08004086 }
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07004087
deadbeefd1a38b52016-12-10 13:15:33 -08004088 // As described in JSEP, calling setConfiguration with new ICE servers or
4089 // candidate policy must set a "needs-ice-restart" bit so that the next offer
4090 // triggers an ICE restart which will pick up the changes.
deadbeef293e9262017-01-11 12:28:30 -08004091 if (modified_config.servers != configuration_.servers ||
4092 modified_config.type != configuration_.type ||
Honghai Zhangf8998cf2019-10-14 11:27:50 -07004093 modified_config.GetTurnPortPrunePolicy() !=
4094 configuration_.GetTurnPortPrunePolicy()) {
Steve Antond25da372017-11-06 14:50:29 -08004095 transport_controller_->SetNeedsIceRestartFlag();
deadbeefd1a38b52016-12-10 13:15:33 -08004096 }
skvladd1f5fda2017-02-03 16:54:05 -08004097
Qingsi Wang9c98f0c2018-02-15 15:10:59 -08004098 transport_controller_->SetIceConfig(ParseIceConfig(modified_config));
Bjorn A Mellem5985a042019-06-28 14:19:38 -07004099
4100 use_datagram_transport_ = datagram_transport_config_.enabled &&
4101 modified_config.use_datagram_transport.value_or(
4102 datagram_transport_config_.default_value);
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004103 use_datagram_transport_for_data_channels_ =
4104 datagram_transport_data_channel_config_.enabled &&
4105 modified_config.use_datagram_transport_for_data_channels.value_or(
4106 datagram_transport_data_channel_config_.default_value);
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07004107 use_datagram_transport_for_data_channels_receive_only_ =
4108 modified_config.use_datagram_transport_for_data_channels_receive_only
4109 .value_or(datagram_transport_data_channel_config_.receive_only);
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08004110 transport_controller_->SetMediaTransportSettings(
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07004111 use_datagram_transport_, use_datagram_transport_for_data_channels_,
4112 use_datagram_transport_for_data_channels_receive_only_);
skvladd1f5fda2017-02-03 16:54:05 -08004113
Zhi Huangb57e1692018-06-12 11:41:11 -07004114 if (configuration_.active_reset_srtp_params !=
4115 modified_config.active_reset_srtp_params) {
4116 transport_controller_->SetActiveResetSrtpParams(
4117 modified_config.active_reset_srtp_params);
4118 }
4119
philipel16cec3b2019-10-25 12:23:02 +02004120 if (modified_config.allow_codec_switching.has_value()) {
philipel01294f02019-11-14 13:03:25 +01004121 cricket::VideoMediaChannel* video_channel = video_media_channel();
4122 if (video_channel) {
4123 video_channel->SetVideoCodecSwitchingEnabled(
4124 *modified_config.allow_codec_switching);
4125 }
philipel16cec3b2019-10-25 12:23:02 +02004126 }
4127
deadbeef293e9262017-01-11 12:28:30 -08004128 configuration_ = modified_config;
Niels Möller2579f0c2019-08-19 09:58:17 +02004129 return RTCError::OK();
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00004130}
4131
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004132bool PeerConnection::AddIceCandidate(
4133 const IceCandidateInterface* ice_candidate) {
Karl Wiberg744310f2019-02-14 10:18:56 +01004134 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 22:15:17 +01004135 TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate");
zhihuang29ff8442016-07-27 11:07:25 -07004136 if (IsClosed()) {
Steve Antonc79268f2018-04-24 09:54:10 -07004137 RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed.";
Harald Alvestrand76829d72018-07-18 23:24:36 +02004138 NoteAddIceCandidateResult(kAddIceCandidateFailClosed);
zhihuang29ff8442016-07-27 11:07:25 -07004139 return false;
4140 }
Steve Antond25da372017-11-06 14:50:29 -08004141
4142 if (!remote_description()) {
Steve Antonc79268f2018-04-24 09:54:10 -07004143 RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added "
Jonas Olsson45cc8902018-02-13 10:37:07 +01004144 "without any remote session description.";
Harald Alvestrand76829d72018-07-18 23:24:36 +02004145 NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription);
Steve Antond25da372017-11-06 14:50:29 -08004146 return false;
4147 }
4148
4149 if (!ice_candidate) {
Steve Antonc79268f2018-04-24 09:54:10 -07004150 RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null.";
Harald Alvestrand76829d72018-07-18 23:24:36 +02004151 NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate);
Steve Antond25da372017-11-06 14:50:29 -08004152 return false;
4153 }
4154
4155 bool valid = false;
4156 bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid);
4157 if (!valid) {
Harald Alvestrand76829d72018-07-18 23:24:36 +02004158 NoteAddIceCandidateResult(kAddIceCandidateFailNotValid);
Steve Antond25da372017-11-06 14:50:29 -08004159 return false;
4160 }
4161
4162 // Add this candidate to the remote session description.
4163 if (!mutable_remote_description()->AddCandidate(ice_candidate)) {
Steve Antonc79268f2018-04-24 09:54:10 -07004164 RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used.";
Harald Alvestrand76829d72018-07-18 23:24:36 +02004165 NoteAddIceCandidateResult(kAddIceCandidateFailInAddition);
Steve Antond25da372017-11-06 14:50:29 -08004166 return false;
4167 }
4168
4169 if (ready) {
Harald Alvestrand76829d72018-07-18 23:24:36 +02004170 bool result = UseCandidate(ice_candidate);
4171 if (result) {
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07004172 NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED);
Harald Alvestrand76829d72018-07-18 23:24:36 +02004173 NoteAddIceCandidateResult(kAddIceCandidateSuccess);
4174 } else {
4175 NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable);
4176 }
4177 return result;
Steve Antond25da372017-11-06 14:50:29 -08004178 } else {
Steve Antonc79268f2018-04-24 09:54:10 -07004179 RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate.";
Harald Alvestrand76829d72018-07-18 23:24:36 +02004180 NoteAddIceCandidateResult(kAddIceCandidateFailNotReady);
Steve Antond25da372017-11-06 14:50:29 -08004181 return true;
4182 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004183}
4184
Henrik Boströmee6f4f62019-11-06 12:36:12 +01004185void PeerConnection::AddIceCandidate(
4186 std::unique_ptr<IceCandidateInterface> candidate,
4187 std::function<void(RTCError)> callback) {
4188 RTC_DCHECK_RUN_ON(signaling_thread());
4189 // Chain this operation. If asynchronous operations are pending on the chain,
4190 // this operation will be queued to be invoked, otherwise the contents of the
4191 // lambda will execute immediately.
4192 operations_chain_->ChainOperation(
4193 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
4194 candidate = std::move(candidate), callback = std::move(callback)](
4195 std::function<void()> operations_chain_callback) {
4196 if (!this_weak_ptr) {
4197 operations_chain_callback();
4198 callback(RTCError(
4199 RTCErrorType::INVALID_STATE,
4200 "AddIceCandidate failed because the session was shut down"));
4201 return;
4202 }
4203 if (!this_weak_ptr->AddIceCandidate(candidate.get())) {
4204 operations_chain_callback();
4205 // Fail with an error type and message consistent with Chromium.
4206 // TODO(hbos): Fail with error types according to spec.
4207 callback(RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
4208 "Error processing ICE candidate"));
4209 return;
4210 }
4211 operations_chain_callback();
4212 callback(RTCError::OK());
4213 });
4214}
4215
Honghai Zhang7fb69db2016-03-14 11:59:18 -07004216bool PeerConnection::RemoveIceCandidates(
4217 const std::vector<cricket::Candidate>& candidates) {
4218 TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates");
Karl Wiberg2cc368f2019-04-02 11:31:56 +02004219 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonc79268f2018-04-24 09:54:10 -07004220 if (IsClosed()) {
4221 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed.";
4222 return false;
4223 }
4224
Steve Antond25da372017-11-06 14:50:29 -08004225 if (!remote_description()) {
Steve Antonc79268f2018-04-24 09:54:10 -07004226 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed "
4227 "without any remote session description.";
Steve Antond25da372017-11-06 14:50:29 -08004228 return false;
4229 }
4230
4231 if (candidates.empty()) {
Steve Antonc79268f2018-04-24 09:54:10 -07004232 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty.";
Steve Antond25da372017-11-06 14:50:29 -08004233 return false;
4234 }
4235
4236 size_t number_removed =
4237 mutable_remote_description()->RemoveCandidates(candidates);
4238 if (number_removed != candidates.size()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01004239 RTC_LOG(LS_ERROR)
Steve Antonc79268f2018-04-24 09:54:10 -07004240 << "RemoveIceCandidates: Failed to remove candidates. Requested "
Jonas Olsson45cc8902018-02-13 10:37:07 +01004241 << candidates.size() << " but only " << number_removed
Mirko Bonadei675513b2017-11-09 11:09:25 +01004242 << " are removed.";
Steve Antond25da372017-11-06 14:50:29 -08004243 }
4244
4245 // Remove the candidates from the transport controller.
Zhi Huange830e682018-03-30 10:48:35 -07004246 RTCError error = transport_controller_->RemoveRemoteCandidates(candidates);
4247 if (!error.ok()) {
Steve Antonc79268f2018-04-24 09:54:10 -07004248 RTC_LOG(LS_ERROR)
4249 << "RemoveIceCandidates: Error when removing remote candidates: "
4250 << error.message();
Steve Antond25da372017-11-06 14:50:29 -08004251 }
4252 return true;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07004253}
4254
Niels Möller0c4f7be2018-05-07 14:01:37 +02004255RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) {
Steve Anton978b8762017-09-29 12:15:02 -07004256 if (!worker_thread()->IsCurrent()) {
4257 return worker_thread()->Invoke<RTCError>(
Yves Gerey665174f2018-06-19 15:03:05 +02004258 RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); });
zstein4b979802017-06-02 14:37:37 -07004259 }
Karl Wiberg6cab5c82019-03-26 09:57:01 +01004260 RTC_DCHECK_RUN_ON(worker_thread());
zstein4b979802017-06-02 14:37:37 -07004261
Niels Möller0c4f7be2018-05-07 14:01:37 +02004262 const bool has_min = bitrate.min_bitrate_bps.has_value();
4263 const bool has_start = bitrate.start_bitrate_bps.has_value();
4264 const bool has_max = bitrate.max_bitrate_bps.has_value();
zstein4b979802017-06-02 14:37:37 -07004265 if (has_min && *bitrate.min_bitrate_bps < 0) {
4266 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4267 "min_bitrate_bps <= 0");
4268 }
Niels Möller0c4f7be2018-05-07 14:01:37 +02004269 if (has_start) {
4270 if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) {
zstein4b979802017-06-02 14:37:37 -07004271 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
Niels Möller0c4f7be2018-05-07 14:01:37 +02004272 "start_bitrate_bps < min_bitrate_bps");
4273 } else if (*bitrate.start_bitrate_bps < 0) {
zstein4b979802017-06-02 14:37:37 -07004274 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4275 "curent_bitrate_bps < 0");
4276 }
4277 }
4278 if (has_max) {
Yves Gerey665174f2018-06-19 15:03:05 +02004279 if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) {
zstein4b979802017-06-02 14:37:37 -07004280 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
Niels Möller0c4f7be2018-05-07 14:01:37 +02004281 "max_bitrate_bps < start_bitrate_bps");
zstein4b979802017-06-02 14:37:37 -07004282 } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) {
4283 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4284 "max_bitrate_bps < min_bitrate_bps");
4285 } else if (*bitrate.max_bitrate_bps < 0) {
4286 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
4287 "max_bitrate_bps < 0");
4288 }
4289 }
4290
zstein4b979802017-06-02 14:37:37 -07004291 RTC_DCHECK(call_.get());
Piotr (Peter) Slatala7fbfaa42019-03-18 10:31:54 -07004292 call_->SetClientBitratePreferences(bitrate);
zstein4b979802017-06-02 14:37:37 -07004293
4294 return RTCError::OK();
4295}
4296
henrika5f6bf242017-11-01 11:06:56 +01004297void PeerConnection::SetAudioPlayout(bool playout) {
4298 if (!worker_thread()->IsCurrent()) {
4299 worker_thread()->Invoke<void>(
4300 RTC_FROM_HERE,
4301 rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout));
4302 return;
4303 }
4304 auto audio_state =
Sebastian Jansson6eb8a162018-11-16 11:29:55 +01004305 factory_->channel_manager()->media_engine()->voice().GetAudioState();
henrika5f6bf242017-11-01 11:06:56 +01004306 audio_state->SetPlayout(playout);
4307}
4308
4309void PeerConnection::SetAudioRecording(bool recording) {
4310 if (!worker_thread()->IsCurrent()) {
4311 worker_thread()->Invoke<void>(
4312 RTC_FROM_HERE,
4313 rtc::Bind(&PeerConnection::SetAudioRecording, this, recording));
4314 return;
4315 }
4316 auto audio_state =
Sebastian Jansson6eb8a162018-11-16 11:29:55 +01004317 factory_->channel_manager()->media_engine()->voice().GetAudioState();
henrika5f6bf242017-11-01 11:06:56 +01004318 audio_state->SetRecording(recording);
4319}
4320
Steve Anton8c0f7a72017-10-03 10:03:10 -07004321std::unique_ptr<rtc::SSLCertificate>
4322PeerConnection::GetRemoteAudioSSLCertificate() {
Taylor Brandstetterc3928662018-02-23 13:04:51 -08004323 std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain();
4324 if (!chain || !chain->GetSize()) {
Steve Anton8c0f7a72017-10-03 10:03:10 -07004325 return nullptr;
4326 }
Steve Antonf25303e2018-10-16 15:23:31 -07004327 return chain->Get(0).Clone();
Steve Anton8c0f7a72017-10-03 10:03:10 -07004328}
4329
Zhi Huang70b820f2018-01-27 14:16:15 -08004330std::unique_ptr<rtc::SSLCertChain>
4331PeerConnection::GetRemoteAudioSSLCertChain() {
Karl Wiberga58e1692019-03-26 13:33:43 +01004332 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonafb0bb72018-02-20 11:35:37 -08004333 auto audio_transceiver = GetFirstAudioTransceiver();
4334 if (!audio_transceiver || !audio_transceiver->internal()->channel()) {
Zhi Huang70b820f2018-01-27 14:16:15 -08004335 return nullptr;
4336 }
Zhi Huang70b820f2018-01-27 14:16:15 -08004337 return transport_controller_->GetRemoteSSLCertChain(
Steve Antonafb0bb72018-02-20 11:35:37 -08004338 audio_transceiver->internal()->channel()->transport_name());
4339}
4340
4341rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
4342PeerConnection::GetFirstAudioTransceiver() const {
4343 for (auto transceiver : transceivers_) {
4344 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
4345 return transceiver;
4346 }
4347 }
4348 return nullptr;
Zhi Huang70b820f2018-01-27 14:16:15 -08004349}
4350
Bjorn Tereliusde939432017-11-20 17:38:14 +01004351bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
4352 int64_t output_period_ms) {
Bjorn Tereliusde939432017-11-20 17:38:14 +01004353 return worker_thread()->Invoke<bool>(
Danil Chapovalov116ffe72019-09-05 10:21:11 +02004354 RTC_FROM_HERE,
4355 [this, output = std::move(output), output_period_ms]() mutable {
4356 return StartRtcEventLog_w(std::move(output), output_period_ms);
4357 });
ivoc14d5dbe2016-07-04 07:06:55 -07004358}
4359
Niels Möllerf00ca1a2019-05-10 11:33:12 +02004360bool PeerConnection::StartRtcEventLog(
4361 std::unique_ptr<RtcEventLogOutput> output) {
Niels Möller695cf6a2019-05-13 12:27:23 +02004362 int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput;
4363 if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) {
4364 output_period_ms = 5000;
4365 }
4366 return StartRtcEventLog(std::move(output), output_period_ms);
Niels Möllerf00ca1a2019-05-10 11:33:12 +02004367}
4368
ivoc14d5dbe2016-07-04 07:06:55 -07004369void PeerConnection::StopRtcEventLog() {
Steve Anton978b8762017-09-29 12:15:02 -07004370 worker_thread()->Invoke<void>(
ivoc14d5dbe2016-07-04 07:06:55 -07004371 RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this));
4372}
4373
Harald Alvestrandad88c882018-11-28 16:47:46 +01004374rtc::scoped_refptr<DtlsTransportInterface>
4375PeerConnection::LookupDtlsTransportByMid(const std::string& mid) {
Karl Wiberg2cc368f2019-04-02 11:31:56 +02004376 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrandad88c882018-11-28 16:47:46 +01004377 return transport_controller_->LookupDtlsTransportByMid(mid);
4378}
4379
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +01004380rtc::scoped_refptr<DtlsTransport>
4381PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) {
Karl Wiberg2cc368f2019-04-02 11:31:56 +02004382 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +01004383 return transport_controller_->LookupDtlsTransportByMid(mid);
4384}
4385
Harald Alvestrandc85328f2019-02-28 07:51:00 +01004386rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport()
4387 const {
Karl Wiberg2cc368f2019-04-02 11:31:56 +02004388 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand7a829a82020-02-12 07:38:21 +01004389 if (!sctp_mid_s_) {
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07004390 return nullptr;
4391 }
Harald Alvestrand7a829a82020-02-12 07:38:21 +01004392 return transport_controller_->GetSctpTransport(*sctp_mid_s_);
Harald Alvestrandc85328f2019-02-28 07:51:00 +01004393}
4394
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004395const SessionDescriptionInterface* PeerConnection::local_description() const {
Karl Wiberg739506e2019-04-03 11:37:28 +02004396 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08004397 return pending_local_description_ ? pending_local_description_.get()
4398 : current_local_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004399}
4400
4401const SessionDescriptionInterface* PeerConnection::remote_description() const {
Karl Wiberg739506e2019-04-03 11:37:28 +02004402 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08004403 return pending_remote_description_ ? pending_remote_description_.get()
4404 : current_remote_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004405}
4406
deadbeeffe4a8a42016-12-20 17:56:17 -08004407const SessionDescriptionInterface* PeerConnection::current_local_description()
4408 const {
Karl Wiberg739506e2019-04-03 11:37:28 +02004409 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08004410 return current_local_description_.get();
deadbeeffe4a8a42016-12-20 17:56:17 -08004411}
4412
4413const SessionDescriptionInterface* PeerConnection::current_remote_description()
4414 const {
Karl Wiberg739506e2019-04-03 11:37:28 +02004415 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08004416 return current_remote_description_.get();
deadbeeffe4a8a42016-12-20 17:56:17 -08004417}
4418
4419const SessionDescriptionInterface* PeerConnection::pending_local_description()
4420 const {
Karl Wiberg739506e2019-04-03 11:37:28 +02004421 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08004422 return pending_local_description_.get();
deadbeeffe4a8a42016-12-20 17:56:17 -08004423}
4424
4425const SessionDescriptionInterface* PeerConnection::pending_remote_description()
4426 const {
Karl Wiberg739506e2019-04-03 11:37:28 +02004427 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08004428 return pending_remote_description_.get();
deadbeeffe4a8a42016-12-20 17:56:17 -08004429}
4430
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004431void PeerConnection::Close() {
Karl Wiberg744310f2019-02-14 10:18:56 +01004432 RTC_DCHECK_RUN_ON(signaling_thread());
Peter Boström1a9d6152015-12-08 22:15:17 +01004433 TRACE_EVENT0("webrtc", "PeerConnection::Close");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004434 // Update stats here so that we have the most recent stats for tracks and
4435 // streams before the channels are closed.
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00004436 stats_->UpdateStats(kStatsOutputLevelStandard);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004437
Steve Anton75737c02017-11-06 10:37:17 -08004438 ChangeSignalingState(PeerConnectionInterface::kClosed);
Harald Alvestrand8ebba742018-05-31 14:00:34 +02004439 NoteUsageEvent(UsageEvent::CLOSE_CALLED);
Steve Anton3fe1b152017-12-12 10:20:08 -08004440
Mirko Bonadei739baf02019-01-27 17:29:42 +01004441 for (const auto& transceiver : transceivers_) {
Steve Anton8af21862017-12-15 11:20:13 -08004442 transceiver->Stop();
4443 }
Steve Anton25cfeb92018-04-26 11:44:00 -07004444
4445 // Ensure that all asynchronous stats requests are completed before destroying
4446 // the transport controller below.
4447 if (stats_collector_) {
4448 stats_collector_->WaitForPendingRequest();
4449 }
4450
4451 // Don't destroy BaseChannels until after stats has been cleaned up so that
4452 // the last stats request can still read from the channels.
Steve Anton8af21862017-12-15 11:20:13 -08004453 DestroyAllChannels();
Steve Anton75737c02017-11-06 10:37:17 -08004454
Qingsi Wang93a84392018-01-30 17:13:09 -08004455 // The event log is used in the transport controller, which must be outlived
4456 // by the former. CreateOffer by the peer connection is implemented
4457 // asynchronously and if the peer connection is closed without resetting the
4458 // WebRTC session description factory, the session description factory would
4459 // call the transport controller.
4460 webrtc_session_desc_factory_.reset();
4461 transport_controller_.reset();
4462
deadbeef42a42632017-03-10 15:18:00 -08004463 network_thread()->Invoke<void>(
Yves Gerey665174f2018-06-19 15:03:05 +02004464 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
4465 port_allocator_.get()));
nisseeaabdf62017-05-05 02:23:02 -07004466
Steve Anton978b8762017-09-29 12:15:02 -07004467 worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
Karl Wibergb03ab712019-02-14 11:59:57 +01004468 RTC_DCHECK_RUN_ON(worker_thread());
eladalon248fd4f2017-09-06 05:18:15 -07004469 call_.reset();
4470 // The event log must outlive call (and any other object that uses it).
4471 event_log_.reset();
4472 });
Harald Alvestrand8ebba742018-05-31 14:00:34 +02004473 ReportUsagePattern();
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02004474 // The .h file says that observer can be discarded after close() returns.
4475 // Make sure this is true.
4476 observer_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004477}
4478
Steve Antonad182762018-09-05 20:22:40 +00004479void PeerConnection::OnMessage(rtc::Message* msg) {
Karl Wiberg744310f2019-02-14 10:18:56 +01004480 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Antonad182762018-09-05 20:22:40 +00004481 switch (msg->message_id) {
4482 case MSG_SET_SESSIONDESCRIPTION_SUCCESS: {
4483 SetSessionDescriptionMsg* param =
4484 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
4485 param->observer->OnSuccess();
4486 delete param;
4487 break;
4488 }
4489 case MSG_SET_SESSIONDESCRIPTION_FAILED: {
4490 SetSessionDescriptionMsg* param =
4491 static_cast<SetSessionDescriptionMsg*>(msg->pdata);
4492 param->observer->OnFailure(std::move(param->error));
4493 delete param;
4494 break;
4495 }
4496 case MSG_CREATE_SESSIONDESCRIPTION_FAILED: {
4497 CreateSessionDescriptionMsg* param =
4498 static_cast<CreateSessionDescriptionMsg*>(msg->pdata);
4499 param->observer->OnFailure(std::move(param->error));
4500 delete param;
4501 break;
4502 }
4503 case MSG_GETSTATS: {
4504 GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata);
4505 StatsReports reports;
4506 stats_->GetStats(param->track, &reports);
4507 param->observer->OnComplete(reports);
4508 delete param;
4509 break;
4510 }
Steve Antonad182762018-09-05 20:22:40 +00004511 case MSG_REPORT_USAGE_PATTERN: {
4512 ReportUsagePattern();
4513 break;
4514 }
4515 default:
4516 RTC_NOTREACHED() << "Not implemented";
4517 break;
4518 }
4519}
4520
Steve Antonafb0bb72018-02-20 11:35:37 -08004521cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const {
4522 RTC_DCHECK(!IsUnifiedPlan());
4523 auto* voice_channel = static_cast<cricket::VoiceChannel*>(
4524 GetAudioTransceiver()->internal()->channel());
4525 if (voice_channel) {
4526 return voice_channel->media_channel();
4527 } else {
4528 return nullptr;
4529 }
4530}
4531
4532cricket::VideoMediaChannel* PeerConnection::video_media_channel() const {
4533 RTC_DCHECK(!IsUnifiedPlan());
4534 auto* video_channel = static_cast<cricket::VideoChannel*>(
4535 GetVideoTransceiver()->internal()->channel());
4536 if (video_channel) {
4537 return video_channel->media_channel();
4538 } else {
4539 return nullptr;
4540 }
4541}
4542
Steve Anton4171afb2017-11-20 10:20:22 -08004543void PeerConnection::CreateAudioReceiver(
4544 MediaStreamInterface* stream,
4545 const RtpSenderInfo& remote_sender_info) {
Henrik Boström9e6fd2b2017-11-21 13:41:51 +01004546 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
4547 streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
Henrik Boström199e27b2018-07-04 20:51:53 +02004548 // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use
4549 // the constructor taking stream IDs instead.
Steve Antond3679212018-01-17 17:41:02 -08004550 auto* audio_receiver = new AudioRtpReceiver(
4551 worker_thread(), remote_sender_info.sender_id, streams);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08004552 audio_receiver->SetMediaChannel(voice_media_channel());
Saurav Das7262fc22019-09-11 16:23:05 -07004553 if (remote_sender_info.sender_id == kDefaultAudioSenderId) {
4554 audio_receiver->SetupUnsignaledMediaChannel();
4555 } else {
4556 audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc);
4557 }
Steve Antond3679212018-01-17 17:41:02 -08004558 auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
4559 signaling_thread(), audio_receiver);
Steve Anton4171afb2017-11-20 10:20:22 -08004560 GetAudioTransceiver()->internal()->AddReceiver(receiver);
Mirko Bonadei05cf6be2019-01-31 21:38:12 +01004561 Observer()->OnAddTrack(receiver, streams);
Harald Alvestrand8ebba742018-05-31 14:00:34 +02004562 NoteUsageEvent(UsageEvent::AUDIO_ADDED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004563}
4564
Steve Anton4171afb2017-11-20 10:20:22 -08004565void PeerConnection::CreateVideoReceiver(
4566 MediaStreamInterface* stream,
4567 const RtpSenderInfo& remote_sender_info) {
Henrik Boström9e6fd2b2017-11-21 13:41:51 +01004568 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
4569 streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
Henrik Boström199e27b2018-07-04 20:51:53 +02004570 // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use
4571 // the constructor taking stream IDs instead.
Steve Antond3679212018-01-17 17:41:02 -08004572 auto* video_receiver = new VideoRtpReceiver(
4573 worker_thread(), remote_sender_info.sender_id, streams);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08004574 video_receiver->SetMediaChannel(video_media_channel());
Saurav Das7262fc22019-09-11 16:23:05 -07004575 if (remote_sender_info.sender_id == kDefaultVideoSenderId) {
4576 video_receiver->SetupUnsignaledMediaChannel();
4577 } else {
4578 video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc);
4579 }
Steve Antond3679212018-01-17 17:41:02 -08004580 auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
4581 signaling_thread(), video_receiver);
Steve Anton4171afb2017-11-20 10:20:22 -08004582 GetVideoTransceiver()->internal()->AddReceiver(receiver);
Mirko Bonadei05cf6be2019-01-31 21:38:12 +01004583 Observer()->OnAddTrack(receiver, streams);
Harald Alvestrand8ebba742018-05-31 14:00:34 +02004584 NoteUsageEvent(UsageEvent::VIDEO_ADDED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004585}
4586
deadbeef70ab1a12015-09-28 16:53:55 -07004587// TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote
4588// description.
Henrik Boström933d8b02017-10-10 10:05:16 -07004589rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver(
Steve Anton4171afb2017-11-20 10:20:22 -08004590 const RtpSenderInfo& remote_sender_info) {
4591 auto receiver = FindReceiverById(remote_sender_info.sender_id);
4592 if (!receiver) {
4593 RTC_LOG(LS_WARNING) << "RtpReceiver for track with id "
4594 << remote_sender_info.sender_id << " doesn't exist.";
Henrik Boström933d8b02017-10-10 10:05:16 -07004595 return nullptr;
deadbeef70ab1a12015-09-28 16:53:55 -07004596 }
Steve Anton4171afb2017-11-20 10:20:22 -08004597 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
4598 GetAudioTransceiver()->internal()->RemoveReceiver(receiver);
4599 } else {
4600 GetVideoTransceiver()->internal()->RemoveReceiver(receiver);
4601 }
Henrik Boström933d8b02017-10-10 10:05:16 -07004602 return receiver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004603}
4604
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004605void PeerConnection::AddAudioTrack(AudioTrackInterface* track,
4606 MediaStreamInterface* stream) {
4607 RTC_DCHECK(!IsClosed());
Steve Anton111fdfd2018-06-25 13:03:36 -07004608 RTC_DCHECK(track);
4609 RTC_DCHECK(stream);
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004610 auto sender = FindSenderForTrack(track);
Steve Anton4171afb2017-11-20 10:20:22 -08004611 if (sender) {
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004612 // We already have a sender for this track, so just change the stream_id
4613 // so that it's correct in the next call to CreateOffer.
Henrik Andreasssoncc189172019-05-20 09:01:38 +00004614 sender->internal()->set_stream_ids({stream->id()});
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004615 return;
4616 }
4617
4618 // Normal case; we've never seen this track before.
Steve Anton111fdfd2018-06-25 13:03:36 -07004619 auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track,
Florent Castelli892acf02018-10-01 22:47:20 +02004620 {stream->id()}, {});
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08004621 new_sender->internal()->SetMediaChannel(voice_media_channel());
Steve Anton4171afb2017-11-20 10:20:22 -08004622 GetAudioTransceiver()->internal()->AddSender(new_sender);
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004623 // If the sender has already been configured in SDP, we call SetSsrc,
4624 // which will connect the sender to the underlying transport. This can
4625 // occur if a local session description that contains the ID of the sender
4626 // is set before AddStream is called. It can also occur if the local
4627 // session description is not changed and RemoveStream is called, and
4628 // later AddStream is called again with the same stream.
Steve Anton4171afb2017-11-20 10:20:22 -08004629 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609ea2018-03-27 21:57:18 +00004630 FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id());
Steve Anton4171afb2017-11-20 10:20:22 -08004631 if (sender_info) {
4632 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004633 }
4634}
4635
4636// TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around
4637// indefinitely, when we have unified plan SDP.
4638void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track,
4639 MediaStreamInterface* stream) {
4640 RTC_DCHECK(!IsClosed());
4641 auto sender = FindSenderForTrack(track);
Steve Anton4171afb2017-11-20 10:20:22 -08004642 if (!sender) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01004643 RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
4644 << " doesn't exist.";
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004645 return;
4646 }
Steve Anton4171afb2017-11-20 10:20:22 -08004647 GetAudioTransceiver()->internal()->RemoveSender(sender);
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004648}
4649
4650void PeerConnection::AddVideoTrack(VideoTrackInterface* track,
4651 MediaStreamInterface* stream) {
4652 RTC_DCHECK(!IsClosed());
Steve Anton111fdfd2018-06-25 13:03:36 -07004653 RTC_DCHECK(track);
4654 RTC_DCHECK(stream);
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004655 auto sender = FindSenderForTrack(track);
Steve Anton4171afb2017-11-20 10:20:22 -08004656 if (sender) {
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004657 // We already have a sender for this track, so just change the stream_id
4658 // so that it's correct in the next call to CreateOffer.
Henrik Andreasssoncc189172019-05-20 09:01:38 +00004659 sender->internal()->set_stream_ids({stream->id()});
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004660 return;
4661 }
4662
4663 // Normal case; we've never seen this track before.
Steve Anton111fdfd2018-06-25 13:03:36 -07004664 auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track,
Florent Castelli892acf02018-10-01 22:47:20 +02004665 {stream->id()}, {});
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08004666 new_sender->internal()->SetMediaChannel(video_media_channel());
Steve Anton4171afb2017-11-20 10:20:22 -08004667 GetVideoTransceiver()->internal()->AddSender(new_sender);
4668 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609ea2018-03-27 21:57:18 +00004669 FindSenderInfo(local_video_sender_infos_, stream->id(), track->id());
Steve Anton4171afb2017-11-20 10:20:22 -08004670 if (sender_info) {
4671 new_sender->internal()->SetSsrc(sender_info->first_ssrc);
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004672 }
4673}
4674
4675void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track,
4676 MediaStreamInterface* stream) {
4677 RTC_DCHECK(!IsClosed());
4678 auto sender = FindSenderForTrack(track);
Steve Anton4171afb2017-11-20 10:20:22 -08004679 if (!sender) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01004680 RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
4681 << " doesn't exist.";
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004682 return;
4683 }
Steve Anton4171afb2017-11-20 10:20:22 -08004684 GetVideoTransceiver()->internal()->RemoveSender(sender);
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004685}
4686
Steve Antonba818672017-11-06 10:21:57 -08004687void PeerConnection::SetIceConnectionState(IceConnectionState new_state) {
Steve Antonba818672017-11-06 10:21:57 -08004688 if (ice_connection_state_ == new_state) {
4689 return;
4690 }
4691
deadbeefcbecd352015-09-23 11:50:27 -07004692 // After transitioning to "closed", ignore any additional states from
Steve Antonba818672017-11-06 10:21:57 -08004693 // TransportController (such as "disconnected").
deadbeefab9b2d12015-10-14 11:33:11 -07004694 if (IsClosed()) {
deadbeefcbecd352015-09-23 11:50:27 -07004695 return;
4696 }
Steve Antonba818672017-11-06 10:21:57 -08004697
Mirko Bonadei675513b2017-11-09 11:09:25 +01004698 RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
4699 << " => " << new_state;
Steve Antonba818672017-11-06 10:21:57 -08004700 RTC_DCHECK(ice_connection_state_ !=
4701 PeerConnectionInterface::kIceConnectionClosed);
4702
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004703 ice_connection_state_ = new_state;
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02004704 Observer()->OnIceConnectionChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004705}
4706
Alex Loiko9289eda2018-11-23 16:18:59 +00004707void PeerConnection::SetStandardizedIceConnectionState(
4708 PeerConnectionInterface::IceConnectionState new_state) {
Qingsi Wang36e31472019-05-29 11:37:26 -07004709 if (standardized_ice_connection_state_ == new_state) {
Alex Loiko9289eda2018-11-23 16:18:59 +00004710 return;
Qingsi Wang36e31472019-05-29 11:37:26 -07004711 }
4712
4713 if (IsClosed()) {
Alex Loiko9289eda2018-11-23 16:18:59 +00004714 return;
Qingsi Wang36e31472019-05-29 11:37:26 -07004715 }
4716
4717 RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState "
4718 << standardized_ice_connection_state_ << " => " << new_state;
4719
Alex Loiko9289eda2018-11-23 16:18:59 +00004720 standardized_ice_connection_state_ = new_state;
Jonas Olsson12046902018-12-06 11:25:14 +01004721 Observer()->OnStandardizedIceConnectionChange(new_state);
Alex Loiko9289eda2018-11-23 16:18:59 +00004722}
4723
Jonas Olsson635474e2018-10-18 15:58:17 +02004724void PeerConnection::SetConnectionState(
4725 PeerConnectionInterface::PeerConnectionState new_state) {
Jonas Olsson635474e2018-10-18 15:58:17 +02004726 if (connection_state_ == new_state)
4727 return;
4728 if (IsClosed())
4729 return;
4730 connection_state_ = new_state;
4731 Observer()->OnConnectionChange(new_state);
4732}
4733
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004734void PeerConnection::OnIceGatheringChange(
4735 PeerConnectionInterface::IceGatheringState new_state) {
4736 if (IsClosed()) {
4737 return;
4738 }
4739 ice_gathering_state_ = new_state;
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02004740 Observer()->OnIceGatheringChange(ice_gathering_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004741}
4742
jbauch81bf7b02017-03-25 08:31:12 -07004743void PeerConnection::OnIceCandidate(
4744 std::unique_ptr<IceCandidateInterface> candidate) {
zhihuang29ff8442016-07-27 11:07:25 -07004745 if (IsClosed()) {
4746 return;
4747 }
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07004748 ReportIceCandidateCollected(candidate->candidate());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02004749 Observer()->OnIceCandidate(candidate.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004750}
4751
Eldar Rello0095d372019-12-02 22:22:07 +02004752void PeerConnection::OnIceCandidateError(const std::string& address,
4753 int port,
Eldar Relloda13ea22019-06-01 12:23:43 +03004754 const std::string& url,
4755 int error_code,
4756 const std::string& error_text) {
4757 if (IsClosed()) {
4758 return;
4759 }
Eldar Rello0095d372019-12-02 22:22:07 +02004760 Observer()->OnIceCandidateError(address, port, url, error_code, error_text);
4761 // Leftover not to break wpt test during migration to the new API.
4762 Observer()->OnIceCandidateError(address + ":", url, error_code, error_text);
Eldar Relloda13ea22019-06-01 12:23:43 +03004763}
4764
Honghai Zhang7fb69db2016-03-14 11:59:18 -07004765void PeerConnection::OnIceCandidatesRemoved(
4766 const std::vector<cricket::Candidate>& candidates) {
zhihuang29ff8442016-07-27 11:07:25 -07004767 if (IsClosed()) {
4768 return;
4769 }
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02004770 Observer()->OnIceCandidatesRemoved(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07004771}
4772
Alex Drake00c7ecf2019-08-06 10:54:47 -07004773void PeerConnection::OnSelectedCandidatePairChanged(
4774 const cricket::CandidatePairChangeEvent& event) {
4775 if (IsClosed()) {
4776 return;
4777 }
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07004778
Qingsi Wangcc46b102019-09-12 11:19:01 -07004779 if (event.selected_candidate_pair.local_candidate().type() ==
4780 LOCAL_PORT_TYPE &&
4781 event.selected_candidate_pair.remote_candidate().type() ==
4782 LOCAL_PORT_TYPE) {
4783 NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED);
4784 }
4785
Alex Drake00c7ecf2019-08-06 10:54:47 -07004786 Observer()->OnIceSelectedCandidatePairChanged(event);
4787}
4788
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004789void PeerConnection::ChangeSignalingState(
4790 PeerConnectionInterface::SignalingState signaling_state) {
Steve Antonba818672017-11-06 10:21:57 -08004791 if (signaling_state_ == signaling_state) {
4792 return;
4793 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004794 RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: "
4795 << GetSignalingStateString(signaling_state_)
4796 << " New state: "
4797 << GetSignalingStateString(signaling_state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004798 signaling_state_ = signaling_state;
4799 if (signaling_state == kClosed) {
4800 ice_connection_state_ = kIceConnectionClosed;
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02004801 Observer()->OnIceConnectionChange(ice_connection_state_);
Alex Loiko9289eda2018-11-23 16:18:59 +00004802 standardized_ice_connection_state_ =
4803 PeerConnectionInterface::IceConnectionState::kIceConnectionClosed;
Jonas Olsson635474e2018-10-18 15:58:17 +02004804 connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed;
4805 Observer()->OnConnectionChange(connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004806 }
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02004807 Observer()->OnSignalingChange(signaling_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004808}
4809
deadbeefeb459812015-12-15 19:24:43 -08004810void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track,
4811 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 11:07:25 -07004812 if (IsClosed()) {
4813 return;
4814 }
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004815 AddAudioTrack(track, stream);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02004816 UpdateNegotiationNeeded();
deadbeefeb459812015-12-15 19:24:43 -08004817}
4818
deadbeefeb459812015-12-15 19:24:43 -08004819void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track,
4820 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 11:07:25 -07004821 if (IsClosed()) {
4822 return;
4823 }
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004824 RemoveAudioTrack(track, stream);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02004825 UpdateNegotiationNeeded();
deadbeefeb459812015-12-15 19:24:43 -08004826}
4827
4828void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track,
4829 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 11:07:25 -07004830 if (IsClosed()) {
4831 return;
4832 }
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004833 AddVideoTrack(track, stream);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02004834 UpdateNegotiationNeeded();
deadbeefeb459812015-12-15 19:24:43 -08004835}
4836
4837void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track,
4838 MediaStreamInterface* stream) {
zhihuang29ff8442016-07-27 11:07:25 -07004839 if (IsClosed()) {
4840 return;
4841 }
korniltsev.anatolyec390b52017-07-24 17:00:25 -07004842 RemoveVideoTrack(track, stream);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02004843 UpdateNegotiationNeeded();
deadbeefeb459812015-12-15 19:24:43 -08004844}
4845
Henrik Boström31638672017-11-23 17:48:32 +01004846void PeerConnection::PostSetSessionDescriptionSuccess(
4847 SetSessionDescriptionObserver* observer) {
Steve Antonad182762018-09-05 20:22:40 +00004848 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
4849 signaling_thread()->Post(RTC_FROM_HERE, this,
4850 MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
Henrik Boström31638672017-11-23 17:48:32 +01004851}
4852
deadbeefab9b2d12015-10-14 11:33:11 -07004853void PeerConnection::PostSetSessionDescriptionFailure(
4854 SetSessionDescriptionObserver* observer,
Steve Antonad182762018-09-05 20:22:40 +00004855 RTCError&& error) {
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01004856 RTC_DCHECK(!error.ok());
Steve Antonad182762018-09-05 20:22:40 +00004857 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
4858 msg->error = std::move(error);
4859 signaling_thread()->Post(RTC_FROM_HERE, this,
4860 MSG_SET_SESSIONDESCRIPTION_FAILED, msg);
deadbeefab9b2d12015-10-14 11:33:11 -07004861}
4862
4863void PeerConnection::PostCreateSessionDescriptionFailure(
4864 CreateSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01004865 RTCError error) {
4866 RTC_DCHECK(!error.ok());
Steve Antonad182762018-09-05 20:22:40 +00004867 CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer);
4868 msg->error = std::move(error);
4869 signaling_thread()->Post(RTC_FROM_HERE, this,
4870 MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg);
deadbeefab9b2d12015-10-14 11:33:11 -07004871}
4872
zhihuang1c378ed2017-08-17 14:10:50 -07004873void PeerConnection::GetOptionsForOffer(
Steve Antondcc3c022017-12-22 16:02:54 -08004874 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
deadbeefab9b2d12015-10-14 11:33:11 -07004875 cricket::MediaSessionOptions* session_options) {
Steve Antondcc3c022017-12-22 16:02:54 -08004876 ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
zhihuang1c378ed2017-08-17 14:10:50 -07004877
Steve Antondcc3c022017-12-22 16:02:54 -08004878 if (IsUnifiedPlan()) {
4879 GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options);
4880 } else {
4881 GetOptionsForPlanBOffer(offer_answer_options, session_options);
4882 }
4883
Steve Antonfa2260d2017-12-28 16:38:23 -08004884 // Intentionally unset the data channel type for RTP data channel with the
4885 // second condition. Otherwise the RTP data channels would be successfully
4886 // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail
4887 // when building with chromium. We want to leave RTP data channels broken, so
4888 // people won't try to use them.
Harald Alvestrand05e4d082019-12-03 14:04:21 +01004889 if (data_channel_controller_.HasRtpDataChannels() ||
4890 data_channel_type() != cricket::DCT_RTP) {
Steve Antonfa2260d2017-12-28 16:38:23 -08004891 session_options->data_channel_type = data_channel_type();
4892 }
4893
Steve Antondcc3c022017-12-22 16:02:54 -08004894 // Apply ICE restart flag and renomination flag.
Henrik Boström79b69802019-07-18 11:16:56 +02004895 bool ice_restart = offer_answer_options.ice_restart ||
4896 local_ice_credentials_to_replace_->HasIceCredentials();
Steve Antondcc3c022017-12-22 16:02:54 -08004897 for (auto& options : session_options->media_description_options) {
Henrik Boström79b69802019-07-18 11:16:56 +02004898 options.transport_options.ice_restart = ice_restart;
Steve Antondcc3c022017-12-22 16:02:54 -08004899 options.transport_options.enable_ice_renomination =
4900 configuration_.enable_ice_renomination;
4901 }
4902
4903 session_options->rtcp_cname = rtcp_cname_;
Benjamin Wright8c27cca2018-10-25 10:16:44 -07004904 session_options->crypto_options = GetCryptoOptions();
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02004905 session_options->pooled_ice_credentials =
4906 network_thread()->Invoke<std::vector<cricket::IceParameters>>(
4907 RTC_FROM_HERE,
4908 rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials,
4909 port_allocator_.get()));
Johannes Kron89f874e2018-11-12 10:25:48 +01004910 session_options->offer_extmap_allow_mixed =
4911 configuration_.offer_extmap_allow_mixed;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08004912
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -07004913 // If datagram transport is in use, add opaque transport parameters.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07004914 if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) {
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -07004915 for (auto& options : session_options->media_description_options) {
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -07004916 absl::optional<cricket::OpaqueTransportParameters> params =
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -07004917 transport_controller_->GetTransportParameters(options.mid);
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -07004918 if (!params) {
4919 continue;
4920 }
4921 options.transport_options.opaque_parameters = params;
4922 if ((use_datagram_transport_ &&
4923 (options.type == cricket::MEDIA_TYPE_AUDIO ||
4924 options.type == cricket::MEDIA_TYPE_VIDEO)) ||
4925 (use_datagram_transport_for_data_channels_ &&
4926 options.type == cricket::MEDIA_TYPE_DATA)) {
4927 options.alt_protocol = params->protocol;
4928 }
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -07004929 }
4930 }
4931
Harald Alvestrand4aa11922019-05-14 22:00:01 +02004932 // Allow fallback for using obsolete SCTP syntax.
4933 // Note that the default in |session_options| is true, while
4934 // the default in |options| is false.
4935 session_options->use_obsolete_sctp_sdp =
4936 offer_answer_options.use_obsolete_sctp_sdp;
Steve Antondcc3c022017-12-22 16:02:54 -08004937}
4938
4939void PeerConnection::GetOptionsForPlanBOffer(
4940 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
4941 cricket::MediaSessionOptions* session_options) {
zhihuang1c378ed2017-08-17 14:10:50 -07004942 // Figure out transceiver directional preferences.
4943 bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO);
4944 bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO);
4945
4946 // By default, generate sendrecv/recvonly m= sections.
4947 bool recv_audio = true;
4948 bool recv_video = true;
4949
4950 // By default, only offer a new m= section if we have media to send with it.
4951 bool offer_new_audio_description = send_audio;
4952 bool offer_new_video_description = send_video;
Harald Alvestrand05e4d082019-12-03 14:04:21 +01004953 bool offer_new_data_description = data_channel_controller_.HasDataChannels();
zhihuang1c378ed2017-08-17 14:10:50 -07004954
4955 // The "offer_to_receive_X" options allow those defaults to be overridden.
Steve Antondcc3c022017-12-22 16:02:54 -08004956 if (offer_answer_options.offer_to_receive_audio !=
4957 RTCOfferAnswerOptions::kUndefined) {
4958 recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
zhihuang1c378ed2017-08-17 14:10:50 -07004959 offer_new_audio_description =
Steve Antondcc3c022017-12-22 16:02:54 -08004960 offer_new_audio_description ||
4961 (offer_answer_options.offer_to_receive_audio > 0);
zhihuang1c378ed2017-08-17 14:10:50 -07004962 }
Steve Antondcc3c022017-12-22 16:02:54 -08004963 if (offer_answer_options.offer_to_receive_video !=
4964 RTCOfferAnswerOptions::kUndefined) {
4965 recv_video = (offer_answer_options.offer_to_receive_video > 0);
zhihuang1c378ed2017-08-17 14:10:50 -07004966 offer_new_video_description =
Steve Antondcc3c022017-12-22 16:02:54 -08004967 offer_new_video_description ||
4968 (offer_answer_options.offer_to_receive_video > 0);
zhihuang1c378ed2017-08-17 14:10:50 -07004969 }
4970
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02004971 absl::optional<size_t> audio_index;
4972 absl::optional<size_t> video_index;
4973 absl::optional<size_t> data_index;
zhihuang1c378ed2017-08-17 14:10:50 -07004974 // If a current description exists, generate m= sections in the same order,
4975 // using the first audio/video/data section that appears and rejecting
4976 // extraneous ones.
Steve Anton75737c02017-11-06 10:37:17 -08004977 if (local_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -07004978 GenerateMediaDescriptionOptions(
Steve Anton75737c02017-11-06 10:37:17 -08004979 local_description(),
Steve Anton1d03a752017-11-27 14:30:09 -08004980 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
4981 RtpTransceiverDirectionFromSendRecv(send_video, recv_video),
4982 &audio_index, &video_index, &data_index, session_options);
deadbeefab9b2d12015-10-14 11:33:11 -07004983 }
4984
zhihuang1c378ed2017-08-17 14:10:50 -07004985 // Add audio/video/data m= sections to the end if needed.
4986 if (!audio_index && offer_new_audio_description) {
4987 session_options->media_description_options.push_back(
4988 cricket::MediaDescriptionOptions(
4989 cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO,
Steve Anton1d03a752017-11-27 14:30:09 -08004990 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
4991 false));
Markus Handell0357b3e2020-03-16 13:40:51 +01004992
Oskar Sundbom9b28a032017-11-16 10:53:30 +01004993 audio_index = session_options->media_description_options.size() - 1;
deadbeefc80741f2015-10-22 13:14:45 -07004994 }
zhihuang1c378ed2017-08-17 14:10:50 -07004995 if (!video_index && offer_new_video_description) {
4996 session_options->media_description_options.push_back(
4997 cricket::MediaDescriptionOptions(
4998 cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO,
Steve Anton1d03a752017-11-27 14:30:09 -08004999 RtpTransceiverDirectionFromSendRecv(send_video, recv_video),
5000 false));
Markus Handell0357b3e2020-03-16 13:40:51 +01005001
Oskar Sundbom9b28a032017-11-16 10:53:30 +01005002 video_index = session_options->media_description_options.size() - 1;
deadbeefc80741f2015-10-22 13:14:45 -07005003 }
zhihuang1c378ed2017-08-17 14:10:50 -07005004 if (!data_index && offer_new_data_description) {
5005 session_options->media_description_options.push_back(
Steve Antonfa2260d2017-12-28 16:38:23 -08005006 GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA));
Oskar Sundbom9b28a032017-11-16 10:53:30 +01005007 data_index = session_options->media_description_options.size() - 1;
zhihuang1c378ed2017-08-17 14:10:50 -07005008 }
5009
5010 cricket::MediaDescriptionOptions* audio_media_description_options =
5011 !audio_index ? nullptr
5012 : &session_options->media_description_options[*audio_index];
5013 cricket::MediaDescriptionOptions* video_media_description_options =
5014 !video_index ? nullptr
5015 : &session_options->media_description_options[*video_index];
zhihuang1c378ed2017-08-17 14:10:50 -07005016
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08005017 AddPlanBRtpSenderOptions(GetSendersInternal(),
5018 audio_media_description_options,
5019 video_media_description_options,
5020 offer_answer_options.num_simulcast_layers);
Steve Antondcc3c022017-12-22 16:02:54 -08005021}
5022
Steve Antondcc3c022017-12-22 16:02:54 -08005023static cricket::MediaDescriptionOptions
5024GetMediaDescriptionOptionsForTransceiver(
5025 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
5026 transceiver,
5027 const std::string& mid) {
5028 cricket::MediaDescriptionOptions media_description_options(
Steve Anton69470252018-02-09 11:43:08 -08005029 transceiver->media_type(), mid, transceiver->direction(),
Steve Antondcc3c022017-12-22 16:02:54 -08005030 transceiver->stopped());
Florent Castelli2d9d82e2019-04-23 19:25:51 +02005031 media_description_options.codec_preferences =
5032 transceiver->codec_preferences();
Steve Anton5f94aa22018-02-01 10:58:30 -08005033 // This behavior is specified in JSEP. The gist is that:
5034 // 1. The MSID is included if the RtpTransceiver's direction is sendonly or
5035 // sendrecv.
5036 // 2. If the MSID is included, then it must be included in any subsequent
5037 // offer/answer exactly the same until the RtpTransceiver is stopped.
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08005038 if (transceiver->stopped() ||
5039 (!RtpTransceiverDirectionHasSend(transceiver->direction()) &&
5040 !transceiver->internal()->has_ever_been_used_to_send())) {
5041 return media_description_options;
Steve Anton5f94aa22018-02-01 10:58:30 -08005042 }
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08005043
5044 cricket::SenderOptions sender_options;
5045 sender_options.track_id = transceiver->sender()->id();
5046 sender_options.stream_ids = transceiver->sender()->stream_ids();
5047
5048 // The following sets up RIDs and Simulcast.
5049 // RIDs are included if Simulcast is requested or if any RID was specified.
Amit Hilbuch619b2942019-02-26 15:55:19 -08005050 RtpParameters send_parameters =
5051 transceiver->internal()->sender_internal()->GetParametersInternal();
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08005052 bool has_rids = std::any_of(send_parameters.encodings.begin(),
5053 send_parameters.encodings.end(),
5054 [](const RtpEncodingParameters& encoding) {
5055 return !encoding.rid.empty();
5056 });
5057
Amit Hilbuchb7446ed2019-01-28 12:25:25 -08005058 std::vector<RidDescription> send_rids;
5059 SimulcastLayerList send_layers;
5060 for (const RtpEncodingParameters& encoding : send_parameters.encodings) {
5061 if (encoding.rid.empty()) {
5062 continue;
5063 }
5064 send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend));
5065 send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active));
5066 }
5067
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08005068 if (has_rids) {
5069 sender_options.rids = send_rids;
5070 }
5071
5072 sender_options.simulcast_layers = send_layers;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08005073 // When RIDs are configured, we must set num_sim_layers to 0 to.
5074 // Otherwise, num_sim_layers must be 1 because either there is no
5075 // simulcast, or simulcast is acheived by munging the SDP.
5076 sender_options.num_sim_layers = has_rids ? 0 : 1;
5077 media_description_options.sender_options.push_back(sender_options);
5078
Steve Antondcc3c022017-12-22 16:02:54 -08005079 return media_description_options;
5080}
5081
Steve Anton5c72e712018-12-10 14:25:30 -08005082// Returns the ContentInfo at mline index |i|, or null if none exists.
5083static const ContentInfo* GetContentByIndex(
5084 const SessionDescriptionInterface* sdesc,
5085 size_t i) {
5086 if (!sdesc) {
5087 return nullptr;
5088 }
5089 const ContentInfos& contents = sdesc->description()->contents();
5090 return (i < contents.size() ? &contents[i] : nullptr);
5091}
5092
Steve Antondcc3c022017-12-22 16:02:54 -08005093void PeerConnection::GetOptionsForUnifiedPlanOffer(
5094 const RTCOfferAnswerOptions& offer_answer_options,
5095 cricket::MediaSessionOptions* session_options) {
5096 // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial
5097 // Offers) and 5.2.2 (Subsequent Offers).
5098 RTC_DCHECK_EQ(session_options->media_description_options.size(), 0);
Harald Alvestrand1716d392019-06-03 20:35:45 +02005099 const ContentInfos no_infos;
Steve Antondcc3c022017-12-22 16:02:54 -08005100 const ContentInfos& local_contents =
5101 (local_description() ? local_description()->description()->contents()
Harald Alvestrand1716d392019-06-03 20:35:45 +02005102 : no_infos);
Steve Antondcc3c022017-12-22 16:02:54 -08005103 const ContentInfos& remote_contents =
5104 (remote_description() ? remote_description()->description()->contents()
Harald Alvestrand1716d392019-06-03 20:35:45 +02005105 : no_infos);
Steve Antondcc3c022017-12-22 16:02:54 -08005106 // The mline indices that can be recycled. New transceivers should reuse these
5107 // slots first.
5108 std::queue<size_t> recycleable_mline_indices;
Steve Antondcc3c022017-12-22 16:02:54 -08005109 // First, go through each media section that exists in either the local or
5110 // remote description and generate a media section in this offer for the
5111 // associated transceiver. If a media section can be recycled, generate a
5112 // default, rejected media section here that can be later overwritten.
5113 for (size_t i = 0;
5114 i < std::max(local_contents.size(), remote_contents.size()); ++i) {
5115 // Either |local_content| or |remote_content| is non-null.
5116 const ContentInfo* local_content =
5117 (i < local_contents.size() ? &local_contents[i] : nullptr);
Steve Anton5c72e712018-12-10 14:25:30 -08005118 const ContentInfo* current_local_content =
5119 GetContentByIndex(current_local_description(), i);
Steve Antondcc3c022017-12-22 16:02:54 -08005120 const ContentInfo* remote_content =
5121 (i < remote_contents.size() ? &remote_contents[i] : nullptr);
Steve Anton5c72e712018-12-10 14:25:30 -08005122 const ContentInfo* current_remote_content =
5123 GetContentByIndex(current_remote_description(), i);
5124 bool had_been_rejected =
5125 (current_local_content && current_local_content->rejected) ||
5126 (current_remote_content && current_remote_content->rejected);
Steve Antondcc3c022017-12-22 16:02:54 -08005127 const std::string& mid =
5128 (local_content ? local_content->name : remote_content->name);
5129 cricket::MediaType media_type =
5130 (local_content ? local_content->media_description()->type()
5131 : remote_content->media_description()->type());
5132 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
5133 media_type == cricket::MEDIA_TYPE_VIDEO) {
5134 auto transceiver = GetAssociatedTransceiver(mid);
5135 RTC_CHECK(transceiver);
5136 // A media section is considered eligible for recycling if it is marked as
Steve Anton5c72e712018-12-10 14:25:30 -08005137 // rejected in either the current local or current remote description.
Seth Hampsonae8a90a2018-02-13 15:33:48 -08005138 if (had_been_rejected && transceiver->stopped()) {
Steve Antondcc3c022017-12-22 16:02:54 -08005139 session_options->media_description_options.push_back(
Steve Anton69470252018-02-09 11:43:08 -08005140 cricket::MediaDescriptionOptions(transceiver->media_type(), mid,
5141 RtpTransceiverDirection::kInactive,
5142 /*stopped=*/true));
Steve Antondcc3c022017-12-22 16:02:54 -08005143 recycleable_mline_indices.push(i);
5144 } else {
5145 session_options->media_description_options.push_back(
5146 GetMediaDescriptionOptionsForTransceiver(transceiver, mid));
5147 // CreateOffer shouldn't really cause any state changes in
5148 // PeerConnection, but we need a way to match new transceivers to new
5149 // media sections in SetLocalDescription and JSEP specifies this is done
5150 // by recording the index of the media section generated for the
5151 // transceiver in the offer.
5152 transceiver->internal()->set_mline_index(i);
5153 }
5154 } else {
5155 RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
Steve Antonc8ff1602020-02-05 13:53:38 -08005156 if (had_been_rejected) {
Steve Antonfa2260d2017-12-28 16:38:23 -08005157 session_options->media_description_options.push_back(
5158 GetMediaDescriptionOptionsForRejectedData(mid));
5159 } else {
Steve Antonc8ff1602020-02-05 13:53:38 -08005160 RTC_CHECK(GetDataMid());
5161 if (mid == *GetDataMid()) {
5162 session_options->media_description_options.push_back(
5163 GetMediaDescriptionOptionsForActiveData(mid));
5164 } else {
5165 session_options->media_description_options.push_back(
5166 GetMediaDescriptionOptionsForRejectedData(mid));
5167 }
Steve Antonfa2260d2017-12-28 16:38:23 -08005168 }
Steve Antondcc3c022017-12-22 16:02:54 -08005169 }
5170 }
Amit Hilbuchae3df542019-01-07 12:13:08 -08005171
Steve Antondcc3c022017-12-22 16:02:54 -08005172 // Next, look for transceivers that are newly added (that is, are not stopped
5173 // and not associated). Reuse media sections marked as recyclable first,
5174 // otherwise append to the end of the offer. New media sections should be
5175 // added in the order they were added to the PeerConnection.
Mirko Bonadei739baf02019-01-27 17:29:42 +01005176 for (const auto& transceiver : transceivers_) {
Steve Antondcc3c022017-12-22 16:02:54 -08005177 if (transceiver->mid() || transceiver->stopped()) {
5178 continue;
5179 }
5180 size_t mline_index;
5181 if (!recycleable_mline_indices.empty()) {
5182 mline_index = recycleable_mline_indices.front();
5183 recycleable_mline_indices.pop();
5184 session_options->media_description_options[mline_index] =
5185 GetMediaDescriptionOptionsForTransceiver(transceiver,
Amit Hilbuchae3df542019-01-07 12:13:08 -08005186 mid_generator_());
Steve Antondcc3c022017-12-22 16:02:54 -08005187 } else {
5188 mline_index = session_options->media_description_options.size();
5189 session_options->media_description_options.push_back(
5190 GetMediaDescriptionOptionsForTransceiver(transceiver,
Amit Hilbuchae3df542019-01-07 12:13:08 -08005191 mid_generator_()));
Steve Antondcc3c022017-12-22 16:02:54 -08005192 }
5193 // See comment above for why CreateOffer changes the transceiver's state.
5194 transceiver->internal()->set_mline_index(mline_index);
5195 }
Steve Antonfa2260d2017-12-28 16:38:23 -08005196 // Lastly, add a m-section if we have local data channels and an m section
5197 // does not already exist.
Harald Alvestrand05e4d082019-12-03 14:04:21 +01005198 if (!GetDataMid() && data_channel_controller_.HasDataChannels()) {
Steve Antonfa2260d2017-12-28 16:38:23 -08005199 session_options->media_description_options.push_back(
Amit Hilbuchae3df542019-01-07 12:13:08 -08005200 GetMediaDescriptionOptionsForActiveData(mid_generator_()));
Steve Antonfa2260d2017-12-28 16:38:23 -08005201 }
Steve Antondcc3c022017-12-22 16:02:54 -08005202}
5203
5204void PeerConnection::GetOptionsForAnswer(
5205 const RTCOfferAnswerOptions& offer_answer_options,
5206 cricket::MediaSessionOptions* session_options) {
5207 ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
5208
5209 if (IsUnifiedPlan()) {
5210 GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options);
5211 } else {
5212 GetOptionsForPlanBAnswer(offer_answer_options, session_options);
5213 }
5214
Steve Antonfa2260d2017-12-28 16:38:23 -08005215 // Intentionally unset the data channel type for RTP data channel. Otherwise
5216 // the RTP data channels would be successfully negotiated by default and the
5217 // unit tests in WebRtcDataBrowserTest will fail when building with chromium.
5218 // We want to leave RTP data channels broken, so people won't try to use them.
Harald Alvestrand05e4d082019-12-03 14:04:21 +01005219 if (data_channel_controller_.HasRtpDataChannels() ||
5220 data_channel_type() != cricket::DCT_RTP) {
Steve Antonfa2260d2017-12-28 16:38:23 -08005221 session_options->data_channel_type = data_channel_type();
5222 }
5223
Steve Antondcc3c022017-12-22 16:02:54 -08005224 // Apply ICE renomination flag.
5225 for (auto& options : session_options->media_description_options) {
5226 options.transport_options.enable_ice_renomination =
5227 configuration_.enable_ice_renomination;
5228 }
zhihuang8f65cdf2016-05-06 18:40:30 -07005229
5230 session_options->rtcp_cname = rtcp_cname_;
Benjamin Wright8c27cca2018-10-25 10:16:44 -07005231 session_options->crypto_options = GetCryptoOptions();
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02005232 session_options->pooled_ice_credentials =
5233 network_thread()->Invoke<std::vector<cricket::IceParameters>>(
5234 RTC_FROM_HERE,
5235 rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials,
5236 port_allocator_.get()));
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -07005237
5238 // If datagram transport is in use, add opaque transport parameters.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07005239 if (use_datagram_transport_ || use_datagram_transport_for_data_channels_) {
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -07005240 for (auto& options : session_options->media_description_options) {
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -07005241 absl::optional<cricket::OpaqueTransportParameters> params =
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -07005242 transport_controller_->GetTransportParameters(options.mid);
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -07005243 if (!params) {
5244 continue;
5245 }
5246 options.transport_options.opaque_parameters = params;
5247 if ((use_datagram_transport_ &&
5248 (options.type == cricket::MEDIA_TYPE_AUDIO ||
5249 options.type == cricket::MEDIA_TYPE_VIDEO)) ||
5250 (use_datagram_transport_for_data_channels_ &&
5251 options.type == cricket::MEDIA_TYPE_DATA)) {
5252 options.alt_protocol = params->protocol;
5253 }
Bjorn A Mellemc85ebbe2019-06-07 10:28:06 -07005254 }
5255 }
deadbeefab9b2d12015-10-14 11:33:11 -07005256}
5257
Steve Antondcc3c022017-12-22 16:02:54 -08005258void PeerConnection::GetOptionsForPlanBAnswer(
5259 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07005260 cricket::MediaSessionOptions* session_options) {
zhihuang1c378ed2017-08-17 14:10:50 -07005261 // Figure out transceiver directional preferences.
5262 bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO);
5263 bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO);
5264
5265 // By default, generate sendrecv/recvonly m= sections. The direction is also
5266 // restricted by the direction in the offer.
5267 bool recv_audio = true;
5268 bool recv_video = true;
5269
5270 // The "offer_to_receive_X" options allow those defaults to be overridden.
Steve Antondcc3c022017-12-22 16:02:54 -08005271 if (offer_answer_options.offer_to_receive_audio !=
5272 RTCOfferAnswerOptions::kUndefined) {
5273 recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
deadbeef0ed85b22016-02-23 17:24:52 -08005274 }
Steve Antondcc3c022017-12-22 16:02:54 -08005275 if (offer_answer_options.offer_to_receive_video !=
5276 RTCOfferAnswerOptions::kUndefined) {
5277 recv_video = (offer_answer_options.offer_to_receive_video > 0);
zhihuang1c378ed2017-08-17 14:10:50 -07005278 }
5279
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02005280 absl::optional<size_t> audio_index;
5281 absl::optional<size_t> video_index;
5282 absl::optional<size_t> data_index;
Steve Antondffead82018-02-06 10:31:29 -08005283
5284 // Generate m= sections that match those in the offer.
5285 // Note that mediasession.cc will handle intersection our preferred
5286 // direction with the offered direction.
5287 GenerateMediaDescriptionOptions(
5288 remote_description(),
5289 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
5290 RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index,
5291 &video_index, &data_index, session_options);
zhihuang1c378ed2017-08-17 14:10:50 -07005292
5293 cricket::MediaDescriptionOptions* audio_media_description_options =
5294 !audio_index ? nullptr
5295 : &session_options->media_description_options[*audio_index];
5296 cricket::MediaDescriptionOptions* video_media_description_options =
5297 !video_index ? nullptr
5298 : &session_options->media_description_options[*video_index];
zhihuang1c378ed2017-08-17 14:10:50 -07005299
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08005300 AddPlanBRtpSenderOptions(GetSendersInternal(),
5301 audio_media_description_options,
5302 video_media_description_options,
5303 offer_answer_options.num_simulcast_layers);
Steve Antondcc3c022017-12-22 16:02:54 -08005304}
zhihuangaf388472016-11-02 16:49:48 -07005305
Steve Antondcc3c022017-12-22 16:02:54 -08005306void PeerConnection::GetOptionsForUnifiedPlanAnswer(
5307 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
5308 cricket::MediaSessionOptions* session_options) {
5309 // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial
5310 // Answers) and 5.3.2 (Subsequent Answers).
5311 RTC_DCHECK(remote_description());
5312 RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer);
5313 for (const ContentInfo& content :
5314 remote_description()->description()->contents()) {
5315 cricket::MediaType media_type = content.media_description()->type();
5316 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
5317 media_type == cricket::MEDIA_TYPE_VIDEO) {
5318 auto transceiver = GetAssociatedTransceiver(content.name);
5319 RTC_CHECK(transceiver);
5320 session_options->media_description_options.push_back(
5321 GetMediaDescriptionOptionsForTransceiver(transceiver, content.name));
5322 } else {
5323 RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
Steve Antondbf9d032018-01-19 15:23:40 -08005324 // Reject all data sections if data channels are disabled.
5325 // Reject a data section if it has already been rejected.
5326 // Reject all data sections except for the first one.
Harald Alvestrand05e4d082019-12-03 14:04:21 +01005327 if (data_channel_type() == cricket::DCT_NONE || content.rejected ||
Steve Antondbf9d032018-01-19 15:23:40 -08005328 content.name != *GetDataMid()) {
Steve Antonfa2260d2017-12-28 16:38:23 -08005329 session_options->media_description_options.push_back(
5330 GetMediaDescriptionOptionsForRejectedData(content.name));
5331 } else {
5332 session_options->media_description_options.push_back(
5333 GetMediaDescriptionOptionsForActiveData(content.name));
5334 }
Steve Antondcc3c022017-12-22 16:02:54 -08005335 }
5336 }
htaa2a49d92016-03-04 02:51:39 -08005337}
5338
zhihuang1c378ed2017-08-17 14:10:50 -07005339void PeerConnection::GenerateMediaDescriptionOptions(
5340 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 14:30:09 -08005341 RtpTransceiverDirection audio_direction,
5342 RtpTransceiverDirection video_direction,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02005343 absl::optional<size_t>* audio_index,
5344 absl::optional<size_t>* video_index,
5345 absl::optional<size_t>* data_index,
htaa2a49d92016-03-04 02:51:39 -08005346 cricket::MediaSessionOptions* session_options) {
zhihuang1c378ed2017-08-17 14:10:50 -07005347 for (const cricket::ContentInfo& content :
5348 session_desc->description()->contents()) {
5349 if (IsAudioContent(&content)) {
5350 // If we already have an audio m= section, reject this extra one.
5351 if (*audio_index) {
5352 session_options->media_description_options.push_back(
5353 cricket::MediaDescriptionOptions(
5354 cricket::MEDIA_TYPE_AUDIO, content.name,
Steve Anton5c72e712018-12-10 14:25:30 -08005355 RtpTransceiverDirection::kInactive, /*stopped=*/true));
zhihuang1c378ed2017-08-17 14:10:50 -07005356 } else {
Steve Anton5c72e712018-12-10 14:25:30 -08005357 bool stopped = (audio_direction == RtpTransceiverDirection::kInactive);
zhihuang1c378ed2017-08-17 14:10:50 -07005358 session_options->media_description_options.push_back(
Steve Anton5c72e712018-12-10 14:25:30 -08005359 cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO,
5360 content.name, audio_direction,
5361 stopped));
Oskar Sundbom9b28a032017-11-16 10:53:30 +01005362 *audio_index = session_options->media_description_options.size() - 1;
zhihuang1c378ed2017-08-17 14:10:50 -07005363 }
5364 } else if (IsVideoContent(&content)) {
5365 // If we already have an video m= section, reject this extra one.
5366 if (*video_index) {
5367 session_options->media_description_options.push_back(
5368 cricket::MediaDescriptionOptions(
5369 cricket::MEDIA_TYPE_VIDEO, content.name,
Steve Anton5c72e712018-12-10 14:25:30 -08005370 RtpTransceiverDirection::kInactive, /*stopped=*/true));
zhihuang1c378ed2017-08-17 14:10:50 -07005371 } else {
Steve Anton5c72e712018-12-10 14:25:30 -08005372 bool stopped = (video_direction == RtpTransceiverDirection::kInactive);
zhihuang1c378ed2017-08-17 14:10:50 -07005373 session_options->media_description_options.push_back(
Steve Anton5c72e712018-12-10 14:25:30 -08005374 cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO,
5375 content.name, video_direction,
5376 stopped));
Oskar Sundbom9b28a032017-11-16 10:53:30 +01005377 *video_index = session_options->media_description_options.size() - 1;
zhihuang1c378ed2017-08-17 14:10:50 -07005378 }
5379 } else {
5380 RTC_DCHECK(IsDataContent(&content));
5381 // If we already have an data m= section, reject this extra one.
5382 if (*data_index) {
5383 session_options->media_description_options.push_back(
Steve Antonfa2260d2017-12-28 16:38:23 -08005384 GetMediaDescriptionOptionsForRejectedData(content.name));
zhihuang1c378ed2017-08-17 14:10:50 -07005385 } else {
5386 session_options->media_description_options.push_back(
Steve Antonfa2260d2017-12-28 16:38:23 -08005387 GetMediaDescriptionOptionsForActiveData(content.name));
Oskar Sundbom9b28a032017-11-16 10:53:30 +01005388 *data_index = session_options->media_description_options.size() - 1;
zhihuang1c378ed2017-08-17 14:10:50 -07005389 }
5390 }
htaa2a49d92016-03-04 02:51:39 -08005391 }
deadbeefab9b2d12015-10-14 11:33:11 -07005392}
5393
Steve Antonfa2260d2017-12-28 16:38:23 -08005394cricket::MediaDescriptionOptions
5395PeerConnection::GetMediaDescriptionOptionsForActiveData(
5396 const std::string& mid) const {
5397 // Direction for data sections is meaningless, but legacy endpoints might
5398 // expect sendrecv.
5399 cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
5400 RtpTransceiverDirection::kSendRecv,
5401 /*stopped=*/false);
Harald Alvestrand05e4d082019-12-03 14:04:21 +01005402 AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(),
5403 &options);
Steve Antonfa2260d2017-12-28 16:38:23 -08005404 return options;
5405}
5406
5407cricket::MediaDescriptionOptions
5408PeerConnection::GetMediaDescriptionOptionsForRejectedData(
5409 const std::string& mid) const {
5410 cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
5411 RtpTransceiverDirection::kInactive,
5412 /*stopped=*/true);
Harald Alvestrand05e4d082019-12-03 14:04:21 +01005413 AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(),
5414 &options);
Steve Antonfa2260d2017-12-28 16:38:23 -08005415 return options;
5416}
5417
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02005418absl::optional<std::string> PeerConnection::GetDataMid() const {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01005419 switch (data_channel_type()) {
Steve Antonfa2260d2017-12-28 16:38:23 -08005420 case cricket::DCT_RTP:
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01005421 if (!data_channel_controller_.rtp_data_channel()) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02005422 return absl::nullopt;
Steve Antonfa2260d2017-12-28 16:38:23 -08005423 }
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01005424 return data_channel_controller_.rtp_data_channel()->content_name();
Steve Antonfa2260d2017-12-28 16:38:23 -08005425 case cricket::DCT_SCTP:
Bjorn A Mellemb689af42019-08-21 10:44:59 -07005426 case cricket::DCT_DATA_CHANNEL_TRANSPORT:
5427 case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP:
Harald Alvestrand7a829a82020-02-12 07:38:21 +01005428 return sctp_mid_s_;
Steve Antonfa2260d2017-12-28 16:38:23 -08005429 default:
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02005430 return absl::nullopt;
Steve Antonfa2260d2017-12-28 16:38:23 -08005431 }
5432}
5433
Steve Anton4171afb2017-11-20 10:20:22 -08005434void PeerConnection::RemoveSenders(cricket::MediaType media_type) {
5435 UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type);
5436 UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false,
deadbeefbda7e0b2015-12-08 17:13:40 -08005437 media_type, nullptr);
deadbeeffaac4972015-11-12 15:33:07 -08005438}
5439
Steve Anton4171afb2017-11-20 10:20:22 -08005440void PeerConnection::UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 11:33:11 -07005441 const cricket::StreamParamsVec& streams,
Steve Anton4171afb2017-11-20 10:20:22 -08005442 bool default_sender_needed,
deadbeefab9b2d12015-10-14 11:33:11 -07005443 cricket::MediaType media_type,
5444 StreamCollection* new_streams) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07005445 RTC_DCHECK(!IsUnifiedPlan());
5446
Steve Anton4171afb2017-11-20 10:20:22 -08005447 std::vector<RtpSenderInfo>* current_senders =
5448 GetRemoteSenderInfos(media_type);
deadbeefab9b2d12015-10-14 11:33:11 -07005449
Steve Anton4171afb2017-11-20 10:20:22 -08005450 // Find removed senders. I.e., senders where the sender id or ssrc don't match
deadbeeffac06552015-11-25 11:26:01 -08005451 // the new StreamParam.
Steve Anton4171afb2017-11-20 10:20:22 -08005452 for (auto sender_it = current_senders->begin();
5453 sender_it != current_senders->end();
5454 /* incremented manually */) {
5455 const RtpSenderInfo& info = *sender_it;
deadbeefab9b2d12015-10-14 11:33:11 -07005456 const cricket::StreamParams* params =
Steve Anton4171afb2017-11-20 10:20:22 -08005457 cricket::GetStreamBySsrc(streams, info.first_ssrc);
Seth Hampson83d676b2018-04-05 18:12:09 -07005458 std::string params_stream_id;
5459 if (params) {
5460 params_stream_id =
5461 (!params->first_stream_id().empty() ? params->first_stream_id()
5462 : kDefaultStreamId);
5463 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07005464 bool sender_exists = params && params->id == info.sender_id &&
Seth Hampson83d676b2018-04-05 18:12:09 -07005465 params_stream_id == info.stream_id;
deadbeefbda7e0b2015-12-08 17:13:40 -08005466 // If this is a default track, and we still need it, don't remove it.
Seth Hampson845e8782018-03-02 11:34:10 -08005467 if ((info.stream_id == kDefaultStreamId && default_sender_needed) ||
Steve Anton4171afb2017-11-20 10:20:22 -08005468 sender_exists) {
5469 ++sender_it;
deadbeefbda7e0b2015-12-08 17:13:40 -08005470 } else {
Steve Anton4171afb2017-11-20 10:20:22 -08005471 OnRemoteSenderRemoved(info, media_type);
5472 sender_it = current_senders->erase(sender_it);
deadbeefab9b2d12015-10-14 11:33:11 -07005473 }
5474 }
5475
Steve Anton4171afb2017-11-20 10:20:22 -08005476 // Find new and active senders.
deadbeefab9b2d12015-10-14 11:33:11 -07005477 for (const cricket::StreamParams& params : streams) {
Seth Hampson5897a6e2018-04-03 11:16:33 -07005478 if (!params.has_ssrcs()) {
5479 // The remote endpoint has streams, but didn't signal ssrcs. For an active
5480 // sender, this means it is coming from a Unified Plan endpoint,so we just
5481 // create a default.
5482 default_sender_needed = true;
5483 break;
5484 }
5485
Seth Hampson845e8782018-03-02 11:34:10 -08005486 // |params.id| is the sender id and the stream id uses the first of
Seth Hampson5b4f0752018-04-02 16:31:36 -07005487 // |params.stream_ids|. The remote description could come from a Unified
Seth Hampson5897a6e2018-04-03 11:16:33 -07005488 // Plan endpoint, with multiple or no stream_ids() signaled. Since this is
5489 // not supported in Plan B, we just take the first here and create the
5490 // default stream ID if none is specified.
Seth Hampson845e8782018-03-02 11:34:10 -08005491 const std::string& stream_id =
Seth Hampson83d676b2018-04-05 18:12:09 -07005492 (!params.first_stream_id().empty() ? params.first_stream_id()
5493 : kDefaultStreamId);
Steve Anton4171afb2017-11-20 10:20:22 -08005494 const std::string& sender_id = params.id;
deadbeefab9b2d12015-10-14 11:33:11 -07005495 uint32_t ssrc = params.first_ssrc();
5496
5497 rtc::scoped_refptr<MediaStreamInterface> stream =
Seth Hampson845e8782018-03-02 11:34:10 -08005498 remote_streams_->find(stream_id);
deadbeefab9b2d12015-10-14 11:33:11 -07005499 if (!stream) {
5500 // This is a new MediaStream. Create a new remote MediaStream.
perkjd61bf802016-03-24 03:16:19 -07005501 stream = MediaStreamProxy::Create(rtc::Thread::Current(),
Seth Hampson845e8782018-03-02 11:34:10 -08005502 MediaStream::Create(stream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07005503 remote_streams_->AddStream(stream);
5504 new_streams->AddStream(stream);
5505 }
5506
Steve Anton4171afb2017-11-20 10:20:22 -08005507 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609ea2018-03-27 21:57:18 +00005508 FindSenderInfo(*current_senders, stream_id, sender_id);
Steve Anton4171afb2017-11-20 10:20:22 -08005509 if (!sender_info) {
Seth Hampson845e8782018-03-02 11:34:10 -08005510 current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
Steve Anton4171afb2017-11-20 10:20:22 -08005511 OnRemoteSenderAdded(current_senders->back(), media_type);
deadbeefab9b2d12015-10-14 11:33:11 -07005512 }
5513 }
deadbeefbda7e0b2015-12-08 17:13:40 -08005514
Steve Anton4171afb2017-11-20 10:20:22 -08005515 // Add default sender if necessary.
5516 if (default_sender_needed) {
deadbeefbda7e0b2015-12-08 17:13:40 -08005517 rtc::scoped_refptr<MediaStreamInterface> default_stream =
Seth Hampson845e8782018-03-02 11:34:10 -08005518 remote_streams_->find(kDefaultStreamId);
deadbeefbda7e0b2015-12-08 17:13:40 -08005519 if (!default_stream) {
5520 // Create the new default MediaStream.
perkjd61bf802016-03-24 03:16:19 -07005521 default_stream = MediaStreamProxy::Create(
Seth Hampson845e8782018-03-02 11:34:10 -08005522 rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId));
deadbeefbda7e0b2015-12-08 17:13:40 -08005523 remote_streams_->AddStream(default_stream);
5524 new_streams->AddStream(default_stream);
5525 }
Steve Anton4171afb2017-11-20 10:20:22 -08005526 std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO)
5527 ? kDefaultAudioSenderId
5528 : kDefaultVideoSenderId;
Seth Hampson845e8782018-03-02 11:34:10 -08005529 const RtpSenderInfo* default_sender_info =
Emircan Uysalerbc609ea2018-03-27 21:57:18 +00005530 FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id);
Steve Anton4171afb2017-11-20 10:20:22 -08005531 if (!default_sender_info) {
5532 current_senders->push_back(
Ruslan Burakov7ea46052019-02-16 02:07:05 +01005533 RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0));
Steve Anton4171afb2017-11-20 10:20:22 -08005534 OnRemoteSenderAdded(current_senders->back(), media_type);
deadbeefbda7e0b2015-12-08 17:13:40 -08005535 }
5536 }
deadbeefab9b2d12015-10-14 11:33:11 -07005537}
5538
Steve Anton4171afb2017-11-20 10:20:22 -08005539void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
5540 cricket::MediaType media_type) {
Steve Anton3d954a62018-04-02 11:27:23 -07005541 RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type)
5542 << " receiver for track_id=" << sender_info.sender_id
5543 << " and stream_id=" << sender_info.stream_id;
deadbeefab9b2d12015-10-14 11:33:11 -07005544
Steve Anton3d954a62018-04-02 11:27:23 -07005545 MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id);
deadbeefab9b2d12015-10-14 11:33:11 -07005546 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton4171afb2017-11-20 10:20:22 -08005547 CreateAudioReceiver(stream, sender_info);
deadbeefab9b2d12015-10-14 11:33:11 -07005548 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Anton4171afb2017-11-20 10:20:22 -08005549 CreateVideoReceiver(stream, sender_info);
deadbeefab9b2d12015-10-14 11:33:11 -07005550 } else {
nisseeb4ca4e2017-01-12 02:24:27 -08005551 RTC_NOTREACHED() << "Invalid media type";
deadbeefab9b2d12015-10-14 11:33:11 -07005552 }
5553}
5554
Steve Anton4171afb2017-11-20 10:20:22 -08005555void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
5556 cricket::MediaType media_type) {
Seth Hampson83d676b2018-04-05 18:12:09 -07005557 RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type)
5558 << " receiver for track_id=" << sender_info.sender_id
5559 << " and stream_id=" << sender_info.stream_id;
5560
Seth Hampson845e8782018-03-02 11:34:10 -08005561 MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id);
deadbeefab9b2d12015-10-14 11:33:11 -07005562
Henrik Boström933d8b02017-10-10 10:05:16 -07005563 rtc::scoped_refptr<RtpReceiverInterface> receiver;
deadbeefab9b2d12015-10-14 11:33:11 -07005564 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
perkjd61bf802016-03-24 03:16:19 -07005565 // When the MediaEngine audio channel is destroyed, the RemoteAudioSource
5566 // will be notified which will end the AudioRtpReceiver::track().
Steve Anton4171afb2017-11-20 10:20:22 -08005567 receiver = RemoveAndStopReceiver(sender_info);
deadbeefab9b2d12015-10-14 11:33:11 -07005568 rtc::scoped_refptr<AudioTrackInterface> audio_track =
Steve Anton4171afb2017-11-20 10:20:22 -08005569 stream->FindAudioTrack(sender_info.sender_id);
deadbeefab9b2d12015-10-14 11:33:11 -07005570 if (audio_track) {
deadbeefab9b2d12015-10-14 11:33:11 -07005571 stream->RemoveTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -07005572 }
5573 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
perkjd61bf802016-03-24 03:16:19 -07005574 // Stopping or destroying a VideoRtpReceiver will end the
5575 // VideoRtpReceiver::track().
Steve Anton4171afb2017-11-20 10:20:22 -08005576 receiver = RemoveAndStopReceiver(sender_info);
deadbeefab9b2d12015-10-14 11:33:11 -07005577 rtc::scoped_refptr<VideoTrackInterface> video_track =
Steve Anton4171afb2017-11-20 10:20:22 -08005578 stream->FindVideoTrack(sender_info.sender_id);
deadbeefab9b2d12015-10-14 11:33:11 -07005579 if (video_track) {
perkjd61bf802016-03-24 03:16:19 -07005580 // There's no guarantee the track is still available, e.g. the track may
5581 // have been removed from the stream by an application.
deadbeefab9b2d12015-10-14 11:33:11 -07005582 stream->RemoveTrack(video_track);
deadbeefab9b2d12015-10-14 11:33:11 -07005583 }
5584 } else {
nisseede5da42017-01-12 05:15:36 -08005585 RTC_NOTREACHED() << "Invalid media type";
deadbeefab9b2d12015-10-14 11:33:11 -07005586 }
Henrik Boström933d8b02017-10-10 10:05:16 -07005587 if (receiver) {
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02005588 Observer()->OnRemoveTrack(receiver);
Henrik Boström933d8b02017-10-10 10:05:16 -07005589 }
deadbeefab9b2d12015-10-14 11:33:11 -07005590}
5591
5592void PeerConnection::UpdateEndedRemoteMediaStreams() {
5593 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove;
5594 for (size_t i = 0; i < remote_streams_->count(); ++i) {
5595 MediaStreamInterface* stream = remote_streams_->at(i);
5596 if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) {
5597 streams_to_remove.push_back(stream);
5598 }
5599 }
5600
Taylor Brandstetter98cde262016-05-31 13:02:21 -07005601 for (auto& stream : streams_to_remove) {
deadbeefab9b2d12015-10-14 11:33:11 -07005602 remote_streams_->RemoveStream(stream);
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02005603 Observer()->OnRemoveStream(std::move(stream));
deadbeefab9b2d12015-10-14 11:33:11 -07005604 }
5605}
5606
Steve Anton4171afb2017-11-20 10:20:22 -08005607void PeerConnection::UpdateLocalSenders(
deadbeefab9b2d12015-10-14 11:33:11 -07005608 const std::vector<cricket::StreamParams>& streams,
5609 cricket::MediaType media_type) {
Steve Anton4171afb2017-11-20 10:20:22 -08005610 std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type);
deadbeefab9b2d12015-10-14 11:33:11 -07005611
Seth Hampson845e8782018-03-02 11:34:10 -08005612 // Find removed tracks. I.e., tracks where the track id, stream id or ssrc
deadbeefab9b2d12015-10-14 11:33:11 -07005613 // don't match the new StreamParam.
Steve Anton4171afb2017-11-20 10:20:22 -08005614 for (auto sender_it = current_senders->begin();
5615 sender_it != current_senders->end();
5616 /* incremented manually */) {
5617 const RtpSenderInfo& info = *sender_it;
deadbeefab9b2d12015-10-14 11:33:11 -07005618 const cricket::StreamParams* params =
Steve Anton4171afb2017-11-20 10:20:22 -08005619 cricket::GetStreamBySsrc(streams, info.first_ssrc);
5620 if (!params || params->id != info.sender_id ||
Seth Hampson845e8782018-03-02 11:34:10 -08005621 params->first_stream_id() != info.stream_id) {
Steve Anton4171afb2017-11-20 10:20:22 -08005622 OnLocalSenderRemoved(info, media_type);
5623 sender_it = current_senders->erase(sender_it);
deadbeefab9b2d12015-10-14 11:33:11 -07005624 } else {
Steve Anton4171afb2017-11-20 10:20:22 -08005625 ++sender_it;
deadbeefab9b2d12015-10-14 11:33:11 -07005626 }
5627 }
5628
Steve Anton4171afb2017-11-20 10:20:22 -08005629 // Find new and active senders.
deadbeefab9b2d12015-10-14 11:33:11 -07005630 for (const cricket::StreamParams& params : streams) {
5631 // The sync_label is the MediaStream label and the |stream.id| is the
Steve Anton4171afb2017-11-20 10:20:22 -08005632 // sender id.
Seth Hampson845e8782018-03-02 11:34:10 -08005633 const std::string& stream_id = params.first_stream_id();
Steve Anton4171afb2017-11-20 10:20:22 -08005634 const std::string& sender_id = params.id;
deadbeefab9b2d12015-10-14 11:33:11 -07005635 uint32_t ssrc = params.first_ssrc();
Steve Anton4171afb2017-11-20 10:20:22 -08005636 const RtpSenderInfo* sender_info =
Emircan Uysalerbc609ea2018-03-27 21:57:18 +00005637 FindSenderInfo(*current_senders, stream_id, sender_id);
Steve Anton4171afb2017-11-20 10:20:22 -08005638 if (!sender_info) {
Seth Hampson845e8782018-03-02 11:34:10 -08005639 current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
Steve Anton4171afb2017-11-20 10:20:22 -08005640 OnLocalSenderAdded(current_senders->back(), media_type);
deadbeefab9b2d12015-10-14 11:33:11 -07005641 }
5642 }
5643}
5644
Steve Anton4171afb2017-11-20 10:20:22 -08005645void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info,
5646 cricket::MediaType media_type) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07005647 RTC_DCHECK(!IsUnifiedPlan());
Steve Anton4171afb2017-11-20 10:20:22 -08005648 auto sender = FindSenderById(sender_info.sender_id);
deadbeeffac06552015-11-25 11:26:01 -08005649 if (!sender) {
Steve Anton4171afb2017-11-20 10:20:22 -08005650 RTC_LOG(LS_WARNING) << "An unknown RtpSender with id "
5651 << sender_info.sender_id
Mirko Bonadei675513b2017-11-09 11:09:25 +01005652 << " has been configured in the local description.";
deadbeefab9b2d12015-10-14 11:33:11 -07005653 return;
5654 }
5655
deadbeeffac06552015-11-25 11:26:01 -08005656 if (sender->media_type() != media_type) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01005657 RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local"
Jonas Olsson45cc8902018-02-13 10:37:07 +01005658 " description with an unexpected media type.";
deadbeeffac06552015-11-25 11:26:01 -08005659 return;
deadbeefab9b2d12015-10-14 11:33:11 -07005660 }
deadbeeffac06552015-11-25 11:26:01 -08005661
Henrik Andreasssoncc189172019-05-20 09:01:38 +00005662 sender->internal()->set_stream_ids({sender_info.stream_id});
Steve Anton4171afb2017-11-20 10:20:22 -08005663 sender->internal()->SetSsrc(sender_info.first_ssrc);
deadbeefab9b2d12015-10-14 11:33:11 -07005664}
5665
Steve Anton4171afb2017-11-20 10:20:22 -08005666void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
5667 cricket::MediaType media_type) {
5668 auto sender = FindSenderById(sender_info.sender_id);
deadbeeffac06552015-11-25 11:26:01 -08005669 if (!sender) {
5670 // This is the normal case. I.e., RemoveStream has been called and the
deadbeefab9b2d12015-10-14 11:33:11 -07005671 // SessionDescriptions has been renegotiated.
5672 return;
5673 }
deadbeeffac06552015-11-25 11:26:01 -08005674
5675 // A sender has been removed from the SessionDescription but it's still
5676 // associated with the PeerConnection. This only occurs if the SDP doesn't
5677 // match with the calls to CreateSender, AddStream and RemoveStream.
5678 if (sender->media_type() != media_type) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01005679 RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local"
Jonas Olsson45cc8902018-02-13 10:37:07 +01005680 " description with an unexpected media type.";
deadbeeffac06552015-11-25 11:26:01 -08005681 return;
deadbeefab9b2d12015-10-14 11:33:11 -07005682 }
deadbeeffac06552015-11-25 11:26:01 -08005683
Steve Anton4171afb2017-11-20 10:20:22 -08005684 sender->internal()->SetSsrc(0);
deadbeefab9b2d12015-10-14 11:33:11 -07005685}
5686
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01005687void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) {
5688 // Since data_channel_controller doesn't do signals, this
5689 // signal is relayed here.
5690 data_channel_controller_.OnSctpDataChannelClosed(channel);
deadbeefab9b2d12015-10-14 11:33:11 -07005691}
5692
Steve Anton4171afb2017-11-20 10:20:22 -08005693rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
5694PeerConnection::GetAudioTransceiver() const {
5695 // This method only works with Plan B SDP, where there is a single
5696 // audio/video transceiver.
5697 RTC_DCHECK(!IsUnifiedPlan());
5698 for (auto transceiver : transceivers_) {
Steve Anton69470252018-02-09 11:43:08 -08005699 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton4171afb2017-11-20 10:20:22 -08005700 return transceiver;
5701 }
5702 }
5703 RTC_NOTREACHED();
5704 return nullptr;
5705}
5706
5707rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
5708PeerConnection::GetVideoTransceiver() const {
5709 // This method only works with Plan B SDP, where there is a single
5710 // audio/video transceiver.
5711 RTC_DCHECK(!IsUnifiedPlan());
5712 for (auto transceiver : transceivers_) {
Steve Anton69470252018-02-09 11:43:08 -08005713 if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
Steve Anton4171afb2017-11-20 10:20:22 -08005714 return transceiver;
5715 }
5716 }
5717 RTC_NOTREACHED();
5718 return nullptr;
5719}
5720
5721// TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with
5722// individual transceiver directions are supported.
zhihuang1c378ed2017-08-17 14:10:50 -07005723bool PeerConnection::HasRtpSender(cricket::MediaType type) const {
Steve Anton4171afb2017-11-20 10:20:22 -08005724 switch (type) {
5725 case cricket::MEDIA_TYPE_AUDIO:
5726 return !GetAudioTransceiver()->internal()->senders().empty();
5727 case cricket::MEDIA_TYPE_VIDEO:
5728 return !GetVideoTransceiver()->internal()->senders().empty();
5729 case cricket::MEDIA_TYPE_DATA:
5730 return false;
5731 }
5732 RTC_NOTREACHED();
5733 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07005734}
5735
Steve Anton4171afb2017-11-20 10:20:22 -08005736rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
5737PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const {
Mirko Bonadei739baf02019-01-27 17:29:42 +01005738 for (const auto& transceiver : transceivers_) {
Steve Anton4171afb2017-11-20 10:20:22 -08005739 for (auto sender : transceiver->internal()->senders()) {
5740 if (sender->track() == track) {
5741 return sender;
5742 }
5743 }
5744 }
5745 return nullptr;
deadbeeffac06552015-11-25 11:26:01 -08005746}
5747
Steve Anton4171afb2017-11-20 10:20:22 -08005748rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
5749PeerConnection::FindSenderById(const std::string& sender_id) const {
Mirko Bonadei739baf02019-01-27 17:29:42 +01005750 for (const auto& transceiver : transceivers_) {
Steve Anton4171afb2017-11-20 10:20:22 -08005751 for (auto sender : transceiver->internal()->senders()) {
5752 if (sender->id() == sender_id) {
5753 return sender;
5754 }
5755 }
5756 }
5757 return nullptr;
deadbeef70ab1a12015-09-28 16:53:55 -07005758}
5759
Steve Anton4171afb2017-11-20 10:20:22 -08005760rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
5761PeerConnection::FindReceiverById(const std::string& receiver_id) const {
Mirko Bonadei739baf02019-01-27 17:29:42 +01005762 for (const auto& transceiver : transceivers_) {
Steve Anton4171afb2017-11-20 10:20:22 -08005763 for (auto receiver : transceiver->internal()->receivers()) {
5764 if (receiver->id() == receiver_id) {
5765 return receiver;
5766 }
5767 }
5768 }
5769 return nullptr;
deadbeef70ab1a12015-09-28 16:53:55 -07005770}
5771
Steve Anton4171afb2017-11-20 10:20:22 -08005772std::vector<PeerConnection::RtpSenderInfo>*
5773PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) {
5774 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
5775 media_type == cricket::MEDIA_TYPE_VIDEO);
5776 return (media_type == cricket::MEDIA_TYPE_AUDIO)
5777 ? &remote_audio_sender_infos_
5778 : &remote_video_sender_infos_;
5779}
5780
5781std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos(
deadbeefab9b2d12015-10-14 11:33:11 -07005782 cricket::MediaType media_type) {
5783 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
5784 media_type == cricket::MEDIA_TYPE_VIDEO);
Steve Anton4171afb2017-11-20 10:20:22 -08005785 return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_
5786 : &local_video_sender_infos_;
deadbeefab9b2d12015-10-14 11:33:11 -07005787}
5788
Steve Anton4171afb2017-11-20 10:20:22 -08005789const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo(
5790 const std::vector<PeerConnection::RtpSenderInfo>& infos,
Emircan Uysalerbc609ea2018-03-27 21:57:18 +00005791 const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -08005792 const std::string sender_id) const {
5793 for (const RtpSenderInfo& sender_info : infos) {
Emircan Uysalerbc609ea2018-03-27 21:57:18 +00005794 if (sender_info.stream_id == stream_id &&
5795 sender_info.sender_id == sender_id) {
Steve Anton4171afb2017-11-20 10:20:22 -08005796 return &sender_info;
deadbeefab9b2d12015-10-14 11:33:11 -07005797 }
5798 }
5799 return nullptr;
5800}
5801
5802DataChannel* PeerConnection::FindDataChannelBySid(int sid) const {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01005803 return data_channel_controller_.FindDataChannelBySid(sid);
deadbeefab9b2d12015-10-14 11:33:11 -07005804}
5805
Karl Wibergfb3be392019-03-22 14:13:22 +01005806PeerConnection::InitializePortAllocatorResult
5807PeerConnection::InitializePortAllocator_n(
Harald Alvestrandb2a74782018-06-28 13:54:07 +02005808 const cricket::ServerAddresses& stun_servers,
5809 const std::vector<cricket::RelayServerConfig>& turn_servers,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005810 const RTCConfiguration& configuration) {
Karl Wibergfb3be392019-03-22 14:13:22 +01005811 RTC_DCHECK_RUN_ON(network_thread());
5812
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07005813 port_allocator_->Initialize();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005814 // To handle both internal and externally created port allocator, we will
5815 // enable BUNDLE here.
Karl Wibergfb3be392019-03-22 14:13:22 +01005816 int port_allocator_flags = port_allocator_->flags();
5817 port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
5818 cricket::PORTALLOCATOR_ENABLE_IPV6 |
5819 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005820 // If the disable-IPv6 flag was specified, we'll not override it
5821 // by experiment.
5822 if (configuration.disable_ipv6) {
Karl Wibergfb3be392019-03-22 14:13:22 +01005823 port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
Mirko Bonadei57cabed2020-04-01 12:03:11 +02005824 } else if (absl::StartsWith(
5825 webrtc::field_trial::FindFullName("WebRTC-IPv6Default"),
5826 "Disabled")) {
Karl Wibergfb3be392019-03-22 14:13:22 +01005827 port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005828 }
5829
zhihuangb09b3f92017-03-07 14:40:51 -08005830 if (configuration.disable_ipv6_on_wifi) {
Karl Wibergfb3be392019-03-22 14:13:22 +01005831 port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Mirko Bonadei675513b2017-11-09 11:09:25 +01005832 RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled.";
zhihuangb09b3f92017-03-07 14:40:51 -08005833 }
5834
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005835 if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) {
Karl Wibergfb3be392019-03-22 14:13:22 +01005836 port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
Mirko Bonadei675513b2017-11-09 11:09:25 +01005837 RTC_LOG(LS_INFO) << "TCP candidates are disabled.";
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005838 }
5839
honghaiz60347052016-05-31 18:29:12 -07005840 if (configuration.candidate_network_policy ==
5841 kCandidateNetworkPolicyLowCost) {
Karl Wibergfb3be392019-03-22 14:13:22 +01005842 port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS;
Mirko Bonadei675513b2017-11-09 11:09:25 +01005843 RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks";
honghaiz60347052016-05-31 18:29:12 -07005844 }
5845
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +01005846 if (configuration.disable_link_local_networks) {
Karl Wibergfb3be392019-03-22 14:13:22 +01005847 port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS;
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +01005848 RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces.";
5849 }
5850
Karl Wibergfb3be392019-03-22 14:13:22 +01005851 port_allocator_->set_flags(port_allocator_flags);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005852 // No step delay is used while allocating ports.
5853 port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
Qingsi Wangc129c352019-04-18 10:41:58 -07005854 port_allocator_->SetCandidateFilter(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005855 ConvertIceTransportTypeToCandidateFilter(configuration.type));
deadbeefd21eab32017-07-26 16:50:11 -07005856 port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005857
Harald Alvestrandb2a74782018-06-28 13:54:07 +02005858 auto turn_servers_copy = turn_servers;
Benjamin Wright6f80f092018-08-13 17:06:26 -07005859 for (auto& turn_server : turn_servers_copy) {
5860 turn_server.tls_cert_verifier = tls_cert_verifier_.get();
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07005861 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005862 // Call this last since it may create pooled allocator sessions using the
5863 // properties set above.
Qingsi Wangdb53f8e2018-02-20 14:45:49 -08005864 port_allocator_->SetConfiguration(
Benjamin Wright6f80f092018-08-13 17:06:26 -07005865 stun_servers, std::move(turn_servers_copy),
Honghai Zhangf8998cf2019-10-14 11:27:50 -07005866 configuration.ice_candidate_pool_size,
5867 configuration.GetTurnPortPrunePolicy(), configuration.turn_customizer,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -08005868 configuration.stun_candidate_keepalive_interval);
Karl Wibergfb3be392019-03-22 14:13:22 +01005869
5870 InitializePortAllocatorResult res;
5871 res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6;
5872 return res;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005873}
5874
deadbeef91dd5672016-05-18 16:55:30 -07005875bool PeerConnection::ReconfigurePortAllocator_n(
deadbeef293e9262017-01-11 12:28:30 -08005876 const cricket::ServerAddresses& stun_servers,
5877 const std::vector<cricket::RelayServerConfig>& turn_servers,
5878 IceTransportsType type,
5879 int candidate_pool_size,
Honghai Zhangf8998cf2019-10-14 11:27:50 -07005880 PortPrunePolicy turn_port_prune_policy,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -08005881 webrtc::TurnCustomizer* turn_customizer,
Karl Wiberg739506e2019-04-03 11:37:28 +02005882 absl::optional<int> stun_candidate_keepalive_interval,
5883 bool have_local_description) {
Qingsi Wangc129c352019-04-18 10:41:58 -07005884 port_allocator_->SetCandidateFilter(
deadbeef293e9262017-01-11 12:28:30 -08005885 ConvertIceTransportTypeToCandidateFilter(type));
Qingsi Wanga2d60672018-04-11 16:57:45 -07005886 // According to JSEP, after setLocalDescription, changing the candidate pool
5887 // size is not allowed, and changing the set of ICE servers will not result
5888 // in new candidates being gathered.
Karl Wiberg739506e2019-04-03 11:37:28 +02005889 if (have_local_description) {
Qingsi Wanga2d60672018-04-11 16:57:45 -07005890 port_allocator_->FreezeCandidatePool();
5891 }
Benjamin Wright6f80f092018-08-13 17:06:26 -07005892 // Add the custom tls turn servers if they exist.
5893 auto turn_servers_copy = turn_servers;
5894 for (auto& turn_server : turn_servers_copy) {
5895 turn_server.tls_cert_verifier = tls_cert_verifier_.get();
5896 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005897 // Call this last since it may create pooled allocator sessions using the
5898 // candidate filter set above.
deadbeef6de92f92016-12-12 18:49:32 -08005899 return port_allocator_->SetConfiguration(
Benjamin Wright6f80f092018-08-13 17:06:26 -07005900 stun_servers, std::move(turn_servers_copy), candidate_pool_size,
Honghai Zhangf8998cf2019-10-14 11:27:50 -07005901 turn_port_prune_policy, turn_customizer,
5902 stun_candidate_keepalive_interval);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07005903}
5904
Steve Antonba818672017-11-06 10:21:57 -08005905cricket::ChannelManager* PeerConnection::channel_manager() const {
5906 return factory_->channel_manager();
5907}
5908
Elad Alon99c3fe52017-10-13 16:29:40 +02005909bool PeerConnection::StartRtcEventLog_w(
Bjorn Tereliusde939432017-11-20 17:38:14 +01005910 std::unique_ptr<RtcEventLogOutput> output,
5911 int64_t output_period_ms) {
Karl Wibergb03ab712019-02-14 11:59:57 +01005912 RTC_DCHECK_RUN_ON(worker_thread());
zhihuang77985012017-02-07 15:45:16 -08005913 if (!event_log_) {
5914 return false;
5915 }
Bjorn Tereliusde939432017-11-20 17:38:14 +01005916 return event_log_->StartLogging(std::move(output), output_period_ms);
ivoc14d5dbe2016-07-04 07:06:55 -07005917}
5918
5919void PeerConnection::StopRtcEventLog_w() {
Karl Wibergb03ab712019-02-14 11:59:57 +01005920 RTC_DCHECK_RUN_ON(worker_thread());
zhihuang77985012017-02-07 15:45:16 -08005921 if (event_log_) {
5922 event_log_->StopLogging();
5923 }
ivoc14d5dbe2016-07-04 07:06:55 -07005924}
nisseeaabdf62017-05-05 02:23:02 -07005925
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08005926cricket::ChannelInterface* PeerConnection::GetChannel(
Steve Anton75737c02017-11-06 10:37:17 -08005927 const std::string& content_name) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01005928 for (const auto& transceiver : transceivers_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08005929 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Antondcc3c022017-12-22 16:02:54 -08005930 if (channel && channel->content_name() == content_name) {
5931 return channel;
5932 }
Steve Anton75737c02017-11-06 10:37:17 -08005933 }
5934 if (rtp_data_channel() &&
5935 rtp_data_channel()->content_name() == content_name) {
5936 return rtp_data_channel();
5937 }
5938 return nullptr;
5939}
5940
5941bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08005942 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08005943 if (!local_description() || !remote_description()) {
Harald Alvestrand977b2652019-12-12 13:40:50 +01005944 RTC_LOG(LS_VERBOSE)
Mirko Bonadei675513b2017-11-09 11:09:25 +01005945 << "Local and Remote descriptions must be applied to get the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01005946 "SSL Role of the SCTP transport.";
Steve Anton75737c02017-11-06 10:37:17 -08005947 return false;
5948 }
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01005949 if (!data_channel_controller_.data_channel_transport()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01005950 RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01005951 "SSL Role of the SCTP transport.";
Steve Anton75737c02017-11-06 10:37:17 -08005952 return false;
5953 }
5954
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08005955 absl::optional<rtc::SSLRole> dtls_role;
Harald Alvestrand7a829a82020-02-12 07:38:21 +01005956 if (sctp_mid_s_) {
5957 dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_s_);
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07005958 if (!dtls_role && is_caller_.has_value()) {
5959 dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT;
5960 }
Zhi Huange830e682018-03-30 10:48:35 -07005961 *role = *dtls_role;
5962 return true;
5963 }
5964 return false;
Steve Anton75737c02017-11-06 10:37:17 -08005965}
5966
5967bool PeerConnection::GetSslRole(const std::string& content_name,
5968 rtc::SSLRole* role) {
Karl Wiberg2cc368f2019-04-02 11:31:56 +02005969 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08005970 if (!local_description() || !remote_description()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01005971 RTC_LOG(LS_INFO)
5972 << "Local and Remote descriptions must be applied to get the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01005973 "SSL Role of the session.";
Steve Anton75737c02017-11-06 10:37:17 -08005974 return false;
5975 }
5976
Zhi Huange830e682018-03-30 10:48:35 -07005977 auto dtls_role = transport_controller_->GetDtlsRole(content_name);
5978 if (dtls_role) {
5979 *role = *dtls_role;
5980 return true;
5981 }
5982 return false;
Steve Anton75737c02017-11-06 10:37:17 -08005983}
5984
Steve Antonf8470812017-12-04 10:46:21 -08005985void PeerConnection::SetSessionError(SessionError error,
5986 const std::string& error_desc) {
5987 RTC_DCHECK_RUN_ON(signaling_thread());
5988 if (error != session_error_) {
5989 session_error_ = error;
5990 session_error_desc_ = error_desc;
Steve Anton75737c02017-11-06 10:37:17 -08005991 }
5992}
5993
Zhi Huange830e682018-03-30 10:48:35 -07005994RTCError PeerConnection::UpdateSessionState(
5995 SdpType type,
5996 cricket::ContentSource source,
5997 const cricket::SessionDescription* description) {
Steve Anton8a006912017-12-04 15:25:56 -08005998 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08005999
6000 // If there's already a pending error then no state transition should happen.
6001 // But all call-sites should be verifying this before calling us!
Steve Antonf8470812017-12-04 10:46:21 -08006002 RTC_DCHECK(session_error() == SessionError::kNone);
Steve Anton6d6a2ae2017-12-04 17:19:47 -08006003
Steve Anton6d6a2ae2017-12-04 17:19:47 -08006004 // If this is answer-ish we're ready to let media flow.
Steve Anton3828c062017-12-06 10:34:51 -08006005 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
Steve Antoned10bd92017-12-05 10:52:59 -08006006 EnableSending();
Steve Anton6d6a2ae2017-12-04 17:19:47 -08006007 }
6008
6009 // Update the signaling state according to the specified state machine (see
6010 // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum).
Steve Anton3828c062017-12-06 10:34:51 -08006011 if (type == SdpType::kOffer) {
Steve Anton6d6a2ae2017-12-04 17:19:47 -08006012 ChangeSignalingState(source == cricket::CS_LOCAL
6013 ? PeerConnectionInterface::kHaveLocalOffer
6014 : PeerConnectionInterface::kHaveRemoteOffer);
Steve Anton3828c062017-12-06 10:34:51 -08006015 } else if (type == SdpType::kPrAnswer) {
Steve Anton6d6a2ae2017-12-04 17:19:47 -08006016 ChangeSignalingState(source == cricket::CS_LOCAL
6017 ? PeerConnectionInterface::kHaveLocalPrAnswer
6018 : PeerConnectionInterface::kHaveRemotePrAnswer);
6019 } else {
Steve Anton3828c062017-12-06 10:34:51 -08006020 RTC_DCHECK(type == SdpType::kAnswer);
Steve Anton6d6a2ae2017-12-04 17:19:47 -08006021 ChangeSignalingState(PeerConnectionInterface::kStable);
Eldar Rello5ab79e62019-10-09 18:29:44 +03006022 transceiver_stable_states_by_transceivers_.clear();
Eldar Rellod9ebe012020-03-18 20:41:45 +02006023 have_pending_rtp_data_channel_ = false;
Steve Anton6d6a2ae2017-12-04 17:19:47 -08006024 }
6025
6026 // Update internal objects according to the session description's media
6027 // descriptions.
Zhi Huange830e682018-03-30 10:48:35 -07006028 RTCError error = PushdownMediaDescription(type, source);
Steve Anton6d6a2ae2017-12-04 17:19:47 -08006029 if (!error.ok()) {
Steve Anton80dd7b52018-02-16 17:08:42 -08006030 return error;
Steve Anton6d6a2ae2017-12-04 17:19:47 -08006031 }
6032
Steve Anton8a006912017-12-04 15:25:56 -08006033 return RTCError::OK();
Steve Anton75737c02017-11-06 10:37:17 -08006034}
6035
Steve Anton8a006912017-12-04 15:25:56 -08006036RTCError PeerConnection::PushdownMediaDescription(
Steve Anton3828c062017-12-06 10:34:51 -08006037 SdpType type,
Steve Anton8a006912017-12-04 15:25:56 -08006038 cricket::ContentSource source) {
Steve Antoned10bd92017-12-05 10:52:59 -08006039 const SessionDescriptionInterface* sdesc =
6040 (source == cricket::CS_LOCAL ? local_description()
6041 : remote_description());
Steve Anton75737c02017-11-06 10:37:17 -08006042 RTC_DCHECK(sdesc);
Steve Antoned10bd92017-12-05 10:52:59 -08006043
6044 // Push down the new SDP media section for each audio/video transceiver.
Mirko Bonadei739baf02019-01-27 17:29:42 +01006045 for (const auto& transceiver : transceivers_) {
Steve Anton75737c02017-11-06 10:37:17 -08006046 const ContentInfo* content_info =
Steve Antoned10bd92017-12-05 10:52:59 -08006047 FindMediaSectionForTransceiver(transceiver, sdesc);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08006048 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Antoned10bd92017-12-05 10:52:59 -08006049 if (!channel || !content_info || content_info->rejected) {
Steve Anton75737c02017-11-06 10:37:17 -08006050 continue;
6051 }
6052 const MediaContentDescription* content_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08006053 content_info->media_description();
Steve Antoned10bd92017-12-05 10:52:59 -08006054 if (!content_desc) {
6055 continue;
6056 }
6057 std::string error;
Yves Gerey665174f2018-06-19 15:03:05 +02006058 bool success = (source == cricket::CS_LOCAL)
6059 ? channel->SetLocalContent(content_desc, type, &error)
6060 : channel->SetRemoteContent(content_desc, type, &error);
Steve Antoned10bd92017-12-05 10:52:59 -08006061 if (!success) {
Yves Gereyae6e0b22019-01-22 21:48:57 +01006062 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error);
Steve Antoned10bd92017-12-05 10:52:59 -08006063 }
6064 }
6065
6066 // If using the RtpDataChannel, push down the new SDP section for it too.
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006067 if (data_channel_controller_.rtp_data_channel()) {
Steve Antoned10bd92017-12-05 10:52:59 -08006068 const ContentInfo* data_content =
6069 cricket::GetFirstDataContent(sdesc->description());
6070 if (data_content && !data_content->rejected) {
6071 const MediaContentDescription* data_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08006072 data_content->media_description();
Steve Antoned10bd92017-12-05 10:52:59 -08006073 if (data_desc) {
6074 std::string error;
6075 bool success =
6076 (source == cricket::CS_LOCAL)
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006077 ? data_channel_controller_.rtp_data_channel()->SetLocalContent(
6078 data_desc, type, &error)
6079 : data_channel_controller_.rtp_data_channel()->SetRemoteContent(
6080 data_desc, type, &error);
Steve Antoned10bd92017-12-05 10:52:59 -08006081 if (!success) {
Yves Gereyae6e0b22019-01-22 21:48:57 +01006082 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error);
Steve Antoned10bd92017-12-05 10:52:59 -08006083 }
Steve Anton75737c02017-11-06 10:37:17 -08006084 }
6085 }
6086 }
Steve Antoned10bd92017-12-05 10:52:59 -08006087
Steve Anton75737c02017-11-06 10:37:17 -08006088 // Need complete offer/answer with an SCTP m= section before starting SCTP,
6089 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006090 if (sctp_mid_s_ && local_description() && remote_description()) {
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07006091 rtc::scoped_refptr<SctpTransport> sctp_transport =
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006092 transport_controller_->GetSctpTransport(*sctp_mid_s_);
Harald Alvestrandfbb45bd2019-05-15 08:07:47 +02006093 auto local_sctp_description = cricket::GetFirstSctpDataContentDescription(
6094 local_description()->description());
6095 auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription(
6096 remote_description()->description());
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07006097 if (sctp_transport && local_sctp_description && remote_sctp_description) {
Harald Alvestrand8d3d6cf2019-05-16 11:49:17 +02006098 int max_message_size;
6099 // A remote max message size of zero means "any size supported".
6100 // We configure the connection with our own max message size.
6101 if (remote_sctp_description->max_message_size() == 0) {
6102 max_message_size = local_sctp_description->max_message_size();
6103 } else {
6104 max_message_size =
6105 std::min(local_sctp_description->max_message_size(),
6106 remote_sctp_description->max_message_size());
Harald Alvestrandfbb45bd2019-05-15 08:07:47 +02006107 }
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07006108 sctp_transport->Start(local_sctp_description->port(),
6109 remote_sctp_description->port(), max_message_size);
Steve Anton8a006912017-12-04 15:25:56 -08006110 }
Steve Anton75737c02017-11-06 10:37:17 -08006111 }
Steve Antoned10bd92017-12-05 10:52:59 -08006112
Steve Anton8a006912017-12-04 15:25:56 -08006113 return RTCError::OK();
Steve Anton75737c02017-11-06 10:37:17 -08006114}
6115
Steve Anton8a006912017-12-04 15:25:56 -08006116RTCError PeerConnection::PushdownTransportDescription(
6117 cricket::ContentSource source,
Steve Anton3828c062017-12-06 10:34:51 -08006118 SdpType type) {
Steve Anton8a006912017-12-04 15:25:56 -08006119 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08006120
Zhi Huange830e682018-03-30 10:48:35 -07006121 if (source == cricket::CS_LOCAL) {
6122 const SessionDescriptionInterface* sdesc = local_description();
6123 RTC_DCHECK(sdesc);
6124 return transport_controller_->SetLocalDescription(type,
6125 sdesc->description());
6126 } else {
6127 const SessionDescriptionInterface* sdesc = remote_description();
6128 RTC_DCHECK(sdesc);
6129 return transport_controller_->SetRemoteDescription(type,
6130 sdesc->description());
Steve Anton75737c02017-11-06 10:37:17 -08006131 }
Steve Anton75737c02017-11-06 10:37:17 -08006132}
6133
6134bool PeerConnection::GetTransportDescription(
6135 const SessionDescription* description,
6136 const std::string& content_name,
6137 cricket::TransportDescription* tdesc) {
6138 if (!description || !tdesc) {
6139 return false;
6140 }
6141 const TransportInfo* transport_info =
6142 description->GetTransportInfoByName(content_name);
6143 if (!transport_info) {
6144 return false;
6145 }
6146 *tdesc = transport_info->description;
6147 return true;
6148}
6149
Steve Anton75737c02017-11-06 10:37:17 -08006150cricket::IceConfig PeerConnection::ParseIceConfig(
6151 const PeerConnectionInterface::RTCConfiguration& config) const {
6152 cricket::ContinualGatheringPolicy gathering_policy;
Steve Anton75737c02017-11-06 10:37:17 -08006153 switch (config.continual_gathering_policy) {
6154 case PeerConnectionInterface::GATHER_ONCE:
6155 gathering_policy = cricket::GATHER_ONCE;
6156 break;
6157 case PeerConnectionInterface::GATHER_CONTINUALLY:
6158 gathering_policy = cricket::GATHER_CONTINUALLY;
6159 break;
6160 default:
6161 RTC_NOTREACHED();
6162 gathering_policy = cricket::GATHER_ONCE;
6163 }
Qingsi Wang9a5c6f82018-02-01 10:38:40 -08006164
Steve Anton75737c02017-11-06 10:37:17 -08006165 cricket::IceConfig ice_config;
Qingsi Wang866e08d2018-03-22 17:54:23 -07006166 ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt(
6167 config.ice_connection_receiving_timeout);
Steve Anton75737c02017-11-06 10:37:17 -08006168 ice_config.prioritize_most_likely_candidate_pairs =
6169 config.prioritize_most_likely_ice_candidate_pairs;
6170 ice_config.backup_connection_ping_interval =
Qingsi Wang866e08d2018-03-22 17:54:23 -07006171 RTCConfigurationToIceConfigOptionalInt(
6172 config.ice_backup_candidate_pair_ping_interval);
Steve Anton75737c02017-11-06 10:37:17 -08006173 ice_config.continual_gathering_policy = gathering_policy;
6174 ice_config.presume_writable_when_fully_relayed =
6175 config.presume_writable_when_fully_relayed;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07006176 ice_config.surface_ice_candidates_on_ice_transport_type_changed =
6177 config.surface_ice_candidates_on_ice_transport_type_changed;
Qingsi Wange6826d22018-03-08 14:55:14 -08006178 ice_config.ice_check_interval_strong_connectivity =
6179 config.ice_check_interval_strong_connectivity;
6180 ice_config.ice_check_interval_weak_connectivity =
6181 config.ice_check_interval_weak_connectivity;
Steve Anton75737c02017-11-06 10:37:17 -08006182 ice_config.ice_check_min_interval = config.ice_check_min_interval;
Jiawei Oucc887372018-11-29 23:08:51 -08006183 ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout;
6184 ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -08006185 ice_config.ice_inactive_timeout = config.ice_inactive_timeout;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -08006186 ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -08006187 ice_config.network_preference = config.network_preference;
Steve Anton75737c02017-11-06 10:37:17 -08006188 return ice_config;
6189}
6190
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02006191absl::optional<std::string> PeerConnection::sctp_transport_name() const {
Karl Wiberg2cc368f2019-04-02 11:31:56 +02006192 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006193 if (sctp_mid_s_ && transport_controller_) {
6194 auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_s_);
Zhi Huange830e682018-03-30 10:48:35 -07006195 if (dtls_transport) {
6196 return dtls_transport->transport_name();
6197 }
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02006198 return absl::optional<std::string>();
Zhi Huange830e682018-03-30 10:48:35 -07006199 }
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02006200 return absl::optional<std::string>();
Zhi Huange830e682018-03-30 10:48:35 -07006201}
6202
Qingsi Wang72a43a12018-02-20 16:03:18 -08006203cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const {
6204 cricket::CandidateStatsList candidate_states_list;
Qingsi Wanga2d60672018-04-11 16:57:45 -07006205 network_thread()->Invoke<void>(
6206 RTC_FROM_HERE,
6207 rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions,
6208 port_allocator_.get(), &candidate_states_list));
Qingsi Wang72a43a12018-02-20 16:03:18 -08006209 return candidate_states_list;
6210}
6211
Steve Anton5dfde182018-02-06 10:34:40 -08006212std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid()
6213 const {
Karl Wiberga58e1692019-03-26 13:33:43 +01006214 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton5dfde182018-02-06 10:34:40 -08006215 std::map<std::string, std::string> transport_names_by_mid;
Mirko Bonadei739baf02019-01-27 17:29:42 +01006216 for (const auto& transceiver : transceivers_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08006217 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton5dfde182018-02-06 10:34:40 -08006218 if (channel) {
6219 transport_names_by_mid[channel->content_name()] =
6220 channel->transport_name();
6221 }
Steve Anton75737c02017-11-06 10:37:17 -08006222 }
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006223 if (data_channel_controller_.rtp_data_channel()) {
6224 transport_names_by_mid[data_channel_controller_.rtp_data_channel()
6225 ->content_name()] =
6226 data_channel_controller_.rtp_data_channel()->transport_name();
Steve Anton75737c02017-11-06 10:37:17 -08006227 }
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006228 if (data_channel_controller_.data_channel_transport()) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02006229 absl::optional<std::string> transport_name = sctp_transport_name();
Zhi Huange830e682018-03-30 10:48:35 -07006230 RTC_DCHECK(transport_name);
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006231 transport_names_by_mid[*sctp_mid_s_] = *transport_name;
Steve Anton75737c02017-11-06 10:37:17 -08006232 }
Steve Anton5dfde182018-02-06 10:34:40 -08006233 return transport_names_by_mid;
Steve Anton75737c02017-11-06 10:37:17 -08006234}
6235
Steve Anton5dfde182018-02-06 10:34:40 -08006236std::map<std::string, cricket::TransportStats>
6237PeerConnection::GetTransportStatsByNames(
6238 const std::set<std::string>& transport_names) {
6239 if (!network_thread()->IsCurrent()) {
6240 return network_thread()
6241 ->Invoke<std::map<std::string, cricket::TransportStats>>(
6242 RTC_FROM_HERE,
6243 [&] { return GetTransportStatsByNames(transport_names); });
Steve Anton75737c02017-11-06 10:37:17 -08006244 }
Karl Wiberg2cc368f2019-04-02 11:31:56 +02006245 RTC_DCHECK_RUN_ON(network_thread());
Steve Anton5dfde182018-02-06 10:34:40 -08006246 std::map<std::string, cricket::TransportStats> transport_stats_by_name;
6247 for (const std::string& transport_name : transport_names) {
6248 cricket::TransportStats transport_stats;
6249 bool success =
6250 transport_controller_->GetStats(transport_name, &transport_stats);
6251 if (success) {
6252 transport_stats_by_name[transport_name] = std::move(transport_stats);
6253 } else {
6254 RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name="
6255 << transport_name;
6256 }
6257 }
6258 return transport_stats_by_name;
Steve Anton75737c02017-11-06 10:37:17 -08006259}
6260
6261bool PeerConnection::GetLocalCertificate(
6262 const std::string& transport_name,
6263 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
Zhi Huange830e682018-03-30 10:48:35 -07006264 if (!certificate) {
6265 return false;
6266 }
6267 *certificate = transport_controller_->GetLocalCertificate(transport_name);
6268 return *certificate != nullptr;
Steve Anton75737c02017-11-06 10:37:17 -08006269}
6270
Taylor Brandstetterc3928662018-02-23 13:04:51 -08006271std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain(
Steve Anton75737c02017-11-06 10:37:17 -08006272 const std::string& transport_name) {
Taylor Brandstetterc3928662018-02-23 13:04:51 -08006273 return transport_controller_->GetRemoteSSLCertChain(transport_name);
Steve Anton75737c02017-11-06 10:37:17 -08006274}
6275
6276cricket::DataChannelType PeerConnection::data_channel_type() const {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01006277 return data_channel_controller_.data_channel_type();
Steve Anton75737c02017-11-06 10:37:17 -08006278}
6279
6280bool PeerConnection::IceRestartPending(const std::string& content_name) const {
Karl Wibergf73f7d62019-04-08 15:36:53 +02006281 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08006282 return pending_ice_restarts_.find(content_name) !=
6283 pending_ice_restarts_.end();
6284}
6285
Steve Anton75737c02017-11-06 10:37:17 -08006286bool PeerConnection::NeedsIceRestart(const std::string& content_name) const {
6287 return transport_controller_->NeedsIceRestart(content_name);
6288}
6289
6290void PeerConnection::OnCertificateReady(
6291 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
6292 transport_controller_->SetLocalCertificate(certificate);
6293}
6294
6295void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
Steve Antonf8470812017-12-04 10:46:21 -08006296 SetSessionError(SessionError::kTransport,
6297 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
Steve Anton75737c02017-11-06 10:37:17 -08006298}
6299
Alex Loiko9289eda2018-11-23 16:18:59 +00006300void PeerConnection::OnTransportControllerConnectionState(
6301 cricket::IceConnectionState state) {
6302 switch (state) {
6303 case cricket::kIceConnectionConnecting:
6304 // If the current state is Connected or Completed, then there were
6305 // writable channels but now there are not, so the next state must
6306 // be Disconnected.
6307 // kIceConnectionConnecting is currently used as the default,
6308 // un-connected state by the TransportController, so its only use is
6309 // detecting disconnections.
6310 if (ice_connection_state_ ==
6311 PeerConnectionInterface::kIceConnectionConnected ||
6312 ice_connection_state_ ==
6313 PeerConnectionInterface::kIceConnectionCompleted) {
6314 SetIceConnectionState(
6315 PeerConnectionInterface::kIceConnectionDisconnected);
6316 }
6317 break;
6318 case cricket::kIceConnectionFailed:
6319 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
6320 break;
6321 case cricket::kIceConnectionConnected:
6322 RTC_LOG(LS_INFO) << "Changing to ICE connected state because "
6323 "all transports are writable.";
6324 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
6325 NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED);
6326 break;
6327 case cricket::kIceConnectionCompleted:
6328 RTC_LOG(LS_INFO) << "Changing to ICE completed state because "
6329 "all transports are complete.";
6330 if (ice_connection_state_ !=
6331 PeerConnectionInterface::kIceConnectionConnected) {
6332 // If jumping directly from "checking" to "connected",
6333 // signal "connected" first.
6334 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
6335 }
6336 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
6337 NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED);
6338 ReportTransportStats();
6339 break;
6340 default:
6341 RTC_NOTREACHED();
6342 }
6343}
6344
Steve Anton75737c02017-11-06 10:37:17 -08006345void PeerConnection::OnTransportControllerCandidatesGathered(
6346 const std::string& transport_name,
6347 const cricket::Candidates& candidates) {
Steve Anton75737c02017-11-06 10:37:17 -08006348 int sdp_mline_index;
6349 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01006350 RTC_LOG(LS_ERROR)
6351 << "OnTransportControllerCandidatesGathered: content name "
6352 << transport_name << " not found";
Steve Anton75737c02017-11-06 10:37:17 -08006353 return;
6354 }
6355
6356 for (cricket::Candidates::const_iterator citer = candidates.begin();
6357 citer != candidates.end(); ++citer) {
6358 // Use transport_name as the candidate media id.
6359 std::unique_ptr<JsepIceCandidate> candidate(
6360 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
6361 if (local_description()) {
6362 mutable_local_description()->AddCandidate(candidate.get());
6363 }
6364 OnIceCandidate(std::move(candidate));
6365 }
6366}
6367
Eldar Relloda13ea22019-06-01 12:23:43 +03006368void PeerConnection::OnTransportControllerCandidateError(
6369 const cricket::IceCandidateErrorEvent& event) {
Eldar Rello0095d372019-12-02 22:22:07 +02006370 OnIceCandidateError(event.address, event.port, event.url, event.error_code,
Eldar Relloda13ea22019-06-01 12:23:43 +03006371 event.error_text);
6372}
6373
Steve Anton75737c02017-11-06 10:37:17 -08006374void PeerConnection::OnTransportControllerCandidatesRemoved(
6375 const std::vector<cricket::Candidate>& candidates) {
Steve Anton75737c02017-11-06 10:37:17 -08006376 // Sanity check.
6377 for (const cricket::Candidate& candidate : candidates) {
6378 if (candidate.transport_name().empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01006379 RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
Jonas Olsson45cc8902018-02-13 10:37:07 +01006380 "empty content name in candidate "
Mirko Bonadei675513b2017-11-09 11:09:25 +01006381 << candidate.ToString();
Steve Anton75737c02017-11-06 10:37:17 -08006382 return;
6383 }
6384 }
6385
6386 if (local_description()) {
6387 mutable_local_description()->RemoveCandidates(candidates);
6388 }
6389 OnIceCandidatesRemoved(candidates);
6390}
6391
Alex Drake00c7ecf2019-08-06 10:54:47 -07006392void PeerConnection::OnTransportControllerCandidateChanged(
6393 const cricket::CandidatePairChangeEvent& event) {
6394 OnSelectedCandidatePairChanged(event);
6395}
6396
Steve Anton75737c02017-11-06 10:37:17 -08006397void PeerConnection::OnTransportControllerDtlsHandshakeError(
6398 rtc::SSLHandshakeError error) {
Qingsi Wang7fc821d2018-07-12 12:54:53 -07006399 RTC_HISTOGRAM_ENUMERATION(
6400 "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error),
6401 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
Steve Anton75737c02017-11-06 10:37:17 -08006402}
6403
Steve Antoned10bd92017-12-05 10:52:59 -08006404void PeerConnection::EnableSending() {
Mirko Bonadei739baf02019-01-27 17:29:42 +01006405 for (const auto& transceiver : transceivers_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08006406 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Antoned10bd92017-12-05 10:52:59 -08006407 if (channel && !channel->enabled()) {
6408 channel->Enable(true);
6409 }
Steve Anton75737c02017-11-06 10:37:17 -08006410 }
6411
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006412 if (data_channel_controller_.rtp_data_channel() &&
6413 !data_channel_controller_.rtp_data_channel()->enabled()) {
6414 data_channel_controller_.rtp_data_channel()->Enable(true);
Steve Anton4171afb2017-11-20 10:20:22 -08006415 }
Steve Anton75737c02017-11-06 10:37:17 -08006416}
6417
6418// Returns the media index for a local ice candidate given the content name.
6419bool PeerConnection::GetLocalCandidateMediaIndex(
6420 const std::string& content_name,
6421 int* sdp_mline_index) {
6422 if (!local_description() || !sdp_mline_index) {
6423 return false;
6424 }
6425
6426 bool content_found = false;
6427 const ContentInfos& contents = local_description()->description()->contents();
6428 for (size_t index = 0; index < contents.size(); ++index) {
6429 if (contents[index].name == content_name) {
6430 *sdp_mline_index = static_cast<int>(index);
6431 content_found = true;
6432 break;
6433 }
6434 }
6435 return content_found;
6436}
6437
6438bool PeerConnection::UseCandidatesInSessionDescription(
6439 const SessionDescriptionInterface* remote_desc) {
6440 if (!remote_desc) {
6441 return true;
6442 }
6443 bool ret = true;
6444
6445 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
6446 const IceCandidateCollection* candidates = remote_desc->candidates(m);
6447 for (size_t n = 0; n < candidates->count(); ++n) {
6448 const IceCandidateInterface* candidate = candidates->at(n);
6449 bool valid = false;
6450 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
6451 if (valid) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01006452 RTC_LOG(LS_INFO)
6453 << "UseCandidatesInSessionDescription: Not ready to use "
Jonas Olsson45cc8902018-02-13 10:37:07 +01006454 "candidate.";
Steve Anton75737c02017-11-06 10:37:17 -08006455 }
6456 continue;
6457 }
6458 ret = UseCandidate(candidate);
6459 if (!ret) {
6460 break;
6461 }
6462 }
6463 }
6464 return ret;
6465}
6466
6467bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) {
Guido Urdaneta41633172019-05-23 20:12:29 +02006468 RTCErrorOr<const cricket::ContentInfo*> result =
6469 FindContentInfo(remote_description(), candidate);
6470 if (!result.ok()) {
6471 RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. "
6472 << result.error().message();
Steve Anton75737c02017-11-06 10:37:17 -08006473 return false;
6474 }
Steve Anton75737c02017-11-06 10:37:17 -08006475 std::vector<cricket::Candidate> candidates;
6476 candidates.push_back(candidate->candidate());
6477 // Invoking BaseSession method to handle remote candidates.
Guido Urdaneta41633172019-05-23 20:12:29 +02006478 RTCError error = transport_controller_->AddRemoteCandidates(
6479 result.value()->name, candidates);
Henrik Boström5d8f8fa2018-04-13 15:22:50 +00006480 if (error.ok()) {
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07006481 ReportRemoteIceCandidateAdded(candidate->candidate());
Henrik Boström5d8f8fa2018-04-13 15:22:50 +00006482 // Candidates successfully submitted for checking.
6483 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
6484 ice_connection_state_ ==
6485 PeerConnectionInterface::kIceConnectionDisconnected) {
6486 // If state is New, then the session has just gotten its first remote ICE
6487 // candidates, so go to Checking.
6488 // If state is Disconnected, the session is re-using old candidates or
6489 // receiving additional ones, so go to Checking.
6490 // If state is Connected, stay Connected.
6491 // TODO(bemasc): If state is Connected, and the new candidates are for a
6492 // newly added transport, then the state actually _should_ move to
6493 // checking. Add a way to distinguish that case.
6494 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
6495 }
6496 // TODO(bemasc): If state is Completed, go back to Connected.
Jonas Olsson941a07c2018-09-13 10:07:07 +02006497 } else {
Zhi Huange830e682018-03-30 10:48:35 -07006498 RTC_LOG(LS_WARNING) << error.message();
Steve Anton75737c02017-11-06 10:37:17 -08006499 }
6500 return true;
6501}
6502
Guido Urdaneta41633172019-05-23 20:12:29 +02006503RTCErrorOr<const cricket::ContentInfo*> PeerConnection::FindContentInfo(
6504 const SessionDescriptionInterface* description,
6505 const IceCandidateInterface* candidate) {
6506 if (candidate->sdp_mline_index() >= 0) {
6507 size_t mediacontent_index =
6508 static_cast<size_t>(candidate->sdp_mline_index());
6509 size_t content_size = description->description()->contents().size();
6510 if (mediacontent_index < content_size) {
6511 return &description->description()->contents()[mediacontent_index];
6512 } else {
6513 return RTCError(RTCErrorType::INVALID_RANGE,
6514 "Media line index (" +
6515 rtc::ToString(candidate->sdp_mline_index()) +
6516 ") out of range (number of mlines: " +
6517 rtc::ToString(content_size) + ").");
6518 }
6519 } else if (!candidate->sdp_mid().empty()) {
6520 auto& contents = description->description()->contents();
6521 auto it = absl::c_find_if(
6522 contents, [candidate](const cricket::ContentInfo& content_info) {
6523 return content_info.mid() == candidate->sdp_mid();
6524 });
6525 if (it == contents.end()) {
6526 return RTCError(
6527 RTCErrorType::INVALID_PARAMETER,
6528 "Mid " + candidate->sdp_mid() +
6529 " specified but no media section with that mid found.");
6530 } else {
6531 return &*it;
6532 }
6533 }
6534
6535 return RTCError(RTCErrorType::INVALID_PARAMETER,
6536 "Neither sdp_mline_index nor sdp_mid specified.");
6537}
6538
Steve Anton75737c02017-11-06 10:37:17 -08006539void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) {
Steve Anton75737c02017-11-06 10:37:17 -08006540 // Destroy video channel first since it may have a pointer to the
6541 // voice channel.
6542 const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc);
Steve Anton6fec8802017-12-04 10:37:29 -08006543 if (!video_info || video_info->rejected) {
6544 DestroyTransceiverChannel(GetVideoTransceiver());
Steve Anton75737c02017-11-06 10:37:17 -08006545 }
6546
Steve Anton6fec8802017-12-04 10:37:29 -08006547 const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc);
6548 if (!audio_info || audio_info->rejected) {
6549 DestroyTransceiverChannel(GetAudioTransceiver());
Steve Anton75737c02017-11-06 10:37:17 -08006550 }
6551
6552 const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc);
6553 if (!data_info || data_info->rejected) {
Harald Alvestrand408cb4b2019-11-16 12:09:08 +01006554 DestroyDataChannelTransport();
Steve Anton75737c02017-11-06 10:37:17 -08006555 }
6556}
6557
Steve Antondcc3c022017-12-22 16:02:54 -08006558RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup(
6559 const SessionDescription& desc) const {
Steve Anton75737c02017-11-06 10:37:17 -08006560 const cricket::ContentGroup* bundle_group = nullptr;
6561 if (configuration_.bundle_policy ==
6562 PeerConnectionInterface::kBundlePolicyMaxBundle) {
Steve Antondcc3c022017-12-22 16:02:54 -08006563 bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
Steve Anton75737c02017-11-06 10:37:17 -08006564 if (!bundle_group) {
Steve Anton8a006912017-12-04 15:25:56 -08006565 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6566 "max-bundle configured but session description "
6567 "has no BUNDLE group");
Steve Anton75737c02017-11-06 10:37:17 -08006568 }
6569 }
Mirko Bonadei9f3a44f2019-01-30 13:47:42 +01006570 return bundle_group;
Steve Antondcc3c022017-12-22 16:02:54 -08006571}
6572
6573RTCError PeerConnection::CreateChannels(const SessionDescription& desc) {
Zhi Huange830e682018-03-30 10:48:35 -07006574 // Creating the media channels. Transports should already have been created
6575 // at this point.
Steve Antondcc3c022017-12-22 16:02:54 -08006576 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc);
Steve Antoneda6ccd2017-12-04 10:21:55 -08006577 if (voice && !voice->rejected &&
6578 !GetAudioTransceiver()->internal()->channel()) {
Zhi Huange830e682018-03-30 10:48:35 -07006579 cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name);
Steve Antoneda6ccd2017-12-04 10:21:55 -08006580 if (!voice_channel) {
Steve Anton8a006912017-12-04 15:25:56 -08006581 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
6582 "Failed to create voice channel.");
Steve Antoneda6ccd2017-12-04 10:21:55 -08006583 }
6584 GetAudioTransceiver()->internal()->SetChannel(voice_channel);
6585 }
6586
Steve Antondcc3c022017-12-22 16:02:54 -08006587 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc);
Steve Antoneda6ccd2017-12-04 10:21:55 -08006588 if (video && !video->rejected &&
6589 !GetVideoTransceiver()->internal()->channel()) {
Zhi Huange830e682018-03-30 10:48:35 -07006590 cricket::VideoChannel* video_channel = CreateVideoChannel(video->name);
Steve Antoneda6ccd2017-12-04 10:21:55 -08006591 if (!video_channel) {
Steve Anton8a006912017-12-04 15:25:56 -08006592 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
6593 "Failed to create video channel.");
Steve Anton75737c02017-11-06 10:37:17 -08006594 }
Steve Antoneda6ccd2017-12-04 10:21:55 -08006595 GetVideoTransceiver()->internal()->SetChannel(video_channel);
Steve Anton75737c02017-11-06 10:37:17 -08006596 }
6597
Steve Antondcc3c022017-12-22 16:02:54 -08006598 const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc);
Harald Alvestrand05e4d082019-12-03 14:04:21 +01006599 if (data_channel_type() != cricket::DCT_NONE && data && !data->rejected &&
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006600 !data_channel_controller_.rtp_data_channel() &&
6601 !data_channel_controller_.data_channel_transport()) {
Zhi Huange830e682018-03-30 10:48:35 -07006602 if (!CreateDataChannel(data->name)) {
Steve Anton8a006912017-12-04 15:25:56 -08006603 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
6604 "Failed to create data channel.");
Steve Anton75737c02017-11-06 10:37:17 -08006605 }
6606 }
6607
Steve Anton8a006912017-12-04 15:25:56 -08006608 return RTCError::OK();
Steve Anton75737c02017-11-06 10:37:17 -08006609}
6610
Steve Anton4171afb2017-11-20 10:20:22 -08006611// TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
Steve Antoneda6ccd2017-12-04 10:21:55 -08006612cricket::VoiceChannel* PeerConnection::CreateVoiceChannel(
Zhi Huange830e682018-03-30 10:48:35 -07006613 const std::string& mid) {
Anton Sukhanov98a462c2018-10-17 13:15:42 -07006614 RtpTransportInternal* rtp_transport = GetRtpTransport(mid);
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07006615 MediaTransportConfig media_transport_config =
6616 transport_controller_->GetMediaTransportConfig(mid);
Anton Sukhanov98a462c2018-10-17 13:15:42 -07006617
Steve Anton75737c02017-11-06 10:37:17 -08006618 cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel(
Anton Sukhanov4f08faa2019-05-21 11:12:57 -07006619 call_ptr_, configuration_.media_config, rtp_transport,
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07006620 media_transport_config, signaling_thread(), mid, SrtpRequired(),
6621 GetCryptoOptions(), &ssrc_generator_, audio_options_);
Steve Anton75737c02017-11-06 10:37:17 -08006622 if (!voice_channel) {
Steve Antoneda6ccd2017-12-04 10:21:55 -08006623 return nullptr;
Steve Anton75737c02017-11-06 10:37:17 -08006624 }
Steve Anton75737c02017-11-06 10:37:17 -08006625 voice_channel->SignalDtlsSrtpSetupFailure.connect(
6626 this, &PeerConnection::OnDtlsSrtpSetupFailure);
Steve Anton75737c02017-11-06 10:37:17 -08006627 voice_channel->SignalSentPacket.connect(this,
6628 &PeerConnection::OnSentPacket_w);
Zhi Huange830e682018-03-30 10:48:35 -07006629 voice_channel->SetRtpTransport(rtp_transport);
Steve Anton4171afb2017-11-20 10:20:22 -08006630
Steve Antoneda6ccd2017-12-04 10:21:55 -08006631 return voice_channel;
Steve Anton75737c02017-11-06 10:37:17 -08006632}
6633
Steve Anton4171afb2017-11-20 10:20:22 -08006634// TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
Steve Antoneda6ccd2017-12-04 10:21:55 -08006635cricket::VideoChannel* PeerConnection::CreateVideoChannel(
Zhi Huange830e682018-03-30 10:48:35 -07006636 const std::string& mid) {
Anton Sukhanov98a462c2018-10-17 13:15:42 -07006637 RtpTransportInternal* rtp_transport = GetRtpTransport(mid);
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07006638 MediaTransportConfig media_transport_config =
6639 transport_controller_->GetMediaTransportConfig(mid);
Anton Sukhanov98a462c2018-10-17 13:15:42 -07006640
Steve Anton75737c02017-11-06 10:37:17 -08006641 cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel(
Anton Sukhanov4f08faa2019-05-21 11:12:57 -07006642 call_ptr_, configuration_.media_config, rtp_transport,
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07006643 media_transport_config, signaling_thread(), mid, SrtpRequired(),
6644 GetCryptoOptions(), &ssrc_generator_, video_options_,
Anton Sukhanov4f08faa2019-05-21 11:12:57 -07006645 video_bitrate_allocator_factory_.get());
Steve Anton75737c02017-11-06 10:37:17 -08006646 if (!video_channel) {
Steve Antoneda6ccd2017-12-04 10:21:55 -08006647 return nullptr;
Steve Anton75737c02017-11-06 10:37:17 -08006648 }
Steve Anton75737c02017-11-06 10:37:17 -08006649 video_channel->SignalDtlsSrtpSetupFailure.connect(
6650 this, &PeerConnection::OnDtlsSrtpSetupFailure);
Steve Anton75737c02017-11-06 10:37:17 -08006651 video_channel->SignalSentPacket.connect(this,
6652 &PeerConnection::OnSentPacket_w);
Zhi Huange830e682018-03-30 10:48:35 -07006653 video_channel->SetRtpTransport(rtp_transport);
Steve Anton4171afb2017-11-20 10:20:22 -08006654
Steve Antoneda6ccd2017-12-04 10:21:55 -08006655 return video_channel;
Steve Anton75737c02017-11-06 10:37:17 -08006656}
6657
Zhi Huange830e682018-03-30 10:48:35 -07006658bool PeerConnection::CreateDataChannel(const std::string& mid) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01006659 switch (data_channel_type()) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006660 case cricket::DCT_SCTP:
Qingsi Wang437077d2019-09-10 17:52:26 +00006661 case cricket::DCT_DATA_CHANNEL_TRANSPORT_SCTP:
Qingsi Wang437077d2019-09-10 17:52:26 +00006662 case cricket::DCT_DATA_CHANNEL_TRANSPORT:
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006663 if (network_thread()->Invoke<bool>(
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07006664 RTC_FROM_HERE,
6665 rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, this,
6666 mid))) {
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006667 sctp_mid_s_ = mid;
6668 } else {
Qingsi Wang437077d2019-09-10 17:52:26 +00006669 return false;
6670 }
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07006671
6672 // All non-RTP data channels must initialize |sctp_data_channels_|.
Harald Alvestrand05e4d082019-12-03 14:04:21 +01006673 for (const auto& channel :
6674 *data_channel_controller_.sctp_data_channels()) {
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07006675 channel->OnTransportChannelCreated();
6676 }
6677 return true;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006678 case cricket::DCT_RTP:
6679 default:
6680 RtpTransportInternal* rtp_transport = GetRtpTransport(mid);
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006681 data_channel_controller_.set_rtp_data_channel(
6682 channel_manager()->CreateRtpDataChannel(
6683 configuration_.media_config, rtp_transport, signaling_thread(),
6684 mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_));
6685 if (!data_channel_controller_.rtp_data_channel()) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006686 return false;
6687 }
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006688 data_channel_controller_.rtp_data_channel()
6689 ->SignalDtlsSrtpSetupFailure.connect(
6690 this, &PeerConnection::OnDtlsSrtpSetupFailure);
6691 data_channel_controller_.rtp_data_channel()->SignalSentPacket.connect(
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006692 this, &PeerConnection::OnSentPacket_w);
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006693 data_channel_controller_.rtp_data_channel()->SetRtpTransport(
6694 rtp_transport);
Eldar Rellod9ebe012020-03-18 20:41:45 +02006695 have_pending_rtp_data_channel_ = true;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006696 return true;
Steve Anton75737c02017-11-06 10:37:17 -08006697 }
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07006698 return false;
Steve Anton75737c02017-11-06 10:37:17 -08006699}
6700
6701Call::Stats PeerConnection::GetCallStats() {
6702 if (!worker_thread()->IsCurrent()) {
6703 return worker_thread()->Invoke<Call::Stats>(
6704 RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this));
6705 }
Karl Wiberg6cab5c82019-03-26 09:57:01 +01006706 RTC_DCHECK_RUN_ON(worker_thread());
Steve Anton75737c02017-11-06 10:37:17 -08006707 if (call_) {
6708 return call_->GetStats();
6709 } else {
6710 return Call::Stats();
6711 }
6712}
6713
Bjorn A Mellemb689af42019-08-21 10:44:59 -07006714bool PeerConnection::SetupDataChannelTransport_n(const std::string& mid) {
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07006715 DataChannelTransportInterface* transport =
6716 transport_controller_->GetDataChannelTransport(mid);
6717 if (!transport) {
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08006718 RTC_LOG(LS_ERROR)
Bjorn A Mellemb689af42019-08-21 10:44:59 -07006719 << "Data channel transport is not available for data channels, mid="
6720 << mid;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006721 return false;
6722 }
Bjorn A Mellemb689af42019-08-21 10:44:59 -07006723 RTC_LOG(LS_INFO) << "Setting up data channel transport for mid=" << mid;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006724
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006725 data_channel_controller_.set_data_channel_transport(transport);
6726 data_channel_controller_.SetupDataChannelTransport_n();
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006727 sctp_mid_n_ = mid;
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07006728
6729 // Note: setting the data sink and checking initial state must be done last,
6730 // after setting up the data channel. Setting the data sink may trigger
6731 // callbacks to PeerConnection which require the transport to be completely
6732 // set up (eg. OnReadyToSend()).
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006733 transport->SetDataSink(&data_channel_controller_);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006734 return true;
6735}
6736
Bjorn A Mellemb689af42019-08-21 10:44:59 -07006737void PeerConnection::TeardownDataChannelTransport_n() {
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006738 if (!sctp_mid_n_ && !data_channel_controller_.data_channel_transport()) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006739 return;
6740 }
Bjorn A Mellemb689af42019-08-21 10:44:59 -07006741 RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid="
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006742 << *sctp_mid_n_;
Bjorn A Mellemb689af42019-08-21 10:44:59 -07006743
Bjorn A Mellemb689af42019-08-21 10:44:59 -07006744 // |sctp_mid_| may still be active through an SCTP transport. If not, unset
6745 // it.
Harald Alvestrand7a829a82020-02-12 07:38:21 +01006746 sctp_mid_n_.reset();
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01006747 data_channel_controller_.TeardownDataChannelTransport_n();
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08006748}
6749
Steve Anton75737c02017-11-06 10:37:17 -08006750// Returns false if bundle is enabled and rtcp_mux is disabled.
6751bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) {
6752 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
6753 if (!bundle_enabled)
6754 return true;
6755
6756 const cricket::ContentGroup* bundle_group =
6757 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
6758 RTC_DCHECK(bundle_group != NULL);
6759
6760 const cricket::ContentInfos& contents = desc->contents();
6761 for (cricket::ContentInfos::const_iterator citer = contents.begin();
6762 citer != contents.end(); ++citer) {
6763 const cricket::ContentInfo* content = (&*citer);
6764 RTC_DCHECK(content != NULL);
6765 if (bundle_group->HasContentName(content->name) && !content->rejected &&
Steve Anton5adfafd2017-12-20 16:34:00 -08006766 content->type == MediaProtocolType::kRtp) {
Steve Anton75737c02017-11-06 10:37:17 -08006767 if (!HasRtcpMuxEnabled(content))
6768 return false;
6769 }
6770 }
6771 // RTCP-MUX is enabled in all the contents.
6772 return true;
6773}
6774
6775bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08006776 return content->media_description()->rtcp_mux();
Steve Anton75737c02017-11-06 10:37:17 -08006777}
6778
Steve Anton06817cd2018-12-18 15:55:30 -08006779static RTCError ValidateMids(const cricket::SessionDescription& description) {
6780 std::set<std::string> mids;
6781 for (const cricket::ContentInfo& content : description.contents()) {
Steve Antonceac0152018-12-19 11:32:20 -08006782 if (content.name.empty()) {
6783 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6784 "A media section is missing a MID attribute.");
6785 }
Steve Anton06817cd2018-12-18 15:55:30 -08006786 if (!mids.insert(content.name).second) {
6787 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6788 "Duplicate a=mid value '" + content.name + "'.");
6789 }
6790 }
6791 return RTCError::OK();
6792}
6793
Steve Anton8a006912017-12-04 15:25:56 -08006794RTCError PeerConnection::ValidateSessionDescription(
Steve Anton75737c02017-11-06 10:37:17 -08006795 const SessionDescriptionInterface* sdesc,
Steve Anton8a006912017-12-04 15:25:56 -08006796 cricket::ContentSource source) {
Steve Antonf8470812017-12-04 10:46:21 -08006797 if (session_error() != SessionError::kNone) {
Steve Anton8a006912017-12-04 15:25:56 -08006798 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
Steve Anton75737c02017-11-06 10:37:17 -08006799 }
6800
6801 if (!sdesc || !sdesc->description()) {
Steve Anton8a006912017-12-04 15:25:56 -08006802 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
Steve Anton75737c02017-11-06 10:37:17 -08006803 }
6804
Steve Anton3828c062017-12-06 10:34:51 -08006805 SdpType type = sdesc->GetType();
6806 if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) ||
6807 (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) {
Steve Anton8a006912017-12-04 15:25:56 -08006808 LOG_AND_RETURN_ERROR(
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01006809 RTCErrorType::INVALID_STATE,
Steve Anton8a006912017-12-04 15:25:56 -08006810 "Called in wrong state: " + GetSignalingStateString(signaling_state()));
Steve Anton75737c02017-11-06 10:37:17 -08006811 }
6812
Steve Anton06817cd2018-12-18 15:55:30 -08006813 RTCError error = ValidateMids(*sdesc->description());
6814 if (!error.ok()) {
6815 return error;
6816 }
6817
Steve Anton75737c02017-11-06 10:37:17 -08006818 // Verify crypto settings.
6819 std::string crypto_error;
Steve Anton8a006912017-12-04 15:25:56 -08006820 if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
6821 dtls_enabled_) {
Qingsi Wang7fc821d2018-07-12 12:54:53 -07006822 RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_);
Steve Anton8a006912017-12-04 15:25:56 -08006823 if (!crypto_error.ok()) {
6824 return crypto_error;
6825 }
Steve Anton75737c02017-11-06 10:37:17 -08006826 }
6827
6828 // Verify ice-ufrag and ice-pwd.
6829 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
Steve Anton8a006912017-12-04 15:25:56 -08006830 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6831 kSdpWithoutIceUfragPwd);
Steve Anton75737c02017-11-06 10:37:17 -08006832 }
6833
6834 if (!ValidateBundleSettings(sdesc->description())) {
Steve Anton8a006912017-12-04 15:25:56 -08006835 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6836 kBundleWithoutRtcpMux);
Steve Anton75737c02017-11-06 10:37:17 -08006837 }
6838
6839 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
6840 // m-lines that do not rtcp-mux enabled.
6841
6842 // Verify m-lines in Answer when compared against Offer.
Steve Anton3828c062017-12-06 10:34:51 -08006843 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
Seth Hampsonae8a90a2018-02-13 15:33:48 -08006844 // With an answer we want to compare the new answer session description with
6845 // the offer's session description from the current negotiation.
Steve Anton75737c02017-11-06 10:37:17 -08006846 const cricket::SessionDescription* offer_desc =
6847 (source == cricket::CS_LOCAL) ? remote_description()->description()
6848 : local_description()->description();
Seth Hampsonae8a90a2018-02-13 15:33:48 -08006849 if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) ||
6850 !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(),
6851 type)) {
Steve Anton8a006912017-12-04 15:25:56 -08006852 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6853 kMlineMismatchInAnswer);
Steve Anton75737c02017-11-06 10:37:17 -08006854 }
6855 } else {
Steve Anton75737c02017-11-06 10:37:17 -08006856 // The re-offers should respect the order of m= sections in current
6857 // description. See RFC3264 Section 8 paragraph 4 for more details.
Seth Hampsonae8a90a2018-02-13 15:33:48 -08006858 // With a re-offer, either the current local or current remote descriptions
6859 // could be the most up to date, so we would like to check against both of
6860 // them if they exist. It could be the case that one of them has a 0 port
6861 // for a media section, but the other does not. This is important to check
6862 // against in the case that we are recycling an m= section.
6863 const cricket::SessionDescription* current_desc = nullptr;
6864 const cricket::SessionDescription* secondary_current_desc = nullptr;
6865 if (local_description()) {
6866 current_desc = local_description()->description();
6867 if (remote_description()) {
6868 secondary_current_desc = remote_description()->description();
6869 }
6870 } else if (remote_description()) {
6871 current_desc = remote_description()->description();
6872 }
Steve Anton75737c02017-11-06 10:37:17 -08006873 if (current_desc &&
Seth Hampsonae8a90a2018-02-13 15:33:48 -08006874 !MediaSectionsInSameOrder(*current_desc, secondary_current_desc,
6875 *sdesc->description(), type)) {
Steve Anton8a006912017-12-04 15:25:56 -08006876 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6877 kMlineMismatchInSubsequentOffer);
Steve Anton75737c02017-11-06 10:37:17 -08006878 }
6879 }
6880
Steve Antonba42e992018-04-09 14:10:01 -07006881 if (IsUnifiedPlan()) {
6882 // Ensure that each audio and video media section has at most one
6883 // "StreamParams". This will return an error if receiving a session
6884 // description from a "Plan B" endpoint which adds multiple tracks of the
6885 // same type. With Unified Plan, there can only be at most one track per
6886 // media section.
6887 for (const ContentInfo& content : sdesc->description()->contents()) {
Harald Alvestrand1716d392019-06-03 20:35:45 +02006888 const MediaContentDescription& desc = *content.media_description();
Steve Antonba42e992018-04-09 14:10:01 -07006889 if ((desc.type() == cricket::MEDIA_TYPE_AUDIO ||
6890 desc.type() == cricket::MEDIA_TYPE_VIDEO) &&
6891 desc.streams().size() > 1u) {
6892 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
6893 "Media section has more than one track specified "
6894 "with a=ssrc lines which is not supported with "
6895 "Unified Plan.");
6896 }
6897 }
6898 }
6899
Steve Anton8a006912017-12-04 15:25:56 -08006900 return RTCError::OK();
Steve Anton75737c02017-11-06 10:37:17 -08006901}
6902
Steve Anton3828c062017-12-06 10:34:51 -08006903bool PeerConnection::ExpectSetLocalDescription(SdpType type) {
Steve Anton75737c02017-11-06 10:37:17 -08006904 PeerConnectionInterface::SignalingState state = signaling_state();
Steve Anton3828c062017-12-06 10:34:51 -08006905 if (type == SdpType::kOffer) {
Steve Anton75737c02017-11-06 10:37:17 -08006906 return (state == PeerConnectionInterface::kStable) ||
6907 (state == PeerConnectionInterface::kHaveLocalOffer);
Steve Anton20393062017-12-04 16:24:52 -08006908 } else {
Steve Anton3828c062017-12-06 10:34:51 -08006909 RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
Steve Anton75737c02017-11-06 10:37:17 -08006910 return (state == PeerConnectionInterface::kHaveRemoteOffer) ||
6911 (state == PeerConnectionInterface::kHaveLocalPrAnswer);
6912 }
6913}
6914
Steve Anton3828c062017-12-06 10:34:51 -08006915bool PeerConnection::ExpectSetRemoteDescription(SdpType type) {
Steve Anton75737c02017-11-06 10:37:17 -08006916 PeerConnectionInterface::SignalingState state = signaling_state();
Steve Anton3828c062017-12-06 10:34:51 -08006917 if (type == SdpType::kOffer) {
Steve Anton75737c02017-11-06 10:37:17 -08006918 return (state == PeerConnectionInterface::kStable) ||
6919 (state == PeerConnectionInterface::kHaveRemoteOffer);
Steve Anton20393062017-12-04 16:24:52 -08006920 } else {
Steve Anton3828c062017-12-06 10:34:51 -08006921 RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
Steve Anton75737c02017-11-06 10:37:17 -08006922 return (state == PeerConnectionInterface::kHaveLocalOffer) ||
6923 (state == PeerConnectionInterface::kHaveRemotePrAnswer);
6924 }
6925}
6926
Steve Antonf8470812017-12-04 10:46:21 -08006927const char* PeerConnection::SessionErrorToString(SessionError error) const {
6928 switch (error) {
6929 case SessionError::kNone:
6930 return "ERROR_NONE";
6931 case SessionError::kContent:
6932 return "ERROR_CONTENT";
6933 case SessionError::kTransport:
6934 return "ERROR_TRANSPORT";
6935 }
6936 RTC_NOTREACHED();
6937 return "";
6938}
6939
Steve Anton75737c02017-11-06 10:37:17 -08006940std::string PeerConnection::GetSessionErrorMsg() {
Jonas Olsson366a50c2018-09-06 13:41:30 +02006941 rtc::StringBuilder desc;
Steve Antonf8470812017-12-04 10:46:21 -08006942 desc << kSessionError << SessionErrorToString(session_error()) << ". ";
6943 desc << kSessionErrorDesc << session_error_desc() << ".";
Jonas Olsson84df1c72018-09-14 16:59:32 +02006944 return desc.Release();
Steve Anton75737c02017-11-06 10:37:17 -08006945}
6946
Steve Anton8e20f172018-03-06 10:55:04 -08006947void PeerConnection::ReportSdpFormatReceived(
6948 const SessionDescriptionInterface& remote_offer) {
Steve Anton8e20f172018-03-06 10:55:04 -08006949 int num_audio_mlines = 0;
6950 int num_video_mlines = 0;
6951 int num_audio_tracks = 0;
6952 int num_video_tracks = 0;
6953 for (const ContentInfo& content : remote_offer.description()->contents()) {
6954 cricket::MediaType media_type = content.media_description()->type();
6955 int num_tracks = std::max(
6956 1, static_cast<int>(content.media_description()->streams().size()));
6957 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
6958 num_audio_mlines += 1;
6959 num_audio_tracks += num_tracks;
6960 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
6961 num_video_mlines += 1;
6962 num_video_tracks += num_tracks;
6963 }
6964 }
6965 SdpFormatReceived format = kSdpFormatReceivedNoTracks;
6966 if (num_audio_mlines > 1 || num_video_mlines > 1) {
6967 format = kSdpFormatReceivedComplexUnifiedPlan;
6968 } else if (num_audio_tracks > 1 || num_video_tracks > 1) {
6969 format = kSdpFormatReceivedComplexPlanB;
6970 } else if (num_audio_tracks > 0 || num_video_tracks > 0) {
6971 format = kSdpFormatReceivedSimple;
6972 }
Qingsi Wang7fc821d2018-07-12 12:54:53 -07006973 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format,
6974 kSdpFormatReceivedMax);
Steve Anton8e20f172018-03-06 10:55:04 -08006975}
6976
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07006977void PeerConnection::ReportIceCandidateCollected(
6978 const cricket::Candidate& candidate) {
6979 NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED);
6980 if (candidate.address().IsPrivateIP()) {
6981 NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED);
6982 }
6983 if (candidate.address().IsUnresolvedIP()) {
6984 NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED);
6985 }
6986 if (candidate.address().family() == AF_INET6) {
6987 NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED);
6988 }
6989}
6990
6991void PeerConnection::ReportRemoteIceCandidateAdded(
6992 const cricket::Candidate& candidate) {
6993 NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED);
6994 if (candidate.address().IsPrivateIP()) {
6995 NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED);
6996 }
6997 if (candidate.address().IsUnresolvedIP()) {
6998 NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED);
6999 }
7000 if (candidate.address().family() == AF_INET6) {
7001 NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED);
7002 }
7003}
7004
Harald Alvestrand8ebba742018-05-31 14:00:34 +02007005void PeerConnection::NoteUsageEvent(UsageEvent event) {
7006 RTC_DCHECK_RUN_ON(signaling_thread());
7007 usage_event_accumulator_ |= static_cast<int>(event);
7008}
7009
7010void PeerConnection::ReportUsagePattern() const {
7011 RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_;
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007012 RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern",
7013 usage_event_accumulator_,
7014 static_cast<int>(UsageEvent::MAX_VALUE));
Harald Alvestrandc0e97252018-07-26 10:39:55 +02007015 const int bad_bits =
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07007016 static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED) |
Harald Alvestrandc0e97252018-07-26 10:39:55 +02007017 static_cast<int>(UsageEvent::CANDIDATE_COLLECTED);
7018 const int good_bits =
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07007019 static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED) |
Harald Alvestrandc0e97252018-07-26 10:39:55 +02007020 static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) |
7021 static_cast<int>(UsageEvent::ICE_STATE_CONNECTED);
7022 if ((usage_event_accumulator_ & bad_bits) == bad_bits &&
7023 (usage_event_accumulator_ & good_bits) == 0) {
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02007024 // If called after close(), we can't report, because observer may have
7025 // been deallocated, and therefore pointer is null. Write to log instead.
7026 if (observer_) {
7027 Observer()->OnInterestingUsage(usage_event_accumulator_);
7028 } else {
7029 RTC_LOG(LS_INFO) << "Interesting usage signature "
7030 << usage_event_accumulator_
7031 << " observed after observer shutdown";
7032 }
Harald Alvestrandc0e97252018-07-26 10:39:55 +02007033 }
Harald Alvestrand8ebba742018-05-31 14:00:34 +02007034}
7035
Steve Anton0ffaaa22018-02-23 10:31:30 -08007036void PeerConnection::ReportNegotiatedSdpSemantics(
7037 const SessionDescriptionInterface& answer) {
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007038 SdpSemanticNegotiated semantics_negotiated;
Steve Anton0ffaaa22018-02-23 10:31:30 -08007039 switch (answer.description()->msid_signaling()) {
7040 case 0:
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007041 semantics_negotiated = kSdpSemanticNegotiatedNone;
Steve Anton0ffaaa22018-02-23 10:31:30 -08007042 break;
7043 case cricket::kMsidSignalingMediaSection:
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007044 semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan;
Steve Anton0ffaaa22018-02-23 10:31:30 -08007045 break;
7046 case cricket::kMsidSignalingSsrcAttribute:
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007047 semantics_negotiated = kSdpSemanticNegotiatedPlanB;
Steve Anton0ffaaa22018-02-23 10:31:30 -08007048 break;
7049 case cricket::kMsidSignalingMediaSection |
7050 cricket::kMsidSignalingSsrcAttribute:
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007051 semantics_negotiated = kSdpSemanticNegotiatedMixed;
Steve Anton0ffaaa22018-02-23 10:31:30 -08007052 break;
7053 default:
7054 RTC_NOTREACHED();
7055 }
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007056 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated",
7057 semantics_negotiated, kSdpSemanticNegotiatedMax);
Steve Anton0ffaaa22018-02-23 10:31:30 -08007058}
7059
Steve Anton75737c02017-11-06 10:37:17 -08007060// We need to check the local/remote description for the Transport instead of
7061// the session, because a new Transport added during renegotiation may have
7062// them unset while the session has them set from the previous negotiation.
7063// Not doing so may trigger the auto generation of transport description and
7064// mess up DTLS identity information, ICE credential, etc.
7065bool PeerConnection::ReadyToUseRemoteCandidate(
7066 const IceCandidateInterface* candidate,
7067 const SessionDescriptionInterface* remote_desc,
7068 bool* valid) {
7069 *valid = true;
7070
7071 const SessionDescriptionInterface* current_remote_desc =
7072 remote_desc ? remote_desc : remote_description();
7073
7074 if (!current_remote_desc) {
7075 return false;
7076 }
7077
Guido Urdaneta41633172019-05-23 20:12:29 +02007078 RTCErrorOr<const cricket::ContentInfo*> result =
7079 FindContentInfo(current_remote_desc, candidate);
7080 if (!result.ok()) {
7081 RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. "
7082 << result.error().message();
Steve Anton75737c02017-11-06 10:37:17 -08007083
7084 *valid = false;
7085 return false;
7086 }
7087
Guido Urdaneta41633172019-05-23 20:12:29 +02007088 std::string transport_name = GetTransportName(result.value()->name);
7089 return !transport_name.empty();
Steve Anton75737c02017-11-06 10:37:17 -08007090}
7091
7092bool PeerConnection::SrtpRequired() const {
Bjorn A Mellem5985a042019-06-28 14:19:38 -07007093 return !use_datagram_transport_ &&
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07007094 (dtls_enabled_ ||
7095 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED);
Steve Anton75737c02017-11-06 10:37:17 -08007096}
7097
7098void PeerConnection::OnTransportControllerGatheringState(
7099 cricket::IceGatheringState state) {
7100 RTC_DCHECK(signaling_thread()->IsCurrent());
7101 if (state == cricket::kIceGatheringGathering) {
7102 OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering);
7103 } else if (state == cricket::kIceGatheringComplete) {
7104 OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete);
7105 }
7106}
7107
7108void PeerConnection::ReportTransportStats() {
Steve Antonc7b964c2018-02-01 14:39:45 -08007109 std::map<std::string, std::set<cricket::MediaType>>
7110 media_types_by_transport_name;
Mirko Bonadei739baf02019-01-27 17:29:42 +01007111 for (const auto& transceiver : transceivers_) {
Steve Antonc7b964c2018-02-01 14:39:45 -08007112 if (transceiver->internal()->channel()) {
7113 const std::string& transport_name =
7114 transceiver->internal()->channel()->transport_name();
7115 media_types_by_transport_name[transport_name].insert(
Steve Anton69470252018-02-09 11:43:08 -08007116 transceiver->media_type());
Steve Antonc7b964c2018-02-01 14:39:45 -08007117 }
Steve Anton75737c02017-11-06 10:37:17 -08007118 }
7119 if (rtp_data_channel()) {
Steve Antonc7b964c2018-02-01 14:39:45 -08007120 media_types_by_transport_name[rtp_data_channel()->transport_name()].insert(
7121 cricket::MEDIA_TYPE_DATA);
Steve Anton75737c02017-11-06 10:37:17 -08007122 }
Zhi Huange830e682018-03-30 10:48:35 -07007123
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02007124 absl::optional<std::string> transport_name = sctp_transport_name();
Zhi Huange830e682018-03-30 10:48:35 -07007125 if (transport_name) {
7126 media_types_by_transport_name[*transport_name].insert(
Steve Antonc7b964c2018-02-01 14:39:45 -08007127 cricket::MEDIA_TYPE_DATA);
Steve Anton75737c02017-11-06 10:37:17 -08007128 }
Zhi Huange830e682018-03-30 10:48:35 -07007129
Steve Antonc7b964c2018-02-01 14:39:45 -08007130 for (const auto& entry : media_types_by_transport_name) {
7131 const std::string& transport_name = entry.first;
7132 const std::set<cricket::MediaType> media_types = entry.second;
Steve Anton75737c02017-11-06 10:37:17 -08007133 cricket::TransportStats stats;
Steve Antonc7b964c2018-02-01 14:39:45 -08007134 if (transport_controller_->GetStats(transport_name, &stats)) {
Steve Anton75737c02017-11-06 10:37:17 -08007135 ReportBestConnectionState(stats);
Steve Antonc7b964c2018-02-01 14:39:45 -08007136 ReportNegotiatedCiphers(stats, media_types);
Steve Anton75737c02017-11-06 10:37:17 -08007137 }
7138 }
7139}
7140// Walk through the ConnectionInfos to gather best connection usage
7141// for IPv4 and IPv6.
7142void PeerConnection::ReportBestConnectionState(
7143 const cricket::TransportStats& stats) {
Steve Antonc7b964c2018-02-01 14:39:45 -08007144 for (const cricket::TransportChannelStats& channel_stats :
7145 stats.channel_stats) {
7146 for (const cricket::ConnectionInfo& connection_info :
Jonas Oreland149dc722019-08-28 08:10:27 +02007147 channel_stats.ice_transport_stats.connection_infos) {
Steve Antonc7b964c2018-02-01 14:39:45 -08007148 if (!connection_info.best_connection) {
Steve Anton75737c02017-11-06 10:37:17 -08007149 continue;
7150 }
7151
Steve Antonc7b964c2018-02-01 14:39:45 -08007152 const cricket::Candidate& local = connection_info.local_candidate;
7153 const cricket::Candidate& remote = connection_info.remote_candidate;
Steve Anton75737c02017-11-06 10:37:17 -08007154
7155 // Increment the counter for IceCandidatePairType.
7156 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
7157 (local.type() == RELAY_PORT_TYPE &&
7158 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007159 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP",
7160 GetIceCandidatePairCounter(local, remote),
7161 kIceCandidatePairMax);
Steve Anton75737c02017-11-06 10:37:17 -08007162 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007163 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP",
7164 GetIceCandidatePairCounter(local, remote),
7165 kIceCandidatePairMax);
Steve Anton75737c02017-11-06 10:37:17 -08007166 } else {
7167 RTC_CHECK(0);
7168 }
Steve Anton75737c02017-11-06 10:37:17 -08007169
7170 // Increment the counter for IP type.
7171 if (local.address().family() == AF_INET) {
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007172 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics",
7173 kBestConnections_IPv4,
7174 kPeerConnectionAddressFamilyCounter_Max);
Steve Anton75737c02017-11-06 10:37:17 -08007175 } else if (local.address().family() == AF_INET6) {
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007176 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics",
7177 kBestConnections_IPv6,
7178 kPeerConnectionAddressFamilyCounter_Max);
Steve Anton75737c02017-11-06 10:37:17 -08007179 } else {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08007180 RTC_CHECK(!local.address().hostname().empty() &&
7181 local.address().IsUnresolvedIP());
Steve Anton75737c02017-11-06 10:37:17 -08007182 }
7183
7184 return;
7185 }
7186 }
7187}
7188
7189void PeerConnection::ReportNegotiatedCiphers(
Steve Antonc7b964c2018-02-01 14:39:45 -08007190 const cricket::TransportStats& stats,
7191 const std::set<cricket::MediaType>& media_types) {
Steve Anton75737c02017-11-06 10:37:17 -08007192 if (!dtls_enabled_ || stats.channel_stats.empty()) {
7193 return;
7194 }
7195
7196 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
7197 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
7198 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
7199 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
7200 return;
7201 }
7202
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007203 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
7204 for (cricket::MediaType media_type : media_types) {
7205 switch (media_type) {
7206 case cricket::MEDIA_TYPE_AUDIO:
7207 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7208 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite,
7209 rtc::SRTP_CRYPTO_SUITE_MAX_VALUE);
7210 break;
7211 case cricket::MEDIA_TYPE_VIDEO:
7212 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7213 "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite,
7214 rtc::SRTP_CRYPTO_SUITE_MAX_VALUE);
7215 break;
7216 case cricket::MEDIA_TYPE_DATA:
7217 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7218 "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite,
7219 rtc::SRTP_CRYPTO_SUITE_MAX_VALUE);
7220 break;
7221 default:
7222 RTC_NOTREACHED();
7223 continue;
7224 }
Steve Antonc7b964c2018-02-01 14:39:45 -08007225 }
Qingsi Wang7fc821d2018-07-12 12:54:53 -07007226 }
7227
7228 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
7229 for (cricket::MediaType media_type : media_types) {
7230 switch (media_type) {
7231 case cricket::MEDIA_TYPE_AUDIO:
7232 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7233 "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite,
7234 rtc::SSL_CIPHER_SUITE_MAX_VALUE);
7235 break;
7236 case cricket::MEDIA_TYPE_VIDEO:
7237 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7238 "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite,
7239 rtc::SSL_CIPHER_SUITE_MAX_VALUE);
7240 break;
7241 case cricket::MEDIA_TYPE_DATA:
7242 RTC_HISTOGRAM_ENUMERATION_SPARSE(
7243 "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite,
7244 rtc::SSL_CIPHER_SUITE_MAX_VALUE);
7245 break;
7246 default:
7247 RTC_NOTREACHED();
7248 continue;
7249 }
Steve Antonc7b964c2018-02-01 14:39:45 -08007250 }
Steve Anton75737c02017-11-06 10:37:17 -08007251 }
7252}
7253
7254void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
Karl Wiberg6cab5c82019-03-26 09:57:01 +01007255 RTC_DCHECK_RUN_ON(worker_thread());
Steve Anton75737c02017-11-06 10:37:17 -08007256 RTC_DCHECK(call_);
7257 call_->OnSentPacket(sent_packet);
7258}
7259
7260const std::string PeerConnection::GetTransportName(
7261 const std::string& content_name) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08007262 cricket::ChannelInterface* channel = GetChannel(content_name);
Steve Anton6fec8802017-12-04 10:37:29 -08007263 if (channel) {
7264 return channel->transport_name();
Steve Anton75737c02017-11-06 10:37:17 -08007265 }
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01007266 if (data_channel_controller_.data_channel_transport()) {
Harald Alvestrand7a829a82020-02-12 07:38:21 +01007267 RTC_DCHECK(sctp_mid_s_);
7268 if (content_name == *sctp_mid_s_) {
Zhi Huange830e682018-03-30 10:48:35 -07007269 return *sctp_transport_name();
Steve Anton6fec8802017-12-04 10:37:29 -08007270 }
7271 }
7272 // Return an empty string if failed to retrieve the transport name.
7273 return "";
Steve Anton75737c02017-11-06 10:37:17 -08007274}
7275
Steve Anton6fec8802017-12-04 10:37:29 -08007276void PeerConnection::DestroyTransceiverChannel(
7277 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
7278 transceiver) {
7279 RTC_DCHECK(transceiver);
Steve Anton75737c02017-11-06 10:37:17 -08007280
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08007281 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton6fec8802017-12-04 10:37:29 -08007282 if (channel) {
7283 transceiver->internal()->SetChannel(nullptr);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08007284 DestroyChannelInterface(channel);
Steve Anton75737c02017-11-06 10:37:17 -08007285 }
7286}
7287
Harald Alvestrand408cb4b2019-11-16 12:09:08 +01007288void PeerConnection::DestroyDataChannelTransport() {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01007289 if (data_channel_controller_.rtp_data_channel()) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01007290 data_channel_controller_.OnTransportChannelClosed();
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01007291 DestroyChannelInterface(data_channel_controller_.rtp_data_channel());
7292 data_channel_controller_.set_rtp_data_channel(nullptr);
Steve Anton6fec8802017-12-04 10:37:29 -08007293 }
7294
7295 // Note: Cannot use rtc::Bind to create a functor to invoke because it will
7296 // grab a reference to this PeerConnection. If this is called from the
7297 // PeerConnection destructor, the RefCountedObject vtable will have already
7298 // been destroyed (since it is a subclass of PeerConnection) and using
7299 // rtc::Bind will cause "Pure virtual function called" error to appear.
7300
Harald Alvestrand7a829a82020-02-12 07:38:21 +01007301 if (sctp_mid_s_) {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01007302 data_channel_controller_.OnTransportChannelClosed();
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08007303 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
7304 RTC_DCHECK_RUN_ON(network_thread());
Bjorn A Mellemb689af42019-08-21 10:44:59 -07007305 TeardownDataChannelTransport_n();
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08007306 });
Harald Alvestrand7a829a82020-02-12 07:38:21 +01007307 sctp_mid_s_.reset();
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08007308 }
Steve Anton6fec8802017-12-04 10:37:29 -08007309}
7310
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08007311void PeerConnection::DestroyChannelInterface(
7312 cricket::ChannelInterface* channel) {
Steve Anton6fec8802017-12-04 10:37:29 -08007313 RTC_DCHECK(channel);
Steve Anton6fec8802017-12-04 10:37:29 -08007314 switch (channel->media_type()) {
7315 case cricket::MEDIA_TYPE_AUDIO:
7316 channel_manager()->DestroyVoiceChannel(
7317 static_cast<cricket::VoiceChannel*>(channel));
7318 break;
7319 case cricket::MEDIA_TYPE_VIDEO:
7320 channel_manager()->DestroyVideoChannel(
7321 static_cast<cricket::VideoChannel*>(channel));
7322 break;
7323 case cricket::MEDIA_TYPE_DATA:
7324 channel_manager()->DestroyRtpDataChannel(
7325 static_cast<cricket::RtpDataChannel*>(channel));
7326 break;
7327 default:
7328 RTC_NOTREACHED() << "Unknown media type: " << channel->media_type();
7329 break;
7330 }
Zhi Huange830e682018-03-30 10:48:35 -07007331}
Steve Anton6fec8802017-12-04 10:37:29 -08007332
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07007333bool PeerConnection::OnTransportChanged(
Zhi Huange830e682018-03-30 10:48:35 -07007334 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07007335 RtpTransportInternal* rtp_transport,
Harald Alvestrandc85328f2019-02-28 07:51:00 +01007336 rtc::scoped_refptr<DtlsTransport> dtls_transport,
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07007337 DataChannelTransportInterface* data_channel_transport) {
Karl Wibergac025892019-03-26 13:08:37 +01007338 RTC_DCHECK_RUN_ON(network_thread());
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07007339 bool ret = true;
Zhi Huange830e682018-03-30 10:48:35 -07007340 auto base_channel = GetChannel(mid);
7341 if (base_channel) {
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07007342 ret = base_channel->SetRtpTransport(rtp_transport);
Zhi Huange830e682018-03-30 10:48:35 -07007343 }
Harald Alvestrand7a829a82020-02-12 07:38:21 +01007344 if (mid == sctp_mid_n_) {
Harald Alvestrand00cf34c2019-12-02 09:56:02 +01007345 data_channel_controller_.OnTransportChanged(data_channel_transport);
Bjorn A Mellemb689af42019-08-21 10:44:59 -07007346 }
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07007347 return ret;
Steve Anton75737c02017-11-06 10:37:17 -08007348}
7349
Guido Urdaneta1ff16c82019-05-20 19:31:53 +02007350void PeerConnection::OnSetStreams() {
7351 RTC_DCHECK_RUN_ON(signaling_thread());
7352 if (IsUnifiedPlan())
7353 UpdateNegotiationNeeded();
7354}
7355
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02007356PeerConnectionObserver* PeerConnection::Observer() const {
Harald Alvestrand05e4d082019-12-03 14:04:21 +01007357 RTC_DCHECK_RUN_ON(signaling_thread());
7358 RTC_DCHECK(observer_);
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02007359 return observer_;
7360}
7361
Benjamin Wright8c27cca2018-10-25 10:16:44 -07007362CryptoOptions PeerConnection::GetCryptoOptions() {
7363 // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions
7364 // after it has been removed.
7365 return configuration_.crypto_options.has_value()
7366 ? *configuration_.crypto_options
7367 : factory_->options().crypto_options;
7368}
7369
Harald Alvestrand89061872018-01-02 14:08:34 +01007370void PeerConnection::ClearStatsCache() {
Karl Wiberg6cab5c82019-03-26 09:57:01 +01007371 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand89061872018-01-02 14:08:34 +01007372 if (stats_collector_) {
7373 stats_collector_->ClearCachedStatsReport();
7374 }
7375}
7376
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02007377void PeerConnection::RequestUsagePatternReportForTesting() {
Steve Antonad182762018-09-05 20:22:40 +00007378 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN,
7379 nullptr);
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02007380}
7381
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007382void PeerConnection::UpdateNegotiationNeeded() {
7383 RTC_DCHECK_RUN_ON(signaling_thread());
7384 if (!IsUnifiedPlan()) {
7385 Observer()->OnRenegotiationNeeded();
7386 return;
7387 }
7388
7389 // If connection's [[IsClosed]] slot is true, abort these steps.
7390 if (IsClosed())
7391 return;
7392
7393 // If connection's signaling state is not "stable", abort these steps.
7394 if (signaling_state() != kStable)
7395 return;
7396
7397 // NOTE
7398 // The negotiation-needed flag will be updated once the state transitions to
7399 // "stable", as part of the steps for setting an RTCSessionDescription.
7400
7401 // If the result of checking if negotiation is needed is false, clear the
7402 // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot
7403 // to false, and abort these steps.
7404 bool is_negotiation_needed = CheckIfNegotiationIsNeeded();
7405 if (!is_negotiation_needed) {
7406 is_negotiation_needed_ = false;
7407 return;
7408 }
7409
7410 // If connection's [[NegotiationNeeded]] slot is already true, abort these
7411 // steps.
7412 if (is_negotiation_needed_)
7413 return;
7414
7415 // Set connection's [[NegotiationNeeded]] slot to true.
7416 is_negotiation_needed_ = true;
7417
7418 // Queue a task that runs the following steps:
7419 // If connection's [[IsClosed]] slot is true, abort these steps.
7420 // If connection's [[NegotiationNeeded]] slot is false, abort these steps.
7421 // Fire an event named negotiationneeded at connection.
7422 Observer()->OnRenegotiationNeeded();
7423}
7424
7425bool PeerConnection::CheckIfNegotiationIsNeeded() {
7426 RTC_DCHECK_RUN_ON(signaling_thread());
7427 // 1. If any implementation-specific negotiation is required, as described at
7428 // the start of this section, return true.
7429
Henrik Boström79b69802019-07-18 11:16:56 +02007430 // 2. If connection's [[RestartIce]] internal slot is true, return true.
7431 if (local_ice_credentials_to_replace_->HasIceCredentials()) {
7432 return true;
7433 }
7434
7435 // 3. Let description be connection.[[CurrentLocalDescription]].
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007436 const SessionDescriptionInterface* description = current_local_description();
7437 if (!description)
7438 return true;
7439
Henrik Boström79b69802019-07-18 11:16:56 +02007440 // 4. If connection has created any RTCDataChannels, and no m= section in
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007441 // description has been negotiated yet for data, return true.
Harald Alvestrand05e4d082019-12-03 14:04:21 +01007442 if (data_channel_controller_.HasSctpDataChannels()) {
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007443 if (!cricket::GetFirstDataContent(description->description()->contents()))
7444 return true;
7445 }
7446
Henrik Boström79b69802019-07-18 11:16:56 +02007447 // 5. For each transceiver in connection's set of transceivers, perform the
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007448 // following checks:
7449 for (const auto& transceiver : transceivers_) {
7450 const ContentInfo* current_local_msection =
7451 FindTransceiverMSection(transceiver.get(), description);
7452
7453 const ContentInfo* current_remote_msection = FindTransceiverMSection(
7454 transceiver.get(), current_remote_description());
7455
Henrik Boström79b69802019-07-18 11:16:56 +02007456 // 5.3 If transceiver is stopped and is associated with an m= section,
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007457 // but the associated m= section is not yet rejected in
7458 // connection.[[CurrentLocalDescription]] or
7459 // connection.[[CurrentRemoteDescription]], return true.
7460 if (transceiver->stopped()) {
7461 if (current_local_msection && !current_local_msection->rejected &&
7462 ((current_remote_msection && !current_remote_msection->rejected) ||
7463 !current_remote_msection)) {
7464 return true;
7465 }
7466 continue;
7467 }
7468
Henrik Boström79b69802019-07-18 11:16:56 +02007469 // 5.1 If transceiver isn't stopped and isn't yet associated with an m=
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007470 // section in description, return true.
7471 if (!current_local_msection)
7472 return true;
7473
7474 const MediaContentDescription* current_local_media_description =
7475 current_local_msection->media_description();
Henrik Boström79b69802019-07-18 11:16:56 +02007476 // 5.2 If transceiver isn't stopped and is associated with an m= section
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007477 // in description then perform the following checks:
7478
Henrik Boström79b69802019-07-18 11:16:56 +02007479 // 5.2.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007480 // associated m= section in description either doesn't contain a single
7481 // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this
7482 // m= section, or the MSID values themselves, differ from what is in
7483 // transceiver.sender.[[AssociatedMediaStreamIds]], return true.
7484 if (RtpTransceiverDirectionHasSend(transceiver->direction())) {
7485 if (current_local_media_description->streams().size() == 0)
7486 return true;
7487
7488 std::vector<std::string> msection_msids;
7489 for (const auto& stream : current_local_media_description->streams()) {
7490 for (const std::string& msid : stream.stream_ids())
7491 msection_msids.push_back(msid);
7492 }
7493
7494 std::vector<std::string> transceiver_msids =
7495 transceiver->sender()->stream_ids();
7496 if (msection_msids.size() != transceiver_msids.size())
7497 return true;
7498
7499 absl::c_sort(transceiver_msids);
7500 absl::c_sort(msection_msids);
7501 if (transceiver_msids != msection_msids)
7502 return true;
7503 }
7504
Henrik Boström79b69802019-07-18 11:16:56 +02007505 // 5.2.2 If description is of type "offer", and the direction of the
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007506 // associated m= section in neither connection.[[CurrentLocalDescription]]
7507 // nor connection.[[CurrentRemoteDescription]] matches
7508 // transceiver.[[Direction]], return true.
7509 if (description->GetType() == SdpType::kOffer) {
7510 if (!current_remote_description())
7511 return true;
7512
7513 if (!current_remote_msection)
7514 return true;
7515
7516 RtpTransceiverDirection current_local_direction =
7517 current_local_media_description->direction();
7518 RtpTransceiverDirection current_remote_direction =
7519 current_remote_msection->media_description()->direction();
7520 if (transceiver->direction() != current_local_direction &&
7521 transceiver->direction() !=
7522 RtpTransceiverDirectionReversed(current_remote_direction)) {
7523 return true;
7524 }
7525 }
7526
Henrik Boström79b69802019-07-18 11:16:56 +02007527 // 5.2.3 If description is of type "answer", and the direction of the
Guido Urdaneta70c2db12019-04-16 12:24:14 +02007528 // associated m= section in the description does not match
7529 // transceiver.[[Direction]] intersected with the offered direction (as
7530 // described in [JSEP] (section 5.3.1.)), return true.
7531 if (description->GetType() == SdpType::kAnswer) {
7532 if (!remote_description())
7533 return true;
7534
7535 const ContentInfo* offered_remote_msection =
7536 FindTransceiverMSection(transceiver.get(), remote_description());
7537
7538 RtpTransceiverDirection offered_direction =
7539 offered_remote_msection
7540 ? offered_remote_msection->media_description()->direction()
7541 : RtpTransceiverDirection::kInactive;
7542
7543 if (current_local_media_description->direction() !=
7544 (RtpTransceiverDirectionIntersection(
7545 transceiver->direction(),
7546 RtpTransceiverDirectionReversed(offered_direction)))) {
7547 return true;
7548 }
7549 }
7550 }
7551
7552 // If all the preceding checks were performed and true was not returned,
7553 // nothing remains to be negotiated; return false.
7554 return false;
7555}
7556
Eldar Relloead0ec92019-10-21 23:01:31 +03007557RTCError PeerConnection::Rollback(SdpType sdp_type) {
Eldar Rello5ab79e62019-10-09 18:29:44 +03007558 auto state = signaling_state();
7559 if (state != PeerConnectionInterface::kHaveLocalOffer &&
7560 state != PeerConnectionInterface::kHaveRemoteOffer) {
7561 return RTCError(RTCErrorType::INVALID_STATE,
7562 "Called in wrong signalingState: " +
7563 GetSignalingStateString(signaling_state()));
7564 }
7565 RTC_DCHECK_RUN_ON(signaling_thread());
7566 RTC_DCHECK(IsUnifiedPlan());
Eldar Rello353a7182019-11-25 18:49:44 +02007567 std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_added_streams;
7568 std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_removed_streams;
7569 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> removed_receivers;
Eldar Rello5ab79e62019-10-09 18:29:44 +03007570
7571 for (auto&& transceivers_stable_state_pair :
7572 transceiver_stable_states_by_transceivers_) {
7573 auto transceiver = transceivers_stable_state_pair.first;
7574 auto state = transceivers_stable_state_pair.second;
Eldar Rello353a7182019-11-25 18:49:44 +02007575
7576 if (state.remote_stream_ids()) {
7577 std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
7578 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
7579 SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(),
7580 state.remote_stream_ids().value(),
7581 &added_streams, &removed_streams);
7582 all_added_streams.insert(all_added_streams.end(), added_streams.begin(),
7583 added_streams.end());
7584 all_removed_streams.insert(all_removed_streams.end(),
7585 removed_streams.begin(),
7586 removed_streams.end());
7587 if (!state.has_m_section() && !state.newly_created()) {
7588 continue;
7589 }
7590 }
7591
Eldar Rello5ab79e62019-10-09 18:29:44 +03007592 RTC_DCHECK(transceiver->internal()->mid().has_value());
Eldar Rello5ab79e62019-10-09 18:29:44 +03007593 DestroyTransceiverChannel(transceiver);
7594
Eldar Relloead0ec92019-10-21 23:01:31 +03007595 if (signaling_state() == PeerConnectionInterface::kHaveRemoteOffer &&
7596 transceiver->receiver()) {
Eldar Rello353a7182019-11-25 18:49:44 +02007597 removed_receivers.push_back(transceiver->receiver());
Eldar Relloead0ec92019-10-21 23:01:31 +03007598 }
Eldar Rello5ab79e62019-10-09 18:29:44 +03007599 if (state.newly_created()) {
Eldar Rello353a7182019-11-25 18:49:44 +02007600 if (transceiver->internal()->reused_for_addtrack()) {
Eldar Rello5ab79e62019-10-09 18:29:44 +03007601 transceiver->internal()->set_created_by_addtrack(true);
7602 } else {
7603 int remaining_transceiver_count = 0;
7604 for (auto&& t : transceivers_) {
7605 if (t != transceiver) {
7606 transceivers_[remaining_transceiver_count++] = t;
7607 }
7608 }
7609 transceivers_.resize(remaining_transceiver_count);
7610 }
7611 }
7612 transceiver->internal()->sender_internal()->set_transport(nullptr);
7613 transceiver->internal()->receiver_internal()->set_transport(nullptr);
Eldar Rello5ab79e62019-10-09 18:29:44 +03007614 transceiver->internal()->set_mid(state.mid());
7615 transceiver->internal()->set_mline_index(state.mline_index());
7616 }
Eldar Rellod85ea752020-02-19 20:41:07 +02007617 transport_controller_->RollbackTransports();
Eldar Rellod9ebe012020-03-18 20:41:45 +02007618 if (have_pending_rtp_data_channel_) {
7619 DestroyDataChannelTransport();
7620 have_pending_rtp_data_channel_ = false;
7621 }
Eldar Rello5ab79e62019-10-09 18:29:44 +03007622 transceiver_stable_states_by_transceivers_.clear();
7623 pending_local_description_.reset();
7624 pending_remote_description_.reset();
7625 ChangeSignalingState(PeerConnectionInterface::kStable);
Eldar Relloead0ec92019-10-21 23:01:31 +03007626
Eldar Rello353a7182019-11-25 18:49:44 +02007627 // Once all processing has finished, fire off callbacks.
7628 for (const auto& receiver : removed_receivers) {
7629 Observer()->OnRemoveTrack(receiver);
7630 }
7631 for (const auto& stream : all_added_streams) {
7632 Observer()->OnAddStream(stream);
7633 }
7634 for (const auto& stream : all_removed_streams) {
7635 Observer()->OnRemoveStream(stream);
7636 }
7637
Eldar Relloead0ec92019-10-21 23:01:31 +03007638 // The assumption is that in case of implicit rollback UpdateNegotiationNeeded
7639 // gets called in SetRemoteDescription.
7640 if (sdp_type == SdpType::kRollback) {
7641 UpdateNegotiationNeeded();
7642 if (is_negotiation_needed_) {
7643 Observer()->OnRenegotiationNeeded();
7644 }
7645 }
Eldar Rello5ab79e62019-10-09 18:29:44 +03007646 return RTCError::OK();
7647}
7648
henrike@webrtc.org28e20752013-07-10 00:45:36 +00007649} // namespace webrtc