henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/peerconnection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 13 | #include <algorithm> |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 14 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 15 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 16 | #include <utility> |
| 17 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/jsepicecandidate.h" |
| 20 | #include "api/jsepsessiondescription.h" |
| 21 | #include "api/mediaconstraintsinterface.h" |
| 22 | #include "api/mediastreamproxy.h" |
| 23 | #include "api/mediastreamtrackproxy.h" |
| 24 | #include "call/call.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 25 | #include "logging/rtc_event_log/icelogger.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 26 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 28 | #include "media/sctp/sctptransport.h" |
| 29 | #include "pc/audiotrack.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 30 | #include "pc/channel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "pc/channelmanager.h" |
| 32 | #include "pc/dtmfsender.h" |
| 33 | #include "pc/mediastream.h" |
| 34 | #include "pc/mediastreamobserver.h" |
| 35 | #include "pc/remoteaudiosource.h" |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 36 | #include "pc/rtpmediautils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "pc/rtpreceiver.h" |
| 38 | #include "pc/rtpsender.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 39 | #include "pc/sctputils.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 40 | #include "pc/sdputils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "pc/streamcollection.h" |
| 42 | #include "pc/videocapturertracksource.h" |
| 43 | #include "pc/videotrack.h" |
| 44 | #include "rtc_base/bind.h" |
| 45 | #include "rtc_base/checks.h" |
| 46 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 47 | #include "rtc_base/numerics/safe_conversions.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 48 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 49 | #include "rtc_base/stringencode.h" |
| 50 | #include "rtc_base/stringutils.h" |
| 51 | #include "rtc_base/trace_event.h" |
| 52 | #include "system_wrappers/include/clock.h" |
| 53 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 55 | using cricket::ContentInfo; |
| 56 | using cricket::ContentInfos; |
| 57 | using cricket::MediaContentDescription; |
| 58 | using cricket::SessionDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 59 | using cricket::MediaProtocolType; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 60 | using cricket::TransportInfo; |
| 61 | |
| 62 | using cricket::LOCAL_PORT_TYPE; |
| 63 | using cricket::STUN_PORT_TYPE; |
| 64 | using cricket::RELAY_PORT_TYPE; |
| 65 | using cricket::PRFLX_PORT_TYPE; |
| 66 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 67 | namespace webrtc { |
| 68 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 69 | // Error messages |
| 70 | const char kBundleWithoutRtcpMux[] = |
| 71 | "rtcp-mux must be enabled when BUNDLE " |
| 72 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 73 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 74 | const char kInvalidSdp[] = "Invalid session description."; |
| 75 | const char kMlineMismatchInAnswer[] = |
| 76 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 77 | "answer."; |
| 78 | const char kMlineMismatchInSubsequentOffer[] = |
| 79 | "The order of m-lines in subsequent offer doesn't match order from " |
| 80 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 81 | const char kSdpWithoutDtlsFingerprint[] = |
| 82 | "Called with SDP without DTLS fingerprint."; |
| 83 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 84 | const char kSdpWithoutIceUfragPwd[] = |
| 85 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 86 | const char kSessionError[] = "Session error code: "; |
| 87 | const char kSessionErrorDesc[] = "Session error description: "; |
| 88 | const char kDtlsSrtpSetupFailureRtp[] = |
| 89 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 90 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 91 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
| 92 | const char kEnableBundleFailed[] = "Failed to enable BUNDLE."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 94 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 96 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 97 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 98 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 99 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 100 | // The length of RTCP CNAMEs. |
| 101 | static const int kRtcpCnameLength = 16; |
| 102 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 104 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 106 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | MSG_GETSTATS, |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 108 | MSG_FREE_DATACHANNELS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 111 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | explicit SetSessionDescriptionMsg( |
| 113 | webrtc::SetSessionDescriptionObserver* observer) |
| 114 | : observer(observer) { |
| 115 | } |
| 116 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 117 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 118 | RTCError error; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 121 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 122 | explicit CreateSessionDescriptionMsg( |
| 123 | webrtc::CreateSessionDescriptionObserver* observer) |
| 124 | : observer(observer) {} |
| 125 | |
| 126 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 127 | RTCError error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 130 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 131 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 132 | webrtc::MediaStreamTrackInterface* track) |
| 133 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 135 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 136 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 139 | // Check if we can send |new_stream| on a PeerConnection. |
| 140 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 141 | webrtc::MediaStreamInterface* new_stream) { |
| 142 | if (!new_stream || !current_streams) { |
| 143 | return false; |
| 144 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 145 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 146 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 147 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 153 | // If the direction is "recvonly" or "inactive", treat the description |
| 154 | // as containing no streams. |
| 155 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 156 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 157 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 158 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 159 | ? desc->streams() |
| 160 | : std::vector<cricket::StreamParams>(); |
| 161 | } |
| 162 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 163 | bool IsValidOfferToReceiveMedia(int value) { |
| 164 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 165 | return (value >= Options::kUndefined) && |
| 166 | (value <= Options::kMaxOfferToReceiveMedia); |
| 167 | } |
| 168 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 169 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 170 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 171 | const std::vector<rtc::scoped_refptr< |
| 172 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 173 | cricket::MediaDescriptionOptions* audio_media_description_options, |
| 174 | cricket::MediaDescriptionOptions* video_media_description_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 175 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 176 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 177 | if (audio_media_description_options) { |
| 178 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 179 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 180 | } |
| 181 | } else { |
| 182 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 183 | if (video_media_description_options) { |
| 184 | video_media_description_options->AddVideoSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 185 | sender->id(), sender->internal()->stream_ids(), 1); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 188 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 189 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 190 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 191 | // Add options to |session_options| from |rtp_data_channels|. |
| 192 | void AddRtpDataChannelOptions( |
| 193 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 194 | rtp_data_channels, |
| 195 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 196 | if (!data_media_description_options) { |
| 197 | return; |
| 198 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 199 | // Check for data channels. |
| 200 | for (const auto& kv : rtp_data_channels) { |
| 201 | const DataChannel* channel = kv.second; |
| 202 | if (channel->state() == DataChannel::kConnecting || |
| 203 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 204 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 205 | // the data channel's label. |
| 206 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 207 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 212 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 213 | PeerConnectionInterface::IceTransportsType type) { |
| 214 | switch (type) { |
| 215 | case PeerConnectionInterface::kNone: |
| 216 | return cricket::CF_NONE; |
| 217 | case PeerConnectionInterface::kRelay: |
| 218 | return cricket::CF_RELAY; |
| 219 | case PeerConnectionInterface::kNoHost: |
| 220 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 221 | case PeerConnectionInterface::kAll: |
| 222 | return cricket::CF_ALL; |
| 223 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 224 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 225 | } |
| 226 | return cricket::CF_NONE; |
| 227 | } |
| 228 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 229 | // Helper to set an error and return from a method. |
| 230 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 231 | if (error) { |
| 232 | error->set_type(type); |
| 233 | } |
| 234 | return type == webrtc::RTCErrorType::NONE; |
| 235 | } |
| 236 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 237 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
| 238 | if (error_out) { |
| 239 | *error_out = std::move(error); |
| 240 | } |
| 241 | return error.ok(); |
| 242 | } |
| 243 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 244 | std::string GetSignalingStateString( |
| 245 | PeerConnectionInterface::SignalingState state) { |
| 246 | switch (state) { |
| 247 | case PeerConnectionInterface::kStable: |
| 248 | return "kStable"; |
| 249 | case PeerConnectionInterface::kHaveLocalOffer: |
| 250 | return "kHaveLocalOffer"; |
| 251 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 252 | return "kHavePrAnswer"; |
| 253 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 254 | return "kHaveRemoteOffer"; |
| 255 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 256 | return "kHaveRemotePrAnswer"; |
| 257 | case PeerConnectionInterface::kClosed: |
| 258 | return "kClosed"; |
| 259 | } |
| 260 | RTC_NOTREACHED(); |
| 261 | return ""; |
| 262 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 263 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 264 | IceCandidatePairType GetIceCandidatePairCounter( |
| 265 | const cricket::Candidate& local, |
| 266 | const cricket::Candidate& remote) { |
| 267 | const auto& l = local.type(); |
| 268 | const auto& r = remote.type(); |
| 269 | const auto& host = LOCAL_PORT_TYPE; |
| 270 | const auto& srflx = STUN_PORT_TYPE; |
| 271 | const auto& relay = RELAY_PORT_TYPE; |
| 272 | const auto& prflx = PRFLX_PORT_TYPE; |
| 273 | if (l == host && r == host) { |
| 274 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 275 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 276 | if (local_private) { |
| 277 | if (remote_private) { |
| 278 | return kIceCandidatePairHostPrivateHostPrivate; |
| 279 | } else { |
| 280 | return kIceCandidatePairHostPrivateHostPublic; |
| 281 | } |
| 282 | } else { |
| 283 | if (remote_private) { |
| 284 | return kIceCandidatePairHostPublicHostPrivate; |
| 285 | } else { |
| 286 | return kIceCandidatePairHostPublicHostPublic; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | if (l == host && r == srflx) |
| 291 | return kIceCandidatePairHostSrflx; |
| 292 | if (l == host && r == relay) |
| 293 | return kIceCandidatePairHostRelay; |
| 294 | if (l == host && r == prflx) |
| 295 | return kIceCandidatePairHostPrflx; |
| 296 | if (l == srflx && r == host) |
| 297 | return kIceCandidatePairSrflxHost; |
| 298 | if (l == srflx && r == srflx) |
| 299 | return kIceCandidatePairSrflxSrflx; |
| 300 | if (l == srflx && r == relay) |
| 301 | return kIceCandidatePairSrflxRelay; |
| 302 | if (l == srflx && r == prflx) |
| 303 | return kIceCandidatePairSrflxPrflx; |
| 304 | if (l == relay && r == host) |
| 305 | return kIceCandidatePairRelayHost; |
| 306 | if (l == relay && r == srflx) |
| 307 | return kIceCandidatePairRelaySrflx; |
| 308 | if (l == relay && r == relay) |
| 309 | return kIceCandidatePairRelayRelay; |
| 310 | if (l == relay && r == prflx) |
| 311 | return kIceCandidatePairRelayPrflx; |
| 312 | if (l == prflx && r == host) |
| 313 | return kIceCandidatePairPrflxHost; |
| 314 | if (l == prflx && r == srflx) |
| 315 | return kIceCandidatePairPrflxSrflx; |
| 316 | if (l == prflx && r == relay) |
| 317 | return kIceCandidatePairPrflxRelay; |
| 318 | return kIceCandidatePairMax; |
| 319 | } |
| 320 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 321 | // Logic to decide if an m= section can be recycled. This means that the new |
| 322 | // m= section is not rejected, but the old local or remote m= section is |
| 323 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 324 | // of the old remote and old local descriptions in no particular order. |
| 325 | // We need to check both the old local and remote because either |
| 326 | // could be the most current from the latest negotation. |
| 327 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 328 | const ContentInfo& content, |
| 329 | const ContentInfo* old_content_one, |
| 330 | const ContentInfo* old_content_two) { |
| 331 | return type == SdpType::kOffer && !content.rejected && |
| 332 | ((old_content_one && old_content_one->rejected) || |
| 333 | (old_content_two && old_content_two->rejected)); |
| 334 | } |
| 335 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 336 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 337 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 338 | // less than |current_desc|. In the case of checking an answer's |
| 339 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 340 | // local or remote. In the case of checking an offer's |new_desc| we |
| 341 | // check against the local and remote descriptions stored from the last |
| 342 | // negotiation, because either of these could be the most up to date for |
| 343 | // possible rejected m sections. These are the |current_desc| and |
| 344 | // |secondary_current_desc|. |
| 345 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 346 | const SessionDescription* secondary_current_desc, |
| 347 | const SessionDescription& new_desc, |
| 348 | const SdpType type) { |
| 349 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 353 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 354 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 355 | if (secondary_current_desc && |
| 356 | i < secondary_current_desc->contents().size()) { |
| 357 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 358 | } |
| 359 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 360 | ¤t_desc.contents()[i], |
| 361 | secondary_content_info)) { |
| 362 | // For new offer descriptions, if the media section can be recycled, it's |
| 363 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 364 | continue; |
| 365 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 366 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 367 | return false; |
| 368 | } |
| 369 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 370 | new_desc.contents()[i].media_description(); |
| 371 | const MediaContentDescription* current_desc_mdesc = |
| 372 | current_desc.contents()[i].media_description(); |
| 373 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 380 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 381 | const SessionDescription& desc2) { |
| 382 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 385 | void NoteKeyProtocolAndMedia( |
| 386 | KeyExchangeProtocolType protocol_type, |
| 387 | cricket::MediaType media_type, |
| 388 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
| 389 | if (!uma_observer) |
| 390 | return; |
| 391 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocol, |
| 392 | protocol_type, |
| 393 | webrtc::kEnumCounterKeyProtocolMax); |
| 394 | static const std::map<std::pair<KeyExchangeProtocolType, cricket::MediaType>, |
| 395 | KeyExchangeProtocolMedia> |
| 396 | proto_media_counter_map = { |
| 397 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO}, |
| 398 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 399 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO}, |
| 400 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 401 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA}, |
| 402 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 403 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO}, |
| 404 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 405 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO}, |
| 406 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 407 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA}, |
| 408 | kEnumCounterKeyProtocolMediaTypeSdesData}}; |
| 409 | |
| 410 | auto it = proto_media_counter_map.find({protocol_type, media_type}); |
| 411 | if (it != proto_media_counter_map.end()) { |
| 412 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocolMediaType, |
| 413 | it->second, |
| 414 | kEnumCounterKeyProtocolMediaTypeMax); |
| 415 | } |
| 416 | } |
| 417 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 418 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 419 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 420 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 421 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 422 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 423 | // by Channel's |srtp_required| check. |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 424 | RTCError VerifyCrypto(const SessionDescription* desc, |
| 425 | bool dtls_enabled, |
| 426 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 427 | const cricket::ContentGroup* bundle = |
| 428 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 429 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 430 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 431 | continue; |
| 432 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 433 | // Note what media is used with each crypto protocol, for all sections. |
| 434 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 435 | : webrtc::kEnumCounterKeyProtocolSdes, |
| 436 | content_info.media_description()->type(), |
| 437 | uma_observer); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 438 | const std::string& mid = content_info.name; |
| 439 | if (bundle && bundle->HasContentName(mid) && |
| 440 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 441 | // This isn't the first media section in the BUNDLE group, so it's not |
| 442 | // required to have crypto attributes, since only the crypto attributes |
| 443 | // from the first section actually get used. |
| 444 | continue; |
| 445 | } |
| 446 | |
| 447 | // If the content isn't rejected or bundled into another m= section, crypto |
| 448 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 449 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 450 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 451 | if (!media || !tinfo) { |
| 452 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 453 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 454 | } |
| 455 | if (dtls_enabled) { |
| 456 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 457 | RTC_LOG(LS_WARNING) |
| 458 | << "Session description must have DTLS fingerprint if " |
| 459 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 460 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 461 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 462 | } |
| 463 | } else { |
| 464 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 465 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 466 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 467 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 471 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 475 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 476 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 477 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 478 | const cricket::ContentGroup* bundle = |
| 479 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 480 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 481 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 482 | continue; |
| 483 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 484 | const std::string& mid = content_info.name; |
| 485 | if (bundle && bundle->HasContentName(mid) && |
| 486 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 487 | // This isn't the first media section in the BUNDLE group, so it's not |
| 488 | // required to have ufrag/password, since only the ufrag/password from |
| 489 | // the first section actually get used. |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | // If the content isn't rejected or bundled into another m= section, |
| 494 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 495 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 496 | if (!tinfo) { |
| 497 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 498 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 499 | return false; |
| 500 | } |
| 501 | if (tinfo->description.ice_ufrag.empty() || |
| 502 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 503 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | bool GetTrackIdBySsrc(const SessionDescription* session_description, |
| 511 | uint32_t ssrc, |
| 512 | std::string* track_id) { |
| 513 | RTC_DCHECK(track_id != NULL); |
| 514 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 515 | const cricket::AudioContentDescription* audio_desc = |
| 516 | cricket::GetFirstAudioContentDescription(session_description); |
| 517 | if (audio_desc) { |
| 518 | const auto* found = cricket::GetStreamBySsrc(audio_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 519 | if (found) { |
| 520 | *track_id = found->id; |
| 521 | return true; |
| 522 | } |
| 523 | } |
| 524 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 525 | const cricket::VideoContentDescription* video_desc = |
| 526 | cricket::GetFirstVideoContentDescription(session_description); |
| 527 | if (video_desc) { |
| 528 | const auto* found = cricket::GetStreamBySsrc(video_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 529 | if (found) { |
| 530 | *track_id = found->id; |
| 531 | return true; |
| 532 | } |
| 533 | } |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | // Get the SCTP port out of a SessionDescription. |
| 538 | // Return -1 if not found. |
| 539 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 540 | const cricket::DataContentDescription* data_desc = |
| 541 | GetFirstDataContentDescription(session_description); |
| 542 | RTC_DCHECK(data_desc); |
| 543 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 544 | return -1; |
| 545 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 546 | std::string value; |
| 547 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 548 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 549 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 550 | if (!codec.Matches(match_pattern)) { |
| 551 | continue; |
| 552 | } |
| 553 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 554 | return rtc::FromString<int>(value); |
| 555 | } |
| 556 | } |
| 557 | return -1; |
| 558 | } |
| 559 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 560 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 561 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 562 | const SessionDescriptionInterface* new_desc, |
| 563 | const std::string& content_name) { |
| 564 | if (!old_desc) { |
| 565 | return false; |
| 566 | } |
| 567 | const SessionDescription* new_sd = new_desc->description(); |
| 568 | const SessionDescription* old_sd = old_desc->description(); |
| 569 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 570 | if (!cinfo || cinfo->rejected) { |
| 571 | return false; |
| 572 | } |
| 573 | // If the content isn't rejected, check if ufrag and password has changed. |
| 574 | const cricket::TransportDescription* new_transport_desc = |
| 575 | new_sd->GetTransportDescriptionByName(content_name); |
| 576 | const cricket::TransportDescription* old_transport_desc = |
| 577 | old_sd->GetTransportDescriptionByName(content_name); |
| 578 | if (!new_transport_desc || !old_transport_desc) { |
| 579 | // No transport description exists. This is not an ICE restart. |
| 580 | return false; |
| 581 | } |
| 582 | if (cricket::IceCredentialsChanged( |
| 583 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 584 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 585 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 586 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 587 | return true; |
| 588 | } |
| 589 | return false; |
| 590 | } |
| 591 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 592 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 593 | // from an RTCError. |
| 594 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 595 | SdpType type, |
| 596 | const RTCError& error) { |
| 597 | std::ostringstream oss; |
| 598 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 599 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
| 600 | return oss.str(); |
| 601 | } |
| 602 | |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 603 | rtc::Optional<int> RTCConfigurationToIceConfigOptionalInt( |
| 604 | int rtc_configuration_parameter) { |
| 605 | if (rtc_configuration_parameter == |
| 606 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
| 607 | return rtc::nullopt; |
| 608 | } |
| 609 | return rtc_configuration_parameter; |
| 610 | } |
| 611 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 612 | } // namespace |
| 613 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 614 | // Upon completion, posts a task to execute the callback of the |
| 615 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 616 | // point, the state of the peer connection might no longer reflect the effects |
| 617 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 618 | // modified during the post. |
| 619 | // TODO(hbos): Remove this class once we remove the version of |
| 620 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 621 | // SetSessionDescriptionObserver as an argument. |
| 622 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 623 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 624 | public: |
| 625 | SetRemoteDescriptionObserverAdapter( |
| 626 | rtc::scoped_refptr<PeerConnection> pc, |
| 627 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 628 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 629 | |
| 630 | // SetRemoteDescriptionObserverInterface implementation. |
| 631 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 632 | if (error.ok()) |
| 633 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 634 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 635 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | private: |
| 639 | rtc::scoped_refptr<PeerConnection> pc_; |
| 640 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 641 | }; |
| 642 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 643 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 644 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 645 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 646 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 647 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 648 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 649 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 650 | BundlePolicy bundle_policy; |
| 651 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 652 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 653 | int ice_candidate_pool_size; |
| 654 | bool disable_ipv6; |
| 655 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 656 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 657 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 658 | bool enable_rtp_data_channel; |
| 659 | rtc::Optional<int> screencast_min_bitrate; |
| 660 | rtc::Optional<bool> combined_audio_video_bwe; |
| 661 | rtc::Optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 662 | TcpCandidatePolicy tcp_candidate_policy; |
| 663 | CandidateNetworkPolicy candidate_network_policy; |
| 664 | int audio_jitter_buffer_max_packets; |
| 665 | bool audio_jitter_buffer_fast_accelerate; |
| 666 | int ice_connection_receiving_timeout; |
| 667 | int ice_backup_candidate_pair_ping_interval; |
| 668 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 669 | bool prioritize_most_likely_ice_candidate_pairs; |
| 670 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 671 | bool prune_turn_ports; |
| 672 | bool presume_writable_when_fully_relayed; |
| 673 | bool enable_ice_renomination; |
| 674 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 675 | rtc::Optional<int> ice_check_interval_strong_connectivity; |
| 676 | rtc::Optional<int> ice_check_interval_weak_connectivity; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 677 | rtc::Optional<int> ice_check_min_interval; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 678 | rtc::Optional<int> ice_unwritable_timeout; |
| 679 | rtc::Optional<int> ice_unwritable_min_checks; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 680 | rtc::Optional<int> stun_candidate_keepalive_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 681 | rtc::Optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 682 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 683 | SdpSemantics sdp_semantics; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 684 | rtc::Optional<rtc::AdapterType> network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 685 | }; |
| 686 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 687 | "Did you add something to RTCConfiguration and forget to " |
| 688 | "update operator==?"); |
| 689 | return type == o.type && servers == o.servers && |
| 690 | bundle_policy == o.bundle_policy && |
| 691 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 692 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 693 | candidate_network_policy == o.candidate_network_policy && |
| 694 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 695 | audio_jitter_buffer_fast_accelerate == |
| 696 | o.audio_jitter_buffer_fast_accelerate && |
| 697 | ice_connection_receiving_timeout == |
| 698 | o.ice_connection_receiving_timeout && |
| 699 | ice_backup_candidate_pair_ping_interval == |
| 700 | o.ice_backup_candidate_pair_ping_interval && |
| 701 | continual_gathering_policy == o.continual_gathering_policy && |
| 702 | certificates == o.certificates && |
| 703 | prioritize_most_likely_ice_candidate_pairs == |
| 704 | o.prioritize_most_likely_ice_candidate_pairs && |
| 705 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 706 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 707 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 708 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 709 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 710 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 711 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 712 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 713 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 714 | prune_turn_ports == o.prune_turn_ports && |
| 715 | presume_writable_when_fully_relayed == |
| 716 | o.presume_writable_when_fully_relayed && |
| 717 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 718 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 719 | ice_check_interval_strong_connectivity == |
| 720 | o.ice_check_interval_strong_connectivity && |
| 721 | ice_check_interval_weak_connectivity == |
| 722 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 723 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 724 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 725 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 726 | stun_candidate_keepalive_interval == |
| 727 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 728 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 729 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 730 | sdp_semantics == o.sdp_semantics && |
| 731 | network_preference == o.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 735 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 736 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 737 | } |
| 738 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 739 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 740 | std::string GenerateRtcpCname() { |
| 741 | std::string cname; |
| 742 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 743 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 744 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 745 | } |
| 746 | return cname; |
| 747 | } |
| 748 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 749 | bool ValidateOfferAnswerOptions( |
| 750 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 751 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 752 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 753 | } |
| 754 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 755 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 756 | // m= sections (in other words, nothing that involves a map/array). |
| 757 | void ExtractSharedMediaSessionOptions( |
| 758 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 759 | cricket::MediaSessionOptions* session_options) { |
| 760 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 761 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 762 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 763 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 764 | bool ConvertConstraintsToOfferAnswerOptions( |
| 765 | const MediaConstraintsInterface* constraints, |
| 766 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 767 | if (!constraints) { |
| 768 | return true; |
| 769 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 770 | |
| 771 | bool value = false; |
| 772 | size_t mandatory_constraints_satisfied = 0; |
| 773 | |
| 774 | if (FindConstraint(constraints, |
| 775 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 776 | &mandatory_constraints_satisfied)) { |
| 777 | offer_answer_options->offer_to_receive_audio = |
| 778 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 779 | kOfferToReceiveMediaTrue |
| 780 | : 0; |
| 781 | } |
| 782 | |
| 783 | if (FindConstraint(constraints, |
| 784 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 785 | &mandatory_constraints_satisfied)) { |
| 786 | offer_answer_options->offer_to_receive_video = |
| 787 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 788 | kOfferToReceiveMediaTrue |
| 789 | : 0; |
| 790 | } |
| 791 | if (FindConstraint(constraints, |
| 792 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 793 | &mandatory_constraints_satisfied)) { |
| 794 | offer_answer_options->voice_activity_detection = value; |
| 795 | } |
| 796 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 797 | &mandatory_constraints_satisfied)) { |
| 798 | offer_answer_options->use_rtp_mux = value; |
| 799 | } |
| 800 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 801 | &value, &mandatory_constraints_satisfied)) { |
| 802 | offer_answer_options->ice_restart = value; |
| 803 | } |
| 804 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 805 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 806 | } |
| 807 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 808 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 809 | std::unique_ptr<RtcEventLog> event_log, |
| 810 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 811 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 812 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 813 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 814 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 815 | remote_streams_(StreamCollection::Create()), |
| 816 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 817 | |
| 818 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 819 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 820 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 821 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 822 | // Need to stop transceivers before destroying the stats collector because |
| 823 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 824 | // stopping. |
| 825 | for (auto transceiver : transceivers_) { |
| 826 | transceiver->Stop(); |
| 827 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 828 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 829 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 830 | if (stats_collector_) { |
| 831 | stats_collector_->WaitForPendingRequest(); |
| 832 | stats_collector_ = nullptr; |
| 833 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 834 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 835 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 836 | // the last stats request can still read from the channels. |
| 837 | DestroyAllChannels(); |
| 838 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 839 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 840 | |
| 841 | webrtc_session_desc_factory_.reset(); |
| 842 | sctp_invoker_.reset(); |
| 843 | sctp_factory_.reset(); |
| 844 | transport_controller_.reset(); |
| 845 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 846 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 847 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 848 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 849 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 850 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 851 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 852 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 853 | event_log_.reset(); |
| 854 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 857 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 858 | // Destroy video channels first since they may have a pointer to a voice |
| 859 | // channel. |
| 860 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 861 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 862 | DestroyTransceiverChannel(transceiver); |
| 863 | } |
| 864 | } |
| 865 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 866 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 867 | DestroyTransceiverChannel(transceiver); |
| 868 | } |
| 869 | } |
| 870 | DestroyDataChannel(); |
| 871 | } |
| 872 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 873 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 874 | const PeerConnectionInterface::RTCConfiguration& configuration, |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 875 | std::unique_ptr<cricket::PortAllocator> allocator, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 876 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 877 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 878 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 879 | |
| 880 | RTCError config_error = ValidateConfiguration(configuration); |
| 881 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 882 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 883 | return false; |
| 884 | } |
| 885 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 886 | if (!allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 887 | RTC_LOG(LS_ERROR) |
| 888 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 889 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 890 | return false; |
| 891 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 892 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 893 | if (!observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 894 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 895 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 896 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 897 | return false; |
| 898 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 899 | observer_ = observer; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 900 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 901 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 902 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 903 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 904 | if (!network_thread()->Invoke<bool>( |
| 905 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, |
| 906 | this, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 907 | return false; |
| 908 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 909 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 910 | // RFC 3264: The numeric value of the session id and version in the |
| 911 | // o line MUST be representable with a "64 bit signed integer". |
| 912 | // Due to this constraint session id |session_id_| is max limited to |
| 913 | // LLONG_MAX. |
| 914 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
| 915 | transport_controller_.reset(factory_->CreateTransportController( |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 916 | port_allocator_.get(), configuration.redetermine_role_on_ice_restart, |
| 917 | event_log_.get())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 918 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 919 | transport_controller_->SignalConnectionState.connect( |
| 920 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 921 | transport_controller_->SignalGatheringState.connect( |
| 922 | this, &PeerConnection::OnTransportControllerGatheringState); |
| 923 | transport_controller_->SignalCandidatesGathered.connect( |
| 924 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
| 925 | transport_controller_->SignalCandidatesRemoved.connect( |
| 926 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 927 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 928 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 929 | |
| 930 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 931 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 932 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 933 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 934 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 935 | configuration_ = configuration; |
| 936 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 937 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 938 | |
| 939 | transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version); |
| 940 | |
| 941 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 942 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 943 | if (!configuration.certificates.empty()) { |
| 944 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 945 | // just picking the first one. The decision should be made based on the DTLS |
| 946 | // handshake. The DTLS negotiations need to know about all certificates. |
| 947 | certificate = configuration.certificates[0]; |
| 948 | } |
| 949 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 950 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 951 | |
| 952 | if (options.disable_encryption) { |
| 953 | dtls_enabled_ = false; |
| 954 | } else { |
| 955 | // Enable DTLS by default if we have an identity store or a certificate. |
| 956 | dtls_enabled_ = (cert_generator || certificate); |
| 957 | // |configuration| can override the default |dtls_enabled_| value. |
| 958 | if (configuration.enable_dtls_srtp) { |
| 959 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 964 | // It takes precendence over the disable_sctp_data_channels |
| 965 | // PeerConnectionFactoryInterface::Options. |
| 966 | if (configuration.enable_rtp_data_channel) { |
| 967 | data_channel_type_ = cricket::DCT_RTP; |
| 968 | } else { |
| 969 | // DTLS has to be enabled to use SCTP. |
| 970 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 971 | data_channel_type_ = cricket::DCT_SCTP; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | video_options_.screencast_min_bitrate_kbps = |
| 976 | configuration.screencast_min_bitrate; |
| 977 | audio_options_.combined_audio_video_bwe = |
| 978 | configuration.combined_audio_video_bwe; |
| 979 | |
| 980 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 981 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 982 | |
| 983 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 984 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 985 | |
| 986 | // Whether the certificate generator/certificate is null or not determines |
| 987 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 988 | // the right instructions by clearing the variables if needed. |
| 989 | if (!dtls_enabled_) { |
| 990 | cert_generator.reset(); |
| 991 | certificate = nullptr; |
| 992 | } else if (certificate) { |
| 993 | // Favor generated certificate over the certificate generator. |
| 994 | cert_generator.reset(); |
| 995 | } |
| 996 | |
| 997 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 998 | signaling_thread(), channel_manager(), this, session_id(), |
| 999 | std::move(cert_generator), certificate)); |
| 1000 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1001 | this, &PeerConnection::OnCertificateReady); |
| 1002 | |
| 1003 | if (options.disable_encryption) { |
| 1004 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1005 | } |
| 1006 | |
| 1007 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 1008 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1009 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1010 | // Add default audio/video transceivers for Plan B SDP. |
| 1011 | if (!IsUnifiedPlan()) { |
| 1012 | transceivers_.push_back( |
| 1013 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1014 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1015 | transceivers_.push_back( |
| 1016 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1017 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1018 | } |
| 1019 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1020 | return true; |
| 1021 | } |
| 1022 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1023 | RTCError PeerConnection::ValidateConfiguration( |
| 1024 | const RTCConfiguration& config) const { |
| 1025 | if (config.ice_regather_interval_range && |
| 1026 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1027 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1028 | "ice_regather_interval_range specified but continual " |
| 1029 | "gathering policy is GATHER_ONCE"); |
| 1030 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1031 | auto result = |
| 1032 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1033 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1036 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1037 | PeerConnection::local_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1038 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1039 | "Plan SdpSemantics. Please use GetSenders " |
| 1040 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1041 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1044 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1045 | PeerConnection::remote_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1046 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1047 | "Plan SdpSemantics. Please use GetReceivers " |
| 1048 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1049 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1052 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1053 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1054 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1055 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1056 | if (IsClosed()) { |
| 1057 | return false; |
| 1058 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1059 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1060 | return false; |
| 1061 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1062 | |
| 1063 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1064 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1065 | observer->SignalAudioTrackAdded.connect(this, |
| 1066 | &PeerConnection::OnAudioTrackAdded); |
| 1067 | observer->SignalAudioTrackRemoved.connect( |
| 1068 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1069 | observer->SignalVideoTrackAdded.connect(this, |
| 1070 | &PeerConnection::OnVideoTrackAdded); |
| 1071 | observer->SignalVideoTrackRemoved.connect( |
| 1072 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1073 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1074 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1075 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1076 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1077 | } |
| 1078 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1079 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1082 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1083 | observer_->OnRenegotiationNeeded(); |
| 1084 | return true; |
| 1085 | } |
| 1086 | |
| 1087 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1088 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1089 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1090 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1091 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1092 | if (!IsClosed()) { |
| 1093 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1094 | RemoveAudioTrack(track.get(), local_stream); |
| 1095 | } |
| 1096 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1097 | RemoveVideoTrack(track.get(), local_stream); |
| 1098 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1099 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1100 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1101 | stream_observers_.erase( |
| 1102 | std::remove_if( |
| 1103 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1104 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1105 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1106 | }), |
| 1107 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1108 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1109 | if (IsClosed()) { |
| 1110 | return; |
| 1111 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1112 | observer_->OnRenegotiationNeeded(); |
| 1113 | } |
| 1114 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1115 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 1116 | MediaStreamTrackInterface* track, |
| 1117 | std::vector<MediaStreamInterface*> streams) { |
| 1118 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1119 | std::vector<std::string> stream_ids; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1120 | for (auto* stream : streams) { |
| 1121 | if (!stream) { |
| 1122 | RTC_LOG(LS_ERROR) << "Stream list has null element."; |
| 1123 | return nullptr; |
| 1124 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1125 | stream_ids.push_back(stream->id()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1126 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1127 | auto sender_or_error = AddTrack(track, stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1128 | if (!sender_or_error.ok()) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1129 | return nullptr; |
| 1130 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1131 | return sender_or_error.MoveValue(); |
| 1132 | } |
| 1133 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1134 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1135 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1136 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1137 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1138 | if (!track) { |
| 1139 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1140 | } |
| 1141 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1142 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1143 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1144 | "Track has invalid kind: " + track->kind()); |
| 1145 | } |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 1146 | // TODO(bugs.webrtc.org/7932): Support adding a track to multiple streams. |
| 1147 | if (stream_ids.size() > 1u) { |
| 1148 | LOG_AND_RETURN_ERROR( |
| 1149 | RTCErrorType::UNSUPPORTED_OPERATION, |
| 1150 | "AddTrack with more than one stream is not currently supported."); |
| 1151 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1152 | if (IsClosed()) { |
| 1153 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1154 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1155 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1156 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1157 | LOG_AND_RETURN_ERROR( |
| 1158 | RTCErrorType::INVALID_PARAMETER, |
| 1159 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1160 | } |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 1161 | // TODO(bugs.webrtc.org/7933): MediaSession expects the sender to have exactly |
| 1162 | // one stream. AddTrackInternal will return an error if there is more than one |
| 1163 | // stream, but if the caller specifies none then we need to generate a random |
| 1164 | // stream id. |
| 1165 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1166 | if (stream_ids.empty()) { |
| 1167 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1168 | } |
| 1169 | RTC_DCHECK_EQ(1, adjusted_stream_ids.size()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1170 | auto sender_or_error = |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 1171 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, adjusted_stream_ids) |
| 1172 | : AddTrackPlanB(track, adjusted_stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1173 | if (sender_or_error.ok()) { |
| 1174 | observer_->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1175 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1176 | } |
| 1177 | return sender_or_error; |
| 1178 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1179 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1180 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1181 | PeerConnection::AddTrackPlanB( |
| 1182 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1183 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1184 | cricket::MediaType media_type = |
| 1185 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1186 | ? cricket::MEDIA_TYPE_AUDIO |
| 1187 | : cricket::MEDIA_TYPE_VIDEO); |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 1188 | auto new_sender = CreateSender(media_type, track, stream_ids); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1189 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1190 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1191 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1192 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1193 | FindSenderInfo(local_audio_sender_infos_, |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 1194 | new_sender->internal()->stream_id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1195 | if (sender_info) { |
| 1196 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1197 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1198 | } else { |
| 1199 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1200 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1201 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1202 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1203 | FindSenderInfo(local_video_sender_infos_, |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 1204 | new_sender->internal()->stream_id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1205 | if (sender_info) { |
| 1206 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1207 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1208 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1209 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1210 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1211 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1212 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1213 | PeerConnection::AddTrackUnifiedPlan( |
| 1214 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1215 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1216 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1217 | if (transceiver) { |
| 1218 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1219 | transceiver->internal()->set_direction( |
| 1220 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1221 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1222 | transceiver->internal()->set_direction( |
| 1223 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1224 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1225 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1226 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1227 | } else { |
| 1228 | cricket::MediaType media_type = |
| 1229 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1230 | ? cricket::MEDIA_TYPE_AUDIO |
| 1231 | : cricket::MEDIA_TYPE_VIDEO); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1232 | auto sender = CreateSender(media_type, track, stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1233 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1234 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1235 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1236 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1237 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1238 | return transceiver->sender(); |
| 1239 | } |
| 1240 | |
| 1241 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1242 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1243 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1244 | RTC_DCHECK(track); |
| 1245 | for (auto transceiver : transceivers_) { |
| 1246 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1247 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1248 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1249 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1250 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1251 | return transceiver; |
| 1252 | } |
| 1253 | } |
| 1254 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1258 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1259 | return RemoveTrackInternal(sender).ok(); |
| 1260 | } |
| 1261 | |
| 1262 | RTCError PeerConnection::RemoveTrackInternal( |
| 1263 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1264 | if (!sender) { |
| 1265 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1266 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1267 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1268 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1269 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1270 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1271 | if (IsUnifiedPlan()) { |
| 1272 | auto transceiver = FindTransceiverBySender(sender); |
| 1273 | if (!transceiver || !sender->track()) { |
| 1274 | return RTCError::OK(); |
| 1275 | } |
| 1276 | sender->SetTrack(nullptr); |
| 1277 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1278 | transceiver->internal()->set_direction( |
| 1279 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1280 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1281 | transceiver->internal()->set_direction( |
| 1282 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1283 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1284 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1285 | bool removed; |
| 1286 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1287 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1288 | } else { |
| 1289 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1290 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1291 | } |
| 1292 | if (!removed) { |
| 1293 | LOG_AND_RETURN_ERROR( |
| 1294 | RTCErrorType::INVALID_PARAMETER, |
| 1295 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1296 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1297 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1298 | observer_->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1299 | return RTCError::OK(); |
| 1300 | } |
| 1301 | |
| 1302 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1303 | PeerConnection::FindTransceiverBySender( |
| 1304 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1305 | for (auto transceiver : transceivers_) { |
| 1306 | if (transceiver->sender() == sender) { |
| 1307 | return transceiver; |
| 1308 | } |
| 1309 | } |
| 1310 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1313 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1314 | PeerConnection::AddTransceiver( |
| 1315 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1316 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1317 | } |
| 1318 | |
| 1319 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1320 | PeerConnection::AddTransceiver( |
| 1321 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1322 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1323 | RTC_CHECK(IsUnifiedPlan()) |
| 1324 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1325 | if (!track) { |
| 1326 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1327 | } |
| 1328 | cricket::MediaType media_type; |
| 1329 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1330 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1331 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1332 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1333 | } else { |
| 1334 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1335 | "Track kind is not audio or video"); |
| 1336 | } |
| 1337 | return AddTransceiver(media_type, track, init); |
| 1338 | } |
| 1339 | |
| 1340 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1341 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1342 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1343 | } |
| 1344 | |
| 1345 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1346 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1347 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1348 | RTC_CHECK(IsUnifiedPlan()) |
| 1349 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1350 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1351 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1352 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1353 | "media type is not audio or video"); |
| 1354 | } |
| 1355 | return AddTransceiver(media_type, nullptr, init); |
| 1356 | } |
| 1357 | |
| 1358 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1359 | PeerConnection::AddTransceiver( |
| 1360 | cricket::MediaType media_type, |
| 1361 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1362 | const RtpTransceiverInit& init, |
| 1363 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1364 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1365 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1366 | if (track) { |
| 1367 | RTC_DCHECK_EQ(media_type, |
| 1368 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1369 | ? cricket::MEDIA_TYPE_AUDIO |
| 1370 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1371 | } |
| 1372 | |
| 1373 | // TODO(bugs.webrtc.org/7600): Verify init. |
| 1374 | |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 1375 | if (init.stream_ids.size() > 1u) { |
| 1376 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1377 | "More than one stream is not yet supported."); |
| 1378 | } |
| 1379 | |
| 1380 | std::vector<std::string> stream_ids = { |
| 1381 | !init.stream_ids.empty() ? init.stream_ids[0] : rtc::CreateRandomUuid()}; |
| 1382 | |
| 1383 | auto sender = CreateSender(media_type, track, stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1384 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1385 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1386 | transceiver->internal()->set_direction(init.direction); |
| 1387 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1388 | if (fire_callback) { |
| 1389 | observer_->OnRenegotiationNeeded(); |
| 1390 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1391 | |
| 1392 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1393 | } |
| 1394 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1395 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1396 | PeerConnection::CreateSender( |
| 1397 | cricket::MediaType media_type, |
| 1398 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1399 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1400 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1401 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1402 | RTC_DCHECK(!track || |
| 1403 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1404 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1405 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1406 | new AudioRtpSender(worker_thread(), |
| 1407 | static_cast<AudioTrackInterface*>(track.get()), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1408 | stream_ids, stats_.get())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1409 | } else { |
| 1410 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1411 | RTC_DCHECK(!track || |
| 1412 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1413 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1414 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1415 | new VideoRtpSender(worker_thread(), |
| 1416 | static_cast<VideoTrackInterface*>(track.get()), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1417 | stream_ids)); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1418 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1419 | sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1420 | return sender; |
| 1421 | } |
| 1422 | |
| 1423 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1424 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1425 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1426 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1427 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1428 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1429 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 1430 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1431 | new AudioRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1432 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1433 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1434 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 1435 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1436 | new VideoRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1437 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1438 | return receiver; |
| 1439 | } |
| 1440 | |
| 1441 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1442 | PeerConnection::CreateAndAddTransceiver( |
| 1443 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1444 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1445 | receiver) { |
| 1446 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1447 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1448 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1449 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1450 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1451 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1452 | } |
| 1453 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1454 | void PeerConnection::OnNegotiationNeeded() { |
| 1455 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1456 | RTC_DCHECK(!IsClosed()); |
| 1457 | observer_->OnRenegotiationNeeded(); |
| 1458 | } |
| 1459 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1460 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1461 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1462 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1463 | if (IsClosed()) { |
| 1464 | return nullptr; |
| 1465 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1466 | if (!track) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1467 | RTC_LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1468 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1469 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1470 | auto track_sender = FindSenderForTrack(track); |
| 1471 | if (!track_sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1472 | RTC_LOG(LS_ERROR) << "CreateDtmfSender called with a non-added track."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1473 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1476 | return track_sender->GetDtmfSender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1479 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1480 | const std::string& kind, |
| 1481 | const std::string& stream_id) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1482 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1483 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1484 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1485 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1486 | if (IsClosed()) { |
| 1487 | return nullptr; |
| 1488 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1489 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1490 | std::vector<std::string> stream_ids; |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 1491 | if (!stream_id.empty()) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1492 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1495 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1496 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1497 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1498 | auto* audio_sender = |
| 1499 | new AudioRtpSender(worker_thread(), nullptr, stream_ids, stats_.get()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1500 | audio_sender->SetVoiceMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1501 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1502 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1503 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1504 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1505 | auto* video_sender = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1506 | new VideoRtpSender(worker_thread(), nullptr, stream_ids); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1507 | video_sender->SetVideoMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1508 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1509 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1510 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1511 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1512 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1513 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1514 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1515 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1516 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1517 | } |
| 1518 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1519 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1520 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1521 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1522 | for (auto sender : GetSendersInternal()) { |
| 1523 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1524 | } |
| 1525 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1528 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1529 | PeerConnection::GetSendersInternal() const { |
| 1530 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1531 | all_senders; |
| 1532 | for (auto transceiver : transceivers_) { |
| 1533 | auto senders = transceiver->internal()->senders(); |
| 1534 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1535 | } |
| 1536 | return all_senders; |
| 1537 | } |
| 1538 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1539 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1540 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1541 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1542 | for (const auto& receiver : GetReceiversInternal()) { |
| 1543 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1544 | } |
| 1545 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1548 | std::vector< |
| 1549 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1550 | PeerConnection::GetReceiversInternal() const { |
| 1551 | std::vector< |
| 1552 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1553 | all_receivers; |
| 1554 | for (auto transceiver : transceivers_) { |
| 1555 | auto receivers = transceiver->internal()->receivers(); |
| 1556 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1557 | receivers.end()); |
| 1558 | } |
| 1559 | return all_receivers; |
| 1560 | } |
| 1561 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1562 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1563 | PeerConnection::GetTransceivers() const { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1564 | RTC_CHECK(IsUnifiedPlan()) |
| 1565 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1566 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
| 1567 | for (auto transceiver : transceivers_) { |
| 1568 | all_transceivers.push_back(transceiver); |
| 1569 | } |
| 1570 | return all_transceivers; |
| 1571 | } |
| 1572 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1573 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1574 | MediaStreamTrackInterface* track, |
| 1575 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1576 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1577 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1578 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1579 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1580 | return false; |
| 1581 | } |
| 1582 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1583 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1584 | // The StatsCollector is used to tell if a track is valid because it may |
| 1585 | // remember tracks that the PeerConnection previously removed. |
| 1586 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1587 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1588 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1589 | return false; |
| 1590 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1591 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1592 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1593 | return true; |
| 1594 | } |
| 1595 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1596 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1597 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1598 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1599 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1600 | stats_collector_->GetStatsReport(callback); |
| 1601 | } |
| 1602 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1603 | void PeerConnection::GetStats( |
| 1604 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1605 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1606 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1607 | RTC_DCHECK(callback); |
| 1608 | RTC_DCHECK(stats_collector_); |
| 1609 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1610 | if (selector) { |
| 1611 | for (const auto& proxy_transceiver : transceivers_) { |
| 1612 | for (const auto& proxy_sender : |
| 1613 | proxy_transceiver->internal()->senders()) { |
| 1614 | if (proxy_sender == selector) { |
| 1615 | internal_sender = proxy_sender->internal(); |
| 1616 | break; |
| 1617 | } |
| 1618 | } |
| 1619 | if (internal_sender) |
| 1620 | break; |
| 1621 | } |
| 1622 | } |
| 1623 | // If there is no |internal_sender|Â then |selector| is either null or does not |
| 1624 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1625 | // PeerConnection). This means that "all the stats objects representing the |
| 1626 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1627 | // produces an empty stats report. |
| 1628 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1629 | } |
| 1630 | |
| 1631 | void PeerConnection::GetStats( |
| 1632 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1633 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1634 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1635 | RTC_DCHECK(callback); |
| 1636 | RTC_DCHECK(stats_collector_); |
| 1637 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1638 | if (selector) { |
| 1639 | for (const auto& proxy_transceiver : transceivers_) { |
| 1640 | for (const auto& proxy_receiver : |
| 1641 | proxy_transceiver->internal()->receivers()) { |
| 1642 | if (proxy_receiver == selector) { |
| 1643 | internal_receiver = proxy_receiver->internal(); |
| 1644 | break; |
| 1645 | } |
| 1646 | } |
| 1647 | if (internal_receiver) |
| 1648 | break; |
| 1649 | } |
| 1650 | } |
| 1651 | // If there is no |internal_receiver|Â then |selector| is either null or does |
| 1652 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1653 | // the PeerConnection). This means that "all the stats objects representing |
| 1654 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1655 | // selector produces an empty stats report. |
| 1656 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1657 | } |
| 1658 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1659 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1660 | return signaling_state_; |
| 1661 | } |
| 1662 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1663 | PeerConnectionInterface::IceConnectionState |
| 1664 | PeerConnection::ice_connection_state() { |
| 1665 | return ice_connection_state_; |
| 1666 | } |
| 1667 | |
| 1668 | PeerConnectionInterface::IceGatheringState |
| 1669 | PeerConnection::ice_gathering_state() { |
| 1670 | return ice_gathering_state_; |
| 1671 | } |
| 1672 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1673 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1674 | PeerConnection::CreateDataChannel( |
| 1675 | const std::string& label, |
| 1676 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1677 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1678 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1679 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1680 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1681 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1682 | if (config) { |
| 1683 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1684 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1685 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1686 | InternalCreateDataChannel(label, internal_config.get())); |
| 1687 | if (!channel.get()) { |
| 1688 | return nullptr; |
| 1689 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1690 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1691 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1692 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1693 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1694 | observer_->OnRenegotiationNeeded(); |
| 1695 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1696 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1697 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1698 | } |
| 1699 | |
| 1700 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1701 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1702 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1703 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1704 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1705 | // Always create an offer even if |ConvertConstraintsToOfferAnswerOptions| |
| 1706 | // returns false for now. Because |ConvertConstraintsToOfferAnswerOptions| |
| 1707 | // compares the mandatory fields parsed with the mandatory fields added in the |
| 1708 | // |constraints| and some downstream applications might create offers with |
| 1709 | // mandatory fields which would not be parsed in the helper method. For |
| 1710 | // example, in Chromium/remoting, |kEnableDtlsSrtp| is added to the |
| 1711 | // |constraints| as a mandatory field but it is not parsed. |
| 1712 | ConvertConstraintsToOfferAnswerOptions(constraints, &offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1713 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1714 | CreateOffer(observer, offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1718 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1719 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1720 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1721 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1722 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1723 | return; |
| 1724 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1725 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1726 | if (IsClosed()) { |
| 1727 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1728 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1729 | PostCreateSessionDescriptionFailure( |
| 1730 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1731 | return; |
| 1732 | } |
| 1733 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1734 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1735 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1736 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1737 | PostCreateSessionDescriptionFailure( |
| 1738 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1739 | return; |
| 1740 | } |
| 1741 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1742 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1743 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1744 | if (IsUnifiedPlan()) { |
| 1745 | RTCError error = HandleLegacyOfferOptions(options); |
| 1746 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1747 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1748 | return; |
| 1749 | } |
| 1750 | } |
| 1751 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1752 | cricket::MediaSessionOptions session_options; |
| 1753 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1754 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1757 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1758 | const RTCOfferAnswerOptions& options) { |
| 1759 | RTC_DCHECK(IsUnifiedPlan()); |
| 1760 | |
| 1761 | if (options.offer_to_receive_audio == 0) { |
| 1762 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1763 | cricket::MEDIA_TYPE_AUDIO); |
| 1764 | } else if (options.offer_to_receive_audio == 1) { |
| 1765 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1766 | } else if (options.offer_to_receive_audio > 1) { |
| 1767 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1768 | "offer_to_receive_audio > 1 is not supported."); |
| 1769 | } |
| 1770 | |
| 1771 | if (options.offer_to_receive_video == 0) { |
| 1772 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1773 | cricket::MEDIA_TYPE_VIDEO); |
| 1774 | } else if (options.offer_to_receive_video == 1) { |
| 1775 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1776 | } else if (options.offer_to_receive_video > 1) { |
| 1777 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1778 | "offer_to_receive_video > 1 is not supported."); |
| 1779 | } |
| 1780 | |
| 1781 | return RTCError::OK(); |
| 1782 | } |
| 1783 | |
| 1784 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1785 | cricket::MediaType media_type) { |
| 1786 | for (auto transceiver : GetReceivingTransceiversOfType(media_type)) { |
| 1787 | transceiver->internal()->set_direction( |
| 1788 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false)); |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1793 | cricket::MediaType media_type) { |
| 1794 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
| 1795 | RtpTransceiverInit init; |
| 1796 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1797 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1802 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1803 | std::vector< |
| 1804 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1805 | receiving_transceivers; |
| 1806 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1807 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1808 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1809 | receiving_transceivers.push_back(transceiver); |
| 1810 | } |
| 1811 | } |
| 1812 | return receiving_transceivers; |
| 1813 | } |
| 1814 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1815 | void PeerConnection::CreateAnswer( |
| 1816 | CreateSessionDescriptionObserver* observer, |
| 1817 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1818 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1819 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1820 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1821 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1822 | return; |
| 1823 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1824 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1825 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1826 | if (!ConvertConstraintsToOfferAnswerOptions(constraints, |
| 1827 | &offer_answer_options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1828 | std::string error = "CreateAnswer called with invalid constraints."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1829 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1830 | PostCreateSessionDescriptionFailure( |
| 1831 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1832 | return; |
| 1833 | } |
| 1834 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1835 | CreateAnswer(observer, offer_answer_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1839 | const RTCOfferAnswerOptions& options) { |
| 1840 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1841 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1842 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1843 | return; |
| 1844 | } |
| 1845 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1846 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1847 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1848 | std::string error = |
| 1849 | "PeerConnection cannot create an answer in a state other than " |
| 1850 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1851 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1852 | PostCreateSessionDescriptionFailure( |
| 1853 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1854 | return; |
| 1855 | } |
| 1856 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1857 | // The remote description should be set if we're in the right state. |
| 1858 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1859 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1860 | if (IsUnifiedPlan()) { |
| 1861 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1862 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1863 | "supported with Unified Plan semantics. Use the " |
| 1864 | "RtpTransceiver API instead."; |
| 1865 | } |
| 1866 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1867 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1868 | "supported with Unified Plan semantics. Use the " |
| 1869 | "RtpTransceiver API instead."; |
| 1870 | } |
| 1871 | } |
| 1872 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1873 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1874 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1875 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1876 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | void PeerConnection::SetLocalDescription( |
| 1880 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1881 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1882 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1883 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1884 | // The SetLocalDescription contract is that we take ownership of the session |
| 1885 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 1886 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 1887 | // description as a unique_ptr argument to formalize this agreement. |
| 1888 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 1889 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1890 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1891 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1892 | return; |
| 1893 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1894 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1895 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1896 | PostSetSessionDescriptionFailure( |
| 1897 | observer, |
| 1898 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1899 | return; |
| 1900 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1901 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1902 | // If a session error has occurred the PeerConnection is in a possibly |
| 1903 | // inconsistent state so fail right away. |
| 1904 | if (session_error() != SessionError::kNone) { |
| 1905 | std::string error_message = GetSessionErrorMsg(); |
| 1906 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1907 | PostSetSessionDescriptionFailure( |
| 1908 | observer, |
| 1909 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1910 | return; |
| 1911 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1912 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1913 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1914 | if (!error.ok()) { |
| 1915 | std::string error_message = GetSetDescriptionErrorMessage( |
| 1916 | cricket::CS_LOCAL, desc->GetType(), error); |
| 1917 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1918 | PostSetSessionDescriptionFailure( |
| 1919 | observer, |
| 1920 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1921 | return; |
| 1922 | } |
| 1923 | |
| 1924 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 1925 | // which may destroy it before returning. |
| 1926 | const SdpType type = desc->GetType(); |
| 1927 | |
| 1928 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1929 | // |desc| may be destroyed at this point. |
| 1930 | |
| 1931 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1932 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 1933 | // inconsistent state, so act conservatively here and set the session error |
| 1934 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 1935 | SetSessionError(SessionError::kContent, error.message()); |
| 1936 | std::string error_message = |
| 1937 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 1938 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1939 | PostSetSessionDescriptionFailure( |
| 1940 | observer, |
| 1941 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1942 | return; |
| 1943 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1944 | RTC_DCHECK(local_description()); |
| 1945 | |
| 1946 | PostSetSessionDescriptionSuccess(observer); |
| 1947 | |
| 1948 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1949 | // size is not allowed, and changing the set of ICE servers will not result |
| 1950 | // in new candidates being gathered. |
| 1951 | port_allocator_->FreezeCandidatePool(); |
| 1952 | |
| 1953 | // MaybeStartGathering needs to be called after posting |
| 1954 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1955 | // before signaling that SetLocalDescription completed. |
| 1956 | transport_controller_->MaybeStartGathering(); |
| 1957 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1958 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1959 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1960 | // MaybeStartGathering... |
| 1961 | network_thread()->Invoke<void>( |
| 1962 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1963 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 1964 | // Make UMA notes about what was agreed to. |
| 1965 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | RTCError PeerConnection::ApplyLocalDescription( |
| 1970 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1971 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1972 | RTC_DCHECK(desc); |
| 1973 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1974 | // Update stats here so that we have the most recent stats for tracks and |
| 1975 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1976 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1977 | |
| 1978 | // Update the initial_offerer flag if this session is the initial_offerer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1979 | SdpType type = desc->GetType(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1980 | if (!initial_offerer_.has_value()) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1981 | initial_offerer_.emplace(type == SdpType::kOffer); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1982 | if (*initial_offerer_) { |
| 1983 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1984 | } else { |
| 1985 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1986 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1987 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1988 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1989 | // Take a reference to the old local description since it's used below to |
| 1990 | // compare against the new local description. When setting the new local |
| 1991 | // description, grab ownership of the replaced session description in case it |
| 1992 | // is the same as |old_local_description|, to keep it alive for the duration |
| 1993 | // of the method. |
| 1994 | const SessionDescriptionInterface* old_local_description = |
| 1995 | local_description(); |
| 1996 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1997 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1998 | replaced_local_description = pending_local_description_ |
| 1999 | ? std::move(pending_local_description_) |
| 2000 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2001 | current_local_description_ = std::move(desc); |
| 2002 | pending_local_description_ = nullptr; |
| 2003 | current_remote_description_ = std::move(pending_remote_description_); |
| 2004 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2005 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2006 | pending_local_description_ = std::move(desc); |
| 2007 | } |
| 2008 | // The session description to apply now must be accessed by |
| 2009 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2010 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2011 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2012 | if (IsUnifiedPlan()) { |
| 2013 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2014 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2015 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2016 | if (!error.ok()) { |
| 2017 | return error; |
| 2018 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2019 | for (auto transceiver : transceivers_) { |
| 2020 | const ContentInfo* content = |
| 2021 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2022 | if (!content) { |
| 2023 | continue; |
| 2024 | } |
| 2025 | const MediaContentDescription* media_desc = content->media_description(); |
| 2026 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2027 | transceiver->internal()->set_current_direction(media_desc->direction()); |
| 2028 | } |
| 2029 | if (content->rejected && !transceiver->stopped()) { |
| 2030 | transceiver->Stop(); |
| 2031 | } |
| 2032 | } |
| 2033 | } else { |
| 2034 | // Transport and Media channels will be created only when offer is set. |
| 2035 | if (type == SdpType::kOffer) { |
| 2036 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2037 | // description is applied. Restore back to old description. |
| 2038 | RTCError error = CreateChannels(*local_description()->description()); |
| 2039 | if (!error.ok()) { |
| 2040 | return error; |
| 2041 | } |
| 2042 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2043 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2044 | // Remove unused channels if MediaContentDescription is rejected. |
| 2045 | RemoveUnusedChannels(local_description()->description()); |
| 2046 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2047 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2048 | RTCError error = UpdateSessionState(type, cricket::CS_LOCAL); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2049 | if (!error.ok()) { |
| 2050 | return error; |
| 2051 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2052 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2053 | if (remote_description()) { |
| 2054 | // Now that we have a local description, we can push down remote candidates. |
| 2055 | UseCandidatesInSessionDescription(remote_description()); |
| 2056 | } |
| 2057 | |
| 2058 | pending_ice_restarts_.clear(); |
| 2059 | if (session_error() != SessionError::kNone) { |
| 2060 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2061 | } |
| 2062 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2063 | // If setting the description decided our SSL role, allocate any necessary |
| 2064 | // SCTP sids. |
| 2065 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2066 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2067 | AllocateSctpSids(role); |
| 2068 | } |
| 2069 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2070 | if (IsUnifiedPlan()) { |
| 2071 | for (auto transceiver : transceivers_) { |
| 2072 | const ContentInfo* content = |
| 2073 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2074 | if (!content) { |
| 2075 | continue; |
| 2076 | } |
| 2077 | if (content->rejected && !transceiver->stopped()) { |
| 2078 | transceiver->Stop(); |
| 2079 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2080 | const auto& streams = content->media_description()->streams(); |
| 2081 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2082 | transceiver->internal()->sender_internal()->set_stream_ids( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 2083 | streams[0].stream_ids()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2084 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2085 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | } else { |
| 2089 | // Plan B semantics. |
| 2090 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2091 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2092 | // local session description. |
| 2093 | const cricket::ContentInfo* audio_content = |
| 2094 | GetFirstAudioContent(local_description()->description()); |
| 2095 | if (audio_content) { |
| 2096 | if (audio_content->rejected) { |
| 2097 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2098 | } else { |
| 2099 | const cricket::AudioContentDescription* audio_desc = |
| 2100 | audio_content->media_description()->as_audio(); |
| 2101 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2102 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2103 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2104 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2105 | const cricket::ContentInfo* video_content = |
| 2106 | GetFirstVideoContent(local_description()->description()); |
| 2107 | if (video_content) { |
| 2108 | if (video_content->rejected) { |
| 2109 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2110 | } else { |
| 2111 | const cricket::VideoContentDescription* video_desc = |
| 2112 | video_content->media_description()->as_video(); |
| 2113 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2114 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2115 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2119 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2120 | if (data_content) { |
| 2121 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2122 | data_content->media_description()->as_data(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2123 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2124 | cricket::kMediaProtocolRtpPrefix)) { |
| 2125 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 2126 | } |
| 2127 | } |
| 2128 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2129 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2133 | SetSessionDescriptionObserver* observer, |
| 2134 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2135 | SetRemoteDescription( |
| 2136 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2137 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2138 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2139 | } |
| 2140 | |
| 2141 | void PeerConnection::SetRemoteDescription( |
| 2142 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2143 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2144 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2145 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2146 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2147 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2148 | return; |
| 2149 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2150 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2151 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2152 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2153 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2154 | return; |
| 2155 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2156 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2157 | // If a session error has occurred the PeerConnection is in a possibly |
| 2158 | // inconsistent state so fail right away. |
| 2159 | if (session_error() != SessionError::kNone) { |
| 2160 | std::string error_message = GetSessionErrorMsg(); |
| 2161 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2162 | observer->OnSetRemoteDescriptionComplete( |
| 2163 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2164 | return; |
| 2165 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2166 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2167 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2168 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2169 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2170 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2171 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2172 | observer->OnSetRemoteDescriptionComplete( |
| 2173 | RTCError(error.type(), std::move(error_message))); |
| 2174 | return; |
| 2175 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2176 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2177 | // Grab the description type before moving ownership to |
| 2178 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2179 | const SdpType type = desc->GetType(); |
| 2180 | |
| 2181 | error = ApplyRemoteDescription(std::move(desc)); |
| 2182 | // |desc| may be destroyed at this point. |
| 2183 | |
| 2184 | if (!error.ok()) { |
| 2185 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2186 | // inconsistent state, so act conservatively here and set the session error |
| 2187 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2188 | SetSessionError(SessionError::kContent, error.message()); |
| 2189 | std::string error_message = |
| 2190 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2191 | RTC_LOG(LS_ERROR) << error_message; |
| 2192 | observer->OnSetRemoteDescriptionComplete( |
| 2193 | RTCError(error.type(), std::move(error_message))); |
| 2194 | return; |
| 2195 | } |
| 2196 | RTC_DCHECK(remote_description()); |
| 2197 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 2198 | if (type == SdpType::kOffer) { |
| 2199 | // Report to UMA the format of the received offer. |
| 2200 | ReportSdpFormatReceived(*remote_description()); |
| 2201 | } |
| 2202 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2203 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2204 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2205 | // MaybeStartGathering... |
| 2206 | network_thread()->Invoke<void>( |
| 2207 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2208 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2209 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2210 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
| 2214 | } |
| 2215 | |
| 2216 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2217 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2218 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2219 | RTC_DCHECK(desc); |
| 2220 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2221 | // Update stats here so that we have the most recent stats for tracks and |
| 2222 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2223 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2224 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2225 | // Take a reference to the old remote description since it's used below to |
| 2226 | // compare against the new remote description. When setting the new remote |
| 2227 | // description, grab ownership of the replaced session description in case it |
| 2228 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2229 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2230 | const SessionDescriptionInterface* old_remote_description = |
| 2231 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2232 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2233 | SdpType type = desc->GetType(); |
| 2234 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2235 | replaced_remote_description = pending_remote_description_ |
| 2236 | ? std::move(pending_remote_description_) |
| 2237 | : std::move(current_remote_description_); |
| 2238 | current_remote_description_ = std::move(desc); |
| 2239 | pending_remote_description_ = nullptr; |
| 2240 | current_local_description_ = std::move(pending_local_description_); |
| 2241 | } else { |
| 2242 | replaced_remote_description = std::move(pending_remote_description_); |
| 2243 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2244 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2245 | // The session description to apply now must be accessed by |
| 2246 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2247 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2248 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2249 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2250 | if (IsUnifiedPlan()) { |
| 2251 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2252 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2253 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2254 | if (!error.ok()) { |
| 2255 | return error; |
| 2256 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2257 | } else { |
| 2258 | if (type == SdpType::kOffer) { |
| 2259 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2260 | // description is applied. Restore back to old description. |
| 2261 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2262 | if (!error.ok()) { |
| 2263 | return error; |
| 2264 | } |
| 2265 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2266 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2267 | // Remove unused channels if MediaContentDescription is rejected. |
| 2268 | RemoveUnusedChannels(remote_description()->description()); |
| 2269 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2270 | |
| 2271 | // NOTE: Candidates allocation will be initiated only when SetLocalDescription |
| 2272 | // is called. |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2273 | RTCError error = UpdateSessionState(type, cricket::CS_REMOTE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2274 | if (!error.ok()) { |
| 2275 | return error; |
| 2276 | } |
| 2277 | |
| 2278 | if (local_description() && |
| 2279 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2280 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2281 | } |
| 2282 | |
| 2283 | if (old_remote_description) { |
| 2284 | for (const cricket::ContentInfo& content : |
| 2285 | old_remote_description->description()->contents()) { |
| 2286 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2287 | // password that indicates the remote peer requests an ICE restart. |
| 2288 | // TODO(deadbeef): When we start storing both the current and pending |
| 2289 | // remote description, this should reset pending_ice_restarts and compare |
| 2290 | // against the current description. |
| 2291 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2292 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2293 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2294 | pending_ice_restarts_.insert(content.name); |
| 2295 | } |
| 2296 | } else { |
| 2297 | // We retain all received candidates only if ICE is not restarted. |
| 2298 | // When ICE is restarted, all previous candidates belong to an old |
| 2299 | // generation and should not be kept. |
| 2300 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2301 | // description should only contain candidates from the last set remote |
| 2302 | // description plus any candidates added since then. We should remove |
| 2303 | // this once we're sure it won't break anything. |
| 2304 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2305 | old_remote_description, content.name, mutable_remote_description()); |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | if (session_error() != SessionError::kNone) { |
| 2311 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2312 | } |
| 2313 | |
| 2314 | // Set the the ICE connection state to connecting since the connection may |
| 2315 | // become writable with peer reflexive candidates before any remote candidate |
| 2316 | // is signaled. |
| 2317 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2318 | // is to have a new signal the indicates a change in checking state from the |
| 2319 | // transport and expose a new checking() member from transport that can be |
| 2320 | // read to determine the current checking state. The existing SignalConnecting |
| 2321 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2322 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2323 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2324 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2325 | } |
| 2326 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2327 | // If setting the description decided our SSL role, allocate any necessary |
| 2328 | // SCTP sids. |
| 2329 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2330 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2331 | AllocateSctpSids(role); |
| 2332 | } |
| 2333 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2334 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2335 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 2336 | receiving_transceivers; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2337 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2338 | for (auto transceiver : transceivers_) { |
| 2339 | const ContentInfo* content = |
| 2340 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2341 | if (!content) { |
| 2342 | continue; |
| 2343 | } |
| 2344 | const MediaContentDescription* media_desc = content->media_description(); |
| 2345 | RtpTransceiverDirection local_direction = |
| 2346 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 2347 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 "Set |
| 2348 | // the RTCSessionDescription: If direction is sendrecv or recvonly, and |
| 2349 | // transceiver's current direction is neither sendrecv nor recvonly, |
| 2350 | // process the addition of a remote track for the media description. |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 2351 | // |
| 2352 | // Create a sync label in the case of an unsignalled msid. |
| 2353 | std::string sync_label = rtc::CreateRandomUuid(); |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2354 | if (!media_desc->streams().empty()) { |
Steve Anton | 5a26a3a | 2018-02-28 11:38:47 -0800 | [diff] [blame] | 2355 | const cricket::StreamParams& stream_params = media_desc->streams()[0]; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 2356 | if (!stream_params.stream_ids().empty()) { |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 2357 | sync_label = stream_params.stream_ids()[0]; |
Steve Anton | 5a26a3a | 2018-02-28 11:38:47 -0800 | [diff] [blame] | 2358 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2359 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2360 | if (RtpTransceiverDirectionHasRecv(local_direction) && |
| 2361 | (!transceiver->current_direction() || |
| 2362 | !RtpTransceiverDirectionHasRecv( |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2363 | *transceiver->current_direction()))) { |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 2364 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2365 | remote_streams_->find(sync_label); |
| 2366 | if (!stream) { |
| 2367 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2368 | MediaStream::Create(sync_label)); |
| 2369 | remote_streams_->AddStream(stream); |
| 2370 | added_streams.push_back(stream); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2371 | } |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 2372 | transceiver->internal()->receiver_internal()->SetStreams({stream}); |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2373 | receiving_transceivers.push_back(transceiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2374 | } |
| 2375 | // If direction is sendonly or inactive, and transceiver's current |
| 2376 | // direction is neither sendonly nor inactive, process the removal of a |
| 2377 | // remote track for the media description. |
| 2378 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2379 | (transceiver->current_direction() && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2380 | RtpTransceiverDirectionHasRecv(*transceiver->current_direction()))) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2381 | transceiver->internal()->receiver_internal()->SetStreams({}); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2382 | } |
| 2383 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2384 | transceiver->internal()->set_current_direction(local_direction); |
| 2385 | } |
| 2386 | if (content->rejected && !transceiver->stopped()) { |
| 2387 | transceiver->Stop(); |
| 2388 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2389 | if (!content->rejected && |
| 2390 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2391 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2392 | uint32_t ssrc = 0; |
| 2393 | if (!media_desc->streams().empty()) { |
| 2394 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2395 | } |
| 2396 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2397 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2398 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2399 | // Once all processing has finished, fire off callbacks. |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2400 | for (auto transceiver : receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2401 | stats_->AddTrack(transceiver->receiver()->track()); |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2402 | observer_->OnTrack(transceiver); |
| 2403 | observer_->OnAddTrack(transceiver->receiver(), |
| 2404 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2405 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2406 | for (auto stream : added_streams) { |
| 2407 | observer_->OnAddStream(stream); |
| 2408 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2409 | } |
| 2410 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2411 | const cricket::ContentInfo* audio_content = |
| 2412 | GetFirstAudioContent(remote_description()->description()); |
| 2413 | const cricket::ContentInfo* video_content = |
| 2414 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2415 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2416 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2417 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2418 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2419 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2420 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2421 | |
| 2422 | // Check if the descriptions include streams, just in case the peer supports |
| 2423 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2424 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2425 | (audio_desc && !audio_desc->streams().empty()) || |
| 2426 | (video_desc && !video_desc->streams().empty())) { |
| 2427 | remote_peer_supports_msid_ = true; |
| 2428 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2429 | |
| 2430 | // We wait to signal new streams until we finish processing the description, |
| 2431 | // since only at that point will new streams have all their tracks. |
| 2432 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2433 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2434 | if (!IsUnifiedPlan()) { |
| 2435 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2436 | // rejected media section, there is some cleanup logic that expects the |
| 2437 | // voice/ video channel to still be set. But in this method the voice/video |
| 2438 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2439 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2440 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2441 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2442 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2443 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2444 | // and MediaStreams. |
| 2445 | if (audio_content) { |
| 2446 | if (audio_content->rejected) { |
| 2447 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2448 | } else { |
| 2449 | bool default_audio_track_needed = |
| 2450 | !remote_peer_supports_msid_ && |
| 2451 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2452 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2453 | default_audio_track_needed, audio_desc->type(), |
| 2454 | new_streams); |
| 2455 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2456 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2457 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2458 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2459 | // and MediaStreams. |
| 2460 | if (video_content) { |
| 2461 | if (video_content->rejected) { |
| 2462 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2463 | } else { |
| 2464 | bool default_video_track_needed = |
| 2465 | !remote_peer_supports_msid_ && |
| 2466 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2467 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2468 | default_video_track_needed, video_desc->type(), |
| 2469 | new_streams); |
| 2470 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2471 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2472 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2473 | // Update the DataChannels with the information from the remote peer. |
| 2474 | if (data_desc) { |
| 2475 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2476 | cricket::kMediaProtocolRtpPrefix)) { |
| 2477 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2478 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2479 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2480 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2481 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 2482 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2483 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2484 | stats_->AddStream(new_stream); |
| 2485 | observer_->OnAddStream( |
| 2486 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2487 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2488 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2489 | UpdateEndedRemoteMediaStreams(); |
| 2490 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2491 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2492 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2493 | } |
| 2494 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2495 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2496 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2497 | const SessionDescriptionInterface& new_session, |
| 2498 | const SessionDescriptionInterface* old_local_description, |
| 2499 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2500 | RTC_DCHECK(IsUnifiedPlan()); |
| 2501 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2502 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2503 | if (new_session.GetType() == SdpType::kOffer) { |
| 2504 | auto bundle_group_or_error = |
| 2505 | GetEarlyBundleGroup(*new_session.description()); |
| 2506 | if (!bundle_group_or_error.ok()) { |
| 2507 | return bundle_group_or_error.MoveError(); |
| 2508 | } |
| 2509 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2510 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2511 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2512 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2513 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2514 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2515 | cricket::MediaType media_type = new_content.media_description()->type(); |
| 2516 | seen_mids_.insert(new_content.name); |
| 2517 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2518 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2519 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2520 | if (old_local_description && |
| 2521 | i < old_local_description->description()->contents().size()) { |
| 2522 | old_local_content = |
| 2523 | &old_local_description->description()->contents()[i]; |
| 2524 | } |
| 2525 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2526 | if (old_remote_description && |
| 2527 | i < old_remote_description->description()->contents().size()) { |
| 2528 | old_remote_content = |
| 2529 | &old_remote_description->description()->contents()[i]; |
| 2530 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2531 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2532 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2533 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2534 | if (!transceiver_or_error.ok()) { |
| 2535 | return transceiver_or_error.MoveError(); |
| 2536 | } |
| 2537 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2538 | RTCError error = |
| 2539 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2540 | if (!error.ok()) { |
| 2541 | return error; |
| 2542 | } |
| 2543 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2544 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2545 | // Ignore all but the first data section. |
| 2546 | continue; |
| 2547 | } |
| 2548 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2549 | if (!error.ok()) { |
| 2550 | return error; |
| 2551 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2552 | } else { |
| 2553 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2554 | "Unknown section type."); |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | return RTCError::OK(); |
| 2559 | } |
| 2560 | |
| 2561 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2562 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2563 | transceiver, |
| 2564 | const cricket::ContentInfo& content, |
| 2565 | const cricket::ContentGroup* bundle_group) { |
| 2566 | RTC_DCHECK(IsUnifiedPlan()); |
| 2567 | RTC_DCHECK(transceiver); |
| 2568 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 2569 | if (content.rejected) { |
| 2570 | if (channel) { |
| 2571 | transceiver->internal()->SetChannel(nullptr); |
| 2572 | DestroyBaseChannel(channel); |
| 2573 | } |
| 2574 | } else { |
| 2575 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2576 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2577 | channel = CreateVoiceChannel( |
| 2578 | content.name, |
| 2579 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2580 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2581 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2582 | channel = CreateVideoChannel( |
| 2583 | content.name, |
| 2584 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2585 | } |
| 2586 | if (!channel) { |
| 2587 | LOG_AND_RETURN_ERROR( |
| 2588 | RTCErrorType::INTERNAL_ERROR, |
| 2589 | "Failed to create channel for mid=" + content.name); |
| 2590 | } |
| 2591 | transceiver->internal()->SetChannel(channel); |
| 2592 | } |
| 2593 | } |
| 2594 | return RTCError::OK(); |
| 2595 | } |
| 2596 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2597 | RTCError PeerConnection::UpdateDataChannel( |
| 2598 | cricket::ContentSource source, |
| 2599 | const cricket::ContentInfo& content, |
| 2600 | const cricket::ContentGroup* bundle_group) { |
| 2601 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2602 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2603 | // will take care of rejecting it. |
| 2604 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2605 | } |
| 2606 | if (content.rejected) { |
| 2607 | DestroyDataChannel(); |
| 2608 | } else { |
| 2609 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 2610 | if (!CreateDataChannel(content.name, GetTransportNameForMediaSection( |
| 2611 | content.name, bundle_group))) { |
| 2612 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2613 | "Failed to create data channel."); |
| 2614 | } |
| 2615 | } |
| 2616 | if (source == cricket::CS_REMOTE) { |
| 2617 | const MediaContentDescription* data_desc = content.media_description(); |
| 2618 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2619 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2620 | } |
| 2621 | } |
| 2622 | } |
| 2623 | return RTCError::OK(); |
| 2624 | } |
| 2625 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2626 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2627 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2628 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2629 | size_t mline_index, |
| 2630 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2631 | const ContentInfo* old_local_content, |
| 2632 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2633 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2634 | // If this is an offer then the m= section might be recycled. If the m= |
| 2635 | // section is being recycled (defined as: rejected in the current local or |
| 2636 | // remote description and not rejected in new description), dissociate the |
| 2637 | // currently associated RtpTransceiver by setting its mid property to null, |
| 2638 | // and discard the mapping between the transceiver and its m= section index. |
| 2639 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 2640 | old_remote_content)) { |
| 2641 | // We want to dissociate the transceiver that has the rejected mid. |
| 2642 | const std::string& old_mid = |
| 2643 | (old_local_content && old_local_content->rejected) |
| 2644 | ? old_local_content->name |
| 2645 | : old_remote_content->name; |
| 2646 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2647 | if (old_transceiver) { |
| 2648 | old_transceiver->internal()->set_mid(rtc::nullopt); |
| 2649 | old_transceiver->internal()->set_mline_index(rtc::nullopt); |
| 2650 | } |
| 2651 | } |
| 2652 | const MediaContentDescription* media_desc = content.media_description(); |
| 2653 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2654 | if (source == cricket::CS_LOCAL) { |
| 2655 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2656 | // mapping between transceivers and m= section indices established when |
| 2657 | // creating the offer. |
| 2658 | if (!transceiver) { |
| 2659 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2660 | } |
| 2661 | if (!transceiver) { |
| 2662 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2663 | "Unknown transceiver"); |
| 2664 | } |
| 2665 | } else { |
| 2666 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2667 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2668 | // of the same type... |
| 2669 | if (!transceiver && |
| 2670 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2671 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2672 | } |
| 2673 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2674 | // recvonly direction. |
| 2675 | if (!transceiver) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2676 | auto sender = |
| 2677 | CreateSender(media_desc->type(), nullptr, {rtc::CreateRandomUuid()}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2678 | std::string receiver_id; |
| 2679 | if (!media_desc->streams().empty()) { |
| 2680 | receiver_id = media_desc->streams()[0].id; |
| 2681 | } else { |
| 2682 | receiver_id = rtc::CreateRandomUuid(); |
| 2683 | } |
| 2684 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2685 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2686 | transceiver->internal()->set_direction( |
| 2687 | RtpTransceiverDirection::kRecvOnly); |
| 2688 | } |
| 2689 | } |
| 2690 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2691 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2692 | LOG_AND_RETURN_ERROR( |
| 2693 | RTCErrorType::INVALID_PARAMETER, |
| 2694 | "Transceiver type does not match media description type."); |
| 2695 | } |
| 2696 | // Associate the found or created RtpTransceiver with the m= section by |
| 2697 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2698 | // section, and establish a mapping between the transceiver and the index of |
| 2699 | // the m= section. |
| 2700 | transceiver->internal()->set_mid(content.name); |
| 2701 | transceiver->internal()->set_mline_index(mline_index); |
| 2702 | return std::move(transceiver); |
| 2703 | } |
| 2704 | |
| 2705 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2706 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 2707 | RTC_DCHECK(IsUnifiedPlan()); |
| 2708 | for (auto transceiver : transceivers_) { |
| 2709 | if (transceiver->mid() == mid) { |
| 2710 | return transceiver; |
| 2711 | } |
| 2712 | } |
| 2713 | return nullptr; |
| 2714 | } |
| 2715 | |
| 2716 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2717 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 2718 | RTC_DCHECK(IsUnifiedPlan()); |
| 2719 | for (auto transceiver : transceivers_) { |
| 2720 | if (transceiver->internal()->mline_index() == mline_index) { |
| 2721 | return transceiver; |
| 2722 | } |
| 2723 | } |
| 2724 | return nullptr; |
| 2725 | } |
| 2726 | |
| 2727 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2728 | PeerConnection::FindAvailableTransceiverToReceive( |
| 2729 | cricket::MediaType media_type) const { |
| 2730 | RTC_DCHECK(IsUnifiedPlan()); |
| 2731 | // From JSEP section 5.10 (Applying a Remote Description): |
| 2732 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 2733 | // the same type that were added to the PeerConnection by addTrack and are not |
| 2734 | // associated with any m= section and are not stopped, find the first such |
| 2735 | // RtpTransceiver. |
| 2736 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2737 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2738 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 2739 | !transceiver->stopped()) { |
| 2740 | return transceiver; |
| 2741 | } |
| 2742 | } |
| 2743 | return nullptr; |
| 2744 | } |
| 2745 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2746 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 2747 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2748 | transceiver, |
| 2749 | const SessionDescriptionInterface* sdesc) const { |
| 2750 | RTC_DCHECK(transceiver); |
| 2751 | RTC_DCHECK(sdesc); |
| 2752 | if (IsUnifiedPlan()) { |
| 2753 | if (!transceiver->internal()->mid()) { |
| 2754 | // This transceiver is not associated with a media section yet. |
| 2755 | return nullptr; |
| 2756 | } |
| 2757 | return sdesc->description()->GetContentByName( |
| 2758 | *transceiver->internal()->mid()); |
| 2759 | } else { |
| 2760 | // Plan B only allows at most one audio and one video section, so use the |
| 2761 | // first media section of that type. |
| 2762 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2763 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2764 | } |
| 2765 | } |
| 2766 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 2767 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 2768 | return configuration_; |
| 2769 | } |
| 2770 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2771 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 2772 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2773 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 2774 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2775 | if (local_description() && configuration.ice_candidate_pool_size != |
| 2776 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2777 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 2778 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2779 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2780 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2781 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2782 | // The simplest (and most future-compatible) way to tell if the config was |
| 2783 | // modified in an invalid way is to copy each property we do support |
| 2784 | // modifying, then use operator==. There are far more properties we don't |
| 2785 | // support modifying than those we do, and more could be added. |
| 2786 | RTCConfiguration modified_config = configuration_; |
| 2787 | modified_config.servers = configuration.servers; |
| 2788 | modified_config.type = configuration.type; |
| 2789 | modified_config.ice_candidate_pool_size = |
| 2790 | configuration.ice_candidate_pool_size; |
| 2791 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2792 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 2793 | modified_config.ice_check_interval_strong_connectivity = |
| 2794 | configuration.ice_check_interval_strong_connectivity; |
| 2795 | modified_config.ice_check_interval_weak_connectivity = |
| 2796 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2797 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 2798 | modified_config.ice_unwritable_min_checks = |
| 2799 | configuration.ice_unwritable_min_checks; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2800 | modified_config.stun_candidate_keepalive_interval = |
| 2801 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2802 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 2803 | modified_config.network_preference = configuration.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2804 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2805 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2806 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2807 | } |
| 2808 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 2809 | // Validate the modified configuration. |
| 2810 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 2811 | if (!validate_error.ok()) { |
| 2812 | return SafeSetError(std::move(validate_error), error); |
| 2813 | } |
| 2814 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2815 | // Note that this isn't possible through chromium, since it's an unsigned |
| 2816 | // short in WebIDL. |
| 2817 | if (configuration.ice_candidate_pool_size < 0 || |
| 2818 | configuration.ice_candidate_pool_size > UINT16_MAX) { |
| 2819 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 2820 | } |
| 2821 | |
| 2822 | // Parse ICE servers before hopping to network thread. |
| 2823 | cricket::ServerAddresses stun_servers; |
| 2824 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 2825 | RTCErrorType parse_error = |
| 2826 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 2827 | if (parse_error != RTCErrorType::NONE) { |
| 2828 | return SafeSetError(parse_error, error); |
| 2829 | } |
| 2830 | |
| 2831 | // In theory this shouldn't fail. |
| 2832 | if (!network_thread()->Invoke<bool>( |
| 2833 | RTC_FROM_HERE, |
| 2834 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 2835 | stun_servers, turn_servers, modified_config.type, |
| 2836 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2837 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2838 | modified_config.turn_customizer, |
| 2839 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2840 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2841 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 2842 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2843 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2844 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 2845 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 2846 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2847 | if (modified_config.servers != configuration_.servers || |
| 2848 | modified_config.type != configuration_.type || |
| 2849 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2850 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2851 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2852 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 2853 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2854 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2855 | configuration_ = modified_config; |
| 2856 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2859 | bool PeerConnection::AddIceCandidate( |
| 2860 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2861 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2862 | if (IsClosed()) { |
| 2863 | return false; |
| 2864 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2865 | |
| 2866 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2867 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2868 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2869 | return false; |
| 2870 | } |
| 2871 | |
| 2872 | if (!ice_candidate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2873 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2874 | return false; |
| 2875 | } |
| 2876 | |
| 2877 | bool valid = false; |
| 2878 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 2879 | if (!valid) { |
| 2880 | return false; |
| 2881 | } |
| 2882 | |
| 2883 | // Add this candidate to the remote session description. |
| 2884 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2885 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2886 | return false; |
| 2887 | } |
| 2888 | |
| 2889 | if (ready) { |
| 2890 | return UseCandidate(ice_candidate); |
| 2891 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2892 | RTC_LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2893 | return true; |
| 2894 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2897 | bool PeerConnection::RemoveIceCandidates( |
| 2898 | const std::vector<cricket::Candidate>& candidates) { |
| 2899 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2900 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2901 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2902 | "removed without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2903 | return false; |
| 2904 | } |
| 2905 | |
| 2906 | if (candidates.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2907 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2908 | return false; |
| 2909 | } |
| 2910 | |
| 2911 | size_t number_removed = |
| 2912 | mutable_remote_description()->RemoveCandidates(candidates); |
| 2913 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2914 | RTC_LOG(LS_ERROR) |
| 2915 | << "RemoveRemoteIceCandidates: Failed to remove candidates. " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2916 | "Requested " |
| 2917 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2918 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | // Remove the candidates from the transport controller. |
| 2922 | std::string error; |
| 2923 | bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error); |
| 2924 | if (!res && !error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2925 | RTC_LOG(LS_ERROR) << "Error when removing remote candidates: " << error; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2926 | } |
| 2927 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2928 | } |
| 2929 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2930 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2931 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2932 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2933 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2934 | if (transport_controller()) { |
| 2935 | transport_controller()->SetMetricsObserver(uma_observer_); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 2938 | for (auto transceiver : transceivers_) { |
| 2939 | auto* channel = transceiver->internal()->channel(); |
| 2940 | if (channel) { |
| 2941 | channel->SetMetricsObserver(uma_observer_); |
| 2942 | } |
| 2943 | } |
| 2944 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2945 | // Send information about IPv4/IPv6 status. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2946 | if (uma_observer_) { |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 2947 | port_allocator_->SetMetricsObserver(uma_observer_); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2948 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2949 | uma_observer_->IncrementEnumCounter( |
| 2950 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 2951 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 2952 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2953 | uma_observer_->IncrementEnumCounter( |
| 2954 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 2955 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2956 | } |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2957 | // Send information about the requested SDP semantics. |
| 2958 | switch (configuration_.sdp_semantics) { |
| 2959 | case SdpSemantics::kDefault: |
| 2960 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2961 | kSdpSemanticRequestDefault, |
| 2962 | kSdpSemanticRequestMax); |
| 2963 | |
| 2964 | break; |
| 2965 | case SdpSemantics::kPlanB: |
| 2966 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2967 | kSdpSemanticRequestPlanB, |
| 2968 | kSdpSemanticRequestMax); |
| 2969 | break; |
| 2970 | case SdpSemantics::kUnifiedPlan: |
| 2971 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2972 | kSdpSemanticRequestUnifiedPlan, |
| 2973 | kSdpSemanticRequestMax); |
| 2974 | break; |
| 2975 | default: |
| 2976 | RTC_NOTREACHED(); |
| 2977 | } |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2978 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2981 | RTCError PeerConnection::SetBitrate(const BitrateParameters& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2982 | if (!worker_thread()->IsCurrent()) { |
| 2983 | return worker_thread()->Invoke<RTCError>( |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2984 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::SetBitrate, this, bitrate)); |
| 2985 | } |
| 2986 | |
| 2987 | const bool has_min = static_cast<bool>(bitrate.min_bitrate_bps); |
| 2988 | const bool has_current = static_cast<bool>(bitrate.current_bitrate_bps); |
| 2989 | const bool has_max = static_cast<bool>(bitrate.max_bitrate_bps); |
| 2990 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 2991 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2992 | "min_bitrate_bps <= 0"); |
| 2993 | } |
| 2994 | if (has_current) { |
| 2995 | if (has_min && *bitrate.current_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 2996 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2997 | "current_bitrate_bps < min_bitrate_bps"); |
| 2998 | } else if (*bitrate.current_bitrate_bps < 0) { |
| 2999 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3000 | "curent_bitrate_bps < 0"); |
| 3001 | } |
| 3002 | } |
| 3003 | if (has_max) { |
| 3004 | if (has_current && |
| 3005 | *bitrate.max_bitrate_bps < *bitrate.current_bitrate_bps) { |
| 3006 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3007 | "max_bitrate_bps < current_bitrate_bps"); |
| 3008 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3009 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3010 | "max_bitrate_bps < min_bitrate_bps"); |
| 3011 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3012 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3013 | "max_bitrate_bps < 0"); |
| 3014 | } |
| 3015 | } |
| 3016 | |
Sebastian Jansson | fc8d26b | 2018-02-21 09:52:06 +0100 | [diff] [blame] | 3017 | BitrateConstraintsMask mask; |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3018 | mask.min_bitrate_bps = bitrate.min_bitrate_bps; |
| 3019 | mask.start_bitrate_bps = bitrate.current_bitrate_bps; |
| 3020 | mask.max_bitrate_bps = bitrate.max_bitrate_bps; |
| 3021 | |
| 3022 | RTC_DCHECK(call_.get()); |
Sebastian Jansson | 8f83b42 | 2018-02-21 13:07:13 +0100 | [diff] [blame] | 3023 | call_->GetTransportControllerSend()->SetClientBitratePreferences(mask); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3024 | |
| 3025 | return RTCError::OK(); |
| 3026 | } |
| 3027 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3028 | void PeerConnection::SetBitrateAllocationStrategy( |
| 3029 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 3030 | bitrate_allocation_strategy) { |
| 3031 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 3032 | if (!worker_thread->IsCurrent()) { |
| 3033 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 3034 | bitrate_allocation_strategy.release(); |
| 3035 | auto functor = [this, strategy_raw]() { |
| 3036 | call_->SetBitrateAllocationStrategy( |
| 3037 | rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
| 3038 | }; |
| 3039 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 3040 | return; |
| 3041 | } |
| 3042 | RTC_DCHECK(call_.get()); |
| 3043 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 3044 | } |
| 3045 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3046 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3047 | if (!worker_thread()->IsCurrent()) { |
| 3048 | worker_thread()->Invoke<void>( |
| 3049 | RTC_FROM_HERE, |
| 3050 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3051 | return; |
| 3052 | } |
| 3053 | auto audio_state = |
| 3054 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3055 | audio_state->SetPlayout(playout); |
| 3056 | } |
| 3057 | |
| 3058 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3059 | if (!worker_thread()->IsCurrent()) { |
| 3060 | worker_thread()->Invoke<void>( |
| 3061 | RTC_FROM_HERE, |
| 3062 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3063 | return; |
| 3064 | } |
| 3065 | auto audio_state = |
| 3066 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3067 | audio_state->SetRecording(recording); |
| 3068 | } |
| 3069 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3070 | std::unique_ptr<rtc::SSLCertificate> |
| 3071 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3072 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3073 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3074 | return nullptr; |
| 3075 | } |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3076 | return chain->Get(0).GetUniqueReference(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3077 | } |
| 3078 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3079 | std::unique_ptr<rtc::SSLCertChain> |
| 3080 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3081 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3082 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3083 | return nullptr; |
| 3084 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3085 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3086 | audio_transceiver->internal()->channel()->transport_name()); |
| 3087 | } |
| 3088 | |
| 3089 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3090 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3091 | for (auto transceiver : transceivers_) { |
| 3092 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3093 | return transceiver; |
| 3094 | } |
| 3095 | } |
| 3096 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3097 | } |
| 3098 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3099 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3100 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3101 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3102 | const size_t max_size = (max_size_bytes < 0) |
| 3103 | ? RtcEventLog::kUnlimitedOutput |
| 3104 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 3105 | return StartRtcEventLog( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3106 | rtc::MakeUnique<RtcEventLogOutputFile>(file, max_size), |
| 3107 | webrtc::RtcEventLog::kImmediateOutput); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3108 | } |
| 3109 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3110 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3111 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3112 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3113 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3114 | bool operator()() { |
| 3115 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3116 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3117 | PeerConnection* const pc; |
| 3118 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3119 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3120 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3121 | return worker_thread()->Invoke<bool>( |
| 3122 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3126 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3127 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3128 | } |
| 3129 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3130 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3131 | return pending_local_description_ ? pending_local_description_.get() |
| 3132 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3136 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3137 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3140 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3141 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3142 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3146 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3147 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3151 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3152 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3156 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3157 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3158 | } |
| 3159 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3160 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3161 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3162 | // Update stats here so that we have the most recent stats for tracks and |
| 3163 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3164 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3165 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3166 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3167 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3168 | for (auto transceiver : transceivers_) { |
| 3169 | transceiver->Stop(); |
| 3170 | } |
| 3171 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3172 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3173 | // The event log is used in the transport controller, which must be outlived |
| 3174 | // by the former. CreateOffer by the peer connection is implemented |
| 3175 | // asynchronously and if the peer connection is closed without resetting the |
| 3176 | // WebRTC session description factory, the session description factory would |
| 3177 | // call the transport controller. |
| 3178 | webrtc_session_desc_factory_.reset(); |
| 3179 | transport_controller_.reset(); |
| 3180 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3181 | network_thread()->Invoke<void>( |
| 3182 | RTC_FROM_HERE, |
| 3183 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3184 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3185 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3186 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3187 | call_.reset(); |
| 3188 | // The event log must outlive call (and any other object that uses it). |
| 3189 | event_log_.reset(); |
| 3190 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 3193 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3194 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3195 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3196 | SetSessionDescriptionMsg* param = |
| 3197 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3198 | param->observer->OnSuccess(); |
| 3199 | delete param; |
| 3200 | break; |
| 3201 | } |
| 3202 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3203 | SetSessionDescriptionMsg* param = |
| 3204 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3205 | param->observer->OnFailure(std::move(param->error)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3206 | delete param; |
| 3207 | break; |
| 3208 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3209 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3210 | CreateSessionDescriptionMsg* param = |
| 3211 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3212 | param->observer->OnFailure(std::move(param->error)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3213 | delete param; |
| 3214 | break; |
| 3215 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3216 | case MSG_GETSTATS: { |
| 3217 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 3218 | StatsReports reports; |
| 3219 | stats_->GetStats(param->track, &reports); |
| 3220 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3221 | delete param; |
| 3222 | break; |
| 3223 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 3224 | case MSG_FREE_DATACHANNELS: { |
| 3225 | sctp_data_channels_to_free_.clear(); |
| 3226 | break; |
| 3227 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3228 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3229 | RTC_NOTREACHED() << "Not implemented"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3230 | break; |
| 3231 | } |
| 3232 | } |
| 3233 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3234 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3235 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3236 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3237 | GetAudioTransceiver()->internal()->channel()); |
| 3238 | if (voice_channel) { |
| 3239 | return voice_channel->media_channel(); |
| 3240 | } else { |
| 3241 | return nullptr; |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3246 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3247 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3248 | GetVideoTransceiver()->internal()->channel()); |
| 3249 | if (video_channel) { |
| 3250 | return video_channel->media_channel(); |
| 3251 | } else { |
| 3252 | return nullptr; |
| 3253 | } |
| 3254 | } |
| 3255 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3256 | void PeerConnection::CreateAudioReceiver( |
| 3257 | MediaStreamInterface* stream, |
| 3258 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3259 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3260 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3261 | auto* audio_receiver = new AudioRtpReceiver( |
| 3262 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3263 | audio_receiver->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3264 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3265 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3266 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3267 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3268 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3271 | void PeerConnection::CreateVideoReceiver( |
| 3272 | MediaStreamInterface* stream, |
| 3273 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3274 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3275 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3276 | auto* video_receiver = new VideoRtpReceiver( |
| 3277 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3278 | video_receiver->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3279 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3280 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3281 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3282 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3283 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3286 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3287 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3288 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3289 | const RtpSenderInfo& remote_sender_info) { |
| 3290 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3291 | if (!receiver) { |
| 3292 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3293 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3294 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3295 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3296 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3297 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3298 | } else { |
| 3299 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3300 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3301 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3304 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3305 | MediaStreamInterface* stream) { |
| 3306 | RTC_DCHECK(!IsClosed()); |
| 3307 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3308 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3309 | // We already have a sender for this track, so just change the stream_id |
| 3310 | // so that it's correct in the next call to CreateOffer. |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 3311 | sender->internal()->set_stream_id(stream->id()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3312 | return; |
| 3313 | } |
| 3314 | |
| 3315 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3316 | auto new_sender = |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 3317 | CreateSender(cricket::MEDIA_TYPE_AUDIO, track, {stream->id()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3318 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3319 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3320 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3321 | // which will connect the sender to the underlying transport. This can |
| 3322 | // occur if a local session description that contains the ID of the sender |
| 3323 | // is set before AddStream is called. It can also occur if the local |
| 3324 | // session description is not changed and RemoveStream is called, and |
| 3325 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3326 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3327 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3328 | if (sender_info) { |
| 3329 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3330 | } |
| 3331 | } |
| 3332 | |
| 3333 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3334 | // indefinitely, when we have unified plan SDP. |
| 3335 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3336 | MediaStreamInterface* stream) { |
| 3337 | RTC_DCHECK(!IsClosed()); |
| 3338 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3339 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3340 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3341 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3342 | return; |
| 3343 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3344 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3348 | MediaStreamInterface* stream) { |
| 3349 | RTC_DCHECK(!IsClosed()); |
| 3350 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3351 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3352 | // We already have a sender for this track, so just change the stream_id |
| 3353 | // so that it's correct in the next call to CreateOffer. |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 3354 | sender->internal()->set_stream_id(stream->id()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3355 | return; |
| 3356 | } |
| 3357 | |
| 3358 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3359 | auto new_sender = |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 3360 | CreateSender(cricket::MEDIA_TYPE_VIDEO, track, {stream->id()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3361 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3362 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3363 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3364 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3365 | if (sender_info) { |
| 3366 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3371 | MediaStreamInterface* stream) { |
| 3372 | RTC_DCHECK(!IsClosed()); |
| 3373 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3374 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3375 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3376 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3377 | return; |
| 3378 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3379 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3382 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3383 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3384 | if (ice_connection_state_ == new_state) { |
| 3385 | return; |
| 3386 | } |
| 3387 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3388 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3389 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3390 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3391 | return; |
| 3392 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3393 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3394 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3395 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3396 | RTC_DCHECK(ice_connection_state_ != |
| 3397 | PeerConnectionInterface::kIceConnectionClosed); |
| 3398 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3399 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3400 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
| 3403 | void PeerConnection::OnIceGatheringChange( |
| 3404 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3405 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3406 | if (IsClosed()) { |
| 3407 | return; |
| 3408 | } |
| 3409 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3410 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3413 | void PeerConnection::OnIceCandidate( |
| 3414 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3415 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3416 | if (IsClosed()) { |
| 3417 | return; |
| 3418 | } |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3419 | observer_->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3422 | void PeerConnection::OnIceCandidatesRemoved( |
| 3423 | const std::vector<cricket::Candidate>& candidates) { |
| 3424 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3425 | if (IsClosed()) { |
| 3426 | return; |
| 3427 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3428 | observer_->OnIceCandidatesRemoved(candidates); |
| 3429 | } |
| 3430 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3431 | void PeerConnection::ChangeSignalingState( |
| 3432 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3433 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3434 | if (signaling_state_ == signaling_state) { |
| 3435 | return; |
| 3436 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3437 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3438 | << GetSignalingStateString(signaling_state_) |
| 3439 | << " New state: " |
| 3440 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3441 | signaling_state_ = signaling_state; |
| 3442 | if (signaling_state == kClosed) { |
| 3443 | ice_connection_state_ = kIceConnectionClosed; |
| 3444 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 3445 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3446 | ice_gathering_state_ = kIceGatheringComplete; |
| 3447 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 3448 | } |
| 3449 | } |
| 3450 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3453 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3454 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3455 | if (IsClosed()) { |
| 3456 | return; |
| 3457 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3458 | AddAudioTrack(track, stream); |
| 3459 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3460 | } |
| 3461 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3462 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3463 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3464 | if (IsClosed()) { |
| 3465 | return; |
| 3466 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3467 | RemoveAudioTrack(track, stream); |
| 3468 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3472 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3473 | if (IsClosed()) { |
| 3474 | return; |
| 3475 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3476 | AddVideoTrack(track, stream); |
| 3477 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3481 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3482 | if (IsClosed()) { |
| 3483 | return; |
| 3484 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3485 | RemoveVideoTrack(track, stream); |
| 3486 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3487 | } |
| 3488 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3489 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3490 | SetSessionDescriptionObserver* observer) { |
| 3491 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3492 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3493 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 3494 | } |
| 3495 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3496 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3497 | SetSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3498 | RTCError&& error) { |
| 3499 | RTC_DCHECK(!error.ok()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3500 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3501 | msg->error = std::move(error); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3502 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3503 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3507 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3508 | RTCError error) { |
| 3509 | RTC_DCHECK(!error.ok()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3510 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3511 | msg->error = std::move(error); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3512 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3513 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3516 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3517 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3518 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3519 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3520 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3521 | if (IsUnifiedPlan()) { |
| 3522 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3523 | } else { |
| 3524 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3525 | } |
| 3526 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3527 | // Intentionally unset the data channel type for RTP data channel with the |
| 3528 | // second condition. Otherwise the RTP data channels would be successfully |
| 3529 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3530 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3531 | // people won't try to use them. |
| 3532 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3533 | session_options->data_channel_type = data_channel_type(); |
| 3534 | } |
| 3535 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3536 | // Apply ICE restart flag and renomination flag. |
| 3537 | for (auto& options : session_options->media_description_options) { |
| 3538 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3539 | options.transport_options.enable_ice_renomination = |
| 3540 | configuration_.enable_ice_renomination; |
| 3541 | } |
| 3542 | |
| 3543 | session_options->rtcp_cname = rtcp_cname_; |
| 3544 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3545 | session_options->is_unified_plan = IsUnifiedPlan(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3549 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3550 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3551 | // Figure out transceiver directional preferences. |
| 3552 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3553 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3554 | |
| 3555 | // By default, generate sendrecv/recvonly m= sections. |
| 3556 | bool recv_audio = true; |
| 3557 | bool recv_video = true; |
| 3558 | |
| 3559 | // By default, only offer a new m= section if we have media to send with it. |
| 3560 | bool offer_new_audio_description = send_audio; |
| 3561 | bool offer_new_video_description = send_video; |
| 3562 | bool offer_new_data_description = HasDataChannels(); |
| 3563 | |
| 3564 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3565 | if (offer_answer_options.offer_to_receive_audio != |
| 3566 | RTCOfferAnswerOptions::kUndefined) { |
| 3567 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3568 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3569 | offer_new_audio_description || |
| 3570 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3571 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3572 | if (offer_answer_options.offer_to_receive_video != |
| 3573 | RTCOfferAnswerOptions::kUndefined) { |
| 3574 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3575 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3576 | offer_new_video_description || |
| 3577 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | rtc::Optional<size_t> audio_index; |
| 3581 | rtc::Optional<size_t> video_index; |
| 3582 | rtc::Optional<size_t> data_index; |
| 3583 | // If a current description exists, generate m= sections in the same order, |
| 3584 | // using the first audio/video/data section that appears and rejecting |
| 3585 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3586 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3587 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3588 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3589 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3590 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3591 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3592 | } |
| 3593 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3594 | // Add audio/video/data m= sections to the end if needed. |
| 3595 | if (!audio_index && offer_new_audio_description) { |
| 3596 | session_options->media_description_options.push_back( |
| 3597 | cricket::MediaDescriptionOptions( |
| 3598 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3599 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3600 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3601 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3602 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3603 | if (!video_index && offer_new_video_description) { |
| 3604 | session_options->media_description_options.push_back( |
| 3605 | cricket::MediaDescriptionOptions( |
| 3606 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3607 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3608 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3609 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3610 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3611 | if (!data_index && offer_new_data_description) { |
| 3612 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3613 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3614 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3618 | !audio_index ? nullptr |
| 3619 | : &session_options->media_description_options[*audio_index]; |
| 3620 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3621 | !video_index ? nullptr |
| 3622 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3623 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3624 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3625 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3626 | } |
| 3627 | |
| 3628 | // Find a new MID that is not already in |used_mids|, then add it to |used_mids| |
| 3629 | // and return a reference to it. |
| 3630 | // Generated MIDs should be no more than 3 bytes long to take up less space in |
| 3631 | // the RTP packet. |
| 3632 | static const std::string& AllocateMid(std::set<std::string>* used_mids) { |
| 3633 | RTC_DCHECK(used_mids); |
| 3634 | // We're boring: just generate MIDs 0, 1, 2, ... |
| 3635 | size_t i = 0; |
| 3636 | std::set<std::string>::iterator it; |
| 3637 | bool inserted; |
| 3638 | do { |
| 3639 | std::string mid = rtc::ToString(i++); |
| 3640 | auto insert_result = used_mids->insert(mid); |
| 3641 | it = insert_result.first; |
| 3642 | inserted = insert_result.second; |
| 3643 | } while (!inserted); |
| 3644 | return *it; |
| 3645 | } |
| 3646 | |
| 3647 | static cricket::MediaDescriptionOptions |
| 3648 | GetMediaDescriptionOptionsForTransceiver( |
| 3649 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3650 | transceiver, |
| 3651 | const std::string& mid) { |
| 3652 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3653 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3654 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3655 | // This behavior is specified in JSEP. The gist is that: |
| 3656 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 3657 | // sendrecv. |
| 3658 | // 2. If the MSID is included, then it must be included in any subsequent |
| 3659 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 3660 | if (!transceiver->stopped() && |
| 3661 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 3662 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 3663 | cricket::SenderOptions sender_options; |
| 3664 | sender_options.track_id = transceiver->sender()->id(); |
| 3665 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 3666 | // TODO(bugs.webrtc.org/7600): Set num_sim_layers to the number of encodings |
| 3667 | // set in the RTP parameters when the transceiver was added. |
| 3668 | sender_options.num_sim_layers = 1; |
| 3669 | media_description_options.sender_options.push_back(sender_options); |
| 3670 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3671 | return media_description_options; |
| 3672 | } |
| 3673 | |
| 3674 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 3675 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3676 | cricket::MediaSessionOptions* session_options) { |
| 3677 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3678 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3679 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3680 | const ContentInfos& local_contents = |
| 3681 | (local_description() ? local_description()->description()->contents() |
| 3682 | : ContentInfos()); |
| 3683 | const ContentInfos& remote_contents = |
| 3684 | (remote_description() ? remote_description()->description()->contents() |
| 3685 | : ContentInfos()); |
| 3686 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3687 | // slots first. |
| 3688 | std::queue<size_t> recycleable_mline_indices; |
| 3689 | // Track the MIDs used in previous offer/answer exchanges and the current |
| 3690 | // offer so that new, unique MIDs are generated. |
| 3691 | std::set<std::string> used_mids = seen_mids_; |
| 3692 | // First, go through each media section that exists in either the local or |
| 3693 | // remote description and generate a media section in this offer for the |
| 3694 | // associated transceiver. If a media section can be recycled, generate a |
| 3695 | // default, rejected media section here that can be later overwritten. |
| 3696 | for (size_t i = 0; |
| 3697 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3698 | // Either |local_content| or |remote_content| is non-null. |
| 3699 | const ContentInfo* local_content = |
| 3700 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3701 | const ContentInfo* remote_content = |
| 3702 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3703 | bool had_been_rejected = (local_content && local_content->rejected) || |
| 3704 | (remote_content && remote_content->rejected); |
| 3705 | const std::string& mid = |
| 3706 | (local_content ? local_content->name : remote_content->name); |
| 3707 | cricket::MediaType media_type = |
| 3708 | (local_content ? local_content->media_description()->type() |
| 3709 | : remote_content->media_description()->type()); |
| 3710 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3711 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3712 | auto transceiver = GetAssociatedTransceiver(mid); |
| 3713 | RTC_CHECK(transceiver); |
| 3714 | // A media section is considered eligible for recycling if it is marked as |
| 3715 | // rejected in either the local or remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3716 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3717 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3718 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 3719 | RtpTransceiverDirection::kInactive, |
| 3720 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3721 | recycleable_mline_indices.push(i); |
| 3722 | } else { |
| 3723 | session_options->media_description_options.push_back( |
| 3724 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 3725 | // CreateOffer shouldn't really cause any state changes in |
| 3726 | // PeerConnection, but we need a way to match new transceivers to new |
| 3727 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 3728 | // by recording the index of the media section generated for the |
| 3729 | // transceiver in the offer. |
| 3730 | transceiver->internal()->set_mline_index(i); |
| 3731 | } |
| 3732 | } else { |
| 3733 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3734 | RTC_CHECK(GetDataMid()); |
| 3735 | if (had_been_rejected || mid != *GetDataMid()) { |
| 3736 | session_options->media_description_options.push_back( |
| 3737 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 3738 | } else { |
| 3739 | session_options->media_description_options.push_back( |
| 3740 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 3741 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3745 | // and not associated). Reuse media sections marked as recyclable first, |
| 3746 | // otherwise append to the end of the offer. New media sections should be |
| 3747 | // added in the order they were added to the PeerConnection. |
| 3748 | for (auto transceiver : transceivers_) { |
| 3749 | if (transceiver->mid() || transceiver->stopped()) { |
| 3750 | continue; |
| 3751 | } |
| 3752 | size_t mline_index; |
| 3753 | if (!recycleable_mline_indices.empty()) { |
| 3754 | mline_index = recycleable_mline_indices.front(); |
| 3755 | recycleable_mline_indices.pop(); |
| 3756 | session_options->media_description_options[mline_index] = |
| 3757 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3758 | AllocateMid(&used_mids)); |
| 3759 | } else { |
| 3760 | mline_index = session_options->media_description_options.size(); |
| 3761 | session_options->media_description_options.push_back( |
| 3762 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3763 | AllocateMid(&used_mids))); |
| 3764 | } |
| 3765 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3766 | transceiver->internal()->set_mline_index(mline_index); |
| 3767 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3768 | // Lastly, add a m-section if we have local data channels and an m section |
| 3769 | // does not already exist. |
| 3770 | if (!GetDataMid() && HasDataChannels()) { |
| 3771 | session_options->media_description_options.push_back( |
| 3772 | GetMediaDescriptionOptionsForActiveData(AllocateMid(&used_mids))); |
| 3773 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | void PeerConnection::GetOptionsForAnswer( |
| 3777 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3778 | cricket::MediaSessionOptions* session_options) { |
| 3779 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3780 | |
| 3781 | if (IsUnifiedPlan()) { |
| 3782 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3783 | } else { |
| 3784 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3785 | } |
| 3786 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3787 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3788 | // the RTP data channels would be successfully negotiated by default and the |
| 3789 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3790 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 3791 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3792 | session_options->data_channel_type = data_channel_type(); |
| 3793 | } |
| 3794 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3795 | // Apply ICE renomination flag. |
| 3796 | for (auto& options : session_options->media_description_options) { |
| 3797 | options.transport_options.enable_ice_renomination = |
| 3798 | configuration_.enable_ice_renomination; |
| 3799 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 3800 | |
| 3801 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 3802 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3803 | session_options->is_unified_plan = IsUnifiedPlan(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3804 | } |
| 3805 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3806 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 3807 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3808 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3809 | // Figure out transceiver directional preferences. |
| 3810 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3811 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3812 | |
| 3813 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 3814 | // restricted by the direction in the offer. |
| 3815 | bool recv_audio = true; |
| 3816 | bool recv_video = true; |
| 3817 | |
| 3818 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3819 | if (offer_answer_options.offer_to_receive_audio != |
| 3820 | RTCOfferAnswerOptions::kUndefined) { |
| 3821 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 3822 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3823 | if (offer_answer_options.offer_to_receive_video != |
| 3824 | RTCOfferAnswerOptions::kUndefined) { |
| 3825 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | rtc::Optional<size_t> audio_index; |
| 3829 | rtc::Optional<size_t> video_index; |
| 3830 | rtc::Optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 3831 | |
| 3832 | // Generate m= sections that match those in the offer. |
| 3833 | // Note that mediasession.cc will handle intersection our preferred |
| 3834 | // direction with the offered direction. |
| 3835 | GenerateMediaDescriptionOptions( |
| 3836 | remote_description(), |
| 3837 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3838 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 3839 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3840 | |
| 3841 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3842 | !audio_index ? nullptr |
| 3843 | : &session_options->media_description_options[*audio_index]; |
| 3844 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3845 | !video_index ? nullptr |
| 3846 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3847 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3848 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3849 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3850 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 3851 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3852 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 3853 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3854 | cricket::MediaSessionOptions* session_options) { |
| 3855 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 3856 | // Answers) and 5.3.2 (Subsequent Answers). |
| 3857 | RTC_DCHECK(remote_description()); |
| 3858 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3859 | for (const ContentInfo& content : |
| 3860 | remote_description()->description()->contents()) { |
| 3861 | cricket::MediaType media_type = content.media_description()->type(); |
| 3862 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3863 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3864 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3865 | RTC_CHECK(transceiver); |
| 3866 | session_options->media_description_options.push_back( |
| 3867 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 3868 | } else { |
| 3869 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3870 | // Reject all data sections if data channels are disabled. |
| 3871 | // Reject a data section if it has already been rejected. |
| 3872 | // Reject all data sections except for the first one. |
| 3873 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 3874 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3875 | session_options->media_description_options.push_back( |
| 3876 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 3877 | } else { |
| 3878 | session_options->media_description_options.push_back( |
| 3879 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 3880 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3881 | } |
| 3882 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3883 | } |
| 3884 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3885 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 3886 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3887 | RtpTransceiverDirection audio_direction, |
| 3888 | RtpTransceiverDirection video_direction, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3889 | rtc::Optional<size_t>* audio_index, |
| 3890 | rtc::Optional<size_t>* video_index, |
| 3891 | rtc::Optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3892 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3893 | for (const cricket::ContentInfo& content : |
| 3894 | session_desc->description()->contents()) { |
| 3895 | if (IsAudioContent(&content)) { |
| 3896 | // If we already have an audio m= section, reject this extra one. |
| 3897 | if (*audio_index) { |
| 3898 | session_options->media_description_options.push_back( |
| 3899 | cricket::MediaDescriptionOptions( |
| 3900 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3901 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3902 | } else { |
| 3903 | session_options->media_description_options.push_back( |
| 3904 | cricket::MediaDescriptionOptions( |
| 3905 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3906 | audio_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3907 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3908 | } |
| 3909 | } else if (IsVideoContent(&content)) { |
| 3910 | // If we already have an video m= section, reject this extra one. |
| 3911 | if (*video_index) { |
| 3912 | session_options->media_description_options.push_back( |
| 3913 | cricket::MediaDescriptionOptions( |
| 3914 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3915 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3916 | } else { |
| 3917 | session_options->media_description_options.push_back( |
| 3918 | cricket::MediaDescriptionOptions( |
| 3919 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3920 | video_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3921 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3922 | } |
| 3923 | } else { |
| 3924 | RTC_DCHECK(IsDataContent(&content)); |
| 3925 | // If we already have an data m= section, reject this extra one. |
| 3926 | if (*data_index) { |
| 3927 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3928 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3929 | } else { |
| 3930 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3931 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3932 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3933 | } |
| 3934 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3935 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3936 | } |
| 3937 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3938 | cricket::MediaDescriptionOptions |
| 3939 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 3940 | const std::string& mid) const { |
| 3941 | // Direction for data sections is meaningless, but legacy endpoints might |
| 3942 | // expect sendrecv. |
| 3943 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3944 | RtpTransceiverDirection::kSendRecv, |
| 3945 | /*stopped=*/false); |
| 3946 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3947 | return options; |
| 3948 | } |
| 3949 | |
| 3950 | cricket::MediaDescriptionOptions |
| 3951 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 3952 | const std::string& mid) const { |
| 3953 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3954 | RtpTransceiverDirection::kInactive, |
| 3955 | /*stopped=*/true); |
| 3956 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3957 | return options; |
| 3958 | } |
| 3959 | |
| 3960 | rtc::Optional<std::string> PeerConnection::GetDataMid() const { |
| 3961 | switch (data_channel_type_) { |
| 3962 | case cricket::DCT_RTP: |
| 3963 | if (!rtp_data_channel_) { |
| 3964 | return rtc::nullopt; |
| 3965 | } |
| 3966 | return rtp_data_channel_->content_name(); |
| 3967 | case cricket::DCT_SCTP: |
| 3968 | return sctp_content_name_; |
| 3969 | default: |
| 3970 | return rtc::nullopt; |
| 3971 | } |
| 3972 | } |
| 3973 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3974 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 3975 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 3976 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3977 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 3978 | } |
| 3979 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3980 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3981 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3982 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3983 | cricket::MediaType media_type, |
| 3984 | StreamCollection* new_streams) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3985 | std::vector<RtpSenderInfo>* current_senders = |
| 3986 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3987 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3988 | // Find removed senders. I.e., senders where the sender id or ssrc don't match |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3989 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3990 | for (auto sender_it = current_senders->begin(); |
| 3991 | sender_it != current_senders->end(); |
| 3992 | /* incremented manually */) { |
| 3993 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3994 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3995 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 3996 | bool sender_exists = params && params->id == info.sender_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3997 | // If this is a default track, and we still need it, don't remove it. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3998 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3999 | sender_exists) { |
| 4000 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4001 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4002 | OnRemoteSenderRemoved(info, media_type); |
| 4003 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4004 | } |
| 4005 | } |
| 4006 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4007 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4008 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4009 | // |params.id| is the sender id and the stream id uses the first of |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 4010 | // |params.stream_ids|. |
| 4011 | // TODO(bugs.webrtc.org/7932): Add support for multiple stream ids. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4012 | const std::string& stream_id = |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 4013 | (!params.stream_ids().empty() ? params.stream_ids()[0] : ""); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4014 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4015 | uint32_t ssrc = params.first_ssrc(); |
| 4016 | |
| 4017 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4018 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4019 | if (!stream) { |
| 4020 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4021 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4022 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4023 | remote_streams_->AddStream(stream); |
| 4024 | new_streams->AddStream(stream); |
| 4025 | } |
| 4026 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4027 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4028 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4029 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4030 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4031 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4032 | } |
| 4033 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4034 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4035 | // Add default sender if necessary. |
| 4036 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4037 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4038 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4039 | if (!default_stream) { |
| 4040 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4041 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4042 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4043 | remote_streams_->AddStream(default_stream); |
| 4044 | new_streams->AddStream(default_stream); |
| 4045 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4046 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4047 | ? kDefaultAudioSenderId |
| 4048 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4049 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4050 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4051 | if (!default_sender_info) { |
| 4052 | current_senders->push_back( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4053 | RtpSenderInfo(kDefaultStreamId, default_sender_id, 0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4054 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4055 | } |
| 4056 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4057 | } |
| 4058 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4059 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 4060 | cricket::MediaType media_type) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4061 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4062 | |
| 4063 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4064 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4065 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4066 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4067 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 4068 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4069 | } |
| 4070 | } |
| 4071 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4072 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 4073 | cricket::MediaType media_type) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4074 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4075 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4076 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4077 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4078 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 4079 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4080 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4081 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4082 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4083 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4084 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4085 | } |
| 4086 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4087 | // Stopping or destroying a VideoRtpReceiver will end the |
| 4088 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4089 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4090 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4091 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4092 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4093 | // There's no guarantee the track is still available, e.g. the track may |
| 4094 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4095 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4096 | } |
| 4097 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 4098 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4099 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4100 | if (receiver) { |
| 4101 | observer_->OnRemoveTrack(receiver); |
| 4102 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4106 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4107 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4108 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4109 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4110 | streams_to_remove.push_back(stream); |
| 4111 | } |
| 4112 | } |
| 4113 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4114 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4115 | remote_streams_->RemoveStream(stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4116 | observer_->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4117 | } |
| 4118 | } |
| 4119 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4120 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4121 | const std::vector<cricket::StreamParams>& streams, |
| 4122 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4123 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4124 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4125 | // Find removed tracks. I.e., tracks where the track id, stream id or ssrc |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4126 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4127 | for (auto sender_it = current_senders->begin(); |
| 4128 | sender_it != current_senders->end(); |
| 4129 | /* incremented manually */) { |
| 4130 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4131 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4132 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4133 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4134 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4135 | OnLocalSenderRemoved(info, media_type); |
| 4136 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4137 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4138 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4139 | } |
| 4140 | } |
| 4141 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4142 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4143 | for (const cricket::StreamParams& params : streams) { |
| 4144 | // The sync_label is the MediaStream label and the |stream.id| is the |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4145 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4146 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4147 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4148 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4149 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4150 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4151 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4152 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4153 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4154 | } |
| 4155 | } |
| 4156 | } |
| 4157 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4158 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4159 | cricket::MediaType media_type) { |
| 4160 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4161 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4162 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4163 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4164 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4165 | return; |
| 4166 | } |
| 4167 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4168 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4169 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4170 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4171 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4172 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4173 | |
Tomas Gunnarsson | 191bf5c | 2018-03-30 10:44:43 +0000 | [diff] [blame^] | 4174 | sender->internal()->set_stream_id(sender_info.stream_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4175 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4176 | } |
| 4177 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4178 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4179 | cricket::MediaType media_type) { |
| 4180 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4181 | if (!sender) { |
| 4182 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4183 | // SessionDescriptions has been renegotiated. |
| 4184 | return; |
| 4185 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4186 | |
| 4187 | // A sender has been removed from the SessionDescription but it's still |
| 4188 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4189 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4190 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4191 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4192 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4193 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4194 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4195 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4196 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4200 | const cricket::StreamParamsVec& streams) { |
| 4201 | std::vector<std::string> existing_channels; |
| 4202 | |
| 4203 | // Find new and active data channels. |
| 4204 | for (const cricket::StreamParams& params : streams) { |
| 4205 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4206 | // we use the same naming of data channels as we do for |
| 4207 | // MediaStreams and Tracks. |
| 4208 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4209 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4210 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4211 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4212 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4213 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4214 | continue; |
| 4215 | } |
| 4216 | // Set the SSRC the data channel should use for sending. |
| 4217 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4218 | existing_channels.push_back(data_channel_it->first); |
| 4219 | } |
| 4220 | |
| 4221 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4222 | } |
| 4223 | |
| 4224 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4225 | const cricket::StreamParamsVec& streams) { |
| 4226 | std::vector<std::string> existing_channels; |
| 4227 | |
| 4228 | // Find new and active data channels. |
| 4229 | for (const cricket::StreamParams& params : streams) { |
| 4230 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4231 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4232 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4233 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4234 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4235 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4236 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4237 | // This is a new data channel. |
| 4238 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4239 | } else { |
| 4240 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4241 | } |
| 4242 | existing_channels.push_back(label); |
| 4243 | } |
| 4244 | |
| 4245 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4246 | } |
| 4247 | |
| 4248 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4249 | const std::vector<std::string>& active_channels, |
| 4250 | bool is_local_update) { |
| 4251 | auto it = rtp_data_channels_.begin(); |
| 4252 | while (it != rtp_data_channels_.end()) { |
| 4253 | DataChannel* data_channel = it->second; |
| 4254 | if (std::find(active_channels.begin(), active_channels.end(), |
| 4255 | data_channel->label()) != active_channels.end()) { |
| 4256 | ++it; |
| 4257 | continue; |
| 4258 | } |
| 4259 | |
| 4260 | if (is_local_update) { |
| 4261 | data_channel->SetSendSsrc(0); |
| 4262 | } else { |
| 4263 | data_channel->RemotePeerRequestClose(); |
| 4264 | } |
| 4265 | |
| 4266 | if (data_channel->state() == DataChannel::kClosed) { |
| 4267 | rtp_data_channels_.erase(it); |
| 4268 | it = rtp_data_channels_.begin(); |
| 4269 | } else { |
| 4270 | ++it; |
| 4271 | } |
| 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4276 | uint32_t remote_ssrc) { |
| 4277 | rtc::scoped_refptr<DataChannel> channel( |
| 4278 | InternalCreateDataChannel(label, nullptr)); |
| 4279 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4280 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4281 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4282 | return; |
| 4283 | } |
| 4284 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4285 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4286 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4287 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4291 | const std::string& label, |
| 4292 | const InternalDataChannelInit* config) { |
| 4293 | if (IsClosed()) { |
| 4294 | return nullptr; |
| 4295 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4296 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4297 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4298 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4299 | return nullptr; |
| 4300 | } |
| 4301 | InternalDataChannelInit new_config = |
| 4302 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4303 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4304 | if (new_config.id < 0) { |
| 4305 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4306 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4307 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4308 | RTC_LOG(LS_ERROR) |
| 4309 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4310 | return nullptr; |
| 4311 | } |
| 4312 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4313 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4314 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4315 | return nullptr; |
| 4316 | } |
| 4317 | } |
| 4318 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4319 | rtc::scoped_refptr<DataChannel> channel( |
| 4320 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4321 | if (!channel) { |
| 4322 | sid_allocator_.ReleaseSid(new_config.id); |
| 4323 | return nullptr; |
| 4324 | } |
| 4325 | |
| 4326 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4327 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4328 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4329 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4330 | return nullptr; |
| 4331 | } |
| 4332 | rtp_data_channels_[channel->label()] = channel; |
| 4333 | } else { |
| 4334 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 4335 | sctp_data_channels_.push_back(channel); |
| 4336 | channel->SignalClosed.connect(this, |
| 4337 | &PeerConnection::OnSctpDataChannelClosed); |
| 4338 | } |
| 4339 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4340 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4341 | return channel; |
| 4342 | } |
| 4343 | |
| 4344 | bool PeerConnection::HasDataChannels() const { |
| 4345 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4346 | } |
| 4347 | |
| 4348 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4349 | for (const auto& channel : sctp_data_channels_) { |
| 4350 | if (channel->id() < 0) { |
| 4351 | int sid; |
| 4352 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4353 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4354 | continue; |
| 4355 | } |
| 4356 | channel->SetSctpSid(sid); |
| 4357 | } |
| 4358 | } |
| 4359 | } |
| 4360 | |
| 4361 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4362 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4363 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4364 | ++it) { |
| 4365 | if (it->get() == channel) { |
| 4366 | if (channel->id() >= 0) { |
| 4367 | sid_allocator_.ReleaseSid(channel->id()); |
| 4368 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4369 | // Since this method is triggered by a signal from the DataChannel, |
| 4370 | // we can't free it directly here; we need to free it asynchronously. |
| 4371 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4372 | sctp_data_channels_.erase(it); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 4373 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4374 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4375 | return; |
| 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4380 | void PeerConnection::OnDataChannelDestroyed() { |
| 4381 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4382 | // DataChannel may callback to us and try to modify the list. |
| 4383 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4384 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4385 | for (const auto& kv : temp_rtp_dcs) { |
| 4386 | kv.second->OnTransportChannelDestroyed(); |
| 4387 | } |
| 4388 | |
| 4389 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4390 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4391 | for (const auto& channel : temp_sctp_dcs) { |
| 4392 | channel->OnTransportChannelDestroyed(); |
| 4393 | } |
| 4394 | } |
| 4395 | |
| 4396 | void PeerConnection::OnDataChannelOpenMessage( |
| 4397 | const std::string& label, |
| 4398 | const InternalDataChannelInit& config) { |
| 4399 | rtc::scoped_refptr<DataChannel> channel( |
| 4400 | InternalCreateDataChannel(label, &config)); |
| 4401 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4402 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4403 | return; |
| 4404 | } |
| 4405 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4406 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4407 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4408 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4409 | } |
| 4410 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4411 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4412 | PeerConnection::GetAudioTransceiver() const { |
| 4413 | // This method only works with Plan B SDP, where there is a single |
| 4414 | // audio/video transceiver. |
| 4415 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4416 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4417 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4418 | return transceiver; |
| 4419 | } |
| 4420 | } |
| 4421 | RTC_NOTREACHED(); |
| 4422 | return nullptr; |
| 4423 | } |
| 4424 | |
| 4425 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4426 | PeerConnection::GetVideoTransceiver() const { |
| 4427 | // This method only works with Plan B SDP, where there is a single |
| 4428 | // audio/video transceiver. |
| 4429 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4430 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4431 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4432 | return transceiver; |
| 4433 | } |
| 4434 | } |
| 4435 | RTC_NOTREACHED(); |
| 4436 | return nullptr; |
| 4437 | } |
| 4438 | |
| 4439 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4440 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4441 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4442 | switch (type) { |
| 4443 | case cricket::MEDIA_TYPE_AUDIO: |
| 4444 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4445 | case cricket::MEDIA_TYPE_VIDEO: |
| 4446 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4447 | case cricket::MEDIA_TYPE_DATA: |
| 4448 | return false; |
| 4449 | } |
| 4450 | RTC_NOTREACHED(); |
| 4451 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4452 | } |
| 4453 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4454 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4455 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 4456 | for (auto transceiver : transceivers_) { |
| 4457 | for (auto sender : transceiver->internal()->senders()) { |
| 4458 | if (sender->track() == track) { |
| 4459 | return sender; |
| 4460 | } |
| 4461 | } |
| 4462 | } |
| 4463 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4464 | } |
| 4465 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4466 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4467 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 4468 | for (auto transceiver : transceivers_) { |
| 4469 | for (auto sender : transceiver->internal()->senders()) { |
| 4470 | if (sender->id() == sender_id) { |
| 4471 | return sender; |
| 4472 | } |
| 4473 | } |
| 4474 | } |
| 4475 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4476 | } |
| 4477 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4478 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4479 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 4480 | for (auto transceiver : transceivers_) { |
| 4481 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4482 | if (receiver->id() == receiver_id) { |
| 4483 | return receiver; |
| 4484 | } |
| 4485 | } |
| 4486 | } |
| 4487 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4488 | } |
| 4489 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4490 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4491 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4492 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4493 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4494 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4495 | ? &remote_audio_sender_infos_ |
| 4496 | : &remote_video_sender_infos_; |
| 4497 | } |
| 4498 | |
| 4499 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4500 | cricket::MediaType media_type) { |
| 4501 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4502 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4503 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4504 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4505 | } |
| 4506 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4507 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4508 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4509 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4510 | const std::string sender_id) const { |
| 4511 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4512 | if (sender_info.stream_id == stream_id && |
| 4513 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4514 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4515 | } |
| 4516 | } |
| 4517 | return nullptr; |
| 4518 | } |
| 4519 | |
| 4520 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4521 | for (const auto& channel : sctp_data_channels_) { |
| 4522 | if (channel->id() == sid) { |
| 4523 | return channel; |
| 4524 | } |
| 4525 | } |
| 4526 | return nullptr; |
| 4527 | } |
| 4528 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4529 | bool PeerConnection::InitializePortAllocator_n( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4530 | const RTCConfiguration& configuration) { |
| 4531 | cricket::ServerAddresses stun_servers; |
| 4532 | std::vector<cricket::RelayServerConfig> turn_servers; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4533 | if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) != |
| 4534 | RTCErrorType::NONE) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4535 | return false; |
| 4536 | } |
| 4537 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 4538 | port_allocator_->Initialize(); |
| 4539 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4540 | // To handle both internal and externally created port allocator, we will |
| 4541 | // enable BUNDLE here. |
| 4542 | int portallocator_flags = port_allocator_->flags(); |
| 4543 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4544 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 4545 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4546 | // If the disable-IPv6 flag was specified, we'll not override it |
| 4547 | // by experiment. |
| 4548 | if (configuration.disable_ipv6) { |
| 4549 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 4550 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 4551 | .find("Disabled") == 0) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4552 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
| 4553 | } |
| 4554 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4555 | if (configuration.disable_ipv6_on_wifi) { |
| 4556 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4557 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4558 | } |
| 4559 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4560 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 4561 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4562 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4563 | } |
| 4564 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4565 | if (configuration.candidate_network_policy == |
| 4566 | kCandidateNetworkPolicyLowCost) { |
| 4567 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4568 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4569 | } |
| 4570 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4571 | if (configuration.disable_link_local_networks) { |
| 4572 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
| 4573 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 4574 | } |
| 4575 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4576 | port_allocator_->set_flags(portallocator_flags); |
| 4577 | // No step delay is used while allocating ports. |
| 4578 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 4579 | port_allocator_->set_candidate_filter( |
| 4580 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 4581 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4582 | |
| 4583 | // Call this last since it may create pooled allocator sessions using the |
| 4584 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4585 | port_allocator_->SetConfiguration( |
| 4586 | stun_servers, turn_servers, configuration.ice_candidate_pool_size, |
| 4587 | configuration.prune_turn_ports, configuration.turn_customizer, |
| 4588 | configuration.stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4589 | return true; |
| 4590 | } |
| 4591 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4592 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4593 | const cricket::ServerAddresses& stun_servers, |
| 4594 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 4595 | IceTransportsType type, |
| 4596 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4597 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4598 | webrtc::TurnCustomizer* turn_customizer, |
| 4599 | rtc::Optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4600 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4601 | ConvertIceTransportTypeToCandidateFilter(type)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4602 | // Call this last since it may create pooled allocator sessions using the |
| 4603 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 4604 | return port_allocator_->SetConfiguration( |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4605 | stun_servers, turn_servers, candidate_pool_size, prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4606 | turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4607 | } |
| 4608 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4609 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 4610 | return factory_->channel_manager(); |
| 4611 | } |
| 4612 | |
| 4613 | MetricsObserverInterface* PeerConnection::metrics_observer() const { |
| 4614 | return uma_observer_; |
| 4615 | } |
| 4616 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 4617 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4618 | std::unique_ptr<RtcEventLogOutput> output, |
| 4619 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4620 | if (!event_log_) { |
| 4621 | return false; |
| 4622 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4623 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4627 | if (event_log_) { |
| 4628 | event_log_->StopLogging(); |
| 4629 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4630 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4631 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4632 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 4633 | const std::string& content_name) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4634 | for (auto transceiver : transceivers_) { |
| 4635 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4636 | if (channel && channel->content_name() == content_name) { |
| 4637 | return channel; |
| 4638 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4639 | } |
| 4640 | if (rtp_data_channel() && |
| 4641 | rtp_data_channel()->content_name() == content_name) { |
| 4642 | return rtp_data_channel(); |
| 4643 | } |
| 4644 | return nullptr; |
| 4645 | } |
| 4646 | |
| 4647 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 4648 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4649 | RTC_LOG(LS_INFO) |
| 4650 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4651 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4652 | return false; |
| 4653 | } |
| 4654 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4655 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4656 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4657 | return false; |
| 4658 | } |
| 4659 | |
| 4660 | return transport_controller_->GetSslRole(*sctp_transport_name_, role); |
| 4661 | } |
| 4662 | |
| 4663 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 4664 | rtc::SSLRole* role) { |
| 4665 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4666 | RTC_LOG(LS_INFO) |
| 4667 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4668 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4669 | return false; |
| 4670 | } |
| 4671 | |
| 4672 | return transport_controller_->GetSslRole(GetTransportName(content_name), |
| 4673 | role); |
| 4674 | } |
| 4675 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4676 | void PeerConnection::SetSessionError(SessionError error, |
| 4677 | const std::string& error_desc) { |
| 4678 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4679 | if (error != session_error_) { |
| 4680 | session_error_ = error; |
| 4681 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4682 | } |
| 4683 | } |
| 4684 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4685 | RTCError PeerConnection::UpdateSessionState(SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4686 | cricket::ContentSource source) { |
| 4687 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4688 | |
| 4689 | // If there's already a pending error then no state transition should happen. |
| 4690 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4691 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4692 | |
| 4693 | // If this is an answer then we know whether to BUNDLE or not. If both the |
| 4694 | // local and remote side have agreed to BUNDLE, go ahead and enable it. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4695 | if (type == SdpType::kAnswer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4696 | const cricket::ContentGroup* local_bundle = |
| 4697 | local_description()->description()->GetGroupByName( |
| 4698 | cricket::GROUP_TYPE_BUNDLE); |
| 4699 | const cricket::ContentGroup* remote_bundle = |
| 4700 | remote_description()->description()->GetGroupByName( |
| 4701 | cricket::GROUP_TYPE_BUNDLE); |
| 4702 | if (local_bundle && remote_bundle) { |
| 4703 | // The answerer decides the transport to bundle on. |
| 4704 | const cricket::ContentGroup* answer_bundle = |
| 4705 | (source == cricket::CS_LOCAL ? local_bundle : remote_bundle); |
| 4706 | if (!EnableBundle(*answer_bundle)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4707 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4708 | kEnableBundleFailed); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4709 | } |
| 4710 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4711 | } |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4712 | |
| 4713 | // Only push down the transport description after potentially enabling BUNDLE; |
| 4714 | // we don't want to push down a description on a transport about to be |
| 4715 | // destroyed. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4716 | RTCError error = PushdownTransportDescription(source, type); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4717 | if (!error.ok()) { |
| 4718 | return error; |
| 4719 | } |
| 4720 | |
| 4721 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4722 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4723 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | // Update the signaling state according to the specified state machine (see |
| 4727 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4728 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4729 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4730 | ? PeerConnectionInterface::kHaveLocalOffer |
| 4731 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4732 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4733 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4734 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 4735 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4736 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4737 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4738 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 4739 | } |
| 4740 | |
| 4741 | // Update internal objects according to the session description's media |
| 4742 | // descriptions. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4743 | error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4744 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 4745 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4746 | } |
| 4747 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4748 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4749 | } |
| 4750 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4751 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4752 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4753 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4754 | const SessionDescriptionInterface* sdesc = |
| 4755 | (source == cricket::CS_LOCAL ? local_description() |
| 4756 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4757 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4758 | |
| 4759 | // Push down the new SDP media section for each audio/video transceiver. |
| 4760 | for (auto transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4761 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4762 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4763 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4764 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4765 | continue; |
| 4766 | } |
| 4767 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4768 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4769 | if (!content_desc) { |
| 4770 | continue; |
| 4771 | } |
| 4772 | std::string error; |
| 4773 | bool success = |
| 4774 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4775 | ? channel->SetLocalContent(content_desc, type, &error) |
| 4776 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4777 | if (!success) { |
| 4778 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, std::move(error)); |
| 4779 | } |
| 4780 | } |
| 4781 | |
| 4782 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 4783 | if (rtp_data_channel_) { |
| 4784 | const ContentInfo* data_content = |
| 4785 | cricket::GetFirstDataContent(sdesc->description()); |
| 4786 | if (data_content && !data_content->rejected) { |
| 4787 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4788 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4789 | if (data_desc) { |
| 4790 | std::string error; |
| 4791 | bool success = |
| 4792 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4793 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
| 4794 | : rtp_data_channel_->SetRemoteContent(data_desc, type, |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4795 | &error); |
| 4796 | if (!success) { |
| 4797 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4798 | std::move(error)); |
| 4799 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4800 | } |
| 4801 | } |
| 4802 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4803 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4804 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 4805 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 4806 | if (sctp_transport_ && local_description() && remote_description() && |
| 4807 | cricket::GetFirstDataContent(local_description()->description()) && |
| 4808 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4809 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4810 | RTC_FROM_HERE, |
| 4811 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4812 | if (!success) { |
| 4813 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4814 | "Failed to push down SCTP parameters."); |
| 4815 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4816 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4817 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4818 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 4822 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4823 | RTC_DCHECK(local_description()); |
| 4824 | RTC_DCHECK(remote_description()); |
| 4825 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 4826 | // When we support "max-message-size", that would also be pushed down here. |
| 4827 | return sctp_transport_->Start( |
| 4828 | GetSctpPort(local_description()->description()), |
| 4829 | GetSctpPort(remote_description()->description())); |
| 4830 | } |
| 4831 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4832 | RTCError PeerConnection::PushdownTransportDescription( |
| 4833 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4834 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4835 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4836 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4837 | const SessionDescriptionInterface* sdesc = |
| 4838 | (source == cricket::CS_LOCAL ? local_description() |
| 4839 | : remote_description()); |
| 4840 | RTC_DCHECK(sdesc); |
| 4841 | for (const cricket::TransportInfo& tinfo : |
| 4842 | sdesc->description()->transport_infos()) { |
| 4843 | std::string error; |
| 4844 | bool success; |
| 4845 | if (source == cricket::CS_LOCAL) { |
| 4846 | success = transport_controller_->SetLocalTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4847 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4848 | } else { |
| 4849 | success = transport_controller_->SetRemoteTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4850 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4851 | } |
| 4852 | if (!success) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4853 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4854 | "Failed to push down transport description for " + |
| 4855 | tinfo.content_name + ": " + error); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4856 | } |
| 4857 | } |
| 4858 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4859 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | bool PeerConnection::GetTransportDescription( |
| 4863 | const SessionDescription* description, |
| 4864 | const std::string& content_name, |
| 4865 | cricket::TransportDescription* tdesc) { |
| 4866 | if (!description || !tdesc) { |
| 4867 | return false; |
| 4868 | } |
| 4869 | const TransportInfo* transport_info = |
| 4870 | description->GetTransportInfoByName(content_name); |
| 4871 | if (!transport_info) { |
| 4872 | return false; |
| 4873 | } |
| 4874 | *tdesc = transport_info->description; |
| 4875 | return true; |
| 4876 | } |
| 4877 | |
| 4878 | bool PeerConnection::EnableBundle(const cricket::ContentGroup& bundle) { |
| 4879 | const std::string* first_content_name = bundle.FirstContentName(); |
| 4880 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4881 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4882 | return false; |
| 4883 | } |
| 4884 | const std::string& transport_name = *first_content_name; |
| 4885 | |
| 4886 | auto maybe_set_transport = [this, bundle, |
| 4887 | transport_name](cricket::BaseChannel* ch) { |
| 4888 | if (!ch || !bundle.HasContentName(ch->content_name())) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4889 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | std::string old_transport_name = ch->transport_name(); |
| 4893 | if (old_transport_name == transport_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4894 | RTC_LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name() |
| 4895 | << " on " << transport_name << "."; |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4896 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 4900 | transport_controller_->CreateDtlsTransport( |
| 4901 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4902 | bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr); |
| 4903 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 4904 | if (need_rtcp) { |
| 4905 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 4906 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4907 | } |
| 4908 | |
| 4909 | ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4910 | RTC_LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on " |
| 4911 | << transport_name << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4912 | transport_controller_->DestroyDtlsTransport( |
| 4913 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4914 | // If the channel needs rtcp, it means that the channel used to have a |
| 4915 | // rtcp transport which needs to be deleted now. |
| 4916 | if (need_rtcp) { |
| 4917 | transport_controller_->DestroyDtlsTransport( |
| 4918 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4919 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4920 | }; |
| 4921 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4922 | for (auto transceiver : transceivers_) { |
| 4923 | maybe_set_transport(transceiver->internal()->channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4924 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4925 | maybe_set_transport(rtp_data_channel_); |
| 4926 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4927 | // For SCTP, transport creation/deletion happens here instead of in the |
| 4928 | // object itself. |
| 4929 | if (sctp_transport_) { |
| 4930 | RTC_DCHECK(sctp_transport_name_); |
| 4931 | RTC_DCHECK(sctp_content_name_); |
| 4932 | if (transport_name != *sctp_transport_name_ && |
| 4933 | bundle.HasContentName(*sctp_content_name_)) { |
| 4934 | network_thread()->Invoke<void>( |
| 4935 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::ChangeSctpTransport_n, this, |
| 4936 | transport_name)); |
| 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | return true; |
| 4941 | } |
| 4942 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4943 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 4944 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 4945 | cricket::ContinualGatheringPolicy gathering_policy; |
| 4946 | // TODO(honghaiz): Add the third continual gathering policy in |
| 4947 | // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER. |
| 4948 | switch (config.continual_gathering_policy) { |
| 4949 | case PeerConnectionInterface::GATHER_ONCE: |
| 4950 | gathering_policy = cricket::GATHER_ONCE; |
| 4951 | break; |
| 4952 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 4953 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 4954 | break; |
| 4955 | default: |
| 4956 | RTC_NOTREACHED(); |
| 4957 | gathering_policy = cricket::GATHER_ONCE; |
| 4958 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4959 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4960 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 4961 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 4962 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4963 | ice_config.prioritize_most_likely_candidate_pairs = |
| 4964 | config.prioritize_most_likely_ice_candidate_pairs; |
| 4965 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 4966 | RTCConfigurationToIceConfigOptionalInt( |
| 4967 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4968 | ice_config.continual_gathering_policy = gathering_policy; |
| 4969 | ice_config.presume_writable_when_fully_relayed = |
| 4970 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 4971 | ice_config.ice_check_interval_strong_connectivity = |
| 4972 | config.ice_check_interval_strong_connectivity; |
| 4973 | ice_config.ice_check_interval_weak_connectivity = |
| 4974 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4975 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4976 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4977 | ice_config.regather_all_networks_interval_range = |
| 4978 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4979 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4980 | return ice_config; |
| 4981 | } |
| 4982 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4983 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 4984 | std::string* track_id) { |
| 4985 | if (!local_description()) { |
| 4986 | return false; |
| 4987 | } |
| 4988 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 4989 | track_id); |
| 4990 | } |
| 4991 | |
| 4992 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 4993 | std::string* track_id) { |
| 4994 | if (!remote_description()) { |
| 4995 | return false; |
| 4996 | } |
| 4997 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 4998 | track_id); |
| 4999 | } |
| 5000 | |
| 5001 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 5002 | const rtc::CopyOnWriteBuffer& payload, |
| 5003 | cricket::SendDataResult* result) { |
| 5004 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5005 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5006 | "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5007 | return false; |
| 5008 | } |
| 5009 | return rtp_data_channel_ |
| 5010 | ? rtp_data_channel_->SendData(params, payload, result) |
| 5011 | : network_thread()->Invoke<bool>( |
| 5012 | RTC_FROM_HERE, |
| 5013 | Bind(&cricket::SctpTransportInternal::SendData, |
| 5014 | sctp_transport_.get(), params, payload, result)); |
| 5015 | } |
| 5016 | |
| 5017 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 5018 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 5019 | // Don't log an error here, because DataChannels are expected to call |
| 5020 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 5021 | // whether or not the underlying transport is ready. |
| 5022 | return false; |
| 5023 | } |
| 5024 | if (rtp_data_channel_) { |
| 5025 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 5026 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 5027 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 5028 | &DataChannel::OnDataReceived); |
| 5029 | } else { |
| 5030 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 5031 | &DataChannel::OnChannelReady); |
| 5032 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 5033 | &DataChannel::OnDataReceived); |
| 5034 | SignalSctpStreamClosedRemotely.connect( |
| 5035 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
| 5036 | } |
| 5037 | return true; |
| 5038 | } |
| 5039 | |
| 5040 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 5041 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5042 | RTC_LOG(LS_ERROR) |
| 5043 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 5044 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5045 | return; |
| 5046 | } |
| 5047 | if (rtp_data_channel_) { |
| 5048 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 5049 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 5050 | } else { |
| 5051 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 5052 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
| 5053 | SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel); |
| 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | void PeerConnection::AddSctpDataStream(int sid) { |
| 5058 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5059 | RTC_LOG(LS_ERROR) |
| 5060 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5061 | return; |
| 5062 | } |
| 5063 | network_thread()->Invoke<void>( |
| 5064 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 5065 | sctp_transport_.get(), sid)); |
| 5066 | } |
| 5067 | |
| 5068 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 5069 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5070 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5071 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5072 | return; |
| 5073 | } |
| 5074 | network_thread()->Invoke<void>( |
| 5075 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 5076 | sctp_transport_.get(), sid)); |
| 5077 | } |
| 5078 | |
| 5079 | bool PeerConnection::ReadyToSendData() const { |
| 5080 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 5081 | sctp_ready_to_send_data_; |
| 5082 | } |
| 5083 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5084 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 5085 | cricket::CandidateStatsList candidate_states_list; |
| 5086 | port_allocator_->GetCandidateStatsFromPooledSessions(&candidate_states_list); |
| 5087 | return candidate_states_list; |
| 5088 | } |
| 5089 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5090 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 5091 | const { |
| 5092 | std::map<std::string, std::string> transport_names_by_mid; |
| 5093 | for (auto transceiver : transceivers_) { |
| 5094 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5095 | if (channel) { |
| 5096 | transport_names_by_mid[channel->content_name()] = |
| 5097 | channel->transport_name(); |
| 5098 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5099 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5100 | if (rtp_data_channel_) { |
| 5101 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5102 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5103 | } |
| 5104 | if (sctp_transport_) { |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5105 | transport_names_by_mid[*sctp_content_name_] = *sctp_transport_name_; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5106 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5107 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5108 | } |
| 5109 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5110 | std::map<std::string, cricket::TransportStats> |
| 5111 | PeerConnection::GetTransportStatsByNames( |
| 5112 | const std::set<std::string>& transport_names) { |
| 5113 | if (!network_thread()->IsCurrent()) { |
| 5114 | return network_thread() |
| 5115 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5116 | RTC_FROM_HERE, |
| 5117 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5118 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5119 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5120 | for (const std::string& transport_name : transport_names) { |
| 5121 | cricket::TransportStats transport_stats; |
| 5122 | bool success = |
| 5123 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5124 | if (success) { |
| 5125 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5126 | } else { |
| 5127 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5128 | << transport_name; |
| 5129 | } |
| 5130 | } |
| 5131 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | bool PeerConnection::GetLocalCertificate( |
| 5135 | const std::string& transport_name, |
| 5136 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 5137 | return transport_controller_->GetLocalCertificate(transport_name, |
| 5138 | certificate); |
| 5139 | } |
| 5140 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5141 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5142 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5143 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 5147 | return data_channel_type_; |
| 5148 | } |
| 5149 | |
| 5150 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5151 | return pending_ice_restarts_.find(content_name) != |
| 5152 | pending_ice_restarts_.end(); |
| 5153 | } |
| 5154 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5155 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5156 | return transport_controller_->NeedsIceRestart(content_name); |
| 5157 | } |
| 5158 | |
| 5159 | void PeerConnection::OnCertificateReady( |
| 5160 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5161 | transport_controller_->SetLocalCertificate(certificate); |
| 5162 | } |
| 5163 | |
| 5164 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5165 | SetSessionError(SessionError::kTransport, |
| 5166 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5167 | } |
| 5168 | |
| 5169 | void PeerConnection::OnTransportControllerConnectionState( |
| 5170 | cricket::IceConnectionState state) { |
| 5171 | switch (state) { |
| 5172 | case cricket::kIceConnectionConnecting: |
| 5173 | // If the current state is Connected or Completed, then there were |
| 5174 | // writable channels but now there are not, so the next state must |
| 5175 | // be Disconnected. |
| 5176 | // kIceConnectionConnecting is currently used as the default, |
| 5177 | // un-connected state by the TransportController, so its only use is |
| 5178 | // detecting disconnections. |
| 5179 | if (ice_connection_state_ == |
| 5180 | PeerConnectionInterface::kIceConnectionConnected || |
| 5181 | ice_connection_state_ == |
| 5182 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5183 | SetIceConnectionState( |
| 5184 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5185 | } |
| 5186 | break; |
| 5187 | case cricket::kIceConnectionFailed: |
| 5188 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5189 | break; |
| 5190 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5191 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5192 | "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5193 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5194 | break; |
| 5195 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5196 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5197 | "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5198 | if (ice_connection_state_ != |
| 5199 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5200 | // If jumping directly from "checking" to "connected", |
| 5201 | // signal "connected" first. |
| 5202 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5203 | } |
| 5204 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 5205 | if (metrics_observer()) { |
| 5206 | ReportTransportStats(); |
| 5207 | } |
| 5208 | break; |
| 5209 | default: |
| 5210 | RTC_NOTREACHED(); |
| 5211 | } |
| 5212 | } |
| 5213 | |
| 5214 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5215 | const std::string& transport_name, |
| 5216 | const cricket::Candidates& candidates) { |
| 5217 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5218 | int sdp_mline_index; |
| 5219 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5220 | RTC_LOG(LS_ERROR) |
| 5221 | << "OnTransportControllerCandidatesGathered: content name " |
| 5222 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5223 | return; |
| 5224 | } |
| 5225 | |
| 5226 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5227 | citer != candidates.end(); ++citer) { |
| 5228 | // Use transport_name as the candidate media id. |
| 5229 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5230 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5231 | if (local_description()) { |
| 5232 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5233 | } |
| 5234 | OnIceCandidate(std::move(candidate)); |
| 5235 | } |
| 5236 | } |
| 5237 | |
| 5238 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5239 | const std::vector<cricket::Candidate>& candidates) { |
| 5240 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5241 | // Sanity check. |
| 5242 | for (const cricket::Candidate& candidate : candidates) { |
| 5243 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5244 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5245 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5246 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5247 | return; |
| 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | if (local_description()) { |
| 5252 | mutable_local_description()->RemoveCandidates(candidates); |
| 5253 | } |
| 5254 | OnIceCandidatesRemoved(candidates); |
| 5255 | } |
| 5256 | |
| 5257 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5258 | rtc::SSLHandshakeError error) { |
| 5259 | if (metrics_observer()) { |
| 5260 | metrics_observer()->IncrementEnumCounter( |
| 5261 | webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error), |
| 5262 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
| 5263 | } |
| 5264 | } |
| 5265 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5266 | void PeerConnection::EnableSending() { |
| 5267 | for (auto transceiver : transceivers_) { |
| 5268 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5269 | if (channel && !channel->enabled()) { |
| 5270 | channel->Enable(true); |
| 5271 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5272 | } |
| 5273 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5274 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5275 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5276 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5277 | } |
| 5278 | |
| 5279 | // Returns the media index for a local ice candidate given the content name. |
| 5280 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5281 | const std::string& content_name, |
| 5282 | int* sdp_mline_index) { |
| 5283 | if (!local_description() || !sdp_mline_index) { |
| 5284 | return false; |
| 5285 | } |
| 5286 | |
| 5287 | bool content_found = false; |
| 5288 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5289 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5290 | if (contents[index].name == content_name) { |
| 5291 | *sdp_mline_index = static_cast<int>(index); |
| 5292 | content_found = true; |
| 5293 | break; |
| 5294 | } |
| 5295 | } |
| 5296 | return content_found; |
| 5297 | } |
| 5298 | |
| 5299 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5300 | const SessionDescriptionInterface* remote_desc) { |
| 5301 | if (!remote_desc) { |
| 5302 | return true; |
| 5303 | } |
| 5304 | bool ret = true; |
| 5305 | |
| 5306 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5307 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5308 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5309 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5310 | bool valid = false; |
| 5311 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5312 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5313 | RTC_LOG(LS_INFO) |
| 5314 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5315 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5316 | } |
| 5317 | continue; |
| 5318 | } |
| 5319 | ret = UseCandidate(candidate); |
| 5320 | if (!ret) { |
| 5321 | break; |
| 5322 | } |
| 5323 | } |
| 5324 | } |
| 5325 | return ret; |
| 5326 | } |
| 5327 | |
| 5328 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5329 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5330 | size_t remote_content_size = |
| 5331 | remote_description()->description()->contents().size(); |
| 5332 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5333 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5334 | return false; |
| 5335 | } |
| 5336 | |
| 5337 | cricket::ContentInfo content = |
| 5338 | remote_description()->description()->contents()[mediacontent_index]; |
| 5339 | std::vector<cricket::Candidate> candidates; |
| 5340 | candidates.push_back(candidate->candidate()); |
| 5341 | // Invoking BaseSession method to handle remote candidates. |
| 5342 | std::string error; |
| 5343 | if (transport_controller_->AddRemoteCandidates(content.name, candidates, |
| 5344 | &error)) { |
| 5345 | // Candidates successfully submitted for checking. |
| 5346 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5347 | ice_connection_state_ == |
| 5348 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5349 | // If state is New, then the session has just gotten its first remote ICE |
| 5350 | // candidates, so go to Checking. |
| 5351 | // If state is Disconnected, the session is re-using old candidates or |
| 5352 | // receiving additional ones, so go to Checking. |
| 5353 | // If state is Connected, stay Connected. |
| 5354 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5355 | // newly added transport, then the state actually _should_ move to |
| 5356 | // checking. Add a way to distinguish that case. |
| 5357 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5358 | } |
| 5359 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 5360 | } else { |
| 5361 | if (!error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5362 | RTC_LOG(LS_WARNING) << error; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5363 | } |
| 5364 | } |
| 5365 | return true; |
| 5366 | } |
| 5367 | |
| 5368 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5369 | // Destroy video channel first since it may have a pointer to the |
| 5370 | // voice channel. |
| 5371 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5372 | if (!video_info || video_info->rejected) { |
| 5373 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5374 | } |
| 5375 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5376 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5377 | if (!audio_info || audio_info->rejected) { |
| 5378 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5382 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5383 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5384 | } |
| 5385 | } |
| 5386 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5387 | std::string PeerConnection::GetTransportNameForMediaSection( |
| 5388 | const std::string& mid, |
| 5389 | const cricket::ContentGroup* bundle_group) const { |
| 5390 | if (!bundle_group) { |
| 5391 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5392 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5393 | const std::string* first_content_name = bundle_group->FirstContentName(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5394 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5395 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5396 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5397 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5398 | if (!bundle_group->HasContentName(mid)) { |
| 5399 | RTC_LOG(LS_WARNING) << mid << " is not part of any bundle group"; |
| 5400 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5401 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5402 | RTC_LOG(LS_INFO) << "Bundling " << mid << " on " << *first_content_name; |
| 5403 | return *first_content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5404 | } |
| 5405 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5406 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5407 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5408 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5409 | if (configuration_.bundle_policy == |
| 5410 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5411 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5412 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5413 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5414 | "max-bundle configured but session description " |
| 5415 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5416 | } |
| 5417 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5418 | return std::move(bundle_group); |
| 5419 | } |
| 5420 | |
| 5421 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
| 5422 | auto bundle_group_or_error = GetEarlyBundleGroup(desc); |
| 5423 | if (!bundle_group_or_error.ok()) { |
| 5424 | return bundle_group_or_error.MoveError(); |
| 5425 | } |
| 5426 | const cricket::ContentGroup* bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5427 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5428 | // Creating the media channels and transport proxies. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5429 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5430 | if (voice && !voice->rejected && |
| 5431 | !GetAudioTransceiver()->internal()->channel()) { |
| 5432 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel( |
| 5433 | voice->name, |
| 5434 | GetTransportNameForMediaSection(voice->name, bundle_group)); |
| 5435 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5436 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5437 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5438 | } |
| 5439 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5440 | } |
| 5441 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5442 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5443 | if (video && !video->rejected && |
| 5444 | !GetVideoTransceiver()->internal()->channel()) { |
| 5445 | cricket::VideoChannel* video_channel = CreateVideoChannel( |
| 5446 | video->name, |
| 5447 | GetTransportNameForMediaSection(video->name, bundle_group)); |
| 5448 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5449 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5450 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5451 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5452 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5453 | } |
| 5454 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5455 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5456 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 5457 | !rtp_data_channel_ && !sctp_transport_) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5458 | if (!CreateDataChannel(data->name, GetTransportNameForMediaSection( |
| 5459 | data->name, bundle_group))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5460 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5461 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5462 | } |
| 5463 | } |
| 5464 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5465 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5466 | } |
| 5467 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5468 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5469 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
| 5470 | const std::string& mid, |
| 5471 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5472 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5473 | transport_controller_->CreateDtlsTransport( |
| 5474 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5475 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5476 | if (configuration_.rtcp_mux_policy != |
| 5477 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5478 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5479 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5480 | } |
| 5481 | |
| 5482 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
| 5483 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5484 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5485 | audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5486 | if (!voice_channel) { |
| 5487 | transport_controller_->DestroyDtlsTransport( |
| 5488 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5489 | if (rtcp_dtls_transport) { |
| 5490 | transport_controller_->DestroyDtlsTransport( |
| 5491 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5492 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5493 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5494 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5495 | voice_channel->SignalRtcpMuxFullyActive.connect( |
| 5496 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5497 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5498 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5499 | voice_channel->SignalSentPacket.connect(this, |
| 5500 | &PeerConnection::OnSentPacket_w); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 5501 | if (uma_observer_) { |
| 5502 | voice_channel->SetMetricsObserver(uma_observer_); |
| 5503 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5504 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5505 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5506 | } |
| 5507 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5508 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5509 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
| 5510 | const std::string& mid, |
| 5511 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5512 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5513 | transport_controller_->CreateDtlsTransport( |
| 5514 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5515 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5516 | if (configuration_.rtcp_mux_policy != |
| 5517 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5518 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5519 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5520 | } |
| 5521 | |
| 5522 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
| 5523 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5524 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5525 | video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5526 | |
| 5527 | if (!video_channel) { |
| 5528 | transport_controller_->DestroyDtlsTransport( |
| 5529 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5530 | if (rtcp_dtls_transport) { |
| 5531 | transport_controller_->DestroyDtlsTransport( |
| 5532 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5533 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5534 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5535 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5536 | video_channel->SignalRtcpMuxFullyActive.connect( |
| 5537 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5538 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5539 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5540 | video_channel->SignalSentPacket.connect(this, |
| 5541 | &PeerConnection::OnSentPacket_w); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 5542 | if (uma_observer_) { |
| 5543 | video_channel->SetMetricsObserver(uma_observer_); |
| 5544 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5545 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5546 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5547 | } |
| 5548 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5549 | bool PeerConnection::CreateDataChannel(const std::string& mid, |
| 5550 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5551 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 5552 | if (sctp) { |
| 5553 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5554 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5555 | << "Trying to create SCTP transport, but didn't compile with " |
| 5556 | "SCTP support (HAVE_SCTP)"; |
| 5557 | return false; |
| 5558 | } |
| 5559 | if (!network_thread()->Invoke<bool>( |
| 5560 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::CreateSctpTransport_n, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5561 | this, mid, transport_name))) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5562 | return false; |
| 5563 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5564 | for (const auto& channel : sctp_data_channels_) { |
| 5565 | channel->OnTransportChannelCreated(); |
| 5566 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5567 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5568 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5569 | transport_controller_->CreateDtlsTransport( |
| 5570 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5571 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5572 | if (configuration_.rtcp_mux_policy != |
| 5573 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5574 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5575 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5576 | } |
| 5577 | |
| 5578 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 5579 | configuration_.media_config, rtp_dtls_transport, rtcp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5580 | signaling_thread(), mid, SrtpRequired()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5581 | |
| 5582 | if (!rtp_data_channel_) { |
| 5583 | transport_controller_->DestroyDtlsTransport( |
| 5584 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5585 | if (rtcp_dtls_transport) { |
| 5586 | transport_controller_->DestroyDtlsTransport( |
| 5587 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5588 | } |
| 5589 | return false; |
| 5590 | } |
| 5591 | |
| 5592 | rtp_data_channel_->SignalRtcpMuxFullyActive.connect( |
| 5593 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5594 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 5595 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 5596 | rtp_data_channel_->SignalSentPacket.connect( |
| 5597 | this, &PeerConnection::OnSentPacket_w); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 5598 | if (uma_observer_) { |
| 5599 | rtp_data_channel_->SetMetricsObserver(uma_observer_); |
| 5600 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5601 | } |
| 5602 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5603 | return true; |
| 5604 | } |
| 5605 | |
| 5606 | Call::Stats PeerConnection::GetCallStats() { |
| 5607 | if (!worker_thread()->IsCurrent()) { |
| 5608 | return worker_thread()->Invoke<Call::Stats>( |
| 5609 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 5610 | } |
| 5611 | if (call_) { |
| 5612 | return call_->GetStats(); |
| 5613 | } else { |
| 5614 | return Call::Stats(); |
| 5615 | } |
| 5616 | } |
| 5617 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5618 | bool PeerConnection::CreateSctpTransport_n(const std::string& content_name, |
| 5619 | const std::string& transport_name) { |
| 5620 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5621 | RTC_DCHECK(sctp_factory_); |
| 5622 | cricket::DtlsTransportInternal* tc = |
| 5623 | transport_controller_->CreateDtlsTransport_n( |
| 5624 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5625 | sctp_transport_ = sctp_factory_->CreateSctpTransport(tc); |
| 5626 | RTC_DCHECK(sctp_transport_); |
| 5627 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 5628 | sctp_transport_->SignalReadyToSendData.connect( |
| 5629 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 5630 | sctp_transport_->SignalDataReceived.connect( |
| 5631 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
| 5632 | sctp_transport_->SignalStreamClosedRemotely.connect( |
| 5633 | this, &PeerConnection::OnSctpStreamClosedRemotely_n); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5634 | sctp_transport_name_ = transport_name; |
| 5635 | sctp_content_name_ = content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5636 | return true; |
| 5637 | } |
| 5638 | |
| 5639 | void PeerConnection::ChangeSctpTransport_n(const std::string& transport_name) { |
| 5640 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5641 | RTC_DCHECK(sctp_transport_); |
| 5642 | RTC_DCHECK(sctp_transport_name_); |
| 5643 | std::string old_sctp_transport_name = *sctp_transport_name_; |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5644 | sctp_transport_name_ = transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5645 | cricket::DtlsTransportInternal* tc = |
| 5646 | transport_controller_->CreateDtlsTransport_n( |
| 5647 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5648 | sctp_transport_->SetTransportChannel(tc); |
| 5649 | transport_controller_->DestroyDtlsTransport_n( |
| 5650 | old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5651 | } |
| 5652 | |
| 5653 | void PeerConnection::DestroySctpTransport_n() { |
| 5654 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5655 | sctp_transport_.reset(nullptr); |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 5656 | transport_controller_->DestroyDtlsTransport_n( |
| 5657 | *sctp_transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5658 | sctp_content_name_.reset(); |
| 5659 | sctp_transport_name_.reset(); |
| 5660 | sctp_invoker_.reset(nullptr); |
| 5661 | sctp_ready_to_send_data_ = false; |
| 5662 | } |
| 5663 | |
| 5664 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 5665 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5666 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5667 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5668 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5669 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5670 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5671 | // pending tasks will be cleared. |
| 5672 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 5673 | OnSctpTransportReadyToSendData_s(true); |
| 5674 | }); |
| 5675 | } |
| 5676 | |
| 5677 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 5678 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5679 | sctp_ready_to_send_data_ = ready; |
| 5680 | SignalSctpReadyToSendData(ready); |
| 5681 | } |
| 5682 | |
| 5683 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 5684 | const cricket::ReceiveDataParams& params, |
| 5685 | const rtc::CopyOnWriteBuffer& payload) { |
| 5686 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5687 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5688 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5689 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5690 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5691 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5692 | // pending tasks will be cleared. |
| 5693 | sctp_invoker_->AsyncInvoke<void>( |
| 5694 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 5695 | OnSctpTransportDataReceived_s(params, payload); |
| 5696 | }); |
| 5697 | } |
| 5698 | |
| 5699 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 5700 | const cricket::ReceiveDataParams& params, |
| 5701 | const rtc::CopyOnWriteBuffer& payload) { |
| 5702 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5703 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 5704 | // Received OPEN message; parse and signal that a new data channel should |
| 5705 | // be created. |
| 5706 | std::string label; |
| 5707 | InternalDataChannelInit config; |
| 5708 | config.id = params.ssrc; |
| 5709 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5710 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 5711 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5712 | return; |
| 5713 | } |
| 5714 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5715 | OnDataChannelOpenMessage(label, config); |
| 5716 | } else { |
| 5717 | // Otherwise just forward the signal. |
| 5718 | SignalSctpDataReceived(params, payload); |
| 5719 | } |
| 5720 | } |
| 5721 | |
| 5722 | void PeerConnection::OnSctpStreamClosedRemotely_n(int sid) { |
| 5723 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5724 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5725 | sctp_invoker_->AsyncInvoke<void>( |
| 5726 | RTC_FROM_HERE, signaling_thread(), |
| 5727 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 5728 | &SignalSctpStreamClosedRemotely, sid)); |
| 5729 | } |
| 5730 | |
| 5731 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 5732 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 5733 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 5734 | if (!bundle_enabled) |
| 5735 | return true; |
| 5736 | |
| 5737 | const cricket::ContentGroup* bundle_group = |
| 5738 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 5739 | RTC_DCHECK(bundle_group != NULL); |
| 5740 | |
| 5741 | const cricket::ContentInfos& contents = desc->contents(); |
| 5742 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 5743 | citer != contents.end(); ++citer) { |
| 5744 | const cricket::ContentInfo* content = (&*citer); |
| 5745 | RTC_DCHECK(content != NULL); |
| 5746 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 5747 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5748 | if (!HasRtcpMuxEnabled(content)) |
| 5749 | return false; |
| 5750 | } |
| 5751 | } |
| 5752 | // RTCP-MUX is enabled in all the contents. |
| 5753 | return true; |
| 5754 | } |
| 5755 | |
| 5756 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5757 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5758 | } |
| 5759 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5760 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5761 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5762 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5763 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5764 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5768 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5769 | } |
| 5770 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5771 | SdpType type = sdesc->GetType(); |
| 5772 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 5773 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5774 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 5775 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5776 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5777 | } |
| 5778 | |
| 5779 | // Verify crypto settings. |
| 5780 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5781 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 5782 | dtls_enabled_) { |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 5783 | RTCError crypto_error = |
| 5784 | VerifyCrypto(sdesc->description(), dtls_enabled_, uma_observer_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5785 | if (!crypto_error.ok()) { |
| 5786 | return crypto_error; |
| 5787 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | // Verify ice-ufrag and ice-pwd. |
| 5791 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5792 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5793 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5797 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5798 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5799 | } |
| 5800 | |
| 5801 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 5802 | // m-lines that do not rtcp-mux enabled. |
| 5803 | |
| 5804 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5805 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5806 | // With an answer we want to compare the new answer session description with |
| 5807 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5808 | const cricket::SessionDescription* offer_desc = |
| 5809 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 5810 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5811 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 5812 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 5813 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5814 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5815 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5816 | } |
| 5817 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5818 | // The re-offers should respect the order of m= sections in current |
| 5819 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5820 | // With a re-offer, either the current local or current remote descriptions |
| 5821 | // could be the most up to date, so we would like to check against both of |
| 5822 | // them if they exist. It could be the case that one of them has a 0 port |
| 5823 | // for a media section, but the other does not. This is important to check |
| 5824 | // against in the case that we are recycling an m= section. |
| 5825 | const cricket::SessionDescription* current_desc = nullptr; |
| 5826 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 5827 | if (local_description()) { |
| 5828 | current_desc = local_description()->description(); |
| 5829 | if (remote_description()) { |
| 5830 | secondary_current_desc = remote_description()->description(); |
| 5831 | } |
| 5832 | } else if (remote_description()) { |
| 5833 | current_desc = remote_description()->description(); |
| 5834 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5835 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5836 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 5837 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5838 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5839 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5840 | } |
| 5841 | } |
| 5842 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5843 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5844 | } |
| 5845 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5846 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5847 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5848 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5849 | return (state == PeerConnectionInterface::kStable) || |
| 5850 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5851 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5852 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5853 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 5854 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 5855 | } |
| 5856 | } |
| 5857 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5858 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5859 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5860 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5861 | return (state == PeerConnectionInterface::kStable) || |
| 5862 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5863 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5864 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5865 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 5866 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5867 | } |
| 5868 | } |
| 5869 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5870 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 5871 | switch (error) { |
| 5872 | case SessionError::kNone: |
| 5873 | return "ERROR_NONE"; |
| 5874 | case SessionError::kContent: |
| 5875 | return "ERROR_CONTENT"; |
| 5876 | case SessionError::kTransport: |
| 5877 | return "ERROR_TRANSPORT"; |
| 5878 | } |
| 5879 | RTC_NOTREACHED(); |
| 5880 | return ""; |
| 5881 | } |
| 5882 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5883 | std::string PeerConnection::GetSessionErrorMsg() { |
| 5884 | std::ostringstream desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5885 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 5886 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5887 | return desc.str(); |
| 5888 | } |
| 5889 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 5890 | void PeerConnection::ReportSdpFormatReceived( |
| 5891 | const SessionDescriptionInterface& remote_offer) { |
| 5892 | if (!uma_observer_) { |
| 5893 | return; |
| 5894 | } |
| 5895 | int num_audio_mlines = 0; |
| 5896 | int num_video_mlines = 0; |
| 5897 | int num_audio_tracks = 0; |
| 5898 | int num_video_tracks = 0; |
| 5899 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 5900 | cricket::MediaType media_type = content.media_description()->type(); |
| 5901 | int num_tracks = std::max( |
| 5902 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 5903 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 5904 | num_audio_mlines += 1; |
| 5905 | num_audio_tracks += num_tracks; |
| 5906 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5907 | num_video_mlines += 1; |
| 5908 | num_video_tracks += num_tracks; |
| 5909 | } |
| 5910 | } |
| 5911 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 5912 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 5913 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 5914 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 5915 | format = kSdpFormatReceivedComplexPlanB; |
| 5916 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 5917 | format = kSdpFormatReceivedSimple; |
| 5918 | } |
| 5919 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpFormatReceived, format, |
| 5920 | kSdpFormatReceivedMax); |
| 5921 | } |
| 5922 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5923 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 5924 | const SessionDescriptionInterface& answer) { |
| 5925 | if (!uma_observer_) { |
| 5926 | return; |
| 5927 | } |
| 5928 | switch (answer.description()->msid_signaling()) { |
| 5929 | case 0: |
| 5930 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5931 | kSdpSemanticNegotiatedNone, |
| 5932 | kSdpSemanticNegotiatedMax); |
| 5933 | break; |
| 5934 | case cricket::kMsidSignalingMediaSection: |
| 5935 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5936 | kSdpSemanticNegotiatedUnifiedPlan, |
| 5937 | kSdpSemanticNegotiatedMax); |
| 5938 | break; |
| 5939 | case cricket::kMsidSignalingSsrcAttribute: |
| 5940 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5941 | kSdpSemanticNegotiatedPlanB, |
| 5942 | kSdpSemanticNegotiatedMax); |
| 5943 | break; |
| 5944 | case cricket::kMsidSignalingMediaSection | |
| 5945 | cricket::kMsidSignalingSsrcAttribute: |
| 5946 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5947 | kSdpSemanticNegotiatedMixed, |
| 5948 | kSdpSemanticNegotiatedMax); |
| 5949 | break; |
| 5950 | default: |
| 5951 | RTC_NOTREACHED(); |
| 5952 | } |
| 5953 | } |
| 5954 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5955 | // We need to check the local/remote description for the Transport instead of |
| 5956 | // the session, because a new Transport added during renegotiation may have |
| 5957 | // them unset while the session has them set from the previous negotiation. |
| 5958 | // Not doing so may trigger the auto generation of transport description and |
| 5959 | // mess up DTLS identity information, ICE credential, etc. |
| 5960 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 5961 | const IceCandidateInterface* candidate, |
| 5962 | const SessionDescriptionInterface* remote_desc, |
| 5963 | bool* valid) { |
| 5964 | *valid = true; |
| 5965 | |
| 5966 | const SessionDescriptionInterface* current_remote_desc = |
| 5967 | remote_desc ? remote_desc : remote_description(); |
| 5968 | |
| 5969 | if (!current_remote_desc) { |
| 5970 | return false; |
| 5971 | } |
| 5972 | |
| 5973 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5974 | size_t remote_content_size = |
| 5975 | current_remote_desc->description()->contents().size(); |
| 5976 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5977 | RTC_LOG(LS_ERROR) |
| 5978 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 5979 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5980 | |
| 5981 | *valid = false; |
| 5982 | return false; |
| 5983 | } |
| 5984 | |
| 5985 | cricket::ContentInfo content = |
| 5986 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 5987 | |
| 5988 | const std::string transport_name = GetTransportName(content.name); |
| 5989 | if (transport_name.empty()) { |
| 5990 | return false; |
| 5991 | } |
| 5992 | return transport_controller_->ReadyForRemoteCandidates(transport_name); |
| 5993 | } |
| 5994 | |
| 5995 | bool PeerConnection::SrtpRequired() const { |
| 5996 | return dtls_enabled_ || |
| 5997 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 5998 | } |
| 5999 | |
| 6000 | void PeerConnection::OnTransportControllerGatheringState( |
| 6001 | cricket::IceGatheringState state) { |
| 6002 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 6003 | if (state == cricket::kIceGatheringGathering) { |
| 6004 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 6005 | } else if (state == cricket::kIceGatheringComplete) { |
| 6006 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 6007 | } |
| 6008 | } |
| 6009 | |
| 6010 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6011 | std::map<std::string, std::set<cricket::MediaType>> |
| 6012 | media_types_by_transport_name; |
| 6013 | for (auto transceiver : transceivers_) { |
| 6014 | if (transceiver->internal()->channel()) { |
| 6015 | const std::string& transport_name = |
| 6016 | transceiver->internal()->channel()->transport_name(); |
| 6017 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 6018 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6019 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6020 | } |
| 6021 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6022 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 6023 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6024 | } |
| 6025 | if (sctp_transport_name_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6026 | media_types_by_transport_name[*sctp_transport_name_].insert( |
| 6027 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6028 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6029 | for (const auto& entry : media_types_by_transport_name) { |
| 6030 | const std::string& transport_name = entry.first; |
| 6031 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6032 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6033 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6034 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6035 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6036 | } |
| 6037 | } |
| 6038 | } |
| 6039 | // Walk through the ConnectionInfos to gather best connection usage |
| 6040 | // for IPv4 and IPv6. |
| 6041 | void PeerConnection::ReportBestConnectionState( |
| 6042 | const cricket::TransportStats& stats) { |
| 6043 | RTC_DCHECK(metrics_observer()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6044 | for (const cricket::TransportChannelStats& channel_stats : |
| 6045 | stats.channel_stats) { |
| 6046 | for (const cricket::ConnectionInfo& connection_info : |
| 6047 | channel_stats.connection_infos) { |
| 6048 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6049 | continue; |
| 6050 | } |
| 6051 | |
| 6052 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6053 | const cricket::Candidate& local = connection_info.local_candidate; |
| 6054 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6055 | |
| 6056 | // Increment the counter for IceCandidatePairType. |
| 6057 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 6058 | (local.type() == RELAY_PORT_TYPE && |
| 6059 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 6060 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 6061 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 6062 | type = kEnumCounterIceCandidatePairTypeUdp; |
| 6063 | } else { |
| 6064 | RTC_CHECK(0); |
| 6065 | } |
| 6066 | metrics_observer()->IncrementEnumCounter( |
| 6067 | type, GetIceCandidatePairCounter(local, remote), |
| 6068 | kIceCandidatePairMax); |
| 6069 | |
| 6070 | // Increment the counter for IP type. |
| 6071 | if (local.address().family() == AF_INET) { |
| 6072 | metrics_observer()->IncrementEnumCounter( |
| 6073 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 6074 | kPeerConnectionAddressFamilyCounter_Max); |
| 6075 | |
| 6076 | } else if (local.address().family() == AF_INET6) { |
| 6077 | metrics_observer()->IncrementEnumCounter( |
| 6078 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 6079 | kPeerConnectionAddressFamilyCounter_Max); |
| 6080 | } else { |
| 6081 | RTC_CHECK(0); |
| 6082 | } |
| 6083 | |
| 6084 | return; |
| 6085 | } |
| 6086 | } |
| 6087 | } |
| 6088 | |
| 6089 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6090 | const cricket::TransportStats& stats, |
| 6091 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6092 | RTC_DCHECK(metrics_observer()); |
| 6093 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 6094 | return; |
| 6095 | } |
| 6096 | |
| 6097 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 6098 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 6099 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 6100 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6101 | return; |
| 6102 | } |
| 6103 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6104 | for (cricket::MediaType media_type : media_types) { |
| 6105 | PeerConnectionEnumCounterType srtp_counter_type; |
| 6106 | PeerConnectionEnumCounterType ssl_counter_type; |
| 6107 | switch (media_type) { |
| 6108 | case cricket::MEDIA_TYPE_AUDIO: |
| 6109 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 6110 | ssl_counter_type = kEnumCounterAudioSslCipher; |
| 6111 | break; |
| 6112 | case cricket::MEDIA_TYPE_VIDEO: |
| 6113 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 6114 | ssl_counter_type = kEnumCounterVideoSslCipher; |
| 6115 | break; |
| 6116 | case cricket::MEDIA_TYPE_DATA: |
| 6117 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 6118 | ssl_counter_type = kEnumCounterDataSslCipher; |
| 6119 | break; |
| 6120 | default: |
| 6121 | RTC_NOTREACHED(); |
| 6122 | continue; |
| 6123 | } |
| 6124 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6125 | metrics_observer()->IncrementSparseEnumCounter(srtp_counter_type, |
| 6126 | srtp_crypto_suite); |
| 6127 | } |
| 6128 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6129 | metrics_observer()->IncrementSparseEnumCounter(ssl_counter_type, |
| 6130 | ssl_cipher_suite); |
| 6131 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6132 | } |
| 6133 | } |
| 6134 | |
| 6135 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 6136 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 6137 | RTC_DCHECK(call_); |
| 6138 | call_->OnSentPacket(sent_packet); |
| 6139 | } |
| 6140 | |
| 6141 | const std::string PeerConnection::GetTransportName( |
| 6142 | const std::string& content_name) { |
| 6143 | cricket::BaseChannel* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6144 | if (channel) { |
| 6145 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6146 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6147 | if (sctp_transport_) { |
| 6148 | RTC_DCHECK(sctp_content_name_); |
| 6149 | RTC_DCHECK(sctp_transport_name_); |
| 6150 | if (content_name == *sctp_content_name_) { |
| 6151 | return *sctp_transport_name_; |
| 6152 | } |
| 6153 | } |
| 6154 | // Return an empty string if failed to retrieve the transport name. |
| 6155 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6156 | } |
| 6157 | |
| 6158 | void PeerConnection::DestroyRtcpTransport_n(const std::string& transport_name) { |
| 6159 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6160 | transport_controller_->DestroyDtlsTransport_n( |
| 6161 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 6162 | } |
| 6163 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6164 | void PeerConnection::DestroyTransceiverChannel( |
| 6165 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 6166 | transceiver) { |
| 6167 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6168 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6169 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 6170 | if (channel) { |
| 6171 | transceiver->internal()->SetChannel(nullptr); |
| 6172 | DestroyBaseChannel(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6173 | } |
| 6174 | } |
| 6175 | |
| 6176 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6177 | if (rtp_data_channel_) { |
| 6178 | OnDataChannelDestroyed(); |
| 6179 | DestroyBaseChannel(rtp_data_channel_); |
| 6180 | rtp_data_channel_ = nullptr; |
| 6181 | } |
| 6182 | |
| 6183 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 6184 | // grab a reference to this PeerConnection. If this is called from the |
| 6185 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 6186 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 6187 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 6188 | |
| 6189 | if (sctp_transport_) { |
| 6190 | OnDataChannelDestroyed(); |
| 6191 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 6192 | [this] { DestroySctpTransport_n(); }); |
| 6193 | } |
| 6194 | } |
| 6195 | |
| 6196 | void PeerConnection::DestroyBaseChannel(cricket::BaseChannel* channel) { |
| 6197 | RTC_DCHECK(channel); |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 6198 | RTC_DCHECK(channel->rtp_dtls_transport()); |
| 6199 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6200 | // Need to cache these before destroying the base channel so that we do not |
| 6201 | // access uninitialized memory. |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 6202 | const std::string transport_name = |
| 6203 | channel->rtp_dtls_transport()->transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6204 | const bool need_to_delete_rtcp = (channel->rtcp_dtls_transport() != nullptr); |
| 6205 | |
| 6206 | switch (channel->media_type()) { |
| 6207 | case cricket::MEDIA_TYPE_AUDIO: |
| 6208 | channel_manager()->DestroyVoiceChannel( |
| 6209 | static_cast<cricket::VoiceChannel*>(channel)); |
| 6210 | break; |
| 6211 | case cricket::MEDIA_TYPE_VIDEO: |
| 6212 | channel_manager()->DestroyVideoChannel( |
| 6213 | static_cast<cricket::VideoChannel*>(channel)); |
| 6214 | break; |
| 6215 | case cricket::MEDIA_TYPE_DATA: |
| 6216 | channel_manager()->DestroyRtpDataChannel( |
| 6217 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 6218 | break; |
| 6219 | default: |
| 6220 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 6221 | break; |
| 6222 | } |
| 6223 | |
| 6224 | // |channel| can no longer be used. |
| 6225 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6226 | transport_controller_->DestroyDtlsTransport( |
| 6227 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 6228 | if (need_to_delete_rtcp) { |
| 6229 | transport_controller_->DestroyDtlsTransport( |
| 6230 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 6231 | } |
| 6232 | } |
| 6233 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 6234 | void PeerConnection::ClearStatsCache() { |
| 6235 | if (stats_collector_) { |
| 6236 | stats_collector_->ClearCachedStatsReport(); |
| 6237 | } |
| 6238 | } |
| 6239 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6240 | } // namespace webrtc |