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> |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 14 | #include <limits> |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 15 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 16 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 17 | #include <utility> |
| 18 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 20 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "api/jsepicecandidate.h" |
| 22 | #include "api/jsepsessiondescription.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "api/mediastreamproxy.h" |
| 24 | #include "api/mediastreamtrackproxy.h" |
| 25 | #include "call/call.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 26 | #include "logging/rtc_event_log/icelogger.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 27 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 29 | #include "media/sctp/sctptransport.h" |
| 30 | #include "pc/audiotrack.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 31 | #include "pc/channel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "pc/channelmanager.h" |
| 33 | #include "pc/dtmfsender.h" |
| 34 | #include "pc/mediastream.h" |
| 35 | #include "pc/mediastreamobserver.h" |
| 36 | #include "pc/remoteaudiosource.h" |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 37 | #include "pc/rtpmediautils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 38 | #include "pc/rtpreceiver.h" |
| 39 | #include "pc/rtpsender.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 40 | #include "pc/sctputils.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 41 | #include "pc/sdputils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 42 | #include "pc/streamcollection.h" |
| 43 | #include "pc/videocapturertracksource.h" |
| 44 | #include "pc/videotrack.h" |
| 45 | #include "rtc_base/bind.h" |
| 46 | #include "rtc_base/checks.h" |
| 47 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 48 | #include "rtc_base/numerics/safe_conversions.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" |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 54 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 56 | using cricket::ContentInfo; |
| 57 | using cricket::ContentInfos; |
| 58 | using cricket::MediaContentDescription; |
| 59 | using cricket::SessionDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 60 | using cricket::MediaProtocolType; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 61 | using cricket::TransportInfo; |
| 62 | |
| 63 | using cricket::LOCAL_PORT_TYPE; |
| 64 | using cricket::STUN_PORT_TYPE; |
| 65 | using cricket::RELAY_PORT_TYPE; |
| 66 | using cricket::PRFLX_PORT_TYPE; |
| 67 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 68 | namespace webrtc { |
| 69 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 70 | // Error messages |
| 71 | const char kBundleWithoutRtcpMux[] = |
| 72 | "rtcp-mux must be enabled when BUNDLE " |
| 73 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 74 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 75 | const char kInvalidSdp[] = "Invalid session description."; |
| 76 | const char kMlineMismatchInAnswer[] = |
| 77 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 78 | "answer."; |
| 79 | const char kMlineMismatchInSubsequentOffer[] = |
| 80 | "The order of m-lines in subsequent offer doesn't match order from " |
| 81 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 82 | const char kSdpWithoutDtlsFingerprint[] = |
| 83 | "Called with SDP without DTLS fingerprint."; |
| 84 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 85 | const char kSdpWithoutIceUfragPwd[] = |
| 86 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 87 | const char kSessionError[] = "Session error code: "; |
| 88 | const char kSessionErrorDesc[] = "Session error description: "; |
| 89 | const char kDtlsSrtpSetupFailureRtp[] = |
| 90 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 91 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 92 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
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 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 103 | enum { |
| 104 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 105 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 106 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 107 | MSG_GETSTATS, |
| 108 | MSG_FREE_DATACHANNELS, |
| 109 | MSG_REPORT_USAGE_PATTERN, |
| 110 | }; |
| 111 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 112 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 113 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 114 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 115 | explicit SetSessionDescriptionMsg( |
| 116 | webrtc::SetSessionDescriptionObserver* observer) |
| 117 | : observer(observer) {} |
| 118 | |
| 119 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 120 | RTCError error; |
| 121 | }; |
| 122 | |
| 123 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 124 | explicit CreateSessionDescriptionMsg( |
| 125 | webrtc::CreateSessionDescriptionObserver* observer) |
| 126 | : observer(observer) {} |
| 127 | |
| 128 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 129 | RTCError error; |
| 130 | }; |
| 131 | |
| 132 | struct GetStatsMsg : public rtc::MessageData { |
| 133 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 134 | webrtc::MediaStreamTrackInterface* track) |
| 135 | : observer(observer), track(track) {} |
| 136 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 137 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 138 | }; |
| 139 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 140 | // Check if we can send |new_stream| on a PeerConnection. |
| 141 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 142 | webrtc::MediaStreamInterface* new_stream) { |
| 143 | if (!new_stream || !current_streams) { |
| 144 | return false; |
| 145 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 146 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 147 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 148 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 154 | // If the direction is "recvonly" or "inactive", treat the description |
| 155 | // as containing no streams. |
| 156 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 157 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 158 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 159 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 160 | ? desc->streams() |
| 161 | : std::vector<cricket::StreamParams>(); |
| 162 | } |
| 163 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 164 | bool IsValidOfferToReceiveMedia(int value) { |
| 165 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 166 | return (value >= Options::kUndefined) && |
| 167 | (value <= Options::kMaxOfferToReceiveMedia); |
| 168 | } |
| 169 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 170 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 171 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 172 | const std::vector<rtc::scoped_refptr< |
| 173 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 174 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 175 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 176 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 177 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 178 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 179 | if (audio_media_description_options) { |
| 180 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 181 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 182 | } |
| 183 | } else { |
| 184 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 185 | if (video_media_description_options) { |
| 186 | video_media_description_options->AddVideoSender( |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 187 | sender->id(), sender->internal()->stream_ids(), |
| 188 | num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 191 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 192 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 193 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 194 | // Add options to |session_options| from |rtp_data_channels|. |
| 195 | void AddRtpDataChannelOptions( |
| 196 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 197 | rtp_data_channels, |
| 198 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 199 | if (!data_media_description_options) { |
| 200 | return; |
| 201 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 202 | // Check for data channels. |
| 203 | for (const auto& kv : rtp_data_channels) { |
| 204 | const DataChannel* channel = kv.second; |
| 205 | if (channel->state() == DataChannel::kConnecting || |
| 206 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 207 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 208 | // the data channel's label. |
| 209 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 210 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 215 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 216 | PeerConnectionInterface::IceTransportsType type) { |
| 217 | switch (type) { |
| 218 | case PeerConnectionInterface::kNone: |
| 219 | return cricket::CF_NONE; |
| 220 | case PeerConnectionInterface::kRelay: |
| 221 | return cricket::CF_RELAY; |
| 222 | case PeerConnectionInterface::kNoHost: |
| 223 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 224 | case PeerConnectionInterface::kAll: |
| 225 | return cricket::CF_ALL; |
| 226 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 227 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 228 | } |
| 229 | return cricket::CF_NONE; |
| 230 | } |
| 231 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 232 | // Helper to set an error and return from a method. |
| 233 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 234 | if (error) { |
| 235 | error->set_type(type); |
| 236 | } |
| 237 | return type == webrtc::RTCErrorType::NONE; |
| 238 | } |
| 239 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 240 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 241 | bool ok = error.ok(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 242 | if (error_out) { |
| 243 | *error_out = std::move(error); |
| 244 | } |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 245 | return ok; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 248 | std::string GetSignalingStateString( |
| 249 | PeerConnectionInterface::SignalingState state) { |
| 250 | switch (state) { |
| 251 | case PeerConnectionInterface::kStable: |
| 252 | return "kStable"; |
| 253 | case PeerConnectionInterface::kHaveLocalOffer: |
| 254 | return "kHaveLocalOffer"; |
| 255 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 256 | return "kHavePrAnswer"; |
| 257 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 258 | return "kHaveRemoteOffer"; |
| 259 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 260 | return "kHaveRemotePrAnswer"; |
| 261 | case PeerConnectionInterface::kClosed: |
| 262 | return "kClosed"; |
| 263 | } |
| 264 | RTC_NOTREACHED(); |
| 265 | return ""; |
| 266 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 267 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 268 | IceCandidatePairType GetIceCandidatePairCounter( |
| 269 | const cricket::Candidate& local, |
| 270 | const cricket::Candidate& remote) { |
| 271 | const auto& l = local.type(); |
| 272 | const auto& r = remote.type(); |
| 273 | const auto& host = LOCAL_PORT_TYPE; |
| 274 | const auto& srflx = STUN_PORT_TYPE; |
| 275 | const auto& relay = RELAY_PORT_TYPE; |
| 276 | const auto& prflx = PRFLX_PORT_TYPE; |
| 277 | if (l == host && r == host) { |
| 278 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 279 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 280 | if (local_private) { |
| 281 | if (remote_private) { |
| 282 | return kIceCandidatePairHostPrivateHostPrivate; |
| 283 | } else { |
| 284 | return kIceCandidatePairHostPrivateHostPublic; |
| 285 | } |
| 286 | } else { |
| 287 | if (remote_private) { |
| 288 | return kIceCandidatePairHostPublicHostPrivate; |
| 289 | } else { |
| 290 | return kIceCandidatePairHostPublicHostPublic; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | if (l == host && r == srflx) |
| 295 | return kIceCandidatePairHostSrflx; |
| 296 | if (l == host && r == relay) |
| 297 | return kIceCandidatePairHostRelay; |
| 298 | if (l == host && r == prflx) |
| 299 | return kIceCandidatePairHostPrflx; |
| 300 | if (l == srflx && r == host) |
| 301 | return kIceCandidatePairSrflxHost; |
| 302 | if (l == srflx && r == srflx) |
| 303 | return kIceCandidatePairSrflxSrflx; |
| 304 | if (l == srflx && r == relay) |
| 305 | return kIceCandidatePairSrflxRelay; |
| 306 | if (l == srflx && r == prflx) |
| 307 | return kIceCandidatePairSrflxPrflx; |
| 308 | if (l == relay && r == host) |
| 309 | return kIceCandidatePairRelayHost; |
| 310 | if (l == relay && r == srflx) |
| 311 | return kIceCandidatePairRelaySrflx; |
| 312 | if (l == relay && r == relay) |
| 313 | return kIceCandidatePairRelayRelay; |
| 314 | if (l == relay && r == prflx) |
| 315 | return kIceCandidatePairRelayPrflx; |
| 316 | if (l == prflx && r == host) |
| 317 | return kIceCandidatePairPrflxHost; |
| 318 | if (l == prflx && r == srflx) |
| 319 | return kIceCandidatePairPrflxSrflx; |
| 320 | if (l == prflx && r == relay) |
| 321 | return kIceCandidatePairPrflxRelay; |
| 322 | return kIceCandidatePairMax; |
| 323 | } |
| 324 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 325 | // Logic to decide if an m= section can be recycled. This means that the new |
| 326 | // m= section is not rejected, but the old local or remote m= section is |
| 327 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 328 | // of the old remote and old local descriptions in no particular order. |
| 329 | // We need to check both the old local and remote because either |
| 330 | // could be the most current from the latest negotation. |
| 331 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 332 | const ContentInfo& content, |
| 333 | const ContentInfo* old_content_one, |
| 334 | const ContentInfo* old_content_two) { |
| 335 | return type == SdpType::kOffer && !content.rejected && |
| 336 | ((old_content_one && old_content_one->rejected) || |
| 337 | (old_content_two && old_content_two->rejected)); |
| 338 | } |
| 339 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 340 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 341 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 342 | // less than |current_desc|. In the case of checking an answer's |
| 343 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 344 | // local or remote. In the case of checking an offer's |new_desc| we |
| 345 | // check against the local and remote descriptions stored from the last |
| 346 | // negotiation, because either of these could be the most up to date for |
| 347 | // possible rejected m sections. These are the |current_desc| and |
| 348 | // |secondary_current_desc|. |
| 349 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 350 | const SessionDescription* secondary_current_desc, |
| 351 | const SessionDescription& new_desc, |
| 352 | const SdpType type) { |
| 353 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 354 | return false; |
| 355 | } |
| 356 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 357 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 358 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 359 | if (secondary_current_desc && |
| 360 | i < secondary_current_desc->contents().size()) { |
| 361 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 362 | } |
| 363 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 364 | ¤t_desc.contents()[i], |
| 365 | secondary_content_info)) { |
| 366 | // For new offer descriptions, if the media section can be recycled, it's |
| 367 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 368 | continue; |
| 369 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 370 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 374 | new_desc.contents()[i].media_description(); |
| 375 | const MediaContentDescription* current_desc_mdesc = |
| 376 | current_desc.contents()[i].media_description(); |
| 377 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | } |
| 381 | return true; |
| 382 | } |
| 383 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 384 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 385 | const SessionDescription& desc2) { |
| 386 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 389 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 390 | cricket::MediaType media_type) { |
| 391 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 392 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 393 | static const std::map<std::pair<KeyExchangeProtocolType, cricket::MediaType>, |
| 394 | KeyExchangeProtocolMedia> |
| 395 | proto_media_counter_map = { |
| 396 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO}, |
| 397 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 398 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO}, |
| 399 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 400 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA}, |
| 401 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 402 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO}, |
| 403 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 404 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO}, |
| 405 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 406 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA}, |
| 407 | kEnumCounterKeyProtocolMediaTypeSdesData}}; |
| 408 | |
| 409 | auto it = proto_media_counter_map.find({protocol_type, media_type}); |
| 410 | if (it != proto_media_counter_map.end()) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 411 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 412 | it->second, kEnumCounterKeyProtocolMediaTypeMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 416 | void NoteAddIceCandidateResult(int result) { |
| 417 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 418 | kAddIceCandidateMax); |
| 419 | } |
| 420 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 421 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 422 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 423 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 424 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 425 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 426 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 427 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 428 | const cricket::ContentGroup* bundle = |
| 429 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 430 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 431 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 432 | continue; |
| 433 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 434 | // Note what media is used with each crypto protocol, for all sections. |
| 435 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 436 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 437 | content_info.media_description()->type()); |
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 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 603 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 604 | std::string output = "streams=["; |
| 605 | const char* separator = ""; |
| 606 | for (const auto& stream_id : stream_ids) { |
| 607 | output.append(separator).append(stream_id); |
| 608 | separator = ", "; |
| 609 | } |
| 610 | output.append("]"); |
| 611 | return output; |
| 612 | } |
| 613 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 614 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 615 | int rtc_configuration_parameter) { |
| 616 | if (rtc_configuration_parameter == |
| 617 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 618 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 619 | } |
| 620 | return rtc_configuration_parameter; |
| 621 | } |
| 622 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 623 | } // namespace |
| 624 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 625 | // Upon completion, posts a task to execute the callback of the |
| 626 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 627 | // point, the state of the peer connection might no longer reflect the effects |
| 628 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 629 | // modified during the post. |
| 630 | // TODO(hbos): Remove this class once we remove the version of |
| 631 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 632 | // SetSessionDescriptionObserver as an argument. |
| 633 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 634 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 635 | public: |
| 636 | SetRemoteDescriptionObserverAdapter( |
| 637 | rtc::scoped_refptr<PeerConnection> pc, |
| 638 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 639 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 640 | |
| 641 | // SetRemoteDescriptionObserverInterface implementation. |
| 642 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 643 | if (error.ok()) |
| 644 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 645 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 646 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | private: |
| 650 | rtc::scoped_refptr<PeerConnection> pc_; |
| 651 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 652 | }; |
| 653 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 654 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 655 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 656 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 657 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 658 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 659 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 660 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 661 | BundlePolicy bundle_policy; |
| 662 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 663 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 664 | int ice_candidate_pool_size; |
| 665 | bool disable_ipv6; |
| 666 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 667 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 668 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 669 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 670 | absl::optional<int> screencast_min_bitrate; |
| 671 | absl::optional<bool> combined_audio_video_bwe; |
| 672 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 673 | TcpCandidatePolicy tcp_candidate_policy; |
| 674 | CandidateNetworkPolicy candidate_network_policy; |
| 675 | int audio_jitter_buffer_max_packets; |
| 676 | bool audio_jitter_buffer_fast_accelerate; |
| 677 | int ice_connection_receiving_timeout; |
| 678 | int ice_backup_candidate_pair_ping_interval; |
| 679 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 680 | bool prioritize_most_likely_ice_candidate_pairs; |
| 681 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 682 | bool prune_turn_ports; |
| 683 | bool presume_writable_when_fully_relayed; |
| 684 | bool enable_ice_renomination; |
| 685 | bool redetermine_role_on_ice_restart; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 686 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 687 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 688 | absl::optional<int> ice_check_min_interval; |
| 689 | absl::optional<int> ice_unwritable_timeout; |
| 690 | absl::optional<int> ice_unwritable_min_checks; |
| 691 | absl::optional<int> stun_candidate_keepalive_interval; |
| 692 | absl::optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 693 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 694 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 695 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 696 | bool active_reset_srtp_params; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 697 | }; |
| 698 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 699 | "Did you add something to RTCConfiguration and forget to " |
| 700 | "update operator==?"); |
| 701 | return type == o.type && servers == o.servers && |
| 702 | bundle_policy == o.bundle_policy && |
| 703 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 704 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 705 | candidate_network_policy == o.candidate_network_policy && |
| 706 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 707 | audio_jitter_buffer_fast_accelerate == |
| 708 | o.audio_jitter_buffer_fast_accelerate && |
| 709 | ice_connection_receiving_timeout == |
| 710 | o.ice_connection_receiving_timeout && |
| 711 | ice_backup_candidate_pair_ping_interval == |
| 712 | o.ice_backup_candidate_pair_ping_interval && |
| 713 | continual_gathering_policy == o.continual_gathering_policy && |
| 714 | certificates == o.certificates && |
| 715 | prioritize_most_likely_ice_candidate_pairs == |
| 716 | o.prioritize_most_likely_ice_candidate_pairs && |
| 717 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 718 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 719 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 720 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 721 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 722 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 723 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 724 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 725 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 726 | prune_turn_ports == o.prune_turn_ports && |
| 727 | presume_writable_when_fully_relayed == |
| 728 | o.presume_writable_when_fully_relayed && |
| 729 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 730 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 731 | ice_check_interval_strong_connectivity == |
| 732 | o.ice_check_interval_strong_connectivity && |
| 733 | ice_check_interval_weak_connectivity == |
| 734 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 735 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 736 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 737 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 738 | stun_candidate_keepalive_interval == |
| 739 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 740 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 741 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 742 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 743 | network_preference == o.network_preference && |
| 744 | active_reset_srtp_params == o.active_reset_srtp_params; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 748 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 749 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 750 | } |
| 751 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 752 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 753 | std::string GenerateRtcpCname() { |
| 754 | std::string cname; |
| 755 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 756 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 757 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 758 | } |
| 759 | return cname; |
| 760 | } |
| 761 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 762 | bool ValidateOfferAnswerOptions( |
| 763 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 764 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 765 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 766 | } |
| 767 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 768 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 769 | // m= sections (in other words, nothing that involves a map/array). |
| 770 | void ExtractSharedMediaSessionOptions( |
| 771 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 772 | cricket::MediaSessionOptions* session_options) { |
| 773 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 774 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 775 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 776 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 777 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 778 | std::unique_ptr<RtcEventLog> event_log, |
| 779 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 780 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 781 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 782 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 783 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 784 | remote_streams_(StreamCollection::Create()), |
| 785 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 786 | |
| 787 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 788 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 789 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 790 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 791 | // Need to stop transceivers before destroying the stats collector because |
| 792 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 793 | // stopping. |
| 794 | for (auto transceiver : transceivers_) { |
| 795 | transceiver->Stop(); |
| 796 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 797 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 798 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 799 | if (stats_collector_) { |
| 800 | stats_collector_->WaitForPendingRequest(); |
| 801 | stats_collector_ = nullptr; |
| 802 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 803 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 804 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 805 | // the last stats request can still read from the channels. |
| 806 | DestroyAllChannels(); |
| 807 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 808 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 809 | |
| 810 | webrtc_session_desc_factory_.reset(); |
| 811 | sctp_invoker_.reset(); |
| 812 | sctp_factory_.reset(); |
| 813 | transport_controller_.reset(); |
| 814 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 815 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 816 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 817 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 818 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 819 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 820 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 821 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 822 | event_log_.reset(); |
| 823 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 826 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 827 | // Destroy video channels first since they may have a pointer to a voice |
| 828 | // channel. |
| 829 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 830 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 831 | DestroyTransceiverChannel(transceiver); |
| 832 | } |
| 833 | } |
| 834 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 835 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 836 | DestroyTransceiverChannel(transceiver); |
| 837 | } |
| 838 | } |
| 839 | DestroyDataChannel(); |
| 840 | } |
| 841 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 842 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 843 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 844 | PeerConnectionDependencies dependencies) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 845 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 846 | |
| 847 | RTCError config_error = ValidateConfiguration(configuration); |
| 848 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 849 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 850 | return false; |
| 851 | } |
| 852 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 853 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 854 | RTC_LOG(LS_ERROR) |
| 855 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 856 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 857 | return false; |
| 858 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 859 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 860 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 861 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 862 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 863 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 864 | return false; |
| 865 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 866 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 867 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 868 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 869 | port_allocator_ = std::move(dependencies.allocator); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 870 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 871 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 872 | cricket::ServerAddresses stun_servers; |
| 873 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 874 | |
| 875 | RTCErrorType parse_error = |
| 876 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 877 | if (parse_error != RTCErrorType::NONE) { |
| 878 | return false; |
| 879 | } |
| 880 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 881 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 882 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 883 | if (!network_thread()->Invoke<bool>( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 884 | RTC_FROM_HERE, |
| 885 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
| 886 | stun_servers, turn_servers, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 887 | return false; |
| 888 | } |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 889 | // If initialization was successful, note if STUN or TURN servers |
| 890 | // were supplied. |
| 891 | if (!stun_servers.empty()) { |
| 892 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 893 | } |
| 894 | if (!turn_servers.empty()) { |
| 895 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 896 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 897 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 898 | // Send information about IPv4/IPv6 status. |
| 899 | PeerConnectionAddressFamilyCounter address_family; |
| 900 | if (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
| 901 | address_family = kPeerConnection_IPv6; |
| 902 | } else { |
| 903 | address_family = kPeerConnection_IPv4; |
| 904 | } |
| 905 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 906 | kPeerConnectionAddressFamilyCounter_Max); |
| 907 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 908 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 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); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 915 | JsepTransportController::Config config; |
| 916 | config.redetermine_role_on_ice_restart = |
| 917 | configuration.redetermine_role_on_ice_restart; |
| 918 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 919 | config.disable_encryption = options.disable_encryption; |
| 920 | config.bundle_policy = configuration.bundle_policy; |
| 921 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
| 922 | config.crypto_options = options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 923 | config.transport_observer = this; |
Qingsi Wang | 7685e86 | 2018-06-11 20:15:46 -0700 | [diff] [blame] | 924 | config.event_log = event_log_.get(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 925 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 926 | config.enable_external_auth = true; |
| 927 | #endif |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 928 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 929 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 930 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 931 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 932 | transport_controller_->SignalIceConnectionState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 933 | this, &PeerConnection::OnTransportControllerConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 934 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 935 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 936 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 937 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 938 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 939 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 940 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 941 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 942 | |
| 943 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 944 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 945 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 946 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 947 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 948 | configuration_ = configuration; |
| 949 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 950 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 951 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 952 | if (!configuration.certificates.empty()) { |
| 953 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 954 | // just picking the first one. The decision should be made based on the DTLS |
| 955 | // handshake. The DTLS negotiations need to know about all certificates. |
| 956 | certificate = configuration.certificates[0]; |
| 957 | } |
| 958 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 959 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 960 | |
| 961 | if (options.disable_encryption) { |
| 962 | dtls_enabled_ = false; |
| 963 | } else { |
| 964 | // Enable DTLS by default if we have an identity store or a certificate. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 965 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 966 | // |configuration| can override the default |dtls_enabled_| value. |
| 967 | if (configuration.enable_dtls_srtp) { |
| 968 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 973 | // It takes precendence over the disable_sctp_data_channels |
| 974 | // PeerConnectionFactoryInterface::Options. |
| 975 | if (configuration.enable_rtp_data_channel) { |
| 976 | data_channel_type_ = cricket::DCT_RTP; |
| 977 | } else { |
| 978 | // DTLS has to be enabled to use SCTP. |
| 979 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 980 | data_channel_type_ = cricket::DCT_SCTP; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | video_options_.screencast_min_bitrate_kbps = |
| 985 | configuration.screencast_min_bitrate; |
| 986 | audio_options_.combined_audio_video_bwe = |
| 987 | configuration.combined_audio_video_bwe; |
| 988 | |
| 989 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 990 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 991 | |
| 992 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 993 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 994 | |
| 995 | // Whether the certificate generator/certificate is null or not determines |
| 996 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 997 | // the right instructions by clearing the variables if needed. |
| 998 | if (!dtls_enabled_) { |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 999 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1000 | certificate = nullptr; |
| 1001 | } else if (certificate) { |
| 1002 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1003 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1007 | signaling_thread(), channel_manager(), this, session_id(), |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1008 | std::move(dependencies.cert_generator), certificate)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1009 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1010 | this, &PeerConnection::OnCertificateReady); |
| 1011 | |
| 1012 | if (options.disable_encryption) { |
| 1013 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1014 | } |
| 1015 | |
| 1016 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 1017 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1018 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1019 | // Add default audio/video transceivers for Plan B SDP. |
| 1020 | if (!IsUnifiedPlan()) { |
| 1021 | transceivers_.push_back( |
| 1022 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1023 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1024 | transceivers_.push_back( |
| 1025 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1026 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1027 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 1028 | int delay_ms = |
| 1029 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 1030 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1031 | MSG_REPORT_USAGE_PATTERN, nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1032 | return true; |
| 1033 | } |
| 1034 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1035 | RTCError PeerConnection::ValidateConfiguration( |
| 1036 | const RTCConfiguration& config) const { |
| 1037 | if (config.ice_regather_interval_range && |
| 1038 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1039 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1040 | "ice_regather_interval_range specified but continual " |
| 1041 | "gathering policy is GATHER_ONCE"); |
| 1042 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1043 | auto result = |
| 1044 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1045 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1048 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1049 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1050 | "Plan SdpSemantics. Please use GetSenders " |
| 1051 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1052 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1055 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1056 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1057 | "Plan SdpSemantics. Please use GetReceivers " |
| 1058 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1059 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1062 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1063 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1064 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1065 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1066 | if (IsClosed()) { |
| 1067 | return false; |
| 1068 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1069 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1070 | return false; |
| 1071 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1072 | |
| 1073 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1074 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1075 | observer->SignalAudioTrackAdded.connect(this, |
| 1076 | &PeerConnection::OnAudioTrackAdded); |
| 1077 | observer->SignalAudioTrackRemoved.connect( |
| 1078 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1079 | observer->SignalVideoTrackAdded.connect(this, |
| 1080 | &PeerConnection::OnVideoTrackAdded); |
| 1081 | observer->SignalVideoTrackRemoved.connect( |
| 1082 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1083 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1084 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1085 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1086 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1087 | } |
| 1088 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1089 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1092 | stats_->AddStream(local_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1093 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1094 | return true; |
| 1095 | } |
| 1096 | |
| 1097 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1098 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1099 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1100 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1101 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1102 | if (!IsClosed()) { |
| 1103 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1104 | RemoveAudioTrack(track.get(), local_stream); |
| 1105 | } |
| 1106 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1107 | RemoveVideoTrack(track.get(), local_stream); |
| 1108 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1109 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1110 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1111 | stream_observers_.erase( |
| 1112 | std::remove_if( |
| 1113 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1114 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1115 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1116 | }), |
| 1117 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1118 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1119 | if (IsClosed()) { |
| 1120 | return; |
| 1121 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1122 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1125 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1126 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1127 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1128 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1129 | if (!track) { |
| 1130 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1131 | } |
| 1132 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1133 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1134 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1135 | "Track has invalid kind: " + track->kind()); |
| 1136 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1137 | if (IsClosed()) { |
| 1138 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1139 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1140 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1141 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1142 | LOG_AND_RETURN_ERROR( |
| 1143 | RTCErrorType::INVALID_PARAMETER, |
| 1144 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1145 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1146 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1147 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1148 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1149 | if (sender_or_error.ok()) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1150 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1151 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1152 | } |
| 1153 | return sender_or_error; |
| 1154 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1155 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1156 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1157 | PeerConnection::AddTrackPlanB( |
| 1158 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1159 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1160 | if (stream_ids.size() > 1u) { |
| 1161 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1162 | "AddTrack with more than one stream is not " |
| 1163 | "supported with Plan B semantics."); |
| 1164 | } |
| 1165 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1166 | if (adjusted_stream_ids.empty()) { |
| 1167 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1168 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1169 | cricket::MediaType media_type = |
| 1170 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1171 | ? cricket::MEDIA_TYPE_AUDIO |
| 1172 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1173 | auto new_sender = |
| 1174 | CreateSender(media_type, track->id(), track, adjusted_stream_ids); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1175 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1176 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1177 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1178 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1179 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1180 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1181 | if (sender_info) { |
| 1182 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1183 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1184 | } else { |
| 1185 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1186 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1187 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1188 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1189 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1190 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1191 | if (sender_info) { |
| 1192 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1193 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1194 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1195 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1196 | } |
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 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1199 | PeerConnection::AddTrackUnifiedPlan( |
| 1200 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1201 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1202 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1203 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1204 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1205 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1206 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1207 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1208 | transceiver->internal()->set_direction( |
| 1209 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1210 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1211 | transceiver->internal()->set_direction( |
| 1212 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1213 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1214 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1215 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1216 | } else { |
| 1217 | cricket::MediaType media_type = |
| 1218 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1219 | ? cricket::MEDIA_TYPE_AUDIO |
| 1220 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1221 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1222 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1223 | std::string sender_id = track->id(); |
| 1224 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1225 | // This can happen if this is the second time AddTrack has created a sender |
| 1226 | // for this track. |
| 1227 | if (FindSenderById(sender_id)) { |
| 1228 | sender_id = rtc::CreateRandomUuid(); |
| 1229 | } |
| 1230 | auto sender = CreateSender(media_type, sender_id, track, stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1231 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1232 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1233 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1234 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1235 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1236 | return transceiver->sender(); |
| 1237 | } |
| 1238 | |
| 1239 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1240 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1241 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1242 | RTC_DCHECK(track); |
| 1243 | for (auto transceiver : transceivers_) { |
| 1244 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1245 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1246 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1247 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1248 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1249 | return transceiver; |
| 1250 | } |
| 1251 | } |
| 1252 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1256 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1257 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1258 | } |
| 1259 | |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1260 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1261 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1262 | if (!sender) { |
| 1263 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1264 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1265 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1266 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1267 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1268 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1269 | if (IsUnifiedPlan()) { |
| 1270 | auto transceiver = FindTransceiverBySender(sender); |
| 1271 | if (!transceiver || !sender->track()) { |
| 1272 | return RTCError::OK(); |
| 1273 | } |
| 1274 | sender->SetTrack(nullptr); |
| 1275 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1276 | transceiver->internal()->set_direction( |
| 1277 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1278 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1279 | transceiver->internal()->set_direction( |
| 1280 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1281 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1282 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1283 | bool removed; |
| 1284 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1285 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1286 | } else { |
| 1287 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1288 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1289 | } |
| 1290 | if (!removed) { |
| 1291 | LOG_AND_RETURN_ERROR( |
| 1292 | RTCErrorType::INVALID_PARAMETER, |
| 1293 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1294 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1295 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1296 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1297 | return RTCError::OK(); |
| 1298 | } |
| 1299 | |
| 1300 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1301 | PeerConnection::FindTransceiverBySender( |
| 1302 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1303 | for (auto transceiver : transceivers_) { |
| 1304 | if (transceiver->sender() == sender) { |
| 1305 | return transceiver; |
| 1306 | } |
| 1307 | } |
| 1308 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1311 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1312 | PeerConnection::AddTransceiver( |
| 1313 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1314 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1315 | } |
| 1316 | |
| 1317 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1318 | PeerConnection::AddTransceiver( |
| 1319 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1320 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1321 | RTC_CHECK(IsUnifiedPlan()) |
| 1322 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1323 | if (!track) { |
| 1324 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1325 | } |
| 1326 | cricket::MediaType media_type; |
| 1327 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1328 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1329 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1330 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1331 | } else { |
| 1332 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1333 | "Track kind is not audio or video"); |
| 1334 | } |
| 1335 | return AddTransceiver(media_type, track, init); |
| 1336 | } |
| 1337 | |
| 1338 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1339 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1340 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1341 | } |
| 1342 | |
| 1343 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1344 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1345 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1346 | RTC_CHECK(IsUnifiedPlan()) |
| 1347 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1348 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1349 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1350 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1351 | "media type is not audio or video"); |
| 1352 | } |
| 1353 | return AddTransceiver(media_type, nullptr, init); |
| 1354 | } |
| 1355 | |
| 1356 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1357 | PeerConnection::AddTransceiver( |
| 1358 | cricket::MediaType media_type, |
| 1359 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1360 | const RtpTransceiverInit& init, |
| 1361 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1362 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1363 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1364 | if (track) { |
| 1365 | RTC_DCHECK_EQ(media_type, |
| 1366 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1367 | ? cricket::MEDIA_TYPE_AUDIO |
| 1368 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1369 | } |
| 1370 | |
| 1371 | // TODO(bugs.webrtc.org/7600): Verify init. |
| 1372 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1373 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1374 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1375 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1376 | // that sender ID is already in use. |
| 1377 | std::string sender_id = |
| 1378 | (track && !FindSenderById(track->id()) ? track->id() |
| 1379 | : rtc::CreateRandomUuid()); |
| 1380 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1381 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1382 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1383 | transceiver->internal()->set_direction(init.direction); |
| 1384 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1385 | if (fire_callback) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1386 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1387 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1388 | |
| 1389 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1390 | } |
| 1391 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1392 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1393 | PeerConnection::CreateSender( |
| 1394 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1395 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1396 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1397 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1398 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1399 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1400 | RTC_DCHECK(!track || |
| 1401 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1402 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1403 | signaling_thread(), |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1404 | new AudioRtpSender(worker_thread(), id, stats_.get())); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1405 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1406 | } else { |
| 1407 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1408 | RTC_DCHECK(!track || |
| 1409 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1410 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1411 | signaling_thread(), new VideoRtpSender(worker_thread(), id)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1412 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1413 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1414 | bool set_track_succeeded = sender->SetTrack(track); |
| 1415 | RTC_DCHECK(set_track_succeeded); |
| 1416 | sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1417 | return sender; |
| 1418 | } |
| 1419 | |
| 1420 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1421 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1422 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1423 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1424 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1425 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1426 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1427 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1428 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1429 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1430 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1431 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1432 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1433 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1434 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1435 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1436 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1437 | return receiver; |
| 1438 | } |
| 1439 | |
| 1440 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1441 | PeerConnection::CreateAndAddTransceiver( |
| 1442 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1443 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1444 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1445 | // Ensure that the new sender does not have an ID that is already in use by |
| 1446 | // another sender. |
| 1447 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1448 | // could be invalid, but should not cause a crash). |
| 1449 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1450 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1451 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1452 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1453 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1454 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1455 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1458 | void PeerConnection::OnNegotiationNeeded() { |
| 1459 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1460 | RTC_DCHECK(!IsClosed()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1461 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1464 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1465 | const std::string& kind, |
| 1466 | const std::string& stream_id) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1467 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1468 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1469 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1470 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1471 | if (IsClosed()) { |
| 1472 | return nullptr; |
| 1473 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1474 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1475 | // Internally we need to have one stream with Plan B semantics, so we |
| 1476 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1477 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1478 | if (stream_id.empty()) { |
| 1479 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1480 | RTC_LOG(LS_INFO) |
| 1481 | << "No stream_id specified for sender. Generated stream ID: " |
| 1482 | << stream_ids[0]; |
| 1483 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1484 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1485 | } |
| 1486 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1487 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1488 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1489 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1490 | auto* audio_sender = new AudioRtpSender( |
| 1491 | worker_thread(), rtc::CreateRandomUuid(), stats_.get()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1492 | audio_sender->SetVoiceMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1493 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1494 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1495 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1496 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1497 | auto* video_sender = |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1498 | new VideoRtpSender(worker_thread(), rtc::CreateRandomUuid()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1499 | video_sender->SetVideoMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1500 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1501 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1502 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1503 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1504 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1505 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1506 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1507 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1508 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1509 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1512 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1513 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1514 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1515 | for (auto sender : GetSendersInternal()) { |
| 1516 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1517 | } |
| 1518 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1521 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1522 | PeerConnection::GetSendersInternal() const { |
| 1523 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1524 | all_senders; |
| 1525 | for (auto transceiver : transceivers_) { |
| 1526 | auto senders = transceiver->internal()->senders(); |
| 1527 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1528 | } |
| 1529 | return all_senders; |
| 1530 | } |
| 1531 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1532 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1533 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1534 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1535 | for (const auto& receiver : GetReceiversInternal()) { |
| 1536 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1537 | } |
| 1538 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1541 | std::vector< |
| 1542 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1543 | PeerConnection::GetReceiversInternal() const { |
| 1544 | std::vector< |
| 1545 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1546 | all_receivers; |
| 1547 | for (auto transceiver : transceivers_) { |
| 1548 | auto receivers = transceiver->internal()->receivers(); |
| 1549 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1550 | receivers.end()); |
| 1551 | } |
| 1552 | return all_receivers; |
| 1553 | } |
| 1554 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1555 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1556 | PeerConnection::GetTransceivers() const { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1557 | RTC_CHECK(IsUnifiedPlan()) |
| 1558 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1559 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
| 1560 | for (auto transceiver : transceivers_) { |
| 1561 | all_transceivers.push_back(transceiver); |
| 1562 | } |
| 1563 | return all_transceivers; |
| 1564 | } |
| 1565 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1566 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1567 | MediaStreamTrackInterface* track, |
| 1568 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1569 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1570 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1571 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1572 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1573 | return false; |
| 1574 | } |
| 1575 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1576 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1577 | // The StatsCollector is used to tell if a track is valid because it may |
| 1578 | // remember tracks that the PeerConnection previously removed. |
| 1579 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1580 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1581 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1582 | return false; |
| 1583 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 1584 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 1585 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1586 | return true; |
| 1587 | } |
| 1588 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1589 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1590 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1591 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1592 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1593 | stats_collector_->GetStatsReport(callback); |
| 1594 | } |
| 1595 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1596 | void PeerConnection::GetStats( |
| 1597 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1598 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1599 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1600 | RTC_DCHECK(callback); |
| 1601 | RTC_DCHECK(stats_collector_); |
| 1602 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1603 | if (selector) { |
| 1604 | for (const auto& proxy_transceiver : transceivers_) { |
| 1605 | for (const auto& proxy_sender : |
| 1606 | proxy_transceiver->internal()->senders()) { |
| 1607 | if (proxy_sender == selector) { |
| 1608 | internal_sender = proxy_sender->internal(); |
| 1609 | break; |
| 1610 | } |
| 1611 | } |
| 1612 | if (internal_sender) |
| 1613 | break; |
| 1614 | } |
| 1615 | } |
| 1616 | // If there is no |internal_sender|Â then |selector| is either null or does not |
| 1617 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1618 | // PeerConnection). This means that "all the stats objects representing the |
| 1619 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1620 | // produces an empty stats report. |
| 1621 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1622 | } |
| 1623 | |
| 1624 | void PeerConnection::GetStats( |
| 1625 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1626 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1627 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1628 | RTC_DCHECK(callback); |
| 1629 | RTC_DCHECK(stats_collector_); |
| 1630 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1631 | if (selector) { |
| 1632 | for (const auto& proxy_transceiver : transceivers_) { |
| 1633 | for (const auto& proxy_receiver : |
| 1634 | proxy_transceiver->internal()->receivers()) { |
| 1635 | if (proxy_receiver == selector) { |
| 1636 | internal_receiver = proxy_receiver->internal(); |
| 1637 | break; |
| 1638 | } |
| 1639 | } |
| 1640 | if (internal_receiver) |
| 1641 | break; |
| 1642 | } |
| 1643 | } |
| 1644 | // If there is no |internal_receiver|Â then |selector| is either null or does |
| 1645 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1646 | // the PeerConnection). This means that "all the stats objects representing |
| 1647 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1648 | // selector produces an empty stats report. |
| 1649 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1650 | } |
| 1651 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1652 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1653 | return signaling_state_; |
| 1654 | } |
| 1655 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1656 | PeerConnectionInterface::IceConnectionState |
| 1657 | PeerConnection::ice_connection_state() { |
| 1658 | return ice_connection_state_; |
| 1659 | } |
| 1660 | |
| 1661 | PeerConnectionInterface::IceGatheringState |
| 1662 | PeerConnection::ice_gathering_state() { |
| 1663 | return ice_gathering_state_; |
| 1664 | } |
| 1665 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1666 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1667 | const std::string& label, |
| 1668 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1669 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1670 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1671 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1672 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1673 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1674 | if (config) { |
| 1675 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1676 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1677 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1678 | InternalCreateDataChannel(label, internal_config.get())); |
| 1679 | if (!channel.get()) { |
| 1680 | return nullptr; |
| 1681 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1682 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1683 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1684 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1685 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1686 | Observer()->OnRenegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1687 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1688 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1689 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1690 | } |
| 1691 | |
| 1692 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1693 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1694 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1695 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1696 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1697 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1698 | return; |
| 1699 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1700 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1701 | if (IsClosed()) { |
| 1702 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1703 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1704 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1705 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1706 | return; |
| 1707 | } |
| 1708 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1709 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1710 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1711 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1712 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1713 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1714 | return; |
| 1715 | } |
| 1716 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1717 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1718 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1719 | if (IsUnifiedPlan()) { |
| 1720 | RTCError error = HandleLegacyOfferOptions(options); |
| 1721 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1722 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1723 | return; |
| 1724 | } |
| 1725 | } |
| 1726 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1727 | cricket::MediaSessionOptions session_options; |
| 1728 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1729 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1732 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1733 | const RTCOfferAnswerOptions& options) { |
| 1734 | RTC_DCHECK(IsUnifiedPlan()); |
| 1735 | |
| 1736 | if (options.offer_to_receive_audio == 0) { |
| 1737 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1738 | cricket::MEDIA_TYPE_AUDIO); |
| 1739 | } else if (options.offer_to_receive_audio == 1) { |
| 1740 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1741 | } else if (options.offer_to_receive_audio > 1) { |
| 1742 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1743 | "offer_to_receive_audio > 1 is not supported."); |
| 1744 | } |
| 1745 | |
| 1746 | if (options.offer_to_receive_video == 0) { |
| 1747 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1748 | cricket::MEDIA_TYPE_VIDEO); |
| 1749 | } else if (options.offer_to_receive_video == 1) { |
| 1750 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1751 | } else if (options.offer_to_receive_video > 1) { |
| 1752 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1753 | "offer_to_receive_video > 1 is not supported."); |
| 1754 | } |
| 1755 | |
| 1756 | return RTCError::OK(); |
| 1757 | } |
| 1758 | |
| 1759 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1760 | cricket::MediaType media_type) { |
| 1761 | for (auto transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1762 | RtpTransceiverDirection new_direction = |
| 1763 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 1764 | if (new_direction != transceiver->direction()) { |
| 1765 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 1766 | << " transceiver (MID=" |
| 1767 | << transceiver->mid().value_or("<not set>") << ") from " |
| 1768 | << RtpTransceiverDirectionToString( |
| 1769 | transceiver->direction()) |
| 1770 | << " to " |
| 1771 | << RtpTransceiverDirectionToString(new_direction) |
| 1772 | << " since CreateOffer specified offer_to_receive=0"; |
| 1773 | transceiver->internal()->set_direction(new_direction); |
| 1774 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1779 | cricket::MediaType media_type) { |
| 1780 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1781 | RTC_LOG(LS_INFO) |
| 1782 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 1783 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1784 | RtpTransceiverInit init; |
| 1785 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1786 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1791 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1792 | std::vector< |
| 1793 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1794 | receiving_transceivers; |
| 1795 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1796 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1797 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1798 | receiving_transceivers.push_back(transceiver); |
| 1799 | } |
| 1800 | } |
| 1801 | return receiving_transceivers; |
| 1802 | } |
| 1803 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1804 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1805 | const RTCOfferAnswerOptions& options) { |
| 1806 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1807 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1808 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1809 | return; |
| 1810 | } |
| 1811 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1812 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1813 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1814 | std::string error = |
| 1815 | "PeerConnection cannot create an answer in a state other than " |
| 1816 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1817 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1818 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1819 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1820 | return; |
| 1821 | } |
| 1822 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1823 | // The remote description should be set if we're in the right state. |
| 1824 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1825 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1826 | if (IsUnifiedPlan()) { |
| 1827 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1828 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1829 | "supported with Unified Plan semantics. Use the " |
| 1830 | "RtpTransceiver API instead."; |
| 1831 | } |
| 1832 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1833 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1834 | "supported with Unified Plan semantics. Use the " |
| 1835 | "RtpTransceiver API instead."; |
| 1836 | } |
| 1837 | } |
| 1838 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1839 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1840 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1841 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1842 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | void PeerConnection::SetLocalDescription( |
| 1846 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1847 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1848 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1849 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1850 | // The SetLocalDescription contract is that we take ownership of the session |
| 1851 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 1852 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 1853 | // description as a unique_ptr argument to formalize this agreement. |
| 1854 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 1855 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1856 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1857 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1858 | return; |
| 1859 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1860 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1861 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1862 | PostSetSessionDescriptionFailure( |
| 1863 | observer, |
| 1864 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1865 | return; |
| 1866 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1867 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1868 | // If a session error has occurred the PeerConnection is in a possibly |
| 1869 | // inconsistent state so fail right away. |
| 1870 | if (session_error() != SessionError::kNone) { |
| 1871 | std::string error_message = GetSessionErrorMsg(); |
| 1872 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1873 | PostSetSessionDescriptionFailure( |
| 1874 | observer, |
| 1875 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1876 | return; |
| 1877 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1878 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1879 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1880 | if (!error.ok()) { |
| 1881 | std::string error_message = GetSetDescriptionErrorMessage( |
| 1882 | cricket::CS_LOCAL, desc->GetType(), error); |
| 1883 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1884 | PostSetSessionDescriptionFailure( |
| 1885 | observer, |
| 1886 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1887 | return; |
| 1888 | } |
| 1889 | |
| 1890 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 1891 | // which may destroy it before returning. |
| 1892 | const SdpType type = desc->GetType(); |
| 1893 | |
| 1894 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1895 | // |desc| may be destroyed at this point. |
| 1896 | |
| 1897 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1898 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 1899 | // inconsistent state, so act conservatively here and set the session error |
| 1900 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 1901 | SetSessionError(SessionError::kContent, error.message()); |
| 1902 | std::string error_message = |
| 1903 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 1904 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1905 | PostSetSessionDescriptionFailure( |
| 1906 | observer, |
| 1907 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1908 | return; |
| 1909 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1910 | RTC_DCHECK(local_description()); |
| 1911 | |
| 1912 | PostSetSessionDescriptionSuccess(observer); |
| 1913 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 1914 | // MaybeStartGathering needs to be called after posting |
| 1915 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1916 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1917 | transport_controller_->MaybeStartGathering(); |
| 1918 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1919 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1920 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1921 | // MaybeStartGathering... |
| 1922 | network_thread()->Invoke<void>( |
| 1923 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1924 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 1925 | // Make UMA notes about what was agreed to. |
| 1926 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1927 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1928 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | RTCError PeerConnection::ApplyLocalDescription( |
| 1932 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1933 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1934 | RTC_DCHECK(desc); |
| 1935 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1936 | // Update stats here so that we have the most recent stats for tracks and |
| 1937 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1938 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1939 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1940 | // Take a reference to the old local description since it's used below to |
| 1941 | // compare against the new local description. When setting the new local |
| 1942 | // description, grab ownership of the replaced session description in case it |
| 1943 | // is the same as |old_local_description|, to keep it alive for the duration |
| 1944 | // of the method. |
| 1945 | const SessionDescriptionInterface* old_local_description = |
| 1946 | local_description(); |
| 1947 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1948 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1949 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1950 | replaced_local_description = pending_local_description_ |
| 1951 | ? std::move(pending_local_description_) |
| 1952 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1953 | current_local_description_ = std::move(desc); |
| 1954 | pending_local_description_ = nullptr; |
| 1955 | current_remote_description_ = std::move(pending_remote_description_); |
| 1956 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1957 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1958 | pending_local_description_ = std::move(desc); |
| 1959 | } |
| 1960 | // The session description to apply now must be accessed by |
| 1961 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1962 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1963 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1964 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 1965 | if (!error.ok()) { |
| 1966 | return error; |
| 1967 | } |
| 1968 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1969 | if (IsUnifiedPlan()) { |
| 1970 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 1971 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 1972 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1973 | if (!error.ok()) { |
| 1974 | return error; |
| 1975 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 1976 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 1977 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1978 | for (auto transceiver : transceivers_) { |
| 1979 | const ContentInfo* content = |
| 1980 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 1981 | if (!content) { |
| 1982 | continue; |
| 1983 | } |
| 1984 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 1985 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 1986 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1987 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 1988 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 1989 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 1990 | // "recvonly", process the removal of a remote track for the media |
| 1991 | // description, given transceiver, removeList, and muteTracks. |
| 1992 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 1993 | (transceiver->internal()->fired_direction() && |
| 1994 | RtpTransceiverDirectionHasRecv( |
| 1995 | *transceiver->internal()->fired_direction()))) { |
| 1996 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 1997 | &removed_streams); |
| 1998 | } |
| 1999 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2000 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2001 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2002 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2003 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2004 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2005 | auto observer = Observer(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2006 | for (auto transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2007 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2008 | } |
| 2009 | for (auto stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2010 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2011 | } |
| 2012 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2013 | // Media channels will be created only when offer is set. These may use new |
| 2014 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2015 | if (type == SdpType::kOffer) { |
| 2016 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2017 | // description is applied. Restore back to old description. |
| 2018 | RTCError error = CreateChannels(*local_description()->description()); |
| 2019 | if (!error.ok()) { |
| 2020 | return error; |
| 2021 | } |
| 2022 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2023 | // Remove unused channels if MediaContentDescription is rejected. |
| 2024 | RemoveUnusedChannels(local_description()->description()); |
| 2025 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2026 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2027 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2028 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2029 | if (!error.ok()) { |
| 2030 | return error; |
| 2031 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2032 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2033 | if (remote_description()) { |
| 2034 | // Now that we have a local description, we can push down remote candidates. |
| 2035 | UseCandidatesInSessionDescription(remote_description()); |
| 2036 | } |
| 2037 | |
| 2038 | pending_ice_restarts_.clear(); |
| 2039 | if (session_error() != SessionError::kNone) { |
| 2040 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2041 | } |
| 2042 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2043 | // If setting the description decided our SSL role, allocate any necessary |
| 2044 | // SCTP sids. |
| 2045 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2046 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2047 | AllocateSctpSids(role); |
| 2048 | } |
| 2049 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2050 | if (IsUnifiedPlan()) { |
| 2051 | for (auto transceiver : transceivers_) { |
| 2052 | const ContentInfo* content = |
| 2053 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2054 | if (!content) { |
| 2055 | continue; |
| 2056 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2057 | const auto& streams = content->media_description()->streams(); |
| 2058 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2059 | transceiver->internal()->sender_internal()->set_stream_ids( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 2060 | streams[0].stream_ids()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2061 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2062 | streams[0].first_ssrc()); |
Steve Anton | 60b6c1d | 2018-06-13 11:32:27 -0700 | [diff] [blame] | 2063 | } else { |
| 2064 | // 0 is a special value meaning "this sender has no associated send |
| 2065 | // stream". Need to call this so the sender won't attempt to configure |
| 2066 | // a no longer existing stream and run into DCHECKs in the lower |
| 2067 | // layers. |
| 2068 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | } else { |
| 2072 | // Plan B semantics. |
| 2073 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2074 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2075 | // local session description. |
| 2076 | const cricket::ContentInfo* audio_content = |
| 2077 | GetFirstAudioContent(local_description()->description()); |
| 2078 | if (audio_content) { |
| 2079 | if (audio_content->rejected) { |
| 2080 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2081 | } else { |
| 2082 | const cricket::AudioContentDescription* audio_desc = |
| 2083 | audio_content->media_description()->as_audio(); |
| 2084 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2085 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2086 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2087 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2088 | const cricket::ContentInfo* video_content = |
| 2089 | GetFirstVideoContent(local_description()->description()); |
| 2090 | if (video_content) { |
| 2091 | if (video_content->rejected) { |
| 2092 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2093 | } else { |
| 2094 | const cricket::VideoContentDescription* video_desc = |
| 2095 | video_content->media_description()->as_video(); |
| 2096 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2097 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2098 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2102 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2103 | if (data_content) { |
| 2104 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2105 | data_content->media_description()->as_data(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2106 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2107 | cricket::kMediaProtocolRtpPrefix)) { |
| 2108 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 2109 | } |
| 2110 | } |
| 2111 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2112 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2116 | SetSessionDescriptionObserver* observer, |
| 2117 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2118 | SetRemoteDescription( |
| 2119 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2120 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2121 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2122 | } |
| 2123 | |
| 2124 | void PeerConnection::SetRemoteDescription( |
| 2125 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2126 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2127 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2128 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2129 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2130 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2131 | return; |
| 2132 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2133 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2134 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2135 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2136 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2137 | return; |
| 2138 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2139 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2140 | // If a session error has occurred the PeerConnection is in a possibly |
| 2141 | // inconsistent state so fail right away. |
| 2142 | if (session_error() != SessionError::kNone) { |
| 2143 | std::string error_message = GetSessionErrorMsg(); |
| 2144 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2145 | observer->OnSetRemoteDescriptionComplete( |
| 2146 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2147 | return; |
| 2148 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2149 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 2150 | if (desc->GetType() == SdpType::kOffer) { |
| 2151 | // Report to UMA the format of the received offer. |
| 2152 | ReportSdpFormatReceived(*desc); |
| 2153 | } |
| 2154 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2155 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2156 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2157 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2158 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2159 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2160 | observer->OnSetRemoteDescriptionComplete( |
| 2161 | RTCError(error.type(), std::move(error_message))); |
| 2162 | return; |
| 2163 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2164 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2165 | // Grab the description type before moving ownership to |
| 2166 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2167 | const SdpType type = desc->GetType(); |
| 2168 | |
| 2169 | error = ApplyRemoteDescription(std::move(desc)); |
| 2170 | // |desc| may be destroyed at this point. |
| 2171 | |
| 2172 | if (!error.ok()) { |
| 2173 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2174 | // inconsistent state, so act conservatively here and set the session error |
| 2175 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2176 | SetSessionError(SessionError::kContent, error.message()); |
| 2177 | std::string error_message = |
| 2178 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2179 | RTC_LOG(LS_ERROR) << error_message; |
| 2180 | observer->OnSetRemoteDescriptionComplete( |
| 2181 | RTCError(error.type(), std::move(error_message))); |
| 2182 | return; |
| 2183 | } |
| 2184 | RTC_DCHECK(remote_description()); |
| 2185 | |
| 2186 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2187 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2188 | // MaybeStartGathering... |
| 2189 | network_thread()->Invoke<void>( |
| 2190 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2191 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2192 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2193 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2197 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2201 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2202 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2203 | RTC_DCHECK(desc); |
| 2204 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2205 | // Update stats here so that we have the most recent stats for tracks and |
| 2206 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2207 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2208 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2209 | // Take a reference to the old remote description since it's used below to |
| 2210 | // compare against the new remote description. When setting the new remote |
| 2211 | // description, grab ownership of the replaced session description in case it |
| 2212 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2213 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2214 | const SessionDescriptionInterface* old_remote_description = |
| 2215 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2216 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2217 | SdpType type = desc->GetType(); |
| 2218 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2219 | replaced_remote_description = pending_remote_description_ |
| 2220 | ? std::move(pending_remote_description_) |
| 2221 | : std::move(current_remote_description_); |
| 2222 | current_remote_description_ = std::move(desc); |
| 2223 | pending_remote_description_ = nullptr; |
| 2224 | current_local_description_ = std::move(pending_local_description_); |
| 2225 | } else { |
| 2226 | replaced_remote_description = std::move(pending_remote_description_); |
| 2227 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2228 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2229 | // The session description to apply now must be accessed by |
| 2230 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2231 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2232 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2233 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2234 | if (!error.ok()) { |
| 2235 | return error; |
| 2236 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2237 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2238 | if (IsUnifiedPlan()) { |
| 2239 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2240 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2241 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2242 | if (!error.ok()) { |
| 2243 | return error; |
| 2244 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2245 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2246 | // Media channels will be created only when offer is set. These may use new |
| 2247 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2248 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2249 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2250 | // description is applied. Restore back to old description. |
| 2251 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2252 | if (!error.ok()) { |
| 2253 | return error; |
| 2254 | } |
| 2255 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2256 | // Remove unused channels if MediaContentDescription is rejected. |
| 2257 | RemoveUnusedChannels(remote_description()->description()); |
| 2258 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2259 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2260 | // NOTE: Candidates allocation will be initiated only when |
| 2261 | // SetLocalDescription is called. |
| 2262 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2263 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2264 | if (!error.ok()) { |
| 2265 | return error; |
| 2266 | } |
| 2267 | |
| 2268 | if (local_description() && |
| 2269 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2270 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2271 | } |
| 2272 | |
| 2273 | if (old_remote_description) { |
| 2274 | for (const cricket::ContentInfo& content : |
| 2275 | old_remote_description->description()->contents()) { |
| 2276 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2277 | // password that indicates the remote peer requests an ICE restart. |
| 2278 | // TODO(deadbeef): When we start storing both the current and pending |
| 2279 | // remote description, this should reset pending_ice_restarts and compare |
| 2280 | // against the current description. |
| 2281 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2282 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2283 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2284 | pending_ice_restarts_.insert(content.name); |
| 2285 | } |
| 2286 | } else { |
| 2287 | // We retain all received candidates only if ICE is not restarted. |
| 2288 | // When ICE is restarted, all previous candidates belong to an old |
| 2289 | // generation and should not be kept. |
| 2290 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2291 | // description should only contain candidates from the last set remote |
| 2292 | // description plus any candidates added since then. We should remove |
| 2293 | // this once we're sure it won't break anything. |
| 2294 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2295 | old_remote_description, content.name, mutable_remote_description()); |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | if (session_error() != SessionError::kNone) { |
| 2301 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2302 | } |
| 2303 | |
| 2304 | // Set the the ICE connection state to connecting since the connection may |
| 2305 | // become writable with peer reflexive candidates before any remote candidate |
| 2306 | // is signaled. |
| 2307 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2308 | // is to have a new signal the indicates a change in checking state from the |
| 2309 | // transport and expose a new checking() member from transport that can be |
| 2310 | // read to determine the current checking state. The existing SignalConnecting |
| 2311 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2312 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 2313 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2314 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2315 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2316 | } |
| 2317 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2318 | // If setting the description decided our SSL role, allocate any necessary |
| 2319 | // SCTP sids. |
| 2320 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2321 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2322 | AllocateSctpSids(role); |
| 2323 | } |
| 2324 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2325 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2326 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2327 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2328 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2329 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2330 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2331 | for (auto transceiver : transceivers_) { |
| 2332 | const ContentInfo* content = |
| 2333 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2334 | if (!content) { |
| 2335 | continue; |
| 2336 | } |
| 2337 | const MediaContentDescription* media_desc = content->media_description(); |
| 2338 | RtpTransceiverDirection local_direction = |
| 2339 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 2340 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 "Set |
| 2341 | // the RTCSessionDescription: If direction is sendrecv or recvonly, and |
| 2342 | // transceiver's current direction is neither sendrecv nor recvonly, |
| 2343 | // process the addition of a remote track for the media description. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 2344 | std::vector<std::string> stream_ids; |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2345 | if (!media_desc->streams().empty()) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2346 | // The remote description has signaled the stream IDs. |
| 2347 | stream_ids = media_desc->streams()[0].stream_ids(); |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2348 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2349 | if (RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2350 | (!transceiver->fired_direction() || |
| 2351 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2352 | RTC_LOG(LS_INFO) << "Processing the addition of a new track for MID=" |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 2353 | << content->name << " (added to " |
| 2354 | << GetStreamIdsString(stream_ids) << ")."; |
| 2355 | |
| 2356 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 2357 | for (const std::string& stream_id : stream_ids) { |
| 2358 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2359 | remote_streams_->find(stream_id); |
| 2360 | if (!stream) { |
| 2361 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2362 | MediaStream::Create(stream_id)); |
| 2363 | remote_streams_->AddStream(stream); |
| 2364 | added_streams.push_back(stream); |
| 2365 | } |
| 2366 | media_streams.push_back(stream); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2367 | } |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2368 | // This will add the remote track to the streams. |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 2369 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 2370 | // instead. https://crbug.com/webrtc/9480 |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 2371 | transceiver->internal()->receiver_internal()->SetStreams(media_streams); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2372 | now_receiving_transceivers.push_back(transceiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2373 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2374 | // 2.2.8.1.7: If direction is "sendonly" or "inactive", and transceiver's |
| 2375 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 2376 | // removal of a remote track for the media description, given transceiver, |
| 2377 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2378 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2379 | (transceiver->fired_direction() && |
| 2380 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 2381 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2382 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2383 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2384 | // 2.2.8.1.8: Set transceiver's [[FiredDirection]] slot to direction. |
| 2385 | transceiver->internal()->set_fired_direction(local_direction); |
| 2386 | // 2.2.8.1.9: If description is of type "answer" or "pranswer", then run |
| 2387 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2388 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2389 | // 2.2.8.1.9.1: Set transceiver's [[CurrentDirection]] slot to |
| 2390 | // direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2391 | transceiver->internal()->set_current_direction(local_direction); |
| 2392 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2393 | // 2.2.8.1.10: If the media description is rejected, and transceiver is |
| 2394 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2395 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2396 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2397 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2398 | transceiver->Stop(); |
| 2399 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2400 | if (!content->rejected && |
| 2401 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2402 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2403 | uint32_t ssrc = 0; |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2404 | if (!media_desc->streams().empty() && |
| 2405 | media_desc->streams()[0].has_ssrcs()) { |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2406 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2407 | } |
| 2408 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2409 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2410 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2411 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2412 | auto observer = Observer(); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2413 | for (auto transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2414 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2415 | observer->OnTrack(transceiver); |
| 2416 | observer->OnAddTrack(transceiver->receiver(), |
| 2417 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2418 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2419 | for (auto stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2420 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2421 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2422 | for (auto transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2423 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2424 | } |
| 2425 | for (auto stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2426 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2427 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2428 | } |
| 2429 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2430 | const cricket::ContentInfo* audio_content = |
| 2431 | GetFirstAudioContent(remote_description()->description()); |
| 2432 | const cricket::ContentInfo* video_content = |
| 2433 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2434 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2435 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2436 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2437 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2438 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2439 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2440 | |
| 2441 | // Check if the descriptions include streams, just in case the peer supports |
| 2442 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2443 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2444 | (audio_desc && !audio_desc->streams().empty()) || |
| 2445 | (video_desc && !video_desc->streams().empty())) { |
| 2446 | remote_peer_supports_msid_ = true; |
| 2447 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2448 | |
| 2449 | // We wait to signal new streams until we finish processing the description, |
| 2450 | // since only at that point will new streams have all their tracks. |
| 2451 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2452 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2453 | if (!IsUnifiedPlan()) { |
| 2454 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2455 | // rejected media section, there is some cleanup logic that expects the |
| 2456 | // voice/ video channel to still be set. But in this method the voice/video |
| 2457 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2458 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2459 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2460 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2461 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2462 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2463 | // and MediaStreams. |
| 2464 | if (audio_content) { |
| 2465 | if (audio_content->rejected) { |
| 2466 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2467 | } else { |
| 2468 | bool default_audio_track_needed = |
| 2469 | !remote_peer_supports_msid_ && |
| 2470 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2471 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2472 | default_audio_track_needed, audio_desc->type(), |
| 2473 | new_streams); |
| 2474 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2475 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2476 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2477 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2478 | // and MediaStreams. |
| 2479 | if (video_content) { |
| 2480 | if (video_content->rejected) { |
| 2481 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2482 | } else { |
| 2483 | bool default_video_track_needed = |
| 2484 | !remote_peer_supports_msid_ && |
| 2485 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2486 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2487 | default_video_track_needed, video_desc->type(), |
| 2488 | new_streams); |
| 2489 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2490 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2491 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2492 | // Update the DataChannels with the information from the remote peer. |
| 2493 | if (data_desc) { |
| 2494 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2495 | cricket::kMediaProtocolRtpPrefix)) { |
| 2496 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2497 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2498 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2499 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2500 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2501 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2502 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2503 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2504 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2505 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2506 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2507 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2508 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2509 | UpdateEndedRemoteMediaStreams(); |
| 2510 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2511 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2512 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2513 | } |
| 2514 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2515 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 2516 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2517 | transceiver, |
| 2518 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 2519 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2520 | RTC_DCHECK(transceiver->mid()); |
| 2521 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 2522 | << *transceiver->mid(); |
| 2523 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams = |
| 2524 | transceiver->internal()->receiver_internal()->streams(); |
| 2525 | // This will remove the remote track from the streams. |
| 2526 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 2527 | remove_list->push_back(transceiver); |
| 2528 | // Remove any streams that no longer have tracks. |
| 2529 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead |
| 2530 | // of streams, see if the stream was removed by checking if this was the |
| 2531 | // last receiver with that stream ID. |
| 2532 | for (auto stream : media_streams) { |
| 2533 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 2534 | remote_streams_->RemoveStream(stream); |
| 2535 | removed_streams->push_back(stream); |
| 2536 | } |
| 2537 | } |
| 2538 | } |
| 2539 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2540 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2541 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2542 | const SessionDescriptionInterface& new_session, |
| 2543 | const SessionDescriptionInterface* old_local_description, |
| 2544 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2545 | RTC_DCHECK(IsUnifiedPlan()); |
| 2546 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2547 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2548 | if (new_session.GetType() == SdpType::kOffer) { |
| 2549 | auto bundle_group_or_error = |
| 2550 | GetEarlyBundleGroup(*new_session.description()); |
| 2551 | if (!bundle_group_or_error.ok()) { |
| 2552 | return bundle_group_or_error.MoveError(); |
| 2553 | } |
| 2554 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2555 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2556 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2557 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2558 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2559 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2560 | cricket::MediaType media_type = new_content.media_description()->type(); |
| 2561 | seen_mids_.insert(new_content.name); |
| 2562 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2563 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2564 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2565 | if (old_local_description && |
| 2566 | i < old_local_description->description()->contents().size()) { |
| 2567 | old_local_content = |
| 2568 | &old_local_description->description()->contents()[i]; |
| 2569 | } |
| 2570 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2571 | if (old_remote_description && |
| 2572 | i < old_remote_description->description()->contents().size()) { |
| 2573 | old_remote_content = |
| 2574 | &old_remote_description->description()->contents()[i]; |
| 2575 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2576 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2577 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2578 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2579 | if (!transceiver_or_error.ok()) { |
| 2580 | return transceiver_or_error.MoveError(); |
| 2581 | } |
| 2582 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2583 | RTCError error = |
| 2584 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2585 | if (!error.ok()) { |
| 2586 | return error; |
| 2587 | } |
| 2588 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2589 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2590 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2591 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 2592 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2593 | continue; |
| 2594 | } |
| 2595 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2596 | if (!error.ok()) { |
| 2597 | return error; |
| 2598 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2599 | } else { |
| 2600 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2601 | "Unknown section type."); |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | return RTCError::OK(); |
| 2606 | } |
| 2607 | |
| 2608 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2609 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2610 | transceiver, |
| 2611 | const cricket::ContentInfo& content, |
| 2612 | const cricket::ContentGroup* bundle_group) { |
| 2613 | RTC_DCHECK(IsUnifiedPlan()); |
| 2614 | RTC_DCHECK(transceiver); |
| 2615 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 2616 | if (content.rejected) { |
| 2617 | if (channel) { |
| 2618 | transceiver->internal()->SetChannel(nullptr); |
| 2619 | DestroyBaseChannel(channel); |
| 2620 | } |
| 2621 | } else { |
| 2622 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2623 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2624 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2625 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2626 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2627 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2628 | } |
| 2629 | if (!channel) { |
| 2630 | LOG_AND_RETURN_ERROR( |
| 2631 | RTCErrorType::INTERNAL_ERROR, |
| 2632 | "Failed to create channel for mid=" + content.name); |
| 2633 | } |
| 2634 | transceiver->internal()->SetChannel(channel); |
| 2635 | } |
| 2636 | } |
| 2637 | return RTCError::OK(); |
| 2638 | } |
| 2639 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2640 | RTCError PeerConnection::UpdateDataChannel( |
| 2641 | cricket::ContentSource source, |
| 2642 | const cricket::ContentInfo& content, |
| 2643 | const cricket::ContentGroup* bundle_group) { |
| 2644 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2645 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2646 | // will take care of rejecting it. |
| 2647 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2648 | } |
| 2649 | if (content.rejected) { |
| 2650 | DestroyDataChannel(); |
| 2651 | } else { |
| 2652 | if (!rtp_data_channel_ && !sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2653 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2654 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2655 | "Failed to create data channel."); |
| 2656 | } |
| 2657 | } |
| 2658 | if (source == cricket::CS_REMOTE) { |
| 2659 | const MediaContentDescription* data_desc = content.media_description(); |
| 2660 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2661 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2662 | } |
| 2663 | } |
| 2664 | } |
| 2665 | return RTCError::OK(); |
| 2666 | } |
| 2667 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2668 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2669 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2670 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2671 | size_t mline_index, |
| 2672 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2673 | const ContentInfo* old_local_content, |
| 2674 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2675 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2676 | // If this is an offer then the m= section might be recycled. If the m= |
| 2677 | // section is being recycled (defined as: rejected in the current local or |
| 2678 | // remote description and not rejected in new description), dissociate the |
| 2679 | // currently associated RtpTransceiver by setting its mid property to null, |
| 2680 | // and discard the mapping between the transceiver and its m= section index. |
| 2681 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 2682 | old_remote_content)) { |
| 2683 | // We want to dissociate the transceiver that has the rejected mid. |
| 2684 | const std::string& old_mid = |
| 2685 | (old_local_content && old_local_content->rejected) |
| 2686 | ? old_local_content->name |
| 2687 | : old_remote_content->name; |
| 2688 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2689 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2690 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 2691 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 2692 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 2693 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | const MediaContentDescription* media_desc = content.media_description(); |
| 2697 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2698 | if (source == cricket::CS_LOCAL) { |
| 2699 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2700 | // mapping between transceivers and m= section indices established when |
| 2701 | // creating the offer. |
| 2702 | if (!transceiver) { |
| 2703 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2704 | } |
| 2705 | if (!transceiver) { |
| 2706 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2707 | "Unknown transceiver"); |
| 2708 | } |
| 2709 | } else { |
| 2710 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2711 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2712 | // of the same type... |
| 2713 | if (!transceiver && |
| 2714 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2715 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2716 | } |
| 2717 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2718 | // recvonly direction. |
| 2719 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2720 | RTC_LOG(LS_INFO) << "Adding " |
| 2721 | << cricket::MediaTypeToString(media_desc->type()) |
| 2722 | << " transceiver for MID=" << content.name |
| 2723 | << " at i=" << mline_index |
| 2724 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 2725 | std::string sender_id = rtc::CreateRandomUuid(); |
Steve Anton | 1bc9716 | 2018-06-25 14:04:01 -0700 | [diff] [blame] | 2726 | auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2727 | std::string receiver_id; |
| 2728 | if (!media_desc->streams().empty()) { |
| 2729 | receiver_id = media_desc->streams()[0].id; |
| 2730 | } else { |
| 2731 | receiver_id = rtc::CreateRandomUuid(); |
| 2732 | } |
| 2733 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2734 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2735 | transceiver->internal()->set_direction( |
| 2736 | RtpTransceiverDirection::kRecvOnly); |
| 2737 | } |
| 2738 | } |
| 2739 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2740 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2741 | LOG_AND_RETURN_ERROR( |
| 2742 | RTCErrorType::INVALID_PARAMETER, |
| 2743 | "Transceiver type does not match media description type."); |
| 2744 | } |
| 2745 | // Associate the found or created RtpTransceiver with the m= section by |
| 2746 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2747 | // section, and establish a mapping between the transceiver and the index of |
| 2748 | // the m= section. |
| 2749 | transceiver->internal()->set_mid(content.name); |
| 2750 | transceiver->internal()->set_mline_index(mline_index); |
| 2751 | return std::move(transceiver); |
| 2752 | } |
| 2753 | |
| 2754 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2755 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 2756 | RTC_DCHECK(IsUnifiedPlan()); |
| 2757 | for (auto transceiver : transceivers_) { |
| 2758 | if (transceiver->mid() == mid) { |
| 2759 | return transceiver; |
| 2760 | } |
| 2761 | } |
| 2762 | return nullptr; |
| 2763 | } |
| 2764 | |
| 2765 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2766 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 2767 | RTC_DCHECK(IsUnifiedPlan()); |
| 2768 | for (auto transceiver : transceivers_) { |
| 2769 | if (transceiver->internal()->mline_index() == mline_index) { |
| 2770 | return transceiver; |
| 2771 | } |
| 2772 | } |
| 2773 | return nullptr; |
| 2774 | } |
| 2775 | |
| 2776 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2777 | PeerConnection::FindAvailableTransceiverToReceive( |
| 2778 | cricket::MediaType media_type) const { |
| 2779 | RTC_DCHECK(IsUnifiedPlan()); |
| 2780 | // From JSEP section 5.10 (Applying a Remote Description): |
| 2781 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 2782 | // the same type that were added to the PeerConnection by addTrack and are not |
| 2783 | // associated with any m= section and are not stopped, find the first such |
| 2784 | // RtpTransceiver. |
| 2785 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2786 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2787 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 2788 | !transceiver->stopped()) { |
| 2789 | return transceiver; |
| 2790 | } |
| 2791 | } |
| 2792 | return nullptr; |
| 2793 | } |
| 2794 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2795 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 2796 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2797 | transceiver, |
| 2798 | const SessionDescriptionInterface* sdesc) const { |
| 2799 | RTC_DCHECK(transceiver); |
| 2800 | RTC_DCHECK(sdesc); |
| 2801 | if (IsUnifiedPlan()) { |
| 2802 | if (!transceiver->internal()->mid()) { |
| 2803 | // This transceiver is not associated with a media section yet. |
| 2804 | return nullptr; |
| 2805 | } |
| 2806 | return sdesc->description()->GetContentByName( |
| 2807 | *transceiver->internal()->mid()); |
| 2808 | } else { |
| 2809 | // Plan B only allows at most one audio and one video section, so use the |
| 2810 | // first media section of that type. |
| 2811 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2812 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2813 | } |
| 2814 | } |
| 2815 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 2816 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 2817 | return configuration_; |
| 2818 | } |
| 2819 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2820 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 2821 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2822 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2823 | if (IsClosed()) { |
| 2824 | RTC_LOG(LS_ERROR) << "SetConfiguration: PeerConnection is closed."; |
| 2825 | return SafeSetError(RTCErrorType::INVALID_STATE, error); |
| 2826 | } |
| 2827 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 2828 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 2829 | // size is not allowed, and changing the set of ICE servers will not result |
| 2830 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2831 | if (local_description() && configuration.ice_candidate_pool_size != |
| 2832 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2833 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 2834 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2835 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2836 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2837 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2838 | // The simplest (and most future-compatible) way to tell if the config was |
| 2839 | // modified in an invalid way is to copy each property we do support |
| 2840 | // modifying, then use operator==. There are far more properties we don't |
| 2841 | // support modifying than those we do, and more could be added. |
| 2842 | RTCConfiguration modified_config = configuration_; |
| 2843 | modified_config.servers = configuration.servers; |
| 2844 | modified_config.type = configuration.type; |
| 2845 | modified_config.ice_candidate_pool_size = |
| 2846 | configuration.ice_candidate_pool_size; |
| 2847 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2848 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 2849 | modified_config.ice_check_interval_strong_connectivity = |
| 2850 | configuration.ice_check_interval_strong_connectivity; |
| 2851 | modified_config.ice_check_interval_weak_connectivity = |
| 2852 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2853 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 2854 | modified_config.ice_unwritable_min_checks = |
| 2855 | configuration.ice_unwritable_min_checks; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2856 | modified_config.stun_candidate_keepalive_interval = |
| 2857 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2858 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 2859 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 2860 | modified_config.active_reset_srtp_params = |
| 2861 | configuration.active_reset_srtp_params; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2862 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2863 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2864 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2865 | } |
| 2866 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 2867 | // Validate the modified configuration. |
| 2868 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 2869 | if (!validate_error.ok()) { |
| 2870 | return SafeSetError(std::move(validate_error), error); |
| 2871 | } |
| 2872 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2873 | // Note that this isn't possible through chromium, since it's an unsigned |
| 2874 | // short in WebIDL. |
| 2875 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 03:34:17 -0700 | [diff] [blame] | 2876 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2877 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 2878 | } |
| 2879 | |
| 2880 | // Parse ICE servers before hopping to network thread. |
| 2881 | cricket::ServerAddresses stun_servers; |
| 2882 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 2883 | RTCErrorType parse_error = |
| 2884 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 2885 | if (parse_error != RTCErrorType::NONE) { |
| 2886 | return SafeSetError(parse_error, error); |
| 2887 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2888 | // Note if STUN or TURN servers were supplied. |
| 2889 | if (!stun_servers.empty()) { |
| 2890 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 2891 | } |
| 2892 | if (!turn_servers.empty()) { |
| 2893 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 2894 | } |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2895 | |
| 2896 | // In theory this shouldn't fail. |
| 2897 | if (!network_thread()->Invoke<bool>( |
| 2898 | RTC_FROM_HERE, |
| 2899 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 2900 | stun_servers, turn_servers, modified_config.type, |
| 2901 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2902 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2903 | modified_config.turn_customizer, |
| 2904 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2905 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2906 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 2907 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2908 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2909 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 2910 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 2911 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2912 | if (modified_config.servers != configuration_.servers || |
| 2913 | modified_config.type != configuration_.type || |
| 2914 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2915 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2916 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2917 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 2918 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2919 | |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 2920 | if (configuration_.active_reset_srtp_params != |
| 2921 | modified_config.active_reset_srtp_params) { |
| 2922 | transport_controller_->SetActiveResetSrtpParams( |
| 2923 | modified_config.active_reset_srtp_params); |
| 2924 | } |
| 2925 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2926 | configuration_ = modified_config; |
| 2927 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2930 | bool PeerConnection::AddIceCandidate( |
| 2931 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2932 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2933 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2934 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 2935 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2936 | return false; |
| 2937 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2938 | |
| 2939 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2940 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2941 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 2942 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2943 | return false; |
| 2944 | } |
| 2945 | |
| 2946 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2947 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 2948 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2949 | return false; |
| 2950 | } |
| 2951 | |
| 2952 | bool valid = false; |
| 2953 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 2954 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 2955 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2956 | return false; |
| 2957 | } |
| 2958 | |
| 2959 | // Add this candidate to the remote session description. |
| 2960 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2961 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 2962 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2963 | return false; |
| 2964 | } |
| 2965 | |
| 2966 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 2967 | bool result = UseCandidate(ice_candidate); |
| 2968 | if (result) { |
| 2969 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 2970 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 2971 | } else { |
| 2972 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 2973 | } |
| 2974 | return result; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2975 | } else { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2976 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 2977 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2978 | return true; |
| 2979 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2982 | bool PeerConnection::RemoveIceCandidates( |
| 2983 | const std::vector<cricket::Candidate>& candidates) { |
| 2984 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2985 | if (IsClosed()) { |
| 2986 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 2987 | return false; |
| 2988 | } |
| 2989 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2990 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2991 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 2992 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2993 | return false; |
| 2994 | } |
| 2995 | |
| 2996 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2997 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2998 | return false; |
| 2999 | } |
| 3000 | |
| 3001 | size_t number_removed = |
| 3002 | mutable_remote_description()->RemoveCandidates(candidates); |
| 3003 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3004 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3005 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3006 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3007 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3011 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 3012 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3013 | RTC_LOG(LS_ERROR) |
| 3014 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 3015 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3016 | } |
| 3017 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3018 | } |
| 3019 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3020 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3021 | if (!worker_thread()->IsCurrent()) { |
| 3022 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3023 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3026 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 3027 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 3028 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3029 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3030 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3031 | "min_bitrate_bps <= 0"); |
| 3032 | } |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3033 | if (has_start) { |
| 3034 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3035 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3036 | "start_bitrate_bps < min_bitrate_bps"); |
| 3037 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3038 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3039 | "curent_bitrate_bps < 0"); |
| 3040 | } |
| 3041 | } |
| 3042 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3043 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3044 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3045 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3046 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3047 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3048 | "max_bitrate_bps < min_bitrate_bps"); |
| 3049 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3050 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3051 | "max_bitrate_bps < 0"); |
| 3052 | } |
| 3053 | } |
| 3054 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3055 | RTC_DCHECK(call_.get()); |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3056 | call_->GetTransportControllerSend()->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3057 | |
| 3058 | return RTCError::OK(); |
| 3059 | } |
| 3060 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3061 | void PeerConnection::SetBitrateAllocationStrategy( |
| 3062 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 3063 | bitrate_allocation_strategy) { |
| 3064 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 3065 | if (!worker_thread->IsCurrent()) { |
| 3066 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 3067 | bitrate_allocation_strategy.release(); |
| 3068 | auto functor = [this, strategy_raw]() { |
| 3069 | call_->SetBitrateAllocationStrategy( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3070 | absl::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3071 | }; |
| 3072 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 3073 | return; |
| 3074 | } |
| 3075 | RTC_DCHECK(call_.get()); |
| 3076 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 3077 | } |
| 3078 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3079 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3080 | if (!worker_thread()->IsCurrent()) { |
| 3081 | worker_thread()->Invoke<void>( |
| 3082 | RTC_FROM_HERE, |
| 3083 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3084 | return; |
| 3085 | } |
| 3086 | auto audio_state = |
| 3087 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3088 | audio_state->SetPlayout(playout); |
| 3089 | } |
| 3090 | |
| 3091 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3092 | if (!worker_thread()->IsCurrent()) { |
| 3093 | worker_thread()->Invoke<void>( |
| 3094 | RTC_FROM_HERE, |
| 3095 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3096 | return; |
| 3097 | } |
| 3098 | auto audio_state = |
| 3099 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3100 | audio_state->SetRecording(recording); |
| 3101 | } |
| 3102 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3103 | std::unique_ptr<rtc::SSLCertificate> |
| 3104 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3105 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3106 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3107 | return nullptr; |
| 3108 | } |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3109 | return chain->Get(0).GetUniqueReference(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3110 | } |
| 3111 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3112 | std::unique_ptr<rtc::SSLCertChain> |
| 3113 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3114 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3115 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3116 | return nullptr; |
| 3117 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3118 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3119 | audio_transceiver->internal()->channel()->transport_name()); |
| 3120 | } |
| 3121 | |
| 3122 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3123 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3124 | for (auto transceiver : transceivers_) { |
| 3125 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3126 | return transceiver; |
| 3127 | } |
| 3128 | } |
| 3129 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3130 | } |
| 3131 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3132 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3133 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3134 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3135 | const size_t max_size = (max_size_bytes < 0) |
| 3136 | ? RtcEventLog::kUnlimitedOutput |
| 3137 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 3138 | return StartRtcEventLog( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3139 | absl::make_unique<RtcEventLogOutputFile>(file, max_size), |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3140 | webrtc::RtcEventLog::kImmediateOutput); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3141 | } |
| 3142 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3143 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3144 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3145 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3146 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3147 | bool operator()() { |
| 3148 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3149 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3150 | PeerConnection* const pc; |
| 3151 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3152 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3153 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3154 | return worker_thread()->Invoke<bool>( |
| 3155 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3159 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3160 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3161 | } |
| 3162 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3163 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3164 | return pending_local_description_ ? pending_local_description_.get() |
| 3165 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3169 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3170 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3173 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3174 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3175 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3179 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3180 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3184 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3185 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3189 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3190 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3191 | } |
| 3192 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3193 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3194 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3195 | // Update stats here so that we have the most recent stats for tracks and |
| 3196 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3197 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3198 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3199 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3200 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3201 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3202 | for (auto transceiver : transceivers_) { |
| 3203 | transceiver->Stop(); |
| 3204 | } |
Steve Anton | 25cfeb9 | 2018-04-26 11:44:00 -0700 | [diff] [blame] | 3205 | |
| 3206 | // Ensure that all asynchronous stats requests are completed before destroying |
| 3207 | // the transport controller below. |
| 3208 | if (stats_collector_) { |
| 3209 | stats_collector_->WaitForPendingRequest(); |
| 3210 | } |
| 3211 | |
| 3212 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 3213 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3214 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3215 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3216 | // The event log is used in the transport controller, which must be outlived |
| 3217 | // by the former. CreateOffer by the peer connection is implemented |
| 3218 | // asynchronously and if the peer connection is closed without resetting the |
| 3219 | // WebRTC session description factory, the session description factory would |
| 3220 | // call the transport controller. |
| 3221 | webrtc_session_desc_factory_.reset(); |
| 3222 | transport_controller_.reset(); |
| 3223 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3224 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3225 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3226 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3227 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3228 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3229 | call_.reset(); |
| 3230 | // The event log must outlive call (and any other object that uses it). |
| 3231 | event_log_.reset(); |
| 3232 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3233 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3234 | // The .h file says that observer can be discarded after close() returns. |
| 3235 | // Make sure this is true. |
| 3236 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 3239 | void PeerConnection::OnMessage(rtc::Message* msg) { |
| 3240 | switch (msg->message_id) { |
| 3241 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3242 | SetSessionDescriptionMsg* param = |
| 3243 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3244 | param->observer->OnSuccess(); |
| 3245 | delete param; |
| 3246 | break; |
| 3247 | } |
| 3248 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3249 | SetSessionDescriptionMsg* param = |
| 3250 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3251 | param->observer->OnFailure(std::move(param->error)); |
| 3252 | delete param; |
| 3253 | break; |
| 3254 | } |
| 3255 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3256 | CreateSessionDescriptionMsg* param = |
| 3257 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 3258 | param->observer->OnFailure(std::move(param->error)); |
| 3259 | delete param; |
| 3260 | break; |
| 3261 | } |
| 3262 | case MSG_GETSTATS: { |
| 3263 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 3264 | StatsReports reports; |
| 3265 | stats_->GetStats(param->track, &reports); |
| 3266 | param->observer->OnComplete(reports); |
| 3267 | delete param; |
| 3268 | break; |
| 3269 | } |
| 3270 | case MSG_FREE_DATACHANNELS: { |
| 3271 | sctp_data_channels_to_free_.clear(); |
| 3272 | break; |
| 3273 | } |
| 3274 | case MSG_REPORT_USAGE_PATTERN: { |
| 3275 | ReportUsagePattern(); |
| 3276 | break; |
| 3277 | } |
| 3278 | default: |
| 3279 | RTC_NOTREACHED() << "Not implemented"; |
| 3280 | break; |
| 3281 | } |
| 3282 | } |
| 3283 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3284 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3285 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3286 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3287 | GetAudioTransceiver()->internal()->channel()); |
| 3288 | if (voice_channel) { |
| 3289 | return voice_channel->media_channel(); |
| 3290 | } else { |
| 3291 | return nullptr; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3296 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3297 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3298 | GetVideoTransceiver()->internal()->channel()); |
| 3299 | if (video_channel) { |
| 3300 | return video_channel->media_channel(); |
| 3301 | } else { |
| 3302 | return nullptr; |
| 3303 | } |
| 3304 | } |
| 3305 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3306 | void PeerConnection::CreateAudioReceiver( |
| 3307 | MediaStreamInterface* stream, |
| 3308 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3309 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3310 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3311 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3312 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3313 | auto* audio_receiver = new AudioRtpReceiver( |
| 3314 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3315 | audio_receiver->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3316 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3317 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3318 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3319 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3320 | Observer()->OnAddTrack(receiver, std::move(streams)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3321 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3324 | void PeerConnection::CreateVideoReceiver( |
| 3325 | MediaStreamInterface* stream, |
| 3326 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3327 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3328 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3329 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3330 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3331 | auto* video_receiver = new VideoRtpReceiver( |
| 3332 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3333 | video_receiver->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3334 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3335 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3336 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3337 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3338 | Observer()->OnAddTrack(receiver, std::move(streams)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3339 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3342 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3343 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3344 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3345 | const RtpSenderInfo& remote_sender_info) { |
| 3346 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3347 | if (!receiver) { |
| 3348 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3349 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3350 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3351 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3352 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3353 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3354 | } else { |
| 3355 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3356 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3357 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3360 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3361 | MediaStreamInterface* stream) { |
| 3362 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3363 | RTC_DCHECK(track); |
| 3364 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3365 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3366 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3367 | // We already have a sender for this track, so just change the stream_id |
| 3368 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3369 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3370 | return; |
| 3371 | } |
| 3372 | |
| 3373 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3374 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
| 3375 | {stream->id()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3376 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3377 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3378 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3379 | // which will connect the sender to the underlying transport. This can |
| 3380 | // occur if a local session description that contains the ID of the sender |
| 3381 | // is set before AddStream is called. It can also occur if the local |
| 3382 | // session description is not changed and RemoveStream is called, and |
| 3383 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3384 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3385 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3386 | if (sender_info) { |
| 3387 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3388 | } |
| 3389 | } |
| 3390 | |
| 3391 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3392 | // indefinitely, when we have unified plan SDP. |
| 3393 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3394 | MediaStreamInterface* stream) { |
| 3395 | RTC_DCHECK(!IsClosed()); |
| 3396 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3397 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3398 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3399 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3400 | return; |
| 3401 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3402 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3406 | MediaStreamInterface* stream) { |
| 3407 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3408 | RTC_DCHECK(track); |
| 3409 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3410 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3411 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3412 | // We already have a sender for this track, so just change the stream_id |
| 3413 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3414 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3415 | return; |
| 3416 | } |
| 3417 | |
| 3418 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3419 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
| 3420 | {stream->id()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3421 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3422 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3423 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3424 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3425 | if (sender_info) { |
| 3426 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3431 | MediaStreamInterface* stream) { |
| 3432 | RTC_DCHECK(!IsClosed()); |
| 3433 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3434 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3435 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3436 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3437 | return; |
| 3438 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3439 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3440 | } |
| 3441 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3442 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3443 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3444 | if (ice_connection_state_ == new_state) { |
| 3445 | return; |
| 3446 | } |
| 3447 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3448 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3449 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3450 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3451 | return; |
| 3452 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3453 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3454 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3455 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3456 | RTC_DCHECK(ice_connection_state_ != |
| 3457 | PeerConnectionInterface::kIceConnectionClosed); |
| 3458 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3459 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3460 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
| 3463 | void PeerConnection::OnIceGatheringChange( |
| 3464 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3465 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3466 | if (IsClosed()) { |
| 3467 | return; |
| 3468 | } |
| 3469 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3470 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3473 | void PeerConnection::OnIceCandidate( |
| 3474 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3475 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3476 | if (IsClosed()) { |
| 3477 | return; |
| 3478 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3479 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 3480 | if (candidate->candidate().type() == LOCAL_PORT_TYPE && |
| 3481 | candidate->candidate().address().IsPrivateIP()) { |
| 3482 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 3483 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3484 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3487 | void PeerConnection::OnIceCandidatesRemoved( |
| 3488 | const std::vector<cricket::Candidate>& candidates) { |
| 3489 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3490 | if (IsClosed()) { |
| 3491 | return; |
| 3492 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3493 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3494 | } |
| 3495 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3496 | void PeerConnection::ChangeSignalingState( |
| 3497 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3498 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3499 | if (signaling_state_ == signaling_state) { |
| 3500 | return; |
| 3501 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3502 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3503 | << GetSignalingStateString(signaling_state_) |
| 3504 | << " New state: " |
| 3505 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3506 | signaling_state_ = signaling_state; |
| 3507 | if (signaling_state == kClosed) { |
| 3508 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3509 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3510 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3511 | ice_gathering_state_ = kIceGatheringComplete; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3512 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3513 | } |
| 3514 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3515 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3518 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3519 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3520 | if (IsClosed()) { |
| 3521 | return; |
| 3522 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3523 | AddAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3524 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3525 | } |
| 3526 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3527 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3528 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3529 | if (IsClosed()) { |
| 3530 | return; |
| 3531 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3532 | RemoveAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3533 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3537 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3538 | if (IsClosed()) { |
| 3539 | return; |
| 3540 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3541 | AddVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3542 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3546 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3547 | if (IsClosed()) { |
| 3548 | return; |
| 3549 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3550 | RemoveVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3551 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3552 | } |
| 3553 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3554 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3555 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 3556 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3557 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3558 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3559 | } |
| 3560 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3561 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3562 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 3563 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3564 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 3565 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3566 | msg->error = std::move(error); |
| 3567 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3568 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3572 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3573 | RTCError error) { |
| 3574 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 3575 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 3576 | msg->error = std::move(error); |
| 3577 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3578 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3579 | } |
| 3580 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3581 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3582 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3583 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3584 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3585 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3586 | if (IsUnifiedPlan()) { |
| 3587 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3588 | } else { |
| 3589 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3590 | } |
| 3591 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3592 | // Intentionally unset the data channel type for RTP data channel with the |
| 3593 | // second condition. Otherwise the RTP data channels would be successfully |
| 3594 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3595 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3596 | // people won't try to use them. |
| 3597 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3598 | session_options->data_channel_type = data_channel_type(); |
| 3599 | } |
| 3600 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3601 | // Apply ICE restart flag and renomination flag. |
| 3602 | for (auto& options : session_options->media_description_options) { |
| 3603 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3604 | options.transport_options.enable_ice_renomination = |
| 3605 | configuration_.enable_ice_renomination; |
| 3606 | } |
| 3607 | |
| 3608 | session_options->rtcp_cname = rtcp_cname_; |
| 3609 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3610 | session_options->is_unified_plan = IsUnifiedPlan(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3614 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3615 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3616 | // Figure out transceiver directional preferences. |
| 3617 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3618 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3619 | |
| 3620 | // By default, generate sendrecv/recvonly m= sections. |
| 3621 | bool recv_audio = true; |
| 3622 | bool recv_video = true; |
| 3623 | |
| 3624 | // By default, only offer a new m= section if we have media to send with it. |
| 3625 | bool offer_new_audio_description = send_audio; |
| 3626 | bool offer_new_video_description = send_video; |
| 3627 | bool offer_new_data_description = HasDataChannels(); |
| 3628 | |
| 3629 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3630 | if (offer_answer_options.offer_to_receive_audio != |
| 3631 | RTCOfferAnswerOptions::kUndefined) { |
| 3632 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3633 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3634 | offer_new_audio_description || |
| 3635 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3636 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3637 | if (offer_answer_options.offer_to_receive_video != |
| 3638 | RTCOfferAnswerOptions::kUndefined) { |
| 3639 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3640 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3641 | offer_new_video_description || |
| 3642 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3643 | } |
| 3644 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3645 | absl::optional<size_t> audio_index; |
| 3646 | absl::optional<size_t> video_index; |
| 3647 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3648 | // If a current description exists, generate m= sections in the same order, |
| 3649 | // using the first audio/video/data section that appears and rejecting |
| 3650 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3651 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3652 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3653 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3654 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3655 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3656 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3657 | } |
| 3658 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3659 | // Add audio/video/data m= sections to the end if needed. |
| 3660 | if (!audio_index && offer_new_audio_description) { |
| 3661 | session_options->media_description_options.push_back( |
| 3662 | cricket::MediaDescriptionOptions( |
| 3663 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3664 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3665 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3666 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3667 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3668 | if (!video_index && offer_new_video_description) { |
| 3669 | session_options->media_description_options.push_back( |
| 3670 | cricket::MediaDescriptionOptions( |
| 3671 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3672 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3673 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3674 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3675 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3676 | if (!data_index && offer_new_data_description) { |
| 3677 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3678 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3679 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3683 | !audio_index ? nullptr |
| 3684 | : &session_options->media_description_options[*audio_index]; |
| 3685 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3686 | !video_index ? nullptr |
| 3687 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3688 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3689 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 3690 | video_media_description_options, |
| 3691 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | // Find a new MID that is not already in |used_mids|, then add it to |used_mids| |
| 3695 | // and return a reference to it. |
| 3696 | // Generated MIDs should be no more than 3 bytes long to take up less space in |
| 3697 | // the RTP packet. |
| 3698 | static const std::string& AllocateMid(std::set<std::string>* used_mids) { |
| 3699 | RTC_DCHECK(used_mids); |
| 3700 | // We're boring: just generate MIDs 0, 1, 2, ... |
| 3701 | size_t i = 0; |
| 3702 | std::set<std::string>::iterator it; |
| 3703 | bool inserted; |
| 3704 | do { |
| 3705 | std::string mid = rtc::ToString(i++); |
| 3706 | auto insert_result = used_mids->insert(mid); |
| 3707 | it = insert_result.first; |
| 3708 | inserted = insert_result.second; |
| 3709 | } while (!inserted); |
| 3710 | return *it; |
| 3711 | } |
| 3712 | |
| 3713 | static cricket::MediaDescriptionOptions |
| 3714 | GetMediaDescriptionOptionsForTransceiver( |
| 3715 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3716 | transceiver, |
| 3717 | const std::string& mid) { |
| 3718 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3719 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3720 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3721 | // This behavior is specified in JSEP. The gist is that: |
| 3722 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 3723 | // sendrecv. |
| 3724 | // 2. If the MSID is included, then it must be included in any subsequent |
| 3725 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 3726 | if (!transceiver->stopped() && |
| 3727 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 3728 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 3729 | cricket::SenderOptions sender_options; |
| 3730 | sender_options.track_id = transceiver->sender()->id(); |
| 3731 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 3732 | // TODO(bugs.webrtc.org/7600): Set num_sim_layers to the number of encodings |
| 3733 | // set in the RTP parameters when the transceiver was added. |
| 3734 | sender_options.num_sim_layers = 1; |
| 3735 | media_description_options.sender_options.push_back(sender_options); |
| 3736 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3737 | return media_description_options; |
| 3738 | } |
| 3739 | |
| 3740 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 3741 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3742 | cricket::MediaSessionOptions* session_options) { |
| 3743 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3744 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3745 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3746 | const ContentInfos& local_contents = |
| 3747 | (local_description() ? local_description()->description()->contents() |
| 3748 | : ContentInfos()); |
| 3749 | const ContentInfos& remote_contents = |
| 3750 | (remote_description() ? remote_description()->description()->contents() |
| 3751 | : ContentInfos()); |
| 3752 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3753 | // slots first. |
| 3754 | std::queue<size_t> recycleable_mline_indices; |
| 3755 | // Track the MIDs used in previous offer/answer exchanges and the current |
| 3756 | // offer so that new, unique MIDs are generated. |
| 3757 | std::set<std::string> used_mids = seen_mids_; |
| 3758 | // First, go through each media section that exists in either the local or |
| 3759 | // remote description and generate a media section in this offer for the |
| 3760 | // associated transceiver. If a media section can be recycled, generate a |
| 3761 | // default, rejected media section here that can be later overwritten. |
| 3762 | for (size_t i = 0; |
| 3763 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3764 | // Either |local_content| or |remote_content| is non-null. |
| 3765 | const ContentInfo* local_content = |
| 3766 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3767 | const ContentInfo* remote_content = |
| 3768 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3769 | bool had_been_rejected = (local_content && local_content->rejected) || |
| 3770 | (remote_content && remote_content->rejected); |
| 3771 | const std::string& mid = |
| 3772 | (local_content ? local_content->name : remote_content->name); |
| 3773 | cricket::MediaType media_type = |
| 3774 | (local_content ? local_content->media_description()->type() |
| 3775 | : remote_content->media_description()->type()); |
| 3776 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3777 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3778 | auto transceiver = GetAssociatedTransceiver(mid); |
| 3779 | RTC_CHECK(transceiver); |
| 3780 | // A media section is considered eligible for recycling if it is marked as |
| 3781 | // rejected in either the local or remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3782 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3783 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3784 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 3785 | RtpTransceiverDirection::kInactive, |
| 3786 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3787 | recycleable_mline_indices.push(i); |
| 3788 | } else { |
| 3789 | session_options->media_description_options.push_back( |
| 3790 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 3791 | // CreateOffer shouldn't really cause any state changes in |
| 3792 | // PeerConnection, but we need a way to match new transceivers to new |
| 3793 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 3794 | // by recording the index of the media section generated for the |
| 3795 | // transceiver in the offer. |
| 3796 | transceiver->internal()->set_mline_index(i); |
| 3797 | } |
| 3798 | } else { |
| 3799 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3800 | RTC_CHECK(GetDataMid()); |
| 3801 | if (had_been_rejected || mid != *GetDataMid()) { |
| 3802 | session_options->media_description_options.push_back( |
| 3803 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 3804 | } else { |
| 3805 | session_options->media_description_options.push_back( |
| 3806 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 3807 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3811 | // and not associated). Reuse media sections marked as recyclable first, |
| 3812 | // otherwise append to the end of the offer. New media sections should be |
| 3813 | // added in the order they were added to the PeerConnection. |
| 3814 | for (auto transceiver : transceivers_) { |
| 3815 | if (transceiver->mid() || transceiver->stopped()) { |
| 3816 | continue; |
| 3817 | } |
| 3818 | size_t mline_index; |
| 3819 | if (!recycleable_mline_indices.empty()) { |
| 3820 | mline_index = recycleable_mline_indices.front(); |
| 3821 | recycleable_mline_indices.pop(); |
| 3822 | session_options->media_description_options[mline_index] = |
| 3823 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3824 | AllocateMid(&used_mids)); |
| 3825 | } else { |
| 3826 | mline_index = session_options->media_description_options.size(); |
| 3827 | session_options->media_description_options.push_back( |
| 3828 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3829 | AllocateMid(&used_mids))); |
| 3830 | } |
| 3831 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3832 | transceiver->internal()->set_mline_index(mline_index); |
| 3833 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3834 | // Lastly, add a m-section if we have local data channels and an m section |
| 3835 | // does not already exist. |
| 3836 | if (!GetDataMid() && HasDataChannels()) { |
| 3837 | session_options->media_description_options.push_back( |
| 3838 | GetMediaDescriptionOptionsForActiveData(AllocateMid(&used_mids))); |
| 3839 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | void PeerConnection::GetOptionsForAnswer( |
| 3843 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3844 | cricket::MediaSessionOptions* session_options) { |
| 3845 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3846 | |
| 3847 | if (IsUnifiedPlan()) { |
| 3848 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3849 | } else { |
| 3850 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3851 | } |
| 3852 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3853 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3854 | // the RTP data channels would be successfully negotiated by default and the |
| 3855 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3856 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 3857 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3858 | session_options->data_channel_type = data_channel_type(); |
| 3859 | } |
| 3860 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3861 | // Apply ICE renomination flag. |
| 3862 | for (auto& options : session_options->media_description_options) { |
| 3863 | options.transport_options.enable_ice_renomination = |
| 3864 | configuration_.enable_ice_renomination; |
| 3865 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 3866 | |
| 3867 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 3868 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3869 | session_options->is_unified_plan = IsUnifiedPlan(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3870 | } |
| 3871 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3872 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 3873 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3874 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3875 | // Figure out transceiver directional preferences. |
| 3876 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3877 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3878 | |
| 3879 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 3880 | // restricted by the direction in the offer. |
| 3881 | bool recv_audio = true; |
| 3882 | bool recv_video = true; |
| 3883 | |
| 3884 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3885 | if (offer_answer_options.offer_to_receive_audio != |
| 3886 | RTCOfferAnswerOptions::kUndefined) { |
| 3887 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 3888 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3889 | if (offer_answer_options.offer_to_receive_video != |
| 3890 | RTCOfferAnswerOptions::kUndefined) { |
| 3891 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3892 | } |
| 3893 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3894 | absl::optional<size_t> audio_index; |
| 3895 | absl::optional<size_t> video_index; |
| 3896 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 3897 | |
| 3898 | // Generate m= sections that match those in the offer. |
| 3899 | // Note that mediasession.cc will handle intersection our preferred |
| 3900 | // direction with the offered direction. |
| 3901 | GenerateMediaDescriptionOptions( |
| 3902 | remote_description(), |
| 3903 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3904 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 3905 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3906 | |
| 3907 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3908 | !audio_index ? nullptr |
| 3909 | : &session_options->media_description_options[*audio_index]; |
| 3910 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3911 | !video_index ? nullptr |
| 3912 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3913 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3914 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 3915 | video_media_description_options, |
| 3916 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3917 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 3918 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3919 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 3920 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3921 | cricket::MediaSessionOptions* session_options) { |
| 3922 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 3923 | // Answers) and 5.3.2 (Subsequent Answers). |
| 3924 | RTC_DCHECK(remote_description()); |
| 3925 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3926 | for (const ContentInfo& content : |
| 3927 | remote_description()->description()->contents()) { |
| 3928 | cricket::MediaType media_type = content.media_description()->type(); |
| 3929 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3930 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3931 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3932 | RTC_CHECK(transceiver); |
| 3933 | session_options->media_description_options.push_back( |
| 3934 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 3935 | } else { |
| 3936 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3937 | // Reject all data sections if data channels are disabled. |
| 3938 | // Reject a data section if it has already been rejected. |
| 3939 | // Reject all data sections except for the first one. |
| 3940 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 3941 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3942 | session_options->media_description_options.push_back( |
| 3943 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 3944 | } else { |
| 3945 | session_options->media_description_options.push_back( |
| 3946 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 3947 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3948 | } |
| 3949 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3950 | } |
| 3951 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3952 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 3953 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3954 | RtpTransceiverDirection audio_direction, |
| 3955 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3956 | absl::optional<size_t>* audio_index, |
| 3957 | absl::optional<size_t>* video_index, |
| 3958 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3959 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3960 | for (const cricket::ContentInfo& content : |
| 3961 | session_desc->description()->contents()) { |
| 3962 | if (IsAudioContent(&content)) { |
| 3963 | // If we already have an audio m= section, reject this extra one. |
| 3964 | if (*audio_index) { |
| 3965 | session_options->media_description_options.push_back( |
| 3966 | cricket::MediaDescriptionOptions( |
| 3967 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3968 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3969 | } else { |
| 3970 | session_options->media_description_options.push_back( |
| 3971 | cricket::MediaDescriptionOptions( |
| 3972 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3973 | audio_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3974 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3975 | } |
| 3976 | } else if (IsVideoContent(&content)) { |
| 3977 | // If we already have an video m= section, reject this extra one. |
| 3978 | if (*video_index) { |
| 3979 | session_options->media_description_options.push_back( |
| 3980 | cricket::MediaDescriptionOptions( |
| 3981 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3982 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3983 | } else { |
| 3984 | session_options->media_description_options.push_back( |
| 3985 | cricket::MediaDescriptionOptions( |
| 3986 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3987 | video_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3988 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3989 | } |
| 3990 | } else { |
| 3991 | RTC_DCHECK(IsDataContent(&content)); |
| 3992 | // If we already have an data m= section, reject this extra one. |
| 3993 | if (*data_index) { |
| 3994 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3995 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3996 | } else { |
| 3997 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3998 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3999 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4000 | } |
| 4001 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4002 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4003 | } |
| 4004 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4005 | cricket::MediaDescriptionOptions |
| 4006 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 4007 | const std::string& mid) const { |
| 4008 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4009 | // expect sendrecv. |
| 4010 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4011 | RtpTransceiverDirection::kSendRecv, |
| 4012 | /*stopped=*/false); |
| 4013 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4014 | return options; |
| 4015 | } |
| 4016 | |
| 4017 | cricket::MediaDescriptionOptions |
| 4018 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4019 | const std::string& mid) const { |
| 4020 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4021 | RtpTransceiverDirection::kInactive, |
| 4022 | /*stopped=*/true); |
| 4023 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4024 | return options; |
| 4025 | } |
| 4026 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4027 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4028 | switch (data_channel_type_) { |
| 4029 | case cricket::DCT_RTP: |
| 4030 | if (!rtp_data_channel_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4031 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4032 | } |
| 4033 | return rtp_data_channel_->content_name(); |
| 4034 | case cricket::DCT_SCTP: |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4035 | return sctp_mid_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4036 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4037 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4041 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 4042 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4043 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4044 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 4045 | } |
| 4046 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4047 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4048 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4049 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4050 | cricket::MediaType media_type, |
| 4051 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4052 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4053 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4054 | std::vector<RtpSenderInfo>* current_senders = |
| 4055 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4056 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4057 | // 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] | 4058 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4059 | for (auto sender_it = current_senders->begin(); |
| 4060 | sender_it != current_senders->end(); |
| 4061 | /* incremented manually */) { |
| 4062 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4063 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4064 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4065 | std::string params_stream_id; |
| 4066 | if (params) { |
| 4067 | params_stream_id = |
| 4068 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 4069 | : kDefaultStreamId); |
| 4070 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4071 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4072 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4073 | // 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] | 4074 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4075 | sender_exists) { |
| 4076 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4077 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4078 | OnRemoteSenderRemoved(info, media_type); |
| 4079 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4083 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4084 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4085 | if (!params.has_ssrcs()) { |
| 4086 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 4087 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 4088 | // create a default. |
| 4089 | default_sender_needed = true; |
| 4090 | break; |
| 4091 | } |
| 4092 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4093 | // |params.id| is the sender id and the stream id uses the first of |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4094 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4095 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 4096 | // not supported in Plan B, we just take the first here and create the |
| 4097 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4098 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4099 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 4100 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4101 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4102 | uint32_t ssrc = params.first_ssrc(); |
| 4103 | |
| 4104 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4105 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4106 | if (!stream) { |
| 4107 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4108 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4109 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4110 | remote_streams_->AddStream(stream); |
| 4111 | new_streams->AddStream(stream); |
| 4112 | } |
| 4113 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4114 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4115 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4116 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4117 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4118 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4119 | } |
| 4120 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4121 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4122 | // Add default sender if necessary. |
| 4123 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4124 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4125 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4126 | if (!default_stream) { |
| 4127 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4128 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4129 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4130 | remote_streams_->AddStream(default_stream); |
| 4131 | new_streams->AddStream(default_stream); |
| 4132 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4133 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4134 | ? kDefaultAudioSenderId |
| 4135 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4136 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4137 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4138 | if (!default_sender_info) { |
| 4139 | current_senders->push_back( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4140 | RtpSenderInfo(kDefaultStreamId, default_sender_id, 0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4141 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4142 | } |
| 4143 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4144 | } |
| 4145 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4146 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 4147 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4148 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 4149 | << " receiver for track_id=" << sender_info.sender_id |
| 4150 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4151 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4152 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4153 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4154 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4155 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4156 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4157 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 4158 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4159 | } |
| 4160 | } |
| 4161 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4162 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 4163 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4164 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 4165 | << " receiver for track_id=" << sender_info.sender_id |
| 4166 | << " and stream_id=" << sender_info.stream_id; |
| 4167 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4168 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4169 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4170 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4171 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4172 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 4173 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4174 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4175 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4176 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4177 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4178 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4179 | } |
| 4180 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4181 | // Stopping or destroying a VideoRtpReceiver will end the |
| 4182 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4183 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4184 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4185 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4186 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4187 | // There's no guarantee the track is still available, e.g. the track may |
| 4188 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4189 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4190 | } |
| 4191 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 4192 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4193 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4194 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4195 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4196 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4200 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4201 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4202 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4203 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4204 | streams_to_remove.push_back(stream); |
| 4205 | } |
| 4206 | } |
| 4207 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4208 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4209 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4210 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4211 | } |
| 4212 | } |
| 4213 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4214 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4215 | const std::vector<cricket::StreamParams>& streams, |
| 4216 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4217 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4218 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4219 | // 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] | 4220 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4221 | for (auto sender_it = current_senders->begin(); |
| 4222 | sender_it != current_senders->end(); |
| 4223 | /* incremented manually */) { |
| 4224 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4225 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4226 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4227 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4228 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4229 | OnLocalSenderRemoved(info, media_type); |
| 4230 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4231 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4232 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4233 | } |
| 4234 | } |
| 4235 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4236 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4237 | for (const cricket::StreamParams& params : streams) { |
| 4238 | // 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] | 4239 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4240 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4241 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4242 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4243 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4244 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4245 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4246 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4247 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4248 | } |
| 4249 | } |
| 4250 | } |
| 4251 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4252 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4253 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4254 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4255 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4256 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4257 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4258 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4259 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4260 | return; |
| 4261 | } |
| 4262 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4263 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4264 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4265 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4266 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4267 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4268 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4269 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4270 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4271 | } |
| 4272 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4273 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4274 | cricket::MediaType media_type) { |
| 4275 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4276 | if (!sender) { |
| 4277 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4278 | // SessionDescriptions has been renegotiated. |
| 4279 | return; |
| 4280 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4281 | |
| 4282 | // A sender has been removed from the SessionDescription but it's still |
| 4283 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4284 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4285 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4286 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4287 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4288 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4289 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4290 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4291 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4295 | const cricket::StreamParamsVec& streams) { |
| 4296 | std::vector<std::string> existing_channels; |
| 4297 | |
| 4298 | // Find new and active data channels. |
| 4299 | for (const cricket::StreamParams& params : streams) { |
| 4300 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4301 | // we use the same naming of data channels as we do for |
| 4302 | // MediaStreams and Tracks. |
| 4303 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4304 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4305 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4306 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4307 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4308 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4309 | continue; |
| 4310 | } |
| 4311 | // Set the SSRC the data channel should use for sending. |
| 4312 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4313 | existing_channels.push_back(data_channel_it->first); |
| 4314 | } |
| 4315 | |
| 4316 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4317 | } |
| 4318 | |
| 4319 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4320 | const cricket::StreamParamsVec& streams) { |
| 4321 | std::vector<std::string> existing_channels; |
| 4322 | |
| 4323 | // Find new and active data channels. |
| 4324 | for (const cricket::StreamParams& params : streams) { |
| 4325 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4326 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4327 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4328 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4329 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4330 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4331 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4332 | // This is a new data channel. |
| 4333 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4334 | } else { |
| 4335 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4336 | } |
| 4337 | existing_channels.push_back(label); |
| 4338 | } |
| 4339 | |
| 4340 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4341 | } |
| 4342 | |
| 4343 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4344 | const std::vector<std::string>& active_channels, |
| 4345 | bool is_local_update) { |
| 4346 | auto it = rtp_data_channels_.begin(); |
| 4347 | while (it != rtp_data_channels_.end()) { |
| 4348 | DataChannel* data_channel = it->second; |
| 4349 | if (std::find(active_channels.begin(), active_channels.end(), |
| 4350 | data_channel->label()) != active_channels.end()) { |
| 4351 | ++it; |
| 4352 | continue; |
| 4353 | } |
| 4354 | |
| 4355 | if (is_local_update) { |
| 4356 | data_channel->SetSendSsrc(0); |
| 4357 | } else { |
| 4358 | data_channel->RemotePeerRequestClose(); |
| 4359 | } |
| 4360 | |
| 4361 | if (data_channel->state() == DataChannel::kClosed) { |
| 4362 | rtp_data_channels_.erase(it); |
| 4363 | it = rtp_data_channels_.begin(); |
| 4364 | } else { |
| 4365 | ++it; |
| 4366 | } |
| 4367 | } |
| 4368 | } |
| 4369 | |
| 4370 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4371 | uint32_t remote_ssrc) { |
| 4372 | rtc::scoped_refptr<DataChannel> channel( |
| 4373 | InternalCreateDataChannel(label, nullptr)); |
| 4374 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4375 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4376 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4377 | return; |
| 4378 | } |
| 4379 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4380 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4381 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4382 | Observer()->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4386 | const std::string& label, |
| 4387 | const InternalDataChannelInit* config) { |
| 4388 | if (IsClosed()) { |
| 4389 | return nullptr; |
| 4390 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4391 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4392 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4393 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4394 | return nullptr; |
| 4395 | } |
| 4396 | InternalDataChannelInit new_config = |
| 4397 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4398 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4399 | if (new_config.id < 0) { |
| 4400 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4401 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4402 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4403 | RTC_LOG(LS_ERROR) |
| 4404 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4405 | return nullptr; |
| 4406 | } |
| 4407 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4408 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4409 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4410 | return nullptr; |
| 4411 | } |
| 4412 | } |
| 4413 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4414 | rtc::scoped_refptr<DataChannel> channel( |
| 4415 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4416 | if (!channel) { |
| 4417 | sid_allocator_.ReleaseSid(new_config.id); |
| 4418 | return nullptr; |
| 4419 | } |
| 4420 | |
| 4421 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4422 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4423 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4424 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4425 | return nullptr; |
| 4426 | } |
| 4427 | rtp_data_channels_[channel->label()] = channel; |
| 4428 | } else { |
| 4429 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 4430 | sctp_data_channels_.push_back(channel); |
| 4431 | channel->SignalClosed.connect(this, |
| 4432 | &PeerConnection::OnSctpDataChannelClosed); |
| 4433 | } |
| 4434 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4435 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4436 | return channel; |
| 4437 | } |
| 4438 | |
| 4439 | bool PeerConnection::HasDataChannels() const { |
| 4440 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4441 | } |
| 4442 | |
| 4443 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4444 | for (const auto& channel : sctp_data_channels_) { |
| 4445 | if (channel->id() < 0) { |
| 4446 | int sid; |
| 4447 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4448 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4449 | continue; |
| 4450 | } |
| 4451 | channel->SetSctpSid(sid); |
| 4452 | } |
| 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4457 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4458 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4459 | ++it) { |
| 4460 | if (it->get() == channel) { |
| 4461 | if (channel->id() >= 0) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 4462 | // After the closing procedure is done, it's safe to use this ID for |
| 4463 | // another data channel. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4464 | sid_allocator_.ReleaseSid(channel->id()); |
| 4465 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4466 | // Since this method is triggered by a signal from the DataChannel, |
| 4467 | // we can't free it directly here; we need to free it asynchronously. |
| 4468 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4469 | sctp_data_channels_.erase(it); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 4470 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4471 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4472 | return; |
| 4473 | } |
| 4474 | } |
| 4475 | } |
| 4476 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4477 | void PeerConnection::OnDataChannelDestroyed() { |
| 4478 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4479 | // DataChannel may callback to us and try to modify the list. |
| 4480 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4481 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4482 | for (const auto& kv : temp_rtp_dcs) { |
| 4483 | kv.second->OnTransportChannelDestroyed(); |
| 4484 | } |
| 4485 | |
| 4486 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4487 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4488 | for (const auto& channel : temp_sctp_dcs) { |
| 4489 | channel->OnTransportChannelDestroyed(); |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | void PeerConnection::OnDataChannelOpenMessage( |
| 4494 | const std::string& label, |
| 4495 | const InternalDataChannelInit& config) { |
| 4496 | rtc::scoped_refptr<DataChannel> channel( |
| 4497 | InternalCreateDataChannel(label, &config)); |
| 4498 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4499 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4500 | return; |
| 4501 | } |
| 4502 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4503 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4504 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4505 | Observer()->OnDataChannel(std::move(proxy_channel)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 4506 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4507 | } |
| 4508 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4509 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4510 | PeerConnection::GetAudioTransceiver() const { |
| 4511 | // This method only works with Plan B SDP, where there is a single |
| 4512 | // audio/video transceiver. |
| 4513 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4514 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4515 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4516 | return transceiver; |
| 4517 | } |
| 4518 | } |
| 4519 | RTC_NOTREACHED(); |
| 4520 | return nullptr; |
| 4521 | } |
| 4522 | |
| 4523 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4524 | PeerConnection::GetVideoTransceiver() const { |
| 4525 | // This method only works with Plan B SDP, where there is a single |
| 4526 | // audio/video transceiver. |
| 4527 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4528 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4529 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4530 | return transceiver; |
| 4531 | } |
| 4532 | } |
| 4533 | RTC_NOTREACHED(); |
| 4534 | return nullptr; |
| 4535 | } |
| 4536 | |
| 4537 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4538 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4539 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4540 | switch (type) { |
| 4541 | case cricket::MEDIA_TYPE_AUDIO: |
| 4542 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4543 | case cricket::MEDIA_TYPE_VIDEO: |
| 4544 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4545 | case cricket::MEDIA_TYPE_DATA: |
| 4546 | return false; |
| 4547 | } |
| 4548 | RTC_NOTREACHED(); |
| 4549 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4550 | } |
| 4551 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4552 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4553 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 4554 | for (auto transceiver : transceivers_) { |
| 4555 | for (auto sender : transceiver->internal()->senders()) { |
| 4556 | if (sender->track() == track) { |
| 4557 | return sender; |
| 4558 | } |
| 4559 | } |
| 4560 | } |
| 4561 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4562 | } |
| 4563 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4564 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4565 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 4566 | for (auto transceiver : transceivers_) { |
| 4567 | for (auto sender : transceiver->internal()->senders()) { |
| 4568 | if (sender->id() == sender_id) { |
| 4569 | return sender; |
| 4570 | } |
| 4571 | } |
| 4572 | } |
| 4573 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4576 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4577 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 4578 | for (auto transceiver : transceivers_) { |
| 4579 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4580 | if (receiver->id() == receiver_id) { |
| 4581 | return receiver; |
| 4582 | } |
| 4583 | } |
| 4584 | } |
| 4585 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4586 | } |
| 4587 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4588 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4589 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4590 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4591 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4592 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4593 | ? &remote_audio_sender_infos_ |
| 4594 | : &remote_video_sender_infos_; |
| 4595 | } |
| 4596 | |
| 4597 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4598 | cricket::MediaType media_type) { |
| 4599 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4600 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4601 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4602 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4603 | } |
| 4604 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4605 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4606 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4607 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4608 | const std::string sender_id) const { |
| 4609 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4610 | if (sender_info.stream_id == stream_id && |
| 4611 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4612 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4613 | } |
| 4614 | } |
| 4615 | return nullptr; |
| 4616 | } |
| 4617 | |
| 4618 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4619 | for (const auto& channel : sctp_data_channels_) { |
| 4620 | if (channel->id() == sid) { |
| 4621 | return channel; |
| 4622 | } |
| 4623 | } |
| 4624 | return nullptr; |
| 4625 | } |
| 4626 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4627 | bool PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 4628 | const cricket::ServerAddresses& stun_servers, |
| 4629 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4630 | const RTCConfiguration& configuration) { |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 4631 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4632 | // To handle both internal and externally created port allocator, we will |
| 4633 | // enable BUNDLE here. |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4634 | port_allocator_flags_ = port_allocator_->flags(); |
| 4635 | port_allocator_flags_ |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 4636 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 4637 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4638 | // If the disable-IPv6 flag was specified, we'll not override it |
| 4639 | // by experiment. |
| 4640 | if (configuration.disable_ipv6) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4641 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 4642 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 4643 | .find("Disabled") == 0) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4644 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4645 | } |
| 4646 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4647 | if (configuration.disable_ipv6_on_wifi) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4648 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4649 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4650 | } |
| 4651 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4652 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4653 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4654 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4655 | } |
| 4656 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4657 | if (configuration.candidate_network_policy == |
| 4658 | kCandidateNetworkPolicyLowCost) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4659 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4660 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4661 | } |
| 4662 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4663 | if (configuration.disable_link_local_networks) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4664 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4665 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 4666 | } |
| 4667 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4668 | port_allocator_->set_flags(port_allocator_flags_); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4669 | // No step delay is used while allocating ports. |
| 4670 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 4671 | port_allocator_->set_candidate_filter( |
| 4672 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 4673 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4674 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 4675 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 4676 | for (auto& turn_server : turn_servers_copy) { |
| 4677 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 4678 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4679 | // Call this last since it may create pooled allocator sessions using the |
| 4680 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4681 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 4682 | stun_servers, std::move(turn_servers_copy), |
| 4683 | configuration.ice_candidate_pool_size, configuration.prune_turn_ports, |
| 4684 | configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4685 | configuration.stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4686 | return true; |
| 4687 | } |
| 4688 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4689 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4690 | const cricket::ServerAddresses& stun_servers, |
| 4691 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 4692 | IceTransportsType type, |
| 4693 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4694 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4695 | webrtc::TurnCustomizer* turn_customizer, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4696 | absl::optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4697 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4698 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4699 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 4700 | // size is not allowed, and changing the set of ICE servers will not result |
| 4701 | // in new candidates being gathered. |
| 4702 | if (local_description()) { |
| 4703 | port_allocator_->FreezeCandidatePool(); |
| 4704 | } |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 4705 | // Add the custom tls turn servers if they exist. |
| 4706 | auto turn_servers_copy = turn_servers; |
| 4707 | for (auto& turn_server : turn_servers_copy) { |
| 4708 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 4709 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4710 | // Call this last since it may create pooled allocator sessions using the |
| 4711 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 4712 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 4713 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
| 4714 | prune_turn_ports, turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4715 | } |
| 4716 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4717 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 4718 | return factory_->channel_manager(); |
| 4719 | } |
| 4720 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 4721 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4722 | std::unique_ptr<RtcEventLogOutput> output, |
| 4723 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4724 | if (!event_log_) { |
| 4725 | return false; |
| 4726 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4727 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4731 | if (event_log_) { |
| 4732 | event_log_->StopLogging(); |
| 4733 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4734 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4735 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4736 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 4737 | const std::string& content_name) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4738 | for (auto transceiver : transceivers_) { |
| 4739 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4740 | if (channel && channel->content_name() == content_name) { |
| 4741 | return channel; |
| 4742 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4743 | } |
| 4744 | if (rtp_data_channel() && |
| 4745 | rtp_data_channel()->content_name() == content_name) { |
| 4746 | return rtp_data_channel(); |
| 4747 | } |
| 4748 | return nullptr; |
| 4749 | } |
| 4750 | |
| 4751 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 4752 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4753 | RTC_LOG(LS_INFO) |
| 4754 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4755 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4756 | return false; |
| 4757 | } |
| 4758 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4759 | 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] | 4760 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4761 | return false; |
| 4762 | } |
| 4763 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4764 | auto dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
| 4765 | if (dtls_role) { |
| 4766 | *role = *dtls_role; |
| 4767 | return true; |
| 4768 | } |
| 4769 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 4773 | rtc::SSLRole* role) { |
| 4774 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4775 | RTC_LOG(LS_INFO) |
| 4776 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4777 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4778 | return false; |
| 4779 | } |
| 4780 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4781 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 4782 | if (dtls_role) { |
| 4783 | *role = *dtls_role; |
| 4784 | return true; |
| 4785 | } |
| 4786 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4787 | } |
| 4788 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4789 | void PeerConnection::SetSessionError(SessionError error, |
| 4790 | const std::string& error_desc) { |
| 4791 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4792 | if (error != session_error_) { |
| 4793 | session_error_ = error; |
| 4794 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4795 | } |
| 4796 | } |
| 4797 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4798 | RTCError PeerConnection::UpdateSessionState( |
| 4799 | SdpType type, |
| 4800 | cricket::ContentSource source, |
| 4801 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4802 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4803 | |
| 4804 | // If there's already a pending error then no state transition should happen. |
| 4805 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4806 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4807 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4808 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4809 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4810 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4811 | } |
| 4812 | |
| 4813 | // Update the signaling state according to the specified state machine (see |
| 4814 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4815 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4816 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4817 | ? PeerConnectionInterface::kHaveLocalOffer |
| 4818 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4819 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4820 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4821 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 4822 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4823 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4824 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4825 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 4826 | } |
| 4827 | |
| 4828 | // Update internal objects according to the session description's media |
| 4829 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4830 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4831 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 4832 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4833 | } |
| 4834 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4835 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4836 | } |
| 4837 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4838 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4839 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4840 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4841 | const SessionDescriptionInterface* sdesc = |
| 4842 | (source == cricket::CS_LOCAL ? local_description() |
| 4843 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4844 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4845 | |
| 4846 | // Push down the new SDP media section for each audio/video transceiver. |
| 4847 | for (auto transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4848 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4849 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4850 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4851 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4852 | continue; |
| 4853 | } |
| 4854 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4855 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4856 | if (!content_desc) { |
| 4857 | continue; |
| 4858 | } |
| 4859 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 4860 | bool success = (source == cricket::CS_LOCAL) |
| 4861 | ? channel->SetLocalContent(content_desc, type, &error) |
| 4862 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4863 | if (!success) { |
| 4864 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, std::move(error)); |
| 4865 | } |
| 4866 | } |
| 4867 | |
| 4868 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 4869 | if (rtp_data_channel_) { |
| 4870 | const ContentInfo* data_content = |
| 4871 | cricket::GetFirstDataContent(sdesc->description()); |
| 4872 | if (data_content && !data_content->rejected) { |
| 4873 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4874 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4875 | if (data_desc) { |
| 4876 | std::string error; |
| 4877 | bool success = |
| 4878 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4879 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 4880 | : rtp_data_channel_->SetRemoteContent(data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4881 | if (!success) { |
| 4882 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4883 | std::move(error)); |
| 4884 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4885 | } |
| 4886 | } |
| 4887 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4888 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4889 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 4890 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 4891 | if (sctp_transport_ && local_description() && remote_description() && |
| 4892 | cricket::GetFirstDataContent(local_description()->description()) && |
| 4893 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4894 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4895 | RTC_FROM_HERE, |
| 4896 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4897 | if (!success) { |
| 4898 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4899 | "Failed to push down SCTP parameters."); |
| 4900 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4901 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4902 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4903 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 4907 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4908 | RTC_DCHECK(local_description()); |
| 4909 | RTC_DCHECK(remote_description()); |
| 4910 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 4911 | // When we support "max-message-size", that would also be pushed down here. |
| 4912 | return sctp_transport_->Start( |
| 4913 | GetSctpPort(local_description()->description()), |
| 4914 | GetSctpPort(remote_description()->description())); |
| 4915 | } |
| 4916 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4917 | RTCError PeerConnection::PushdownTransportDescription( |
| 4918 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4919 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4920 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4921 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4922 | if (source == cricket::CS_LOCAL) { |
| 4923 | const SessionDescriptionInterface* sdesc = local_description(); |
| 4924 | RTC_DCHECK(sdesc); |
| 4925 | return transport_controller_->SetLocalDescription(type, |
| 4926 | sdesc->description()); |
| 4927 | } else { |
| 4928 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 4929 | RTC_DCHECK(sdesc); |
| 4930 | return transport_controller_->SetRemoteDescription(type, |
| 4931 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4932 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | bool PeerConnection::GetTransportDescription( |
| 4936 | const SessionDescription* description, |
| 4937 | const std::string& content_name, |
| 4938 | cricket::TransportDescription* tdesc) { |
| 4939 | if (!description || !tdesc) { |
| 4940 | return false; |
| 4941 | } |
| 4942 | const TransportInfo* transport_info = |
| 4943 | description->GetTransportInfoByName(content_name); |
| 4944 | if (!transport_info) { |
| 4945 | return false; |
| 4946 | } |
| 4947 | *tdesc = transport_info->description; |
| 4948 | return true; |
| 4949 | } |
| 4950 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4951 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 4952 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 4953 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4954 | switch (config.continual_gathering_policy) { |
| 4955 | case PeerConnectionInterface::GATHER_ONCE: |
| 4956 | gathering_policy = cricket::GATHER_ONCE; |
| 4957 | break; |
| 4958 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 4959 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 4960 | break; |
| 4961 | default: |
| 4962 | RTC_NOTREACHED(); |
| 4963 | gathering_policy = cricket::GATHER_ONCE; |
| 4964 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4965 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4966 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 4967 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 4968 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4969 | ice_config.prioritize_most_likely_candidate_pairs = |
| 4970 | config.prioritize_most_likely_ice_candidate_pairs; |
| 4971 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 4972 | RTCConfigurationToIceConfigOptionalInt( |
| 4973 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4974 | ice_config.continual_gathering_policy = gathering_policy; |
| 4975 | ice_config.presume_writable_when_fully_relayed = |
| 4976 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 4977 | ice_config.ice_check_interval_strong_connectivity = |
| 4978 | config.ice_check_interval_strong_connectivity; |
| 4979 | ice_config.ice_check_interval_weak_connectivity = |
| 4980 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4981 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4982 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4983 | ice_config.regather_all_networks_interval_range = |
| 4984 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4985 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4986 | return ice_config; |
| 4987 | } |
| 4988 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4989 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 4990 | std::string* track_id) { |
| 4991 | if (!local_description()) { |
| 4992 | return false; |
| 4993 | } |
| 4994 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 4995 | track_id); |
| 4996 | } |
| 4997 | |
| 4998 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 4999 | std::string* track_id) { |
| 5000 | if (!remote_description()) { |
| 5001 | return false; |
| 5002 | } |
| 5003 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 5004 | track_id); |
| 5005 | } |
| 5006 | |
| 5007 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 5008 | const rtc::CopyOnWriteBuffer& payload, |
| 5009 | cricket::SendDataResult* result) { |
| 5010 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5011 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5012 | "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5013 | return false; |
| 5014 | } |
| 5015 | return rtp_data_channel_ |
| 5016 | ? rtp_data_channel_->SendData(params, payload, result) |
| 5017 | : network_thread()->Invoke<bool>( |
| 5018 | RTC_FROM_HERE, |
| 5019 | Bind(&cricket::SctpTransportInternal::SendData, |
| 5020 | sctp_transport_.get(), params, payload, result)); |
| 5021 | } |
| 5022 | |
| 5023 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 5024 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 5025 | // Don't log an error here, because DataChannels are expected to call |
| 5026 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 5027 | // whether or not the underlying transport is ready. |
| 5028 | return false; |
| 5029 | } |
| 5030 | if (rtp_data_channel_) { |
| 5031 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 5032 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 5033 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 5034 | &DataChannel::OnDataReceived); |
| 5035 | } else { |
| 5036 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 5037 | &DataChannel::OnChannelReady); |
| 5038 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 5039 | &DataChannel::OnDataReceived); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5040 | SignalSctpClosingProcedureStartedRemotely.connect( |
| 5041 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5042 | SignalSctpClosingProcedureComplete.connect( |
| 5043 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5044 | } |
| 5045 | return true; |
| 5046 | } |
| 5047 | |
| 5048 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 5049 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5050 | RTC_LOG(LS_ERROR) |
| 5051 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 5052 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5053 | return; |
| 5054 | } |
| 5055 | if (rtp_data_channel_) { |
| 5056 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 5057 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 5058 | } else { |
| 5059 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 5060 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5061 | SignalSctpClosingProcedureStartedRemotely.disconnect(webrtc_data_channel); |
| 5062 | SignalSctpClosingProcedureComplete.disconnect(webrtc_data_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5063 | } |
| 5064 | } |
| 5065 | |
| 5066 | void PeerConnection::AddSctpDataStream(int sid) { |
| 5067 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5068 | RTC_LOG(LS_ERROR) |
| 5069 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5070 | return; |
| 5071 | } |
| 5072 | network_thread()->Invoke<void>( |
| 5073 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 5074 | sctp_transport_.get(), sid)); |
| 5075 | } |
| 5076 | |
| 5077 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 5078 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5079 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5080 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5081 | return; |
| 5082 | } |
| 5083 | network_thread()->Invoke<void>( |
| 5084 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 5085 | sctp_transport_.get(), sid)); |
| 5086 | } |
| 5087 | |
| 5088 | bool PeerConnection::ReadyToSendData() const { |
| 5089 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 5090 | sctp_ready_to_send_data_; |
| 5091 | } |
| 5092 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5093 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5094 | if (sctp_mid_ && transport_controller_) { |
| 5095 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 5096 | if (dtls_transport) { |
| 5097 | return dtls_transport->transport_name(); |
| 5098 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5099 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5100 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5101 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5102 | } |
| 5103 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5104 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 5105 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5106 | network_thread()->Invoke<void>( |
| 5107 | RTC_FROM_HERE, |
| 5108 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 5109 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5110 | return candidate_states_list; |
| 5111 | } |
| 5112 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5113 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 5114 | const { |
| 5115 | std::map<std::string, std::string> transport_names_by_mid; |
| 5116 | for (auto transceiver : transceivers_) { |
| 5117 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5118 | if (channel) { |
| 5119 | transport_names_by_mid[channel->content_name()] = |
| 5120 | channel->transport_name(); |
| 5121 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5122 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5123 | if (rtp_data_channel_) { |
| 5124 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5125 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5126 | } |
| 5127 | if (sctp_transport_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5128 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5129 | RTC_DCHECK(transport_name); |
| 5130 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5131 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5132 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5133 | } |
| 5134 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5135 | std::map<std::string, cricket::TransportStats> |
| 5136 | PeerConnection::GetTransportStatsByNames( |
| 5137 | const std::set<std::string>& transport_names) { |
| 5138 | if (!network_thread()->IsCurrent()) { |
| 5139 | return network_thread() |
| 5140 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5141 | RTC_FROM_HERE, |
| 5142 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5143 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5144 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5145 | for (const std::string& transport_name : transport_names) { |
| 5146 | cricket::TransportStats transport_stats; |
| 5147 | bool success = |
| 5148 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5149 | if (success) { |
| 5150 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5151 | } else { |
| 5152 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5153 | << transport_name; |
| 5154 | } |
| 5155 | } |
| 5156 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | bool PeerConnection::GetLocalCertificate( |
| 5160 | const std::string& transport_name, |
| 5161 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5162 | if (!certificate) { |
| 5163 | return false; |
| 5164 | } |
| 5165 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 5166 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5167 | } |
| 5168 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5169 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5170 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5171 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5172 | } |
| 5173 | |
| 5174 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 5175 | return data_channel_type_; |
| 5176 | } |
| 5177 | |
| 5178 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5179 | return pending_ice_restarts_.find(content_name) != |
| 5180 | pending_ice_restarts_.end(); |
| 5181 | } |
| 5182 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5183 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5184 | return transport_controller_->NeedsIceRestart(content_name); |
| 5185 | } |
| 5186 | |
| 5187 | void PeerConnection::OnCertificateReady( |
| 5188 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5189 | transport_controller_->SetLocalCertificate(certificate); |
| 5190 | } |
| 5191 | |
| 5192 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5193 | SetSessionError(SessionError::kTransport, |
| 5194 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | void PeerConnection::OnTransportControllerConnectionState( |
| 5198 | cricket::IceConnectionState state) { |
| 5199 | switch (state) { |
| 5200 | case cricket::kIceConnectionConnecting: |
| 5201 | // If the current state is Connected or Completed, then there were |
| 5202 | // writable channels but now there are not, so the next state must |
| 5203 | // be Disconnected. |
| 5204 | // kIceConnectionConnecting is currently used as the default, |
| 5205 | // un-connected state by the TransportController, so its only use is |
| 5206 | // detecting disconnections. |
| 5207 | if (ice_connection_state_ == |
| 5208 | PeerConnectionInterface::kIceConnectionConnected || |
| 5209 | ice_connection_state_ == |
| 5210 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5211 | SetIceConnectionState( |
| 5212 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5213 | } |
| 5214 | break; |
| 5215 | case cricket::kIceConnectionFailed: |
| 5216 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5217 | break; |
| 5218 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5219 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5220 | "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5221 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 5222 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5223 | break; |
| 5224 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5225 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5226 | "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5227 | if (ice_connection_state_ != |
| 5228 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5229 | // If jumping directly from "checking" to "connected", |
| 5230 | // signal "connected" first. |
| 5231 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5232 | } |
| 5233 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 5234 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5235 | ReportTransportStats(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5236 | break; |
| 5237 | default: |
| 5238 | RTC_NOTREACHED(); |
| 5239 | } |
| 5240 | } |
| 5241 | |
| 5242 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5243 | const std::string& transport_name, |
| 5244 | const cricket::Candidates& candidates) { |
| 5245 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5246 | int sdp_mline_index; |
| 5247 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5248 | RTC_LOG(LS_ERROR) |
| 5249 | << "OnTransportControllerCandidatesGathered: content name " |
| 5250 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5251 | return; |
| 5252 | } |
| 5253 | |
| 5254 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5255 | citer != candidates.end(); ++citer) { |
| 5256 | // Use transport_name as the candidate media id. |
| 5257 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5258 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5259 | if (local_description()) { |
| 5260 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5261 | } |
| 5262 | OnIceCandidate(std::move(candidate)); |
| 5263 | } |
| 5264 | } |
| 5265 | |
| 5266 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5267 | const std::vector<cricket::Candidate>& candidates) { |
| 5268 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5269 | // Sanity check. |
| 5270 | for (const cricket::Candidate& candidate : candidates) { |
| 5271 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5272 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5273 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5274 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5275 | return; |
| 5276 | } |
| 5277 | } |
| 5278 | |
| 5279 | if (local_description()) { |
| 5280 | mutable_local_description()->RemoveCandidates(candidates); |
| 5281 | } |
| 5282 | OnIceCandidatesRemoved(candidates); |
| 5283 | } |
| 5284 | |
| 5285 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5286 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5287 | RTC_HISTOGRAM_ENUMERATION( |
| 5288 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 5289 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5290 | } |
| 5291 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5292 | void PeerConnection::EnableSending() { |
| 5293 | for (auto transceiver : transceivers_) { |
| 5294 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5295 | if (channel && !channel->enabled()) { |
| 5296 | channel->Enable(true); |
| 5297 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5298 | } |
| 5299 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5300 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5301 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5302 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | // Returns the media index for a local ice candidate given the content name. |
| 5306 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5307 | const std::string& content_name, |
| 5308 | int* sdp_mline_index) { |
| 5309 | if (!local_description() || !sdp_mline_index) { |
| 5310 | return false; |
| 5311 | } |
| 5312 | |
| 5313 | bool content_found = false; |
| 5314 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5315 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5316 | if (contents[index].name == content_name) { |
| 5317 | *sdp_mline_index = static_cast<int>(index); |
| 5318 | content_found = true; |
| 5319 | break; |
| 5320 | } |
| 5321 | } |
| 5322 | return content_found; |
| 5323 | } |
| 5324 | |
| 5325 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5326 | const SessionDescriptionInterface* remote_desc) { |
| 5327 | if (!remote_desc) { |
| 5328 | return true; |
| 5329 | } |
| 5330 | bool ret = true; |
| 5331 | |
| 5332 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5333 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5334 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5335 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5336 | bool valid = false; |
| 5337 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5338 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5339 | RTC_LOG(LS_INFO) |
| 5340 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5341 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5342 | } |
| 5343 | continue; |
| 5344 | } |
| 5345 | ret = UseCandidate(candidate); |
| 5346 | if (!ret) { |
| 5347 | break; |
| 5348 | } |
| 5349 | } |
| 5350 | } |
| 5351 | return ret; |
| 5352 | } |
| 5353 | |
| 5354 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5355 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5356 | size_t remote_content_size = |
| 5357 | remote_description()->description()->contents().size(); |
| 5358 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5359 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5360 | return false; |
| 5361 | } |
| 5362 | |
| 5363 | cricket::ContentInfo content = |
| 5364 | remote_description()->description()->contents()[mediacontent_index]; |
| 5365 | std::vector<cricket::Candidate> candidates; |
| 5366 | candidates.push_back(candidate->candidate()); |
| 5367 | // Invoking BaseSession method to handle remote candidates. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5368 | RTCError error = |
| 5369 | transport_controller_->AddRemoteCandidates(content.name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 5370 | if (error.ok()) { |
| 5371 | // Candidates successfully submitted for checking. |
| 5372 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5373 | ice_connection_state_ == |
| 5374 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5375 | // If state is New, then the session has just gotten its first remote ICE |
| 5376 | // candidates, so go to Checking. |
| 5377 | // If state is Disconnected, the session is re-using old candidates or |
| 5378 | // receiving additional ones, so go to Checking. |
| 5379 | // If state is Connected, stay Connected. |
| 5380 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5381 | // newly added transport, then the state actually _should_ move to |
| 5382 | // checking. Add a way to distinguish that case. |
| 5383 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5384 | } |
| 5385 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 5386 | } else if (error.message()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5387 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5388 | } |
| 5389 | return true; |
| 5390 | } |
| 5391 | |
| 5392 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5393 | // Destroy video channel first since it may have a pointer to the |
| 5394 | // voice channel. |
| 5395 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5396 | if (!video_info || video_info->rejected) { |
| 5397 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5398 | } |
| 5399 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5400 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5401 | if (!audio_info || audio_info->rejected) { |
| 5402 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5403 | } |
| 5404 | |
| 5405 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5406 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5407 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5408 | } |
| 5409 | } |
| 5410 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5411 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5412 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5413 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5414 | if (configuration_.bundle_policy == |
| 5415 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5416 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5417 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5418 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5419 | "max-bundle configured but session description " |
| 5420 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5421 | } |
| 5422 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5423 | return std::move(bundle_group); |
| 5424 | } |
| 5425 | |
| 5426 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5427 | // Creating the media channels. Transports should already have been created |
| 5428 | // at this point. |
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()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5432 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5433 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5434 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5435 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5436 | } |
| 5437 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5438 | } |
| 5439 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5440 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5441 | if (video && !video->rejected && |
| 5442 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5443 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5444 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5445 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5446 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5447 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5448 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5449 | } |
| 5450 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5451 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5452 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 5453 | !rtp_data_channel_ && !sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5454 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5455 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5456 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5457 | } |
| 5458 | } |
| 5459 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5460 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5461 | } |
| 5462 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5463 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5464 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5465 | const std::string& mid) { |
| 5466 | RtpTransportInternal* rtp_transport = |
| 5467 | transport_controller_->GetRtpTransport(mid); |
| 5468 | RTC_DCHECK(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5469 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5470 | call_.get(), configuration_.media_config, rtp_transport, |
| 5471 | signaling_thread(), mid, SrtpRequired(), |
| 5472 | factory_->options().crypto_options, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5473 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5474 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5475 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5476 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5477 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5478 | voice_channel->SignalSentPacket.connect(this, |
| 5479 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5480 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5481 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5482 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5483 | } |
| 5484 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5485 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5486 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5487 | const std::string& mid) { |
| 5488 | RtpTransportInternal* rtp_transport = |
| 5489 | transport_controller_->GetRtpTransport(mid); |
| 5490 | RTC_DCHECK(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5491 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5492 | call_.get(), configuration_.media_config, rtp_transport, |
| 5493 | signaling_thread(), mid, SrtpRequired(), |
| 5494 | factory_->options().crypto_options, video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5495 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5496 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5497 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5498 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5499 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5500 | video_channel->SignalSentPacket.connect(this, |
| 5501 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5502 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5503 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5504 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5505 | } |
| 5506 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5507 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5508 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 5509 | if (sctp) { |
| 5510 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5511 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5512 | << "Trying to create SCTP transport, but didn't compile with " |
| 5513 | "SCTP support (HAVE_SCTP)"; |
| 5514 | return false; |
| 5515 | } |
| 5516 | if (!network_thread()->Invoke<bool>( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5517 | RTC_FROM_HERE, |
| 5518 | rtc::Bind(&PeerConnection::CreateSctpTransport_n, this, mid))) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5519 | return false; |
| 5520 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5521 | for (const auto& channel : sctp_data_channels_) { |
| 5522 | channel->OnTransportChannelCreated(); |
| 5523 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5524 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5525 | RtpTransportInternal* rtp_transport = |
| 5526 | transport_controller_->GetRtpTransport(mid); |
| 5527 | RTC_DCHECK(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5528 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5529 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
| 5530 | SrtpRequired(), factory_->options().crypto_options); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5531 | if (!rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5532 | return false; |
| 5533 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5534 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 5535 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 5536 | rtp_data_channel_->SignalSentPacket.connect( |
| 5537 | this, &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5538 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5539 | } |
| 5540 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5541 | return true; |
| 5542 | } |
| 5543 | |
| 5544 | Call::Stats PeerConnection::GetCallStats() { |
| 5545 | if (!worker_thread()->IsCurrent()) { |
| 5546 | return worker_thread()->Invoke<Call::Stats>( |
| 5547 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 5548 | } |
| 5549 | if (call_) { |
| 5550 | return call_->GetStats(); |
| 5551 | } else { |
| 5552 | return Call::Stats(); |
| 5553 | } |
| 5554 | } |
| 5555 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5556 | bool PeerConnection::CreateSctpTransport_n(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5557 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5558 | RTC_DCHECK(sctp_factory_); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 5559 | cricket::DtlsTransportInternal* dtls_transport = |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5560 | transport_controller_->GetDtlsTransport(mid); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 5561 | RTC_DCHECK(dtls_transport); |
| 5562 | sctp_transport_ = sctp_factory_->CreateSctpTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5563 | RTC_DCHECK(sctp_transport_); |
| 5564 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 5565 | sctp_transport_->SignalReadyToSendData.connect( |
| 5566 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 5567 | sctp_transport_->SignalDataReceived.connect( |
| 5568 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5569 | // TODO(deadbeef): All we do here is AsyncInvoke to fire the signal on |
| 5570 | // another thread. Would be nice if there was a helper class similar to |
| 5571 | // sigslot::repeater that did this for us, eliminating a bunch of boilerplate |
| 5572 | // code. |
| 5573 | sctp_transport_->SignalClosingProcedureStartedRemotely.connect( |
| 5574 | this, &PeerConnection::OnSctpClosingProcedureStartedRemotely_n); |
| 5575 | sctp_transport_->SignalClosingProcedureComplete.connect( |
| 5576 | this, &PeerConnection::OnSctpClosingProcedureComplete_n); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5577 | sctp_mid_ = mid; |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 5578 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5579 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | void PeerConnection::DestroySctpTransport_n() { |
| 5583 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5584 | sctp_transport_.reset(nullptr); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5585 | sctp_mid_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5586 | sctp_invoker_.reset(nullptr); |
| 5587 | sctp_ready_to_send_data_ = false; |
| 5588 | } |
| 5589 | |
| 5590 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 5591 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5592 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5593 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5594 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5595 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5596 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5597 | // pending tasks will be cleared. |
| 5598 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 5599 | OnSctpTransportReadyToSendData_s(true); |
| 5600 | }); |
| 5601 | } |
| 5602 | |
| 5603 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 5604 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5605 | sctp_ready_to_send_data_ = ready; |
| 5606 | SignalSctpReadyToSendData(ready); |
| 5607 | } |
| 5608 | |
| 5609 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 5610 | const cricket::ReceiveDataParams& params, |
| 5611 | const rtc::CopyOnWriteBuffer& payload) { |
| 5612 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5613 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5614 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5615 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5616 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5617 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5618 | // pending tasks will be cleared. |
| 5619 | sctp_invoker_->AsyncInvoke<void>( |
| 5620 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 5621 | OnSctpTransportDataReceived_s(params, payload); |
| 5622 | }); |
| 5623 | } |
| 5624 | |
| 5625 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 5626 | const cricket::ReceiveDataParams& params, |
| 5627 | const rtc::CopyOnWriteBuffer& payload) { |
| 5628 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5629 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 5630 | // Received OPEN message; parse and signal that a new data channel should |
| 5631 | // be created. |
| 5632 | std::string label; |
| 5633 | InternalDataChannelInit config; |
| 5634 | config.id = params.ssrc; |
| 5635 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5636 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 5637 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5638 | return; |
| 5639 | } |
| 5640 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5641 | OnDataChannelOpenMessage(label, config); |
| 5642 | } else { |
| 5643 | // Otherwise just forward the signal. |
| 5644 | SignalSctpDataReceived(params, payload); |
| 5645 | } |
| 5646 | } |
| 5647 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5648 | void PeerConnection::OnSctpClosingProcedureStartedRemotely_n(int sid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5649 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5650 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5651 | sctp_invoker_->AsyncInvoke<void>( |
| 5652 | RTC_FROM_HERE, signaling_thread(), |
| 5653 | rtc::Bind(&sigslot::signal1<int>::operator(), |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5654 | &SignalSctpClosingProcedureStartedRemotely, sid)); |
| 5655 | } |
| 5656 | |
| 5657 | void PeerConnection::OnSctpClosingProcedureComplete_n(int sid) { |
| 5658 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5659 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5660 | sctp_invoker_->AsyncInvoke<void>( |
| 5661 | RTC_FROM_HERE, signaling_thread(), |
| 5662 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 5663 | &SignalSctpClosingProcedureComplete, sid)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5664 | } |
| 5665 | |
| 5666 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 5667 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 5668 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 5669 | if (!bundle_enabled) |
| 5670 | return true; |
| 5671 | |
| 5672 | const cricket::ContentGroup* bundle_group = |
| 5673 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 5674 | RTC_DCHECK(bundle_group != NULL); |
| 5675 | |
| 5676 | const cricket::ContentInfos& contents = desc->contents(); |
| 5677 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 5678 | citer != contents.end(); ++citer) { |
| 5679 | const cricket::ContentInfo* content = (&*citer); |
| 5680 | RTC_DCHECK(content != NULL); |
| 5681 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 5682 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5683 | if (!HasRtcpMuxEnabled(content)) |
| 5684 | return false; |
| 5685 | } |
| 5686 | } |
| 5687 | // RTCP-MUX is enabled in all the contents. |
| 5688 | return true; |
| 5689 | } |
| 5690 | |
| 5691 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5692 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5693 | } |
| 5694 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5695 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5696 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5697 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5698 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5699 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5703 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5704 | } |
| 5705 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5706 | SdpType type = sdesc->GetType(); |
| 5707 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 5708 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5709 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 5710 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5711 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | // Verify crypto settings. |
| 5715 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5716 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 5717 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5718 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5719 | if (!crypto_error.ok()) { |
| 5720 | return crypto_error; |
| 5721 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | // Verify ice-ufrag and ice-pwd. |
| 5725 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5726 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5727 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5731 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5732 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5733 | } |
| 5734 | |
| 5735 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 5736 | // m-lines that do not rtcp-mux enabled. |
| 5737 | |
| 5738 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5739 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5740 | // With an answer we want to compare the new answer session description with |
| 5741 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5742 | const cricket::SessionDescription* offer_desc = |
| 5743 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 5744 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5745 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 5746 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 5747 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5748 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5749 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5750 | } |
| 5751 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5752 | // The re-offers should respect the order of m= sections in current |
| 5753 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5754 | // With a re-offer, either the current local or current remote descriptions |
| 5755 | // could be the most up to date, so we would like to check against both of |
| 5756 | // them if they exist. It could be the case that one of them has a 0 port |
| 5757 | // for a media section, but the other does not. This is important to check |
| 5758 | // against in the case that we are recycling an m= section. |
| 5759 | const cricket::SessionDescription* current_desc = nullptr; |
| 5760 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 5761 | if (local_description()) { |
| 5762 | current_desc = local_description()->description(); |
| 5763 | if (remote_description()) { |
| 5764 | secondary_current_desc = remote_description()->description(); |
| 5765 | } |
| 5766 | } else if (remote_description()) { |
| 5767 | current_desc = remote_description()->description(); |
| 5768 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5769 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5770 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 5771 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5772 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5773 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5774 | } |
| 5775 | } |
| 5776 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 5777 | if (IsUnifiedPlan()) { |
| 5778 | // Ensure that each audio and video media section has at most one |
| 5779 | // "StreamParams". This will return an error if receiving a session |
| 5780 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 5781 | // same type. With Unified Plan, there can only be at most one track per |
| 5782 | // media section. |
| 5783 | for (const ContentInfo& content : sdesc->description()->contents()) { |
| 5784 | const MediaContentDescription& desc = *content.description; |
| 5785 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 5786 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 5787 | desc.streams().size() > 1u) { |
| 5788 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5789 | "Media section has more than one track specified " |
| 5790 | "with a=ssrc lines which is not supported with " |
| 5791 | "Unified Plan."); |
| 5792 | } |
| 5793 | } |
| 5794 | } |
| 5795 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5796 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5797 | } |
| 5798 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5799 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5800 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5801 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5802 | return (state == PeerConnectionInterface::kStable) || |
| 5803 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5804 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5805 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5806 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 5807 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 5808 | } |
| 5809 | } |
| 5810 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5811 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5812 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5813 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5814 | return (state == PeerConnectionInterface::kStable) || |
| 5815 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5816 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5817 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5818 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 5819 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5820 | } |
| 5821 | } |
| 5822 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5823 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 5824 | switch (error) { |
| 5825 | case SessionError::kNone: |
| 5826 | return "ERROR_NONE"; |
| 5827 | case SessionError::kContent: |
| 5828 | return "ERROR_CONTENT"; |
| 5829 | case SessionError::kTransport: |
| 5830 | return "ERROR_TRANSPORT"; |
| 5831 | } |
| 5832 | RTC_NOTREACHED(); |
| 5833 | return ""; |
| 5834 | } |
| 5835 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5836 | std::string PeerConnection::GetSessionErrorMsg() { |
| 5837 | std::ostringstream desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5838 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 5839 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5840 | return desc.str(); |
| 5841 | } |
| 5842 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 5843 | void PeerConnection::ReportSdpFormatReceived( |
| 5844 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 5845 | int num_audio_mlines = 0; |
| 5846 | int num_video_mlines = 0; |
| 5847 | int num_audio_tracks = 0; |
| 5848 | int num_video_tracks = 0; |
| 5849 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 5850 | cricket::MediaType media_type = content.media_description()->type(); |
| 5851 | int num_tracks = std::max( |
| 5852 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 5853 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 5854 | num_audio_mlines += 1; |
| 5855 | num_audio_tracks += num_tracks; |
| 5856 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5857 | num_video_mlines += 1; |
| 5858 | num_video_tracks += num_tracks; |
| 5859 | } |
| 5860 | } |
| 5861 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 5862 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 5863 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 5864 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 5865 | format = kSdpFormatReceivedComplexPlanB; |
| 5866 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 5867 | format = kSdpFormatReceivedSimple; |
| 5868 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5869 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 5870 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 5871 | } |
| 5872 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 5873 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 5874 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5875 | usage_event_accumulator_ |= static_cast<int>(event); |
| 5876 | } |
| 5877 | |
| 5878 | void PeerConnection::ReportUsagePattern() const { |
| 5879 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5880 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 5881 | usage_event_accumulator_, |
| 5882 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 5883 | const int bad_bits = |
| 5884 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED) | |
| 5885 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 5886 | const int good_bits = |
| 5887 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED) | |
| 5888 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 5889 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 5890 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 5891 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5892 | // If called after close(), we can't report, because observer may have |
| 5893 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 5894 | if (observer_) { |
| 5895 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 5896 | } else { |
| 5897 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 5898 | << usage_event_accumulator_ |
| 5899 | << " observed after observer shutdown"; |
| 5900 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 5901 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 5902 | } |
| 5903 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5904 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 5905 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5906 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5907 | switch (answer.description()->msid_signaling()) { |
| 5908 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5909 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5910 | break; |
| 5911 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5912 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5913 | break; |
| 5914 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5915 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5916 | break; |
| 5917 | case cricket::kMsidSignalingMediaSection | |
| 5918 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5919 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5920 | break; |
| 5921 | default: |
| 5922 | RTC_NOTREACHED(); |
| 5923 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5924 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 5925 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5926 | } |
| 5927 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5928 | // We need to check the local/remote description for the Transport instead of |
| 5929 | // the session, because a new Transport added during renegotiation may have |
| 5930 | // them unset while the session has them set from the previous negotiation. |
| 5931 | // Not doing so may trigger the auto generation of transport description and |
| 5932 | // mess up DTLS identity information, ICE credential, etc. |
| 5933 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 5934 | const IceCandidateInterface* candidate, |
| 5935 | const SessionDescriptionInterface* remote_desc, |
| 5936 | bool* valid) { |
| 5937 | *valid = true; |
| 5938 | |
| 5939 | const SessionDescriptionInterface* current_remote_desc = |
| 5940 | remote_desc ? remote_desc : remote_description(); |
| 5941 | |
| 5942 | if (!current_remote_desc) { |
| 5943 | return false; |
| 5944 | } |
| 5945 | |
| 5946 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5947 | size_t remote_content_size = |
| 5948 | current_remote_desc->description()->contents().size(); |
| 5949 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5950 | RTC_LOG(LS_ERROR) |
| 5951 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 5952 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5953 | |
| 5954 | *valid = false; |
| 5955 | return false; |
| 5956 | } |
| 5957 | |
| 5958 | cricket::ContentInfo content = |
| 5959 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 5960 | |
| 5961 | const std::string transport_name = GetTransportName(content.name); |
| 5962 | if (transport_name.empty()) { |
| 5963 | return false; |
| 5964 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5965 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5966 | } |
| 5967 | |
| 5968 | bool PeerConnection::SrtpRequired() const { |
| 5969 | return dtls_enabled_ || |
| 5970 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 5971 | } |
| 5972 | |
| 5973 | void PeerConnection::OnTransportControllerGatheringState( |
| 5974 | cricket::IceGatheringState state) { |
| 5975 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5976 | if (state == cricket::kIceGatheringGathering) { |
| 5977 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 5978 | } else if (state == cricket::kIceGatheringComplete) { |
| 5979 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 5980 | } |
| 5981 | } |
| 5982 | |
| 5983 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5984 | std::map<std::string, std::set<cricket::MediaType>> |
| 5985 | media_types_by_transport_name; |
| 5986 | for (auto transceiver : transceivers_) { |
| 5987 | if (transceiver->internal()->channel()) { |
| 5988 | const std::string& transport_name = |
| 5989 | transceiver->internal()->channel()->transport_name(); |
| 5990 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5991 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5992 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5993 | } |
| 5994 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5995 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 5996 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5997 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5998 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5999 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6000 | if (transport_name) { |
| 6001 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6002 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6003 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6004 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6005 | for (const auto& entry : media_types_by_transport_name) { |
| 6006 | const std::string& transport_name = entry.first; |
| 6007 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6008 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6009 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6010 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6011 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6012 | } |
| 6013 | } |
| 6014 | } |
| 6015 | // Walk through the ConnectionInfos to gather best connection usage |
| 6016 | // for IPv4 and IPv6. |
| 6017 | void PeerConnection::ReportBestConnectionState( |
| 6018 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6019 | for (const cricket::TransportChannelStats& channel_stats : |
| 6020 | stats.channel_stats) { |
| 6021 | for (const cricket::ConnectionInfo& connection_info : |
| 6022 | channel_stats.connection_infos) { |
| 6023 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6024 | continue; |
| 6025 | } |
| 6026 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6027 | const cricket::Candidate& local = connection_info.local_candidate; |
| 6028 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6029 | |
| 6030 | // Increment the counter for IceCandidatePairType. |
| 6031 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 6032 | (local.type() == RELAY_PORT_TYPE && |
| 6033 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6034 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 6035 | GetIceCandidatePairCounter(local, remote), |
| 6036 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6037 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6038 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 6039 | GetIceCandidatePairCounter(local, remote), |
| 6040 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6041 | } else { |
| 6042 | RTC_CHECK(0); |
| 6043 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6044 | |
| 6045 | // Increment the counter for IP type. |
| 6046 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6047 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6048 | kBestConnections_IPv4, |
| 6049 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6050 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6051 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6052 | kBestConnections_IPv6, |
| 6053 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6054 | } else { |
| 6055 | RTC_CHECK(0); |
| 6056 | } |
| 6057 | |
| 6058 | return; |
| 6059 | } |
| 6060 | } |
| 6061 | } |
| 6062 | |
| 6063 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6064 | const cricket::TransportStats& stats, |
| 6065 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6066 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 6067 | return; |
| 6068 | } |
| 6069 | |
| 6070 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 6071 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 6072 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 6073 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6074 | return; |
| 6075 | } |
| 6076 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6077 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6078 | for (cricket::MediaType media_type : media_types) { |
| 6079 | switch (media_type) { |
| 6080 | case cricket::MEDIA_TYPE_AUDIO: |
| 6081 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6082 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 6083 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6084 | break; |
| 6085 | case cricket::MEDIA_TYPE_VIDEO: |
| 6086 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6087 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 6088 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6089 | break; |
| 6090 | case cricket::MEDIA_TYPE_DATA: |
| 6091 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6092 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 6093 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6094 | break; |
| 6095 | default: |
| 6096 | RTC_NOTREACHED(); |
| 6097 | continue; |
| 6098 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6099 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6100 | } |
| 6101 | |
| 6102 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6103 | for (cricket::MediaType media_type : media_types) { |
| 6104 | switch (media_type) { |
| 6105 | case cricket::MEDIA_TYPE_AUDIO: |
| 6106 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6107 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 6108 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6109 | break; |
| 6110 | case cricket::MEDIA_TYPE_VIDEO: |
| 6111 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6112 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 6113 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6114 | break; |
| 6115 | case cricket::MEDIA_TYPE_DATA: |
| 6116 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6117 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 6118 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6119 | break; |
| 6120 | default: |
| 6121 | RTC_NOTREACHED(); |
| 6122 | continue; |
| 6123 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6124 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6125 | } |
| 6126 | } |
| 6127 | |
| 6128 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 6129 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 6130 | RTC_DCHECK(call_); |
| 6131 | call_->OnSentPacket(sent_packet); |
| 6132 | } |
| 6133 | |
| 6134 | const std::string PeerConnection::GetTransportName( |
| 6135 | const std::string& content_name) { |
| 6136 | cricket::BaseChannel* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6137 | if (channel) { |
| 6138 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6139 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6140 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6141 | RTC_DCHECK(sctp_mid_); |
| 6142 | if (content_name == *sctp_mid_) { |
| 6143 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6144 | } |
| 6145 | } |
| 6146 | // Return an empty string if failed to retrieve the transport name. |
| 6147 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6148 | } |
| 6149 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6150 | void PeerConnection::DestroyTransceiverChannel( |
| 6151 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 6152 | transceiver) { |
| 6153 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6154 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6155 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 6156 | if (channel) { |
| 6157 | transceiver->internal()->SetChannel(nullptr); |
| 6158 | DestroyBaseChannel(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6159 | } |
| 6160 | } |
| 6161 | |
| 6162 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6163 | if (rtp_data_channel_) { |
| 6164 | OnDataChannelDestroyed(); |
| 6165 | DestroyBaseChannel(rtp_data_channel_); |
| 6166 | rtp_data_channel_ = nullptr; |
| 6167 | } |
| 6168 | |
| 6169 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 6170 | // grab a reference to this PeerConnection. If this is called from the |
| 6171 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 6172 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 6173 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 6174 | |
| 6175 | if (sctp_transport_) { |
| 6176 | OnDataChannelDestroyed(); |
| 6177 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 6178 | [this] { DestroySctpTransport_n(); }); |
| 6179 | } |
| 6180 | } |
| 6181 | |
| 6182 | void PeerConnection::DestroyBaseChannel(cricket::BaseChannel* channel) { |
| 6183 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6184 | switch (channel->media_type()) { |
| 6185 | case cricket::MEDIA_TYPE_AUDIO: |
| 6186 | channel_manager()->DestroyVoiceChannel( |
| 6187 | static_cast<cricket::VoiceChannel*>(channel)); |
| 6188 | break; |
| 6189 | case cricket::MEDIA_TYPE_VIDEO: |
| 6190 | channel_manager()->DestroyVideoChannel( |
| 6191 | static_cast<cricket::VideoChannel*>(channel)); |
| 6192 | break; |
| 6193 | case cricket::MEDIA_TYPE_DATA: |
| 6194 | channel_manager()->DestroyRtpDataChannel( |
| 6195 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 6196 | break; |
| 6197 | default: |
| 6198 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 6199 | break; |
| 6200 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6201 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6202 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6203 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6204 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6205 | RtpTransportInternal* rtp_transport, |
| 6206 | cricket::DtlsTransportInternal* dtls_transport) { |
| 6207 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6208 | auto base_channel = GetChannel(mid); |
| 6209 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6210 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6211 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6212 | if (sctp_transport_ && mid == sctp_mid_) { |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6213 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6214 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6215 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6216 | } |
| 6217 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6218 | PeerConnectionObserver* PeerConnection::Observer() const { |
| 6219 | // In earlier production code, the pointer was not cleared on close, |
| 6220 | // which might have led to undefined behavior if the observer was not |
| 6221 | // deallocated, or strange crashes if it was. |
| 6222 | // We use CHECK in order to catch such behavior if it exists. |
| 6223 | // TODO(hta): Remove or replace with DCHECK if nothing is found. |
| 6224 | RTC_CHECK(observer_); |
| 6225 | return observer_; |
| 6226 | } |
| 6227 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 6228 | void PeerConnection::ClearStatsCache() { |
| 6229 | if (stats_collector_) { |
| 6230 | stats_collector_->ClearCachedStatsReport(); |
| 6231 | } |
| 6232 | } |
| 6233 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6234 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame^] | 6235 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 6236 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6237 | } |
| 6238 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6239 | } // namespace webrtc |