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" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 25 | #include "api/umametrics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "call/call.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 27 | #include "logging/rtc_event_log/icelogger.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 28 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 30 | #include "media/sctp/sctptransport.h" |
| 31 | #include "pc/audiotrack.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 32 | #include "pc/channel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "pc/channelmanager.h" |
| 34 | #include "pc/dtmfsender.h" |
| 35 | #include "pc/mediastream.h" |
| 36 | #include "pc/mediastreamobserver.h" |
| 37 | #include "pc/remoteaudiosource.h" |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 38 | #include "pc/rtpmediautils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 39 | #include "pc/rtpreceiver.h" |
| 40 | #include "pc/rtpsender.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 41 | #include "pc/sctputils.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 42 | #include "pc/sdputils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "pc/streamcollection.h" |
| 44 | #include "pc/videocapturertracksource.h" |
| 45 | #include "pc/videotrack.h" |
| 46 | #include "rtc_base/bind.h" |
| 47 | #include "rtc_base/checks.h" |
| 48 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 49 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 50 | #include "rtc_base/stringencode.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 51 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 52 | #include "rtc_base/stringutils.h" |
| 53 | #include "rtc_base/trace_event.h" |
| 54 | #include "system_wrappers/include/clock.h" |
| 55 | #include "system_wrappers/include/field_trial.h" |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 56 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 58 | using cricket::ContentInfo; |
| 59 | using cricket::ContentInfos; |
| 60 | using cricket::MediaContentDescription; |
| 61 | using cricket::SessionDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 62 | using cricket::MediaProtocolType; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 63 | using cricket::TransportInfo; |
| 64 | |
| 65 | using cricket::LOCAL_PORT_TYPE; |
| 66 | using cricket::STUN_PORT_TYPE; |
| 67 | using cricket::RELAY_PORT_TYPE; |
| 68 | using cricket::PRFLX_PORT_TYPE; |
| 69 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 70 | namespace webrtc { |
| 71 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 72 | // Error messages |
| 73 | const char kBundleWithoutRtcpMux[] = |
| 74 | "rtcp-mux must be enabled when BUNDLE " |
| 75 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 76 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 77 | const char kInvalidSdp[] = "Invalid session description."; |
| 78 | const char kMlineMismatchInAnswer[] = |
| 79 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 80 | "answer."; |
| 81 | const char kMlineMismatchInSubsequentOffer[] = |
| 82 | "The order of m-lines in subsequent offer doesn't match order from " |
| 83 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 84 | const char kSdpWithoutDtlsFingerprint[] = |
| 85 | "Called with SDP without DTLS fingerprint."; |
| 86 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 87 | const char kSdpWithoutIceUfragPwd[] = |
| 88 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 89 | const char kSessionError[] = "Session error code: "; |
| 90 | const char kSessionErrorDesc[] = "Session error description: "; |
| 91 | const char kDtlsSrtpSetupFailureRtp[] = |
| 92 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 93 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 94 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 96 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 98 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 99 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 100 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 101 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 102 | // The length of RTCP CNAMEs. |
| 103 | static const int kRtcpCnameLength = 16; |
| 104 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 105 | enum { |
| 106 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 107 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 108 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 109 | MSG_GETSTATS, |
| 110 | MSG_FREE_DATACHANNELS, |
| 111 | MSG_REPORT_USAGE_PATTERN, |
| 112 | }; |
| 113 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 114 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 115 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 116 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 117 | explicit SetSessionDescriptionMsg( |
| 118 | webrtc::SetSessionDescriptionObserver* observer) |
| 119 | : observer(observer) {} |
| 120 | |
| 121 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 122 | RTCError error; |
| 123 | }; |
| 124 | |
| 125 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 126 | explicit CreateSessionDescriptionMsg( |
| 127 | webrtc::CreateSessionDescriptionObserver* observer) |
| 128 | : observer(observer) {} |
| 129 | |
| 130 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 131 | RTCError error; |
| 132 | }; |
| 133 | |
| 134 | struct GetStatsMsg : public rtc::MessageData { |
| 135 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 136 | webrtc::MediaStreamTrackInterface* track) |
| 137 | : observer(observer), track(track) {} |
| 138 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 139 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 140 | }; |
| 141 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 142 | // Check if we can send |new_stream| on a PeerConnection. |
| 143 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 144 | webrtc::MediaStreamInterface* new_stream) { |
| 145 | if (!new_stream || !current_streams) { |
| 146 | return false; |
| 147 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 148 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 149 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 150 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | return true; |
| 154 | } |
| 155 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 156 | // If the direction is "recvonly" or "inactive", treat the description |
| 157 | // as containing no streams. |
| 158 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 159 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 160 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 161 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 162 | ? desc->streams() |
| 163 | : std::vector<cricket::StreamParams>(); |
| 164 | } |
| 165 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 166 | bool IsValidOfferToReceiveMedia(int value) { |
| 167 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 168 | return (value >= Options::kUndefined) && |
| 169 | (value <= Options::kMaxOfferToReceiveMedia); |
| 170 | } |
| 171 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 172 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 173 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 174 | const std::vector<rtc::scoped_refptr< |
| 175 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 176 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 177 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 178 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 179 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 180 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 181 | if (audio_media_description_options) { |
| 182 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 183 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 184 | } |
| 185 | } else { |
| 186 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 187 | if (video_media_description_options) { |
| 188 | video_media_description_options->AddVideoSender( |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 189 | sender->id(), sender->internal()->stream_ids(), |
| 190 | num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 193 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 194 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 195 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 196 | // Add options to |session_options| from |rtp_data_channels|. |
| 197 | void AddRtpDataChannelOptions( |
| 198 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 199 | rtp_data_channels, |
| 200 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 201 | if (!data_media_description_options) { |
| 202 | return; |
| 203 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 204 | // Check for data channels. |
| 205 | for (const auto& kv : rtp_data_channels) { |
| 206 | const DataChannel* channel = kv.second; |
| 207 | if (channel->state() == DataChannel::kConnecting || |
| 208 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 209 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 210 | // the data channel's label. |
| 211 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 212 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 217 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 218 | PeerConnectionInterface::IceTransportsType type) { |
| 219 | switch (type) { |
| 220 | case PeerConnectionInterface::kNone: |
| 221 | return cricket::CF_NONE; |
| 222 | case PeerConnectionInterface::kRelay: |
| 223 | return cricket::CF_RELAY; |
| 224 | case PeerConnectionInterface::kNoHost: |
| 225 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 226 | case PeerConnectionInterface::kAll: |
| 227 | return cricket::CF_ALL; |
| 228 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 229 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 230 | } |
| 231 | return cricket::CF_NONE; |
| 232 | } |
| 233 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 234 | // Helper to set an error and return from a method. |
| 235 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 236 | if (error) { |
| 237 | error->set_type(type); |
| 238 | } |
| 239 | return type == webrtc::RTCErrorType::NONE; |
| 240 | } |
| 241 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 242 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 243 | bool ok = error.ok(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 244 | if (error_out) { |
| 245 | *error_out = std::move(error); |
| 246 | } |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 247 | return ok; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 250 | std::string GetSignalingStateString( |
| 251 | PeerConnectionInterface::SignalingState state) { |
| 252 | switch (state) { |
| 253 | case PeerConnectionInterface::kStable: |
| 254 | return "kStable"; |
| 255 | case PeerConnectionInterface::kHaveLocalOffer: |
| 256 | return "kHaveLocalOffer"; |
| 257 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 258 | return "kHavePrAnswer"; |
| 259 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 260 | return "kHaveRemoteOffer"; |
| 261 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 262 | return "kHaveRemotePrAnswer"; |
| 263 | case PeerConnectionInterface::kClosed: |
| 264 | return "kClosed"; |
| 265 | } |
| 266 | RTC_NOTREACHED(); |
| 267 | return ""; |
| 268 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 269 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 270 | IceCandidatePairType GetIceCandidatePairCounter( |
| 271 | const cricket::Candidate& local, |
| 272 | const cricket::Candidate& remote) { |
| 273 | const auto& l = local.type(); |
| 274 | const auto& r = remote.type(); |
| 275 | const auto& host = LOCAL_PORT_TYPE; |
| 276 | const auto& srflx = STUN_PORT_TYPE; |
| 277 | const auto& relay = RELAY_PORT_TYPE; |
| 278 | const auto& prflx = PRFLX_PORT_TYPE; |
| 279 | if (l == host && r == host) { |
| 280 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 281 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 282 | if (local_private) { |
| 283 | if (remote_private) { |
| 284 | return kIceCandidatePairHostPrivateHostPrivate; |
| 285 | } else { |
| 286 | return kIceCandidatePairHostPrivateHostPublic; |
| 287 | } |
| 288 | } else { |
| 289 | if (remote_private) { |
| 290 | return kIceCandidatePairHostPublicHostPrivate; |
| 291 | } else { |
| 292 | return kIceCandidatePairHostPublicHostPublic; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | if (l == host && r == srflx) |
| 297 | return kIceCandidatePairHostSrflx; |
| 298 | if (l == host && r == relay) |
| 299 | return kIceCandidatePairHostRelay; |
| 300 | if (l == host && r == prflx) |
| 301 | return kIceCandidatePairHostPrflx; |
| 302 | if (l == srflx && r == host) |
| 303 | return kIceCandidatePairSrflxHost; |
| 304 | if (l == srflx && r == srflx) |
| 305 | return kIceCandidatePairSrflxSrflx; |
| 306 | if (l == srflx && r == relay) |
| 307 | return kIceCandidatePairSrflxRelay; |
| 308 | if (l == srflx && r == prflx) |
| 309 | return kIceCandidatePairSrflxPrflx; |
| 310 | if (l == relay && r == host) |
| 311 | return kIceCandidatePairRelayHost; |
| 312 | if (l == relay && r == srflx) |
| 313 | return kIceCandidatePairRelaySrflx; |
| 314 | if (l == relay && r == relay) |
| 315 | return kIceCandidatePairRelayRelay; |
| 316 | if (l == relay && r == prflx) |
| 317 | return kIceCandidatePairRelayPrflx; |
| 318 | if (l == prflx && r == host) |
| 319 | return kIceCandidatePairPrflxHost; |
| 320 | if (l == prflx && r == srflx) |
| 321 | return kIceCandidatePairPrflxSrflx; |
| 322 | if (l == prflx && r == relay) |
| 323 | return kIceCandidatePairPrflxRelay; |
| 324 | return kIceCandidatePairMax; |
| 325 | } |
| 326 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 327 | // Logic to decide if an m= section can be recycled. This means that the new |
| 328 | // m= section is not rejected, but the old local or remote m= section is |
| 329 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 330 | // of the old remote and old local descriptions in no particular order. |
| 331 | // We need to check both the old local and remote because either |
| 332 | // could be the most current from the latest negotation. |
| 333 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 334 | const ContentInfo& content, |
| 335 | const ContentInfo* old_content_one, |
| 336 | const ContentInfo* old_content_two) { |
| 337 | return type == SdpType::kOffer && !content.rejected && |
| 338 | ((old_content_one && old_content_one->rejected) || |
| 339 | (old_content_two && old_content_two->rejected)); |
| 340 | } |
| 341 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 342 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 343 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 344 | // less than |current_desc|. In the case of checking an answer's |
| 345 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 346 | // local or remote. In the case of checking an offer's |new_desc| we |
| 347 | // check against the local and remote descriptions stored from the last |
| 348 | // negotiation, because either of these could be the most up to date for |
| 349 | // possible rejected m sections. These are the |current_desc| and |
| 350 | // |secondary_current_desc|. |
| 351 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 352 | const SessionDescription* secondary_current_desc, |
| 353 | const SessionDescription& new_desc, |
| 354 | const SdpType type) { |
| 355 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 356 | return false; |
| 357 | } |
| 358 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 359 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 360 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 361 | if (secondary_current_desc && |
| 362 | i < secondary_current_desc->contents().size()) { |
| 363 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 364 | } |
| 365 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 366 | ¤t_desc.contents()[i], |
| 367 | secondary_content_info)) { |
| 368 | // For new offer descriptions, if the media section can be recycled, it's |
| 369 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 370 | continue; |
| 371 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 372 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 376 | new_desc.contents()[i].media_description(); |
| 377 | const MediaContentDescription* current_desc_mdesc = |
| 378 | current_desc.contents()[i].media_description(); |
| 379 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | return true; |
| 384 | } |
| 385 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 386 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 387 | const SessionDescription& desc2) { |
| 388 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 391 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 392 | cricket::MediaType media_type) { |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 393 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 394 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 395 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 396 | static constexpr struct { |
| 397 | KeyExchangeProtocolType protocol_type; |
| 398 | cricket::MediaType media_type; |
| 399 | KeyExchangeProtocolMedia protocol_media; |
| 400 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 401 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 402 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 403 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 404 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 405 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 406 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 407 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 408 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 409 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 410 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 411 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 412 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 413 | }; |
| 414 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 415 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 416 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 417 | |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 418 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 419 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 420 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 421 | i.protocol_media, |
| 422 | kEnumCounterKeyProtocolMediaTypeMax); |
| 423 | } |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 427 | void NoteAddIceCandidateResult(int result) { |
| 428 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 429 | kAddIceCandidateMax); |
| 430 | } |
| 431 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 432 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 433 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 434 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 435 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 436 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 437 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 438 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 439 | const cricket::ContentGroup* bundle = |
| 440 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 441 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 442 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 443 | continue; |
| 444 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 445 | // Note what media is used with each crypto protocol, for all sections. |
| 446 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 447 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 448 | content_info.media_description()->type()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 449 | const std::string& mid = content_info.name; |
| 450 | if (bundle && bundle->HasContentName(mid) && |
| 451 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 452 | // This isn't the first media section in the BUNDLE group, so it's not |
| 453 | // required to have crypto attributes, since only the crypto attributes |
| 454 | // from the first section actually get used. |
| 455 | continue; |
| 456 | } |
| 457 | |
| 458 | // If the content isn't rejected or bundled into another m= section, crypto |
| 459 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 460 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 461 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 462 | if (!media || !tinfo) { |
| 463 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 464 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 465 | } |
| 466 | if (dtls_enabled) { |
| 467 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 468 | RTC_LOG(LS_WARNING) |
| 469 | << "Session description must have DTLS fingerprint if " |
| 470 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 471 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 472 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 473 | } |
| 474 | } else { |
| 475 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 476 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 477 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 478 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 482 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 486 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 487 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 488 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 489 | const cricket::ContentGroup* bundle = |
| 490 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 491 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 492 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 493 | continue; |
| 494 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 495 | const std::string& mid = content_info.name; |
| 496 | if (bundle && bundle->HasContentName(mid) && |
| 497 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 498 | // This isn't the first media section in the BUNDLE group, so it's not |
| 499 | // required to have ufrag/password, since only the ufrag/password from |
| 500 | // the first section actually get used. |
| 501 | continue; |
| 502 | } |
| 503 | |
| 504 | // If the content isn't rejected or bundled into another m= section, |
| 505 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 506 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 507 | if (!tinfo) { |
| 508 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 509 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | if (tinfo->description.ice_ufrag.empty() || |
| 513 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 514 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 515 | return false; |
| 516 | } |
| 517 | } |
| 518 | return true; |
| 519 | } |
| 520 | |
| 521 | bool GetTrackIdBySsrc(const SessionDescription* session_description, |
| 522 | uint32_t ssrc, |
| 523 | std::string* track_id) { |
| 524 | RTC_DCHECK(track_id != NULL); |
| 525 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 526 | const cricket::AudioContentDescription* audio_desc = |
| 527 | cricket::GetFirstAudioContentDescription(session_description); |
| 528 | if (audio_desc) { |
| 529 | const auto* found = cricket::GetStreamBySsrc(audio_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 530 | if (found) { |
| 531 | *track_id = found->id; |
| 532 | return true; |
| 533 | } |
| 534 | } |
| 535 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 536 | const cricket::VideoContentDescription* video_desc = |
| 537 | cricket::GetFirstVideoContentDescription(session_description); |
| 538 | if (video_desc) { |
| 539 | const auto* found = cricket::GetStreamBySsrc(video_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 540 | if (found) { |
| 541 | *track_id = found->id; |
| 542 | return true; |
| 543 | } |
| 544 | } |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | // Get the SCTP port out of a SessionDescription. |
| 549 | // Return -1 if not found. |
| 550 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 551 | const cricket::DataContentDescription* data_desc = |
| 552 | GetFirstDataContentDescription(session_description); |
| 553 | RTC_DCHECK(data_desc); |
| 554 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 555 | return -1; |
| 556 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 557 | std::string value; |
| 558 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 559 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 560 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 561 | if (!codec.Matches(match_pattern)) { |
| 562 | continue; |
| 563 | } |
| 564 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 565 | return rtc::FromString<int>(value); |
| 566 | } |
| 567 | } |
| 568 | return -1; |
| 569 | } |
| 570 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 571 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 572 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 573 | const SessionDescriptionInterface* new_desc, |
| 574 | const std::string& content_name) { |
| 575 | if (!old_desc) { |
| 576 | return false; |
| 577 | } |
| 578 | const SessionDescription* new_sd = new_desc->description(); |
| 579 | const SessionDescription* old_sd = old_desc->description(); |
| 580 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 581 | if (!cinfo || cinfo->rejected) { |
| 582 | return false; |
| 583 | } |
| 584 | // If the content isn't rejected, check if ufrag and password has changed. |
| 585 | const cricket::TransportDescription* new_transport_desc = |
| 586 | new_sd->GetTransportDescriptionByName(content_name); |
| 587 | const cricket::TransportDescription* old_transport_desc = |
| 588 | old_sd->GetTransportDescriptionByName(content_name); |
| 589 | if (!new_transport_desc || !old_transport_desc) { |
| 590 | // No transport description exists. This is not an ICE restart. |
| 591 | return false; |
| 592 | } |
| 593 | if (cricket::IceCredentialsChanged( |
| 594 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 595 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 596 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 597 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 598 | return true; |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 603 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 604 | // from an RTCError. |
| 605 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 606 | SdpType type, |
| 607 | const RTCError& error) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 608 | rtc::StringBuilder oss; |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 609 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 610 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 611 | return oss.Release(); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 614 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 615 | std::string output = "streams=["; |
| 616 | const char* separator = ""; |
| 617 | for (const auto& stream_id : stream_ids) { |
| 618 | output.append(separator).append(stream_id); |
| 619 | separator = ", "; |
| 620 | } |
| 621 | output.append("]"); |
| 622 | return output; |
| 623 | } |
| 624 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 625 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 626 | int rtc_configuration_parameter) { |
| 627 | if (rtc_configuration_parameter == |
| 628 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 629 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 630 | } |
| 631 | return rtc_configuration_parameter; |
| 632 | } |
| 633 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 634 | } // namespace |
| 635 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 636 | // Upon completion, posts a task to execute the callback of the |
| 637 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 638 | // point, the state of the peer connection might no longer reflect the effects |
| 639 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 640 | // modified during the post. |
| 641 | // TODO(hbos): Remove this class once we remove the version of |
| 642 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 643 | // SetSessionDescriptionObserver as an argument. |
| 644 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 645 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 646 | public: |
| 647 | SetRemoteDescriptionObserverAdapter( |
| 648 | rtc::scoped_refptr<PeerConnection> pc, |
| 649 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 650 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 651 | |
| 652 | // SetRemoteDescriptionObserverInterface implementation. |
| 653 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 654 | if (error.ok()) |
| 655 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 656 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 657 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | private: |
| 661 | rtc::scoped_refptr<PeerConnection> pc_; |
| 662 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 663 | }; |
| 664 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 665 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 666 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 667 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 668 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 669 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 670 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 671 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 672 | BundlePolicy bundle_policy; |
| 673 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 674 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 675 | int ice_candidate_pool_size; |
| 676 | bool disable_ipv6; |
| 677 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 678 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 679 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 680 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 681 | absl::optional<int> screencast_min_bitrate; |
| 682 | absl::optional<bool> combined_audio_video_bwe; |
| 683 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 684 | TcpCandidatePolicy tcp_candidate_policy; |
| 685 | CandidateNetworkPolicy candidate_network_policy; |
| 686 | int audio_jitter_buffer_max_packets; |
| 687 | bool audio_jitter_buffer_fast_accelerate; |
| 688 | int ice_connection_receiving_timeout; |
| 689 | int ice_backup_candidate_pair_ping_interval; |
| 690 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 691 | bool prioritize_most_likely_ice_candidate_pairs; |
| 692 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 693 | bool prune_turn_ports; |
| 694 | bool presume_writable_when_fully_relayed; |
| 695 | bool enable_ice_renomination; |
| 696 | bool redetermine_role_on_ice_restart; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 697 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 698 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 699 | absl::optional<int> ice_check_min_interval; |
| 700 | absl::optional<int> ice_unwritable_timeout; |
| 701 | absl::optional<int> ice_unwritable_min_checks; |
| 702 | absl::optional<int> stun_candidate_keepalive_interval; |
| 703 | absl::optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 704 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 705 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 706 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 707 | bool active_reset_srtp_params; |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 708 | bool use_media_transport; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 709 | absl::optional<CryptoOptions> crypto_options; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 710 | }; |
| 711 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 712 | "Did you add something to RTCConfiguration and forget to " |
| 713 | "update operator==?"); |
| 714 | return type == o.type && servers == o.servers && |
| 715 | bundle_policy == o.bundle_policy && |
| 716 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 717 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 718 | candidate_network_policy == o.candidate_network_policy && |
| 719 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 720 | audio_jitter_buffer_fast_accelerate == |
| 721 | o.audio_jitter_buffer_fast_accelerate && |
| 722 | ice_connection_receiving_timeout == |
| 723 | o.ice_connection_receiving_timeout && |
| 724 | ice_backup_candidate_pair_ping_interval == |
| 725 | o.ice_backup_candidate_pair_ping_interval && |
| 726 | continual_gathering_policy == o.continual_gathering_policy && |
| 727 | certificates == o.certificates && |
| 728 | prioritize_most_likely_ice_candidate_pairs == |
| 729 | o.prioritize_most_likely_ice_candidate_pairs && |
| 730 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 731 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 732 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 733 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 734 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 735 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 736 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 737 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 738 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 739 | prune_turn_ports == o.prune_turn_ports && |
| 740 | presume_writable_when_fully_relayed == |
| 741 | o.presume_writable_when_fully_relayed && |
| 742 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 743 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 744 | ice_check_interval_strong_connectivity == |
| 745 | o.ice_check_interval_strong_connectivity && |
| 746 | ice_check_interval_weak_connectivity == |
| 747 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 748 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 749 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 750 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 751 | stun_candidate_keepalive_interval == |
| 752 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 753 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 754 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 755 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 756 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 757 | active_reset_srtp_params == o.active_reset_srtp_params && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 758 | use_media_transport == o.use_media_transport && |
| 759 | crypto_options == o.crypto_options; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 763 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 764 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 765 | } |
| 766 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 767 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 768 | std::string GenerateRtcpCname() { |
| 769 | std::string cname; |
| 770 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 771 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 772 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 773 | } |
| 774 | return cname; |
| 775 | } |
| 776 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 777 | bool ValidateOfferAnswerOptions( |
| 778 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 779 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 780 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 781 | } |
| 782 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 783 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 784 | // m= sections (in other words, nothing that involves a map/array). |
| 785 | void ExtractSharedMediaSessionOptions( |
| 786 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 787 | cricket::MediaSessionOptions* session_options) { |
| 788 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 789 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 790 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 791 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 792 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 793 | std::unique_ptr<RtcEventLog> event_log, |
| 794 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 795 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 796 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 797 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 798 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 799 | remote_streams_(StreamCollection::Create()), |
| 800 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 801 | |
| 802 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 803 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 804 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 805 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 806 | // Need to stop transceivers before destroying the stats collector because |
| 807 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 808 | // stopping. |
| 809 | for (auto transceiver : transceivers_) { |
| 810 | transceiver->Stop(); |
| 811 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 812 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 813 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 814 | if (stats_collector_) { |
| 815 | stats_collector_->WaitForPendingRequest(); |
| 816 | stats_collector_ = nullptr; |
| 817 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 818 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 819 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 820 | // the last stats request can still read from the channels. |
| 821 | DestroyAllChannels(); |
| 822 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 823 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 824 | |
| 825 | webrtc_session_desc_factory_.reset(); |
| 826 | sctp_invoker_.reset(); |
| 827 | sctp_factory_.reset(); |
| 828 | transport_controller_.reset(); |
| 829 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 830 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 831 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 832 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 833 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 834 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 835 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 836 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 837 | event_log_.reset(); |
| 838 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 841 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 842 | // Destroy video channels first since they may have a pointer to a voice |
| 843 | // channel. |
| 844 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 845 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 846 | DestroyTransceiverChannel(transceiver); |
| 847 | } |
| 848 | } |
| 849 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 850 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 851 | DestroyTransceiverChannel(transceiver); |
| 852 | } |
| 853 | } |
| 854 | DestroyDataChannel(); |
| 855 | } |
| 856 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 857 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 858 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 859 | PeerConnectionDependencies dependencies) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 860 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 861 | |
| 862 | RTCError config_error = ValidateConfiguration(configuration); |
| 863 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 864 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 865 | return false; |
| 866 | } |
| 867 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 868 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 869 | RTC_LOG(LS_ERROR) |
| 870 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 871 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 872 | return false; |
| 873 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 874 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 875 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 876 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 877 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 878 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 879 | return false; |
| 880 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 881 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 882 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 883 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 884 | port_allocator_ = std::move(dependencies.allocator); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 885 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 886 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 887 | cricket::ServerAddresses stun_servers; |
| 888 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 889 | |
| 890 | RTCErrorType parse_error = |
| 891 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 892 | if (parse_error != RTCErrorType::NONE) { |
| 893 | return false; |
| 894 | } |
| 895 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 896 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 897 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 898 | if (!network_thread()->Invoke<bool>( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 899 | RTC_FROM_HERE, |
| 900 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
| 901 | stun_servers, turn_servers, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 902 | return false; |
| 903 | } |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 904 | // If initialization was successful, note if STUN or TURN servers |
| 905 | // were supplied. |
| 906 | if (!stun_servers.empty()) { |
| 907 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 908 | } |
| 909 | if (!turn_servers.empty()) { |
| 910 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 911 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 912 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 913 | // Send information about IPv4/IPv6 status. |
| 914 | PeerConnectionAddressFamilyCounter address_family; |
| 915 | if (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
| 916 | address_family = kPeerConnection_IPv6; |
| 917 | } else { |
| 918 | address_family = kPeerConnection_IPv4; |
| 919 | } |
| 920 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 921 | kPeerConnectionAddressFamilyCounter_Max); |
| 922 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 923 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 924 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 925 | // RFC 3264: The numeric value of the session id and version in the |
| 926 | // o line MUST be representable with a "64 bit signed integer". |
| 927 | // Due to this constraint session id |session_id_| is max limited to |
| 928 | // LLONG_MAX. |
| 929 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 930 | JsepTransportController::Config config; |
| 931 | config.redetermine_role_on_ice_restart = |
| 932 | configuration.redetermine_role_on_ice_restart; |
| 933 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 934 | config.disable_encryption = options.disable_encryption; |
| 935 | config.bundle_policy = configuration.bundle_policy; |
| 936 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 937 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 938 | // stub. |
| 939 | config.crypto_options = configuration.crypto_options.has_value() |
| 940 | ? *configuration.crypto_options |
| 941 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 942 | config.transport_observer = this; |
Qingsi Wang | 7685e86 | 2018-06-11 20:15:46 -0700 | [diff] [blame] | 943 | config.event_log = event_log_.get(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 944 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 945 | config.enable_external_auth = true; |
| 946 | #endif |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 947 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 948 | |
| 949 | if (configuration.use_media_transport) { |
| 950 | if (!factory_->media_transport_factory()) { |
| 951 | RTC_DCHECK(false) |
| 952 | << "PeerConnecton is initialized with use_media_transport = true, " |
| 953 | << "but media transport factory is not set in PeerConnectioFactory"; |
| 954 | return false; |
| 955 | } |
| 956 | |
| 957 | config.media_transport_factory = factory_->media_transport_factory(); |
| 958 | } |
| 959 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 960 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 961 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 962 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 963 | transport_controller_->SignalIceConnectionState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 964 | this, &PeerConnection::OnTransportControllerConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 965 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 966 | this, &PeerConnection::SetStandardizedIceConnectionState); |
| 967 | transport_controller_->SignalConnectionState.connect( |
| 968 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 969 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 970 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 971 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 972 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 973 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 974 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 975 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 976 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 977 | |
| 978 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 979 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 980 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 981 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 982 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 983 | configuration_ = configuration; |
| 984 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 985 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 986 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 987 | if (!configuration.certificates.empty()) { |
| 988 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 989 | // just picking the first one. The decision should be made based on the DTLS |
| 990 | // handshake. The DTLS negotiations need to know about all certificates. |
| 991 | certificate = configuration.certificates[0]; |
| 992 | } |
| 993 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 994 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 995 | |
| 996 | if (options.disable_encryption) { |
| 997 | dtls_enabled_ = false; |
| 998 | } else { |
| 999 | // 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] | 1000 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1001 | // |configuration| can override the default |dtls_enabled_| value. |
| 1002 | if (configuration.enable_dtls_srtp) { |
| 1003 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 1008 | // It takes precendence over the disable_sctp_data_channels |
| 1009 | // PeerConnectionFactoryInterface::Options. |
| 1010 | if (configuration.enable_rtp_data_channel) { |
| 1011 | data_channel_type_ = cricket::DCT_RTP; |
| 1012 | } else { |
| 1013 | // DTLS has to be enabled to use SCTP. |
| 1014 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 1015 | data_channel_type_ = cricket::DCT_SCTP; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | video_options_.screencast_min_bitrate_kbps = |
| 1020 | configuration.screencast_min_bitrate; |
| 1021 | audio_options_.combined_audio_video_bwe = |
| 1022 | configuration.combined_audio_video_bwe; |
| 1023 | |
| 1024 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1025 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1026 | |
| 1027 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1028 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1029 | |
| 1030 | // Whether the certificate generator/certificate is null or not determines |
| 1031 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1032 | // the right instructions by clearing the variables if needed. |
| 1033 | if (!dtls_enabled_) { |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1034 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1035 | certificate = nullptr; |
| 1036 | } else if (certificate) { |
| 1037 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1038 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1042 | signaling_thread(), channel_manager(), this, session_id(), |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1043 | std::move(dependencies.cert_generator), certificate)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1044 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1045 | this, &PeerConnection::OnCertificateReady); |
| 1046 | |
| 1047 | if (options.disable_encryption) { |
| 1048 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1049 | } |
| 1050 | |
| 1051 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 1052 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1053 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1054 | // Add default audio/video transceivers for Plan B SDP. |
| 1055 | if (!IsUnifiedPlan()) { |
| 1056 | transceivers_.push_back( |
| 1057 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1058 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1059 | transceivers_.push_back( |
| 1060 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1061 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1062 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 1063 | int delay_ms = |
| 1064 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1065 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1066 | MSG_REPORT_USAGE_PATTERN, nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1067 | return true; |
| 1068 | } |
| 1069 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1070 | RTCError PeerConnection::ValidateConfiguration( |
| 1071 | const RTCConfiguration& config) const { |
| 1072 | if (config.ice_regather_interval_range && |
| 1073 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1074 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1075 | "ice_regather_interval_range specified but continual " |
| 1076 | "gathering policy is GATHER_ONCE"); |
| 1077 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1078 | auto result = |
| 1079 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1080 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1083 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1084 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1085 | "Plan SdpSemantics. Please use GetSenders " |
| 1086 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1087 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1090 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1091 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1092 | "Plan SdpSemantics. Please use GetReceivers " |
| 1093 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1094 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1097 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1098 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1099 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1100 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1101 | if (IsClosed()) { |
| 1102 | return false; |
| 1103 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1104 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1105 | return false; |
| 1106 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1107 | |
| 1108 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1109 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1110 | observer->SignalAudioTrackAdded.connect(this, |
| 1111 | &PeerConnection::OnAudioTrackAdded); |
| 1112 | observer->SignalAudioTrackRemoved.connect( |
| 1113 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1114 | observer->SignalVideoTrackAdded.connect(this, |
| 1115 | &PeerConnection::OnVideoTrackAdded); |
| 1116 | observer->SignalVideoTrackRemoved.connect( |
| 1117 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1118 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1119 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1120 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1121 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1122 | } |
| 1123 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1124 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1127 | stats_->AddStream(local_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1128 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1129 | return true; |
| 1130 | } |
| 1131 | |
| 1132 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1133 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1134 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1135 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1136 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1137 | if (!IsClosed()) { |
| 1138 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1139 | RemoveAudioTrack(track.get(), local_stream); |
| 1140 | } |
| 1141 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1142 | RemoveVideoTrack(track.get(), local_stream); |
| 1143 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1144 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1145 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1146 | stream_observers_.erase( |
| 1147 | std::remove_if( |
| 1148 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1149 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1150 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1151 | }), |
| 1152 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1153 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1154 | if (IsClosed()) { |
| 1155 | return; |
| 1156 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1157 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1160 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1161 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1162 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1163 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1164 | if (!track) { |
| 1165 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1166 | } |
| 1167 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1168 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1169 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1170 | "Track has invalid kind: " + track->kind()); |
| 1171 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1172 | if (IsClosed()) { |
| 1173 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1174 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1175 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1176 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1177 | LOG_AND_RETURN_ERROR( |
| 1178 | RTCErrorType::INVALID_PARAMETER, |
| 1179 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1180 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1181 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1182 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1183 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1184 | if (sender_or_error.ok()) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1185 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1186 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1187 | } |
| 1188 | return sender_or_error; |
| 1189 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1190 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1191 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1192 | PeerConnection::AddTrackPlanB( |
| 1193 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1194 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1195 | if (stream_ids.size() > 1u) { |
| 1196 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1197 | "AddTrack with more than one stream is not " |
| 1198 | "supported with Plan B semantics."); |
| 1199 | } |
| 1200 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1201 | if (adjusted_stream_ids.empty()) { |
| 1202 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1203 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1204 | cricket::MediaType media_type = |
| 1205 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1206 | ? cricket::MEDIA_TYPE_AUDIO |
| 1207 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1208 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1209 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1210 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1211 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1212 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1213 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1214 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1215 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1216 | if (sender_info) { |
| 1217 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1218 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1219 | } else { |
| 1220 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1221 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1222 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1223 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1224 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1225 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1226 | if (sender_info) { |
| 1227 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1228 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1229 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1230 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1231 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1232 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1233 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1234 | PeerConnection::AddTrackUnifiedPlan( |
| 1235 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1236 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1237 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1238 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1239 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1240 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1241 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1242 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1243 | transceiver->internal()->set_direction( |
| 1244 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1245 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1246 | transceiver->internal()->set_direction( |
| 1247 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1248 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1249 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1250 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1251 | } else { |
| 1252 | cricket::MediaType media_type = |
| 1253 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1254 | ? cricket::MEDIA_TYPE_AUDIO |
| 1255 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1256 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1257 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1258 | std::string sender_id = track->id(); |
| 1259 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1260 | // This can happen if this is the second time AddTrack has created a sender |
| 1261 | // for this track. |
| 1262 | if (FindSenderById(sender_id)) { |
| 1263 | sender_id = rtc::CreateRandomUuid(); |
| 1264 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1265 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1266 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1267 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1268 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1269 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1270 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1271 | return transceiver->sender(); |
| 1272 | } |
| 1273 | |
| 1274 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1275 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1276 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1277 | RTC_DCHECK(track); |
| 1278 | for (auto transceiver : transceivers_) { |
| 1279 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1280 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1281 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1282 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1283 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1284 | return transceiver; |
| 1285 | } |
| 1286 | } |
| 1287 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1291 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1292 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1295 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1296 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1297 | if (!sender) { |
| 1298 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1299 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1300 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1301 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1302 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1303 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1304 | if (IsUnifiedPlan()) { |
| 1305 | auto transceiver = FindTransceiverBySender(sender); |
| 1306 | if (!transceiver || !sender->track()) { |
| 1307 | return RTCError::OK(); |
| 1308 | } |
| 1309 | sender->SetTrack(nullptr); |
| 1310 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1311 | transceiver->internal()->set_direction( |
| 1312 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1313 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1314 | transceiver->internal()->set_direction( |
| 1315 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1316 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1317 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1318 | bool removed; |
| 1319 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1320 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1321 | } else { |
| 1322 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1323 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1324 | } |
| 1325 | if (!removed) { |
| 1326 | LOG_AND_RETURN_ERROR( |
| 1327 | RTCErrorType::INVALID_PARAMETER, |
| 1328 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1329 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1330 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1331 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1332 | return RTCError::OK(); |
| 1333 | } |
| 1334 | |
| 1335 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1336 | PeerConnection::FindTransceiverBySender( |
| 1337 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1338 | for (auto transceiver : transceivers_) { |
| 1339 | if (transceiver->sender() == sender) { |
| 1340 | return transceiver; |
| 1341 | } |
| 1342 | } |
| 1343 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1346 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1347 | PeerConnection::AddTransceiver( |
| 1348 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1349 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1350 | } |
| 1351 | |
| 1352 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1353 | PeerConnection::AddTransceiver( |
| 1354 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1355 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1356 | RTC_CHECK(IsUnifiedPlan()) |
| 1357 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1358 | if (!track) { |
| 1359 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1360 | } |
| 1361 | cricket::MediaType media_type; |
| 1362 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1363 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1364 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1365 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1366 | } else { |
| 1367 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1368 | "Track kind is not audio or video"); |
| 1369 | } |
| 1370 | return AddTransceiver(media_type, track, init); |
| 1371 | } |
| 1372 | |
| 1373 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1374 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1375 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1376 | } |
| 1377 | |
| 1378 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1379 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1380 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1381 | RTC_CHECK(IsUnifiedPlan()) |
| 1382 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1383 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1384 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1385 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1386 | "media type is not audio or video"); |
| 1387 | } |
| 1388 | return AddTransceiver(media_type, nullptr, init); |
| 1389 | } |
| 1390 | |
| 1391 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1392 | PeerConnection::AddTransceiver( |
| 1393 | cricket::MediaType media_type, |
| 1394 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1395 | const RtpTransceiverInit& init, |
| 1396 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1397 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1398 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1399 | if (track) { |
| 1400 | RTC_DCHECK_EQ(media_type, |
| 1401 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1402 | ? cricket::MEDIA_TYPE_AUDIO |
| 1403 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1404 | } |
| 1405 | |
| 1406 | // TODO(bugs.webrtc.org/7600): Verify init. |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1407 | if (init.send_encodings.size() > 1) { |
| 1408 | LOG_AND_RETURN_ERROR( |
| 1409 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1410 | "Attempted to create an encoder with more than 1 encoding parameter."); |
| 1411 | } |
| 1412 | |
| 1413 | for (const auto& encoding : init.send_encodings) { |
| 1414 | if (encoding.ssrc.has_value()) { |
| 1415 | LOG_AND_RETURN_ERROR( |
| 1416 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1417 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | RtpParameters parameters; |
| 1422 | parameters.encodings = init.send_encodings; |
| 1423 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1424 | LOG_AND_RETURN_ERROR( |
| 1425 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1426 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1427 | } |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1428 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1429 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1430 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1431 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1432 | // that sender ID is already in use. |
| 1433 | std::string sender_id = |
| 1434 | (track && !FindSenderById(track->id()) ? track->id() |
| 1435 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1436 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
| 1437 | init.send_encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1438 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1439 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1440 | transceiver->internal()->set_direction(init.direction); |
| 1441 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1442 | if (fire_callback) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1443 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1444 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1445 | |
| 1446 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1447 | } |
| 1448 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1449 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1450 | PeerConnection::CreateSender( |
| 1451 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1452 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1453 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1454 | const std::vector<std::string>& stream_ids, |
| 1455 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1456 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1457 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1458 | RTC_DCHECK(!track || |
| 1459 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1460 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1461 | signaling_thread(), |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1462 | new AudioRtpSender(worker_thread(), id, stats_.get())); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1463 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1464 | } else { |
| 1465 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1466 | RTC_DCHECK(!track || |
| 1467 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1468 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1469 | signaling_thread(), new VideoRtpSender(worker_thread(), id)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1470 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1471 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1472 | bool set_track_succeeded = sender->SetTrack(track); |
| 1473 | RTC_DCHECK(set_track_succeeded); |
| 1474 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1475 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1476 | return sender; |
| 1477 | } |
| 1478 | |
| 1479 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1480 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1481 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1482 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1483 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1484 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1485 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1486 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1487 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1488 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1489 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1490 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1491 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1492 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1493 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1494 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1495 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1496 | return receiver; |
| 1497 | } |
| 1498 | |
| 1499 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1500 | PeerConnection::CreateAndAddTransceiver( |
| 1501 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1502 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1503 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1504 | // Ensure that the new sender does not have an ID that is already in use by |
| 1505 | // another sender. |
| 1506 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1507 | // could be invalid, but should not cause a crash). |
| 1508 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1509 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1510 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1511 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1512 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1513 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1514 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1517 | void PeerConnection::OnNegotiationNeeded() { |
| 1518 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1519 | RTC_DCHECK(!IsClosed()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1520 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1523 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1524 | const std::string& kind, |
| 1525 | const std::string& stream_id) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1526 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1527 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1528 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1529 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1530 | if (IsClosed()) { |
| 1531 | return nullptr; |
| 1532 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1533 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1534 | // Internally we need to have one stream with Plan B semantics, so we |
| 1535 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1536 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1537 | if (stream_id.empty()) { |
| 1538 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1539 | RTC_LOG(LS_INFO) |
| 1540 | << "No stream_id specified for sender. Generated stream ID: " |
| 1541 | << stream_ids[0]; |
| 1542 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1543 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1546 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1547 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1548 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1549 | auto* audio_sender = new AudioRtpSender( |
| 1550 | worker_thread(), rtc::CreateRandomUuid(), stats_.get()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1551 | audio_sender->SetVoiceMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1552 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1553 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1554 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1555 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1556 | auto* video_sender = |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1557 | new VideoRtpSender(worker_thread(), rtc::CreateRandomUuid()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1558 | video_sender->SetVideoMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1559 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1560 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1561 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1562 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1563 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1564 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1565 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1566 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1567 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1568 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1569 | } |
| 1570 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1571 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1572 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1573 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1574 | for (auto sender : GetSendersInternal()) { |
| 1575 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1576 | } |
| 1577 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1580 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1581 | PeerConnection::GetSendersInternal() const { |
| 1582 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1583 | all_senders; |
| 1584 | for (auto transceiver : transceivers_) { |
| 1585 | auto senders = transceiver->internal()->senders(); |
| 1586 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1587 | } |
| 1588 | return all_senders; |
| 1589 | } |
| 1590 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1591 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1592 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1593 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1594 | for (const auto& receiver : GetReceiversInternal()) { |
| 1595 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1596 | } |
| 1597 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1600 | std::vector< |
| 1601 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1602 | PeerConnection::GetReceiversInternal() const { |
| 1603 | std::vector< |
| 1604 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1605 | all_receivers; |
| 1606 | for (auto transceiver : transceivers_) { |
| 1607 | auto receivers = transceiver->internal()->receivers(); |
| 1608 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1609 | receivers.end()); |
| 1610 | } |
| 1611 | return all_receivers; |
| 1612 | } |
| 1613 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1614 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1615 | PeerConnection::GetTransceivers() const { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1616 | RTC_CHECK(IsUnifiedPlan()) |
| 1617 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1618 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
| 1619 | for (auto transceiver : transceivers_) { |
| 1620 | all_transceivers.push_back(transceiver); |
| 1621 | } |
| 1622 | return all_transceivers; |
| 1623 | } |
| 1624 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1625 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1626 | MediaStreamTrackInterface* track, |
| 1627 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1628 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1629 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1630 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1631 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1632 | return false; |
| 1633 | } |
| 1634 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1635 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1636 | // The StatsCollector is used to tell if a track is valid because it may |
| 1637 | // remember tracks that the PeerConnection previously removed. |
| 1638 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1639 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1640 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1641 | return false; |
| 1642 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1643 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 1644 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1645 | return true; |
| 1646 | } |
| 1647 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1648 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1649 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1650 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1651 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1652 | stats_collector_->GetStatsReport(callback); |
| 1653 | } |
| 1654 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1655 | void PeerConnection::GetStats( |
| 1656 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1657 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1658 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1659 | RTC_DCHECK(callback); |
| 1660 | RTC_DCHECK(stats_collector_); |
| 1661 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1662 | if (selector) { |
| 1663 | for (const auto& proxy_transceiver : transceivers_) { |
| 1664 | for (const auto& proxy_sender : |
| 1665 | proxy_transceiver->internal()->senders()) { |
| 1666 | if (proxy_sender == selector) { |
| 1667 | internal_sender = proxy_sender->internal(); |
| 1668 | break; |
| 1669 | } |
| 1670 | } |
| 1671 | if (internal_sender) |
| 1672 | break; |
| 1673 | } |
| 1674 | } |
| 1675 | // If there is no |internal_sender|Â then |selector| is either null or does not |
| 1676 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1677 | // PeerConnection). This means that "all the stats objects representing the |
| 1678 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1679 | // produces an empty stats report. |
| 1680 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1681 | } |
| 1682 | |
| 1683 | void PeerConnection::GetStats( |
| 1684 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1685 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1686 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1687 | RTC_DCHECK(callback); |
| 1688 | RTC_DCHECK(stats_collector_); |
| 1689 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1690 | if (selector) { |
| 1691 | for (const auto& proxy_transceiver : transceivers_) { |
| 1692 | for (const auto& proxy_receiver : |
| 1693 | proxy_transceiver->internal()->receivers()) { |
| 1694 | if (proxy_receiver == selector) { |
| 1695 | internal_receiver = proxy_receiver->internal(); |
| 1696 | break; |
| 1697 | } |
| 1698 | } |
| 1699 | if (internal_receiver) |
| 1700 | break; |
| 1701 | } |
| 1702 | } |
| 1703 | // If there is no |internal_receiver|Â then |selector| is either null or does |
| 1704 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1705 | // the PeerConnection). This means that "all the stats objects representing |
| 1706 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1707 | // selector produces an empty stats report. |
| 1708 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1709 | } |
| 1710 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1711 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1712 | return signaling_state_; |
| 1713 | } |
| 1714 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1715 | PeerConnectionInterface::IceConnectionState |
| 1716 | PeerConnection::ice_connection_state() { |
| 1717 | return ice_connection_state_; |
| 1718 | } |
| 1719 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1720 | PeerConnectionInterface::IceConnectionState |
| 1721 | PeerConnection::standardized_ice_connection_state() { |
| 1722 | return standardized_ice_connection_state_; |
| 1723 | } |
| 1724 | |
| 1725 | PeerConnectionInterface::PeerConnectionState |
| 1726 | PeerConnection::peer_connection_state() { |
| 1727 | return connection_state_; |
| 1728 | } |
| 1729 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1730 | PeerConnectionInterface::IceGatheringState |
| 1731 | PeerConnection::ice_gathering_state() { |
| 1732 | return ice_gathering_state_; |
| 1733 | } |
| 1734 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1735 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1736 | const std::string& label, |
| 1737 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1738 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1739 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1740 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1741 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1742 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1743 | if (config) { |
| 1744 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1745 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1746 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1747 | InternalCreateDataChannel(label, internal_config.get())); |
| 1748 | if (!channel.get()) { |
| 1749 | return nullptr; |
| 1750 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1751 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1752 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1753 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1754 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1755 | Observer()->OnRenegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1756 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1757 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1758 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1759 | } |
| 1760 | |
| 1761 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1762 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1763 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1764 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1765 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1766 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1767 | return; |
| 1768 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1769 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1770 | if (IsClosed()) { |
| 1771 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1772 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1773 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1774 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1775 | return; |
| 1776 | } |
| 1777 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1778 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1779 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1780 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1781 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1782 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1783 | return; |
| 1784 | } |
| 1785 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1786 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1787 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1788 | if (IsUnifiedPlan()) { |
| 1789 | RTCError error = HandleLegacyOfferOptions(options); |
| 1790 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1791 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1792 | return; |
| 1793 | } |
| 1794 | } |
| 1795 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1796 | cricket::MediaSessionOptions session_options; |
| 1797 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1798 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1801 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1802 | const RTCOfferAnswerOptions& options) { |
| 1803 | RTC_DCHECK(IsUnifiedPlan()); |
| 1804 | |
| 1805 | if (options.offer_to_receive_audio == 0) { |
| 1806 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1807 | cricket::MEDIA_TYPE_AUDIO); |
| 1808 | } else if (options.offer_to_receive_audio == 1) { |
| 1809 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1810 | } else if (options.offer_to_receive_audio > 1) { |
| 1811 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1812 | "offer_to_receive_audio > 1 is not supported."); |
| 1813 | } |
| 1814 | |
| 1815 | if (options.offer_to_receive_video == 0) { |
| 1816 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1817 | cricket::MEDIA_TYPE_VIDEO); |
| 1818 | } else if (options.offer_to_receive_video == 1) { |
| 1819 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1820 | } else if (options.offer_to_receive_video > 1) { |
| 1821 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1822 | "offer_to_receive_video > 1 is not supported."); |
| 1823 | } |
| 1824 | |
| 1825 | return RTCError::OK(); |
| 1826 | } |
| 1827 | |
| 1828 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1829 | cricket::MediaType media_type) { |
| 1830 | for (auto transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1831 | RtpTransceiverDirection new_direction = |
| 1832 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 1833 | if (new_direction != transceiver->direction()) { |
| 1834 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 1835 | << " transceiver (MID=" |
| 1836 | << transceiver->mid().value_or("<not set>") << ") from " |
| 1837 | << RtpTransceiverDirectionToString( |
| 1838 | transceiver->direction()) |
| 1839 | << " to " |
| 1840 | << RtpTransceiverDirectionToString(new_direction) |
| 1841 | << " since CreateOffer specified offer_to_receive=0"; |
| 1842 | transceiver->internal()->set_direction(new_direction); |
| 1843 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1848 | cricket::MediaType media_type) { |
| 1849 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1850 | RTC_LOG(LS_INFO) |
| 1851 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 1852 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1853 | RtpTransceiverInit init; |
| 1854 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1855 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1860 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1861 | std::vector< |
| 1862 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1863 | receiving_transceivers; |
| 1864 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1865 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1866 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1867 | receiving_transceivers.push_back(transceiver); |
| 1868 | } |
| 1869 | } |
| 1870 | return receiving_transceivers; |
| 1871 | } |
| 1872 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1873 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1874 | const RTCOfferAnswerOptions& options) { |
| 1875 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1876 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1877 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1878 | return; |
| 1879 | } |
| 1880 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1881 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1882 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1883 | std::string error = |
| 1884 | "PeerConnection cannot create an answer in a state other than " |
| 1885 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1886 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1887 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1888 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1889 | return; |
| 1890 | } |
| 1891 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1892 | // The remote description should be set if we're in the right state. |
| 1893 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1894 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1895 | if (IsUnifiedPlan()) { |
| 1896 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1897 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1898 | "supported with Unified Plan semantics. Use the " |
| 1899 | "RtpTransceiver API instead."; |
| 1900 | } |
| 1901 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1902 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1903 | "supported with Unified Plan semantics. Use the " |
| 1904 | "RtpTransceiver API instead."; |
| 1905 | } |
| 1906 | } |
| 1907 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1908 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1909 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1910 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1911 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | void PeerConnection::SetLocalDescription( |
| 1915 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1916 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1917 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1918 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1919 | // The SetLocalDescription contract is that we take ownership of the session |
| 1920 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 1921 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 1922 | // description as a unique_ptr argument to formalize this agreement. |
| 1923 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 1924 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1925 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1926 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1927 | return; |
| 1928 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1929 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1930 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1931 | PostSetSessionDescriptionFailure( |
| 1932 | observer, |
| 1933 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1934 | return; |
| 1935 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1936 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1937 | // If a session error has occurred the PeerConnection is in a possibly |
| 1938 | // inconsistent state so fail right away. |
| 1939 | if (session_error() != SessionError::kNone) { |
| 1940 | std::string error_message = GetSessionErrorMsg(); |
| 1941 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1942 | PostSetSessionDescriptionFailure( |
| 1943 | observer, |
| 1944 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1945 | return; |
| 1946 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1947 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1948 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1949 | if (!error.ok()) { |
| 1950 | std::string error_message = GetSetDescriptionErrorMessage( |
| 1951 | cricket::CS_LOCAL, desc->GetType(), error); |
| 1952 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1953 | PostSetSessionDescriptionFailure( |
| 1954 | observer, |
| 1955 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1956 | return; |
| 1957 | } |
| 1958 | |
| 1959 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 1960 | // which may destroy it before returning. |
| 1961 | const SdpType type = desc->GetType(); |
| 1962 | |
| 1963 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1964 | // |desc| may be destroyed at this point. |
| 1965 | |
| 1966 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1967 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 1968 | // inconsistent state, so act conservatively here and set the session error |
| 1969 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 1970 | SetSessionError(SessionError::kContent, error.message()); |
| 1971 | std::string error_message = |
| 1972 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 1973 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1974 | PostSetSessionDescriptionFailure( |
| 1975 | observer, |
| 1976 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1977 | return; |
| 1978 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1979 | RTC_DCHECK(local_description()); |
| 1980 | |
| 1981 | PostSetSessionDescriptionSuccess(observer); |
| 1982 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1983 | // MaybeStartGathering needs to be called after posting |
| 1984 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1985 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1986 | transport_controller_->MaybeStartGathering(); |
| 1987 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1988 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1989 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1990 | // MaybeStartGathering... |
| 1991 | network_thread()->Invoke<void>( |
| 1992 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1993 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 1994 | // Make UMA notes about what was agreed to. |
| 1995 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1996 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1997 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | RTCError PeerConnection::ApplyLocalDescription( |
| 2001 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2002 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2003 | RTC_DCHECK(desc); |
| 2004 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2005 | // Update stats here so that we have the most recent stats for tracks and |
| 2006 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2007 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2008 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2009 | // Take a reference to the old local description since it's used below to |
| 2010 | // compare against the new local description. When setting the new local |
| 2011 | // description, grab ownership of the replaced session description in case it |
| 2012 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2013 | // of the method. |
| 2014 | const SessionDescriptionInterface* old_local_description = |
| 2015 | local_description(); |
| 2016 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2017 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2018 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2019 | replaced_local_description = pending_local_description_ |
| 2020 | ? std::move(pending_local_description_) |
| 2021 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2022 | current_local_description_ = std::move(desc); |
| 2023 | pending_local_description_ = nullptr; |
| 2024 | current_remote_description_ = std::move(pending_remote_description_); |
| 2025 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2026 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2027 | pending_local_description_ = std::move(desc); |
| 2028 | } |
| 2029 | // The session description to apply now must be accessed by |
| 2030 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2031 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2032 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2033 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2034 | if (!error.ok()) { |
| 2035 | return error; |
| 2036 | } |
| 2037 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2038 | if (IsUnifiedPlan()) { |
| 2039 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2040 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2041 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2042 | if (!error.ok()) { |
| 2043 | return error; |
| 2044 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2045 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2046 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2047 | for (auto transceiver : transceivers_) { |
| 2048 | const ContentInfo* content = |
| 2049 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2050 | if (!content) { |
| 2051 | continue; |
| 2052 | } |
| 2053 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2054 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2055 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2056 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2057 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2058 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2059 | // "recvonly", process the removal of a remote track for the media |
| 2060 | // description, given transceiver, removeList, and muteTracks. |
| 2061 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2062 | (transceiver->internal()->fired_direction() && |
| 2063 | RtpTransceiverDirectionHasRecv( |
| 2064 | *transceiver->internal()->fired_direction()))) { |
| 2065 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2066 | &removed_streams); |
| 2067 | } |
| 2068 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2069 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2070 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2071 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2072 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2073 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2074 | auto observer = Observer(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2075 | for (auto transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2076 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2077 | } |
| 2078 | for (auto stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2079 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2080 | } |
| 2081 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2082 | // Media channels will be created only when offer is set. These may use new |
| 2083 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2084 | if (type == SdpType::kOffer) { |
| 2085 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2086 | // description is applied. Restore back to old description. |
| 2087 | RTCError error = CreateChannels(*local_description()->description()); |
| 2088 | if (!error.ok()) { |
| 2089 | return error; |
| 2090 | } |
| 2091 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2092 | // Remove unused channels if MediaContentDescription is rejected. |
| 2093 | RemoveUnusedChannels(local_description()->description()); |
| 2094 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2095 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2096 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2097 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2098 | if (!error.ok()) { |
| 2099 | return error; |
| 2100 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2101 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2102 | if (remote_description()) { |
| 2103 | // Now that we have a local description, we can push down remote candidates. |
| 2104 | UseCandidatesInSessionDescription(remote_description()); |
| 2105 | } |
| 2106 | |
| 2107 | pending_ice_restarts_.clear(); |
| 2108 | if (session_error() != SessionError::kNone) { |
| 2109 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2110 | } |
| 2111 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2112 | // If setting the description decided our SSL role, allocate any necessary |
| 2113 | // SCTP sids. |
| 2114 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2115 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2116 | AllocateSctpSids(role); |
| 2117 | } |
| 2118 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2119 | if (IsUnifiedPlan()) { |
| 2120 | for (auto transceiver : transceivers_) { |
| 2121 | const ContentInfo* content = |
| 2122 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2123 | if (!content) { |
| 2124 | continue; |
| 2125 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2126 | const auto& streams = content->media_description()->streams(); |
| 2127 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2128 | transceiver->internal()->sender_internal()->set_stream_ids( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 2129 | streams[0].stream_ids()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2130 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2131 | streams[0].first_ssrc()); |
Steve Anton | 60b6c1d | 2018-06-13 11:32:27 -0700 | [diff] [blame] | 2132 | } else { |
| 2133 | // 0 is a special value meaning "this sender has no associated send |
| 2134 | // stream". Need to call this so the sender won't attempt to configure |
| 2135 | // a no longer existing stream and run into DCHECKs in the lower |
| 2136 | // layers. |
| 2137 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2138 | } |
| 2139 | } |
| 2140 | } else { |
| 2141 | // Plan B semantics. |
| 2142 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2143 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2144 | // local session description. |
| 2145 | const cricket::ContentInfo* audio_content = |
| 2146 | GetFirstAudioContent(local_description()->description()); |
| 2147 | if (audio_content) { |
| 2148 | if (audio_content->rejected) { |
| 2149 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2150 | } else { |
| 2151 | const cricket::AudioContentDescription* audio_desc = |
| 2152 | audio_content->media_description()->as_audio(); |
| 2153 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2154 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2155 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2156 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2157 | const cricket::ContentInfo* video_content = |
| 2158 | GetFirstVideoContent(local_description()->description()); |
| 2159 | if (video_content) { |
| 2160 | if (video_content->rejected) { |
| 2161 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2162 | } else { |
| 2163 | const cricket::VideoContentDescription* video_desc = |
| 2164 | video_content->media_description()->as_video(); |
| 2165 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2166 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2167 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2171 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2172 | if (data_content) { |
| 2173 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2174 | data_content->media_description()->as_data(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2175 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2176 | cricket::kMediaProtocolRtpPrefix)) { |
| 2177 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 2178 | } |
| 2179 | } |
| 2180 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2181 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2185 | SetSessionDescriptionObserver* observer, |
| 2186 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2187 | SetRemoteDescription( |
| 2188 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2189 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2190 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2191 | } |
| 2192 | |
| 2193 | void PeerConnection::SetRemoteDescription( |
| 2194 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2195 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2196 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2197 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2198 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2199 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2200 | return; |
| 2201 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2202 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2203 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2204 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2205 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2206 | return; |
| 2207 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2208 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2209 | // If a session error has occurred the PeerConnection is in a possibly |
| 2210 | // inconsistent state so fail right away. |
| 2211 | if (session_error() != SessionError::kNone) { |
| 2212 | std::string error_message = GetSessionErrorMsg(); |
| 2213 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2214 | observer->OnSetRemoteDescriptionComplete( |
| 2215 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2216 | return; |
| 2217 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2218 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 2219 | if (desc->GetType() == SdpType::kOffer) { |
| 2220 | // Report to UMA the format of the received offer. |
| 2221 | ReportSdpFormatReceived(*desc); |
| 2222 | } |
| 2223 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2224 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2225 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2226 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2227 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2228 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2229 | observer->OnSetRemoteDescriptionComplete( |
| 2230 | RTCError(error.type(), std::move(error_message))); |
| 2231 | return; |
| 2232 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2233 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2234 | // Grab the description type before moving ownership to |
| 2235 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2236 | const SdpType type = desc->GetType(); |
| 2237 | |
| 2238 | error = ApplyRemoteDescription(std::move(desc)); |
| 2239 | // |desc| may be destroyed at this point. |
| 2240 | |
| 2241 | if (!error.ok()) { |
| 2242 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2243 | // inconsistent state, so act conservatively here and set the session error |
| 2244 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2245 | SetSessionError(SessionError::kContent, error.message()); |
| 2246 | std::string error_message = |
| 2247 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2248 | RTC_LOG(LS_ERROR) << error_message; |
| 2249 | observer->OnSetRemoteDescriptionComplete( |
| 2250 | RTCError(error.type(), std::move(error_message))); |
| 2251 | return; |
| 2252 | } |
| 2253 | RTC_DCHECK(remote_description()); |
| 2254 | |
| 2255 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2256 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2257 | // MaybeStartGathering... |
| 2258 | network_thread()->Invoke<void>( |
| 2259 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2260 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2261 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2262 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2266 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2270 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2271 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2272 | RTC_DCHECK(desc); |
| 2273 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2274 | // Update stats here so that we have the most recent stats for tracks and |
| 2275 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2276 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2277 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2278 | // Take a reference to the old remote description since it's used below to |
| 2279 | // compare against the new remote description. When setting the new remote |
| 2280 | // description, grab ownership of the replaced session description in case it |
| 2281 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2282 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2283 | const SessionDescriptionInterface* old_remote_description = |
| 2284 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2285 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2286 | SdpType type = desc->GetType(); |
| 2287 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2288 | replaced_remote_description = pending_remote_description_ |
| 2289 | ? std::move(pending_remote_description_) |
| 2290 | : std::move(current_remote_description_); |
| 2291 | current_remote_description_ = std::move(desc); |
| 2292 | pending_remote_description_ = nullptr; |
| 2293 | current_local_description_ = std::move(pending_local_description_); |
| 2294 | } else { |
| 2295 | replaced_remote_description = std::move(pending_remote_description_); |
| 2296 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2297 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2298 | // The session description to apply now must be accessed by |
| 2299 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2300 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2301 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2302 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2303 | if (!error.ok()) { |
| 2304 | return error; |
| 2305 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2306 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2307 | if (IsUnifiedPlan()) { |
| 2308 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2309 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2310 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2311 | if (!error.ok()) { |
| 2312 | return error; |
| 2313 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2314 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2315 | // Media channels will be created only when offer is set. These may use new |
| 2316 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2317 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2318 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2319 | // description is applied. Restore back to old description. |
| 2320 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2321 | if (!error.ok()) { |
| 2322 | return error; |
| 2323 | } |
| 2324 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2325 | // Remove unused channels if MediaContentDescription is rejected. |
| 2326 | RemoveUnusedChannels(remote_description()->description()); |
| 2327 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2328 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2329 | // NOTE: Candidates allocation will be initiated only when |
| 2330 | // SetLocalDescription is called. |
| 2331 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2332 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2333 | if (!error.ok()) { |
| 2334 | return error; |
| 2335 | } |
| 2336 | |
| 2337 | if (local_description() && |
| 2338 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2339 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2340 | } |
| 2341 | |
| 2342 | if (old_remote_description) { |
| 2343 | for (const cricket::ContentInfo& content : |
| 2344 | old_remote_description->description()->contents()) { |
| 2345 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2346 | // password that indicates the remote peer requests an ICE restart. |
| 2347 | // TODO(deadbeef): When we start storing both the current and pending |
| 2348 | // remote description, this should reset pending_ice_restarts and compare |
| 2349 | // against the current description. |
| 2350 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2351 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2352 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2353 | pending_ice_restarts_.insert(content.name); |
| 2354 | } |
| 2355 | } else { |
| 2356 | // We retain all received candidates only if ICE is not restarted. |
| 2357 | // When ICE is restarted, all previous candidates belong to an old |
| 2358 | // generation and should not be kept. |
| 2359 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2360 | // description should only contain candidates from the last set remote |
| 2361 | // description plus any candidates added since then. We should remove |
| 2362 | // this once we're sure it won't break anything. |
| 2363 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2364 | old_remote_description, content.name, mutable_remote_description()); |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | if (session_error() != SessionError::kNone) { |
| 2370 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2371 | } |
| 2372 | |
| 2373 | // Set the the ICE connection state to connecting since the connection may |
| 2374 | // become writable with peer reflexive candidates before any remote candidate |
| 2375 | // is signaled. |
| 2376 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2377 | // is to have a new signal the indicates a change in checking state from the |
| 2378 | // transport and expose a new checking() member from transport that can be |
| 2379 | // read to determine the current checking state. The existing SignalConnecting |
| 2380 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2381 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 2382 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2383 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2384 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2385 | } |
| 2386 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2387 | // If setting the description decided our SSL role, allocate any necessary |
| 2388 | // SCTP sids. |
| 2389 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2390 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2391 | AllocateSctpSids(role); |
| 2392 | } |
| 2393 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2394 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2395 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2396 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2397 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2398 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2399 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2400 | for (auto transceiver : transceivers_) { |
| 2401 | const ContentInfo* content = |
| 2402 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2403 | if (!content) { |
| 2404 | continue; |
| 2405 | } |
| 2406 | const MediaContentDescription* media_desc = content->media_description(); |
| 2407 | RtpTransceiverDirection local_direction = |
| 2408 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 2409 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 "Set |
| 2410 | // the RTCSessionDescription: If direction is sendrecv or recvonly, and |
| 2411 | // transceiver's current direction is neither sendrecv nor recvonly, |
| 2412 | // process the addition of a remote track for the media description. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 2413 | std::vector<std::string> stream_ids; |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2414 | if (!media_desc->streams().empty()) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2415 | // The remote description has signaled the stream IDs. |
| 2416 | stream_ids = media_desc->streams()[0].stream_ids(); |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2417 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2418 | if (RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2419 | (!transceiver->fired_direction() || |
| 2420 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2421 | 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] | 2422 | << content->name << " (added to " |
| 2423 | << GetStreamIdsString(stream_ids) << ")."; |
| 2424 | |
| 2425 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 2426 | for (const std::string& stream_id : stream_ids) { |
| 2427 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2428 | remote_streams_->find(stream_id); |
| 2429 | if (!stream) { |
| 2430 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2431 | MediaStream::Create(stream_id)); |
| 2432 | remote_streams_->AddStream(stream); |
| 2433 | added_streams.push_back(stream); |
| 2434 | } |
| 2435 | media_streams.push_back(stream); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2436 | } |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2437 | // This will add the remote track to the streams. |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 2438 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 2439 | // instead. https://crbug.com/webrtc/9480 |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 2440 | transceiver->internal()->receiver_internal()->SetStreams(media_streams); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2441 | now_receiving_transceivers.push_back(transceiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2442 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2443 | // 2.2.8.1.7: If direction is "sendonly" or "inactive", and transceiver's |
| 2444 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 2445 | // removal of a remote track for the media description, given transceiver, |
| 2446 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2447 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2448 | (transceiver->fired_direction() && |
| 2449 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 2450 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2451 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2452 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2453 | // 2.2.8.1.8: Set transceiver's [[FiredDirection]] slot to direction. |
| 2454 | transceiver->internal()->set_fired_direction(local_direction); |
| 2455 | // 2.2.8.1.9: If description is of type "answer" or "pranswer", then run |
| 2456 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2457 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2458 | // 2.2.8.1.9.1: Set transceiver's [[CurrentDirection]] slot to |
| 2459 | // direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2460 | transceiver->internal()->set_current_direction(local_direction); |
| 2461 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2462 | // 2.2.8.1.10: If the media description is rejected, and transceiver is |
| 2463 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2464 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2465 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2466 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2467 | transceiver->Stop(); |
| 2468 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2469 | if (!content->rejected && |
| 2470 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2471 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2472 | uint32_t ssrc = 0; |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2473 | if (!media_desc->streams().empty() && |
| 2474 | media_desc->streams()[0].has_ssrcs()) { |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2475 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2476 | } |
| 2477 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2478 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2479 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2480 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2481 | auto observer = Observer(); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2482 | for (auto transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2483 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2484 | observer->OnTrack(transceiver); |
| 2485 | observer->OnAddTrack(transceiver->receiver(), |
| 2486 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2487 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2488 | for (auto stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2489 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2490 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2491 | for (auto transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2492 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2493 | } |
| 2494 | for (auto stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2495 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2496 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2497 | } |
| 2498 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2499 | const cricket::ContentInfo* audio_content = |
| 2500 | GetFirstAudioContent(remote_description()->description()); |
| 2501 | const cricket::ContentInfo* video_content = |
| 2502 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2503 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2504 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2505 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2506 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2507 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2508 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2509 | |
| 2510 | // Check if the descriptions include streams, just in case the peer supports |
| 2511 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2512 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2513 | (audio_desc && !audio_desc->streams().empty()) || |
| 2514 | (video_desc && !video_desc->streams().empty())) { |
| 2515 | remote_peer_supports_msid_ = true; |
| 2516 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2517 | |
| 2518 | // We wait to signal new streams until we finish processing the description, |
| 2519 | // since only at that point will new streams have all their tracks. |
| 2520 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2521 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2522 | if (!IsUnifiedPlan()) { |
| 2523 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2524 | // rejected media section, there is some cleanup logic that expects the |
| 2525 | // voice/ video channel to still be set. But in this method the voice/video |
| 2526 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2527 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2528 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2529 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2530 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2531 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2532 | // and MediaStreams. |
| 2533 | if (audio_content) { |
| 2534 | if (audio_content->rejected) { |
| 2535 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2536 | } else { |
| 2537 | bool default_audio_track_needed = |
| 2538 | !remote_peer_supports_msid_ && |
| 2539 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2540 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2541 | default_audio_track_needed, audio_desc->type(), |
| 2542 | new_streams); |
| 2543 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2544 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2545 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2546 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2547 | // and MediaStreams. |
| 2548 | if (video_content) { |
| 2549 | if (video_content->rejected) { |
| 2550 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2551 | } else { |
| 2552 | bool default_video_track_needed = |
| 2553 | !remote_peer_supports_msid_ && |
| 2554 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2555 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2556 | default_video_track_needed, video_desc->type(), |
| 2557 | new_streams); |
| 2558 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2559 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2560 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2561 | // Update the DataChannels with the information from the remote peer. |
| 2562 | if (data_desc) { |
| 2563 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2564 | cricket::kMediaProtocolRtpPrefix)) { |
| 2565 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2566 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2567 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2568 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2569 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2570 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2571 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2572 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2573 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2574 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2575 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2576 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2577 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2578 | UpdateEndedRemoteMediaStreams(); |
| 2579 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2580 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2581 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2582 | } |
| 2583 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2584 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 2585 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2586 | transceiver, |
| 2587 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 2588 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2589 | RTC_DCHECK(transceiver->mid()); |
| 2590 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 2591 | << *transceiver->mid(); |
| 2592 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams = |
| 2593 | transceiver->internal()->receiver_internal()->streams(); |
| 2594 | // This will remove the remote track from the streams. |
| 2595 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 2596 | remove_list->push_back(transceiver); |
| 2597 | // Remove any streams that no longer have tracks. |
| 2598 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead |
| 2599 | // of streams, see if the stream was removed by checking if this was the |
| 2600 | // last receiver with that stream ID. |
| 2601 | for (auto stream : media_streams) { |
| 2602 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 2603 | remote_streams_->RemoveStream(stream); |
| 2604 | removed_streams->push_back(stream); |
| 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2609 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2610 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2611 | const SessionDescriptionInterface& new_session, |
| 2612 | const SessionDescriptionInterface* old_local_description, |
| 2613 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2614 | RTC_DCHECK(IsUnifiedPlan()); |
| 2615 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2616 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2617 | if (new_session.GetType() == SdpType::kOffer) { |
| 2618 | auto bundle_group_or_error = |
| 2619 | GetEarlyBundleGroup(*new_session.description()); |
| 2620 | if (!bundle_group_or_error.ok()) { |
| 2621 | return bundle_group_or_error.MoveError(); |
| 2622 | } |
| 2623 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2624 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2625 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2626 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2627 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2628 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2629 | cricket::MediaType media_type = new_content.media_description()->type(); |
| 2630 | seen_mids_.insert(new_content.name); |
| 2631 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2632 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2633 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2634 | if (old_local_description && |
| 2635 | i < old_local_description->description()->contents().size()) { |
| 2636 | old_local_content = |
| 2637 | &old_local_description->description()->contents()[i]; |
| 2638 | } |
| 2639 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2640 | if (old_remote_description && |
| 2641 | i < old_remote_description->description()->contents().size()) { |
| 2642 | old_remote_content = |
| 2643 | &old_remote_description->description()->contents()[i]; |
| 2644 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2645 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2646 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2647 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2648 | if (!transceiver_or_error.ok()) { |
| 2649 | return transceiver_or_error.MoveError(); |
| 2650 | } |
| 2651 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2652 | RTCError error = |
| 2653 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2654 | if (!error.ok()) { |
| 2655 | return error; |
| 2656 | } |
| 2657 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2658 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2659 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2660 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 2661 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2662 | continue; |
| 2663 | } |
| 2664 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2665 | if (!error.ok()) { |
| 2666 | return error; |
| 2667 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2668 | } else { |
| 2669 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2670 | "Unknown section type."); |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | return RTCError::OK(); |
| 2675 | } |
| 2676 | |
| 2677 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2678 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2679 | transceiver, |
| 2680 | const cricket::ContentInfo& content, |
| 2681 | const cricket::ContentGroup* bundle_group) { |
| 2682 | RTC_DCHECK(IsUnifiedPlan()); |
| 2683 | RTC_DCHECK(transceiver); |
| 2684 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 2685 | if (content.rejected) { |
| 2686 | if (channel) { |
| 2687 | transceiver->internal()->SetChannel(nullptr); |
| 2688 | DestroyBaseChannel(channel); |
| 2689 | } |
| 2690 | } else { |
| 2691 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2692 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2693 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2694 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2695 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2696 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2697 | } |
| 2698 | if (!channel) { |
| 2699 | LOG_AND_RETURN_ERROR( |
| 2700 | RTCErrorType::INTERNAL_ERROR, |
| 2701 | "Failed to create channel for mid=" + content.name); |
| 2702 | } |
| 2703 | transceiver->internal()->SetChannel(channel); |
| 2704 | } |
| 2705 | } |
| 2706 | return RTCError::OK(); |
| 2707 | } |
| 2708 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2709 | RTCError PeerConnection::UpdateDataChannel( |
| 2710 | cricket::ContentSource source, |
| 2711 | const cricket::ContentInfo& content, |
| 2712 | const cricket::ContentGroup* bundle_group) { |
| 2713 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2714 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2715 | // will take care of rejecting it. |
| 2716 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2717 | } |
| 2718 | if (content.rejected) { |
| 2719 | DestroyDataChannel(); |
| 2720 | } else { |
| 2721 | if (!rtp_data_channel_ && !sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2722 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2723 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2724 | "Failed to create data channel."); |
| 2725 | } |
| 2726 | } |
| 2727 | if (source == cricket::CS_REMOTE) { |
| 2728 | const MediaContentDescription* data_desc = content.media_description(); |
| 2729 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2730 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2731 | } |
| 2732 | } |
| 2733 | } |
| 2734 | return RTCError::OK(); |
| 2735 | } |
| 2736 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2737 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2738 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2739 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2740 | size_t mline_index, |
| 2741 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2742 | const ContentInfo* old_local_content, |
| 2743 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2744 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2745 | // If this is an offer then the m= section might be recycled. If the m= |
| 2746 | // section is being recycled (defined as: rejected in the current local or |
| 2747 | // remote description and not rejected in new description), dissociate the |
| 2748 | // currently associated RtpTransceiver by setting its mid property to null, |
| 2749 | // and discard the mapping between the transceiver and its m= section index. |
| 2750 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 2751 | old_remote_content)) { |
| 2752 | // We want to dissociate the transceiver that has the rejected mid. |
| 2753 | const std::string& old_mid = |
| 2754 | (old_local_content && old_local_content->rejected) |
| 2755 | ? old_local_content->name |
| 2756 | : old_remote_content->name; |
| 2757 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2758 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2759 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 2760 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 2761 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 2762 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | const MediaContentDescription* media_desc = content.media_description(); |
| 2766 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2767 | if (source == cricket::CS_LOCAL) { |
| 2768 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2769 | // mapping between transceivers and m= section indices established when |
| 2770 | // creating the offer. |
| 2771 | if (!transceiver) { |
| 2772 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2773 | } |
| 2774 | if (!transceiver) { |
| 2775 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2776 | "Unknown transceiver"); |
| 2777 | } |
| 2778 | } else { |
| 2779 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2780 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2781 | // of the same type... |
| 2782 | if (!transceiver && |
| 2783 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2784 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2785 | } |
| 2786 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2787 | // recvonly direction. |
| 2788 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2789 | RTC_LOG(LS_INFO) << "Adding " |
| 2790 | << cricket::MediaTypeToString(media_desc->type()) |
| 2791 | << " transceiver for MID=" << content.name |
| 2792 | << " at i=" << mline_index |
| 2793 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 2794 | std::string sender_id = rtc::CreateRandomUuid(); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 2795 | auto sender = |
| 2796 | CreateSender(media_desc->type(), sender_id, nullptr, {}, {}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2797 | std::string receiver_id; |
| 2798 | if (!media_desc->streams().empty()) { |
| 2799 | receiver_id = media_desc->streams()[0].id; |
| 2800 | } else { |
| 2801 | receiver_id = rtc::CreateRandomUuid(); |
| 2802 | } |
| 2803 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2804 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2805 | transceiver->internal()->set_direction( |
| 2806 | RtpTransceiverDirection::kRecvOnly); |
| 2807 | } |
| 2808 | } |
| 2809 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2810 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2811 | LOG_AND_RETURN_ERROR( |
| 2812 | RTCErrorType::INVALID_PARAMETER, |
| 2813 | "Transceiver type does not match media description type."); |
| 2814 | } |
| 2815 | // Associate the found or created RtpTransceiver with the m= section by |
| 2816 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2817 | // section, and establish a mapping between the transceiver and the index of |
| 2818 | // the m= section. |
| 2819 | transceiver->internal()->set_mid(content.name); |
| 2820 | transceiver->internal()->set_mline_index(mline_index); |
| 2821 | return std::move(transceiver); |
| 2822 | } |
| 2823 | |
| 2824 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2825 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 2826 | RTC_DCHECK(IsUnifiedPlan()); |
| 2827 | for (auto transceiver : transceivers_) { |
| 2828 | if (transceiver->mid() == mid) { |
| 2829 | return transceiver; |
| 2830 | } |
| 2831 | } |
| 2832 | return nullptr; |
| 2833 | } |
| 2834 | |
| 2835 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2836 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 2837 | RTC_DCHECK(IsUnifiedPlan()); |
| 2838 | for (auto transceiver : transceivers_) { |
| 2839 | if (transceiver->internal()->mline_index() == mline_index) { |
| 2840 | return transceiver; |
| 2841 | } |
| 2842 | } |
| 2843 | return nullptr; |
| 2844 | } |
| 2845 | |
| 2846 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2847 | PeerConnection::FindAvailableTransceiverToReceive( |
| 2848 | cricket::MediaType media_type) const { |
| 2849 | RTC_DCHECK(IsUnifiedPlan()); |
| 2850 | // From JSEP section 5.10 (Applying a Remote Description): |
| 2851 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 2852 | // the same type that were added to the PeerConnection by addTrack and are not |
| 2853 | // associated with any m= section and are not stopped, find the first such |
| 2854 | // RtpTransceiver. |
| 2855 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2856 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2857 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 2858 | !transceiver->stopped()) { |
| 2859 | return transceiver; |
| 2860 | } |
| 2861 | } |
| 2862 | return nullptr; |
| 2863 | } |
| 2864 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2865 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 2866 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2867 | transceiver, |
| 2868 | const SessionDescriptionInterface* sdesc) const { |
| 2869 | RTC_DCHECK(transceiver); |
| 2870 | RTC_DCHECK(sdesc); |
| 2871 | if (IsUnifiedPlan()) { |
| 2872 | if (!transceiver->internal()->mid()) { |
| 2873 | // This transceiver is not associated with a media section yet. |
| 2874 | return nullptr; |
| 2875 | } |
| 2876 | return sdesc->description()->GetContentByName( |
| 2877 | *transceiver->internal()->mid()); |
| 2878 | } else { |
| 2879 | // Plan B only allows at most one audio and one video section, so use the |
| 2880 | // first media section of that type. |
| 2881 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2882 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2883 | } |
| 2884 | } |
| 2885 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 2886 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 2887 | return configuration_; |
| 2888 | } |
| 2889 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2890 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 2891 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2892 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 2893 | if (IsClosed()) { |
| 2894 | RTC_LOG(LS_ERROR) << "SetConfiguration: PeerConnection is closed."; |
| 2895 | return SafeSetError(RTCErrorType::INVALID_STATE, error); |
| 2896 | } |
| 2897 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 2898 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 2899 | // size is not allowed, and changing the set of ICE servers will not result |
| 2900 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2901 | if (local_description() && configuration.ice_candidate_pool_size != |
| 2902 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2903 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 2904 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2905 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2906 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2907 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 2908 | if (local_description() && |
| 2909 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 2910 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 2911 | "SetLocalDescription."; |
| 2912 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2913 | } |
| 2914 | |
| 2915 | if (remote_description() && |
| 2916 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 2917 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 2918 | "SetRemoteDescription."; |
| 2919 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2920 | } |
| 2921 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 2922 | if (local_description() && |
| 2923 | configuration.crypto_options != configuration_.crypto_options) { |
| 2924 | RTC_LOG(LS_ERROR) << "Can't change crypto_options after calling " |
| 2925 | "SetLocalDescription."; |
| 2926 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2927 | } |
| 2928 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2929 | // The simplest (and most future-compatible) way to tell if the config was |
| 2930 | // modified in an invalid way is to copy each property we do support |
| 2931 | // modifying, then use operator==. There are far more properties we don't |
| 2932 | // support modifying than those we do, and more could be added. |
| 2933 | RTCConfiguration modified_config = configuration_; |
| 2934 | modified_config.servers = configuration.servers; |
| 2935 | modified_config.type = configuration.type; |
| 2936 | modified_config.ice_candidate_pool_size = |
| 2937 | configuration.ice_candidate_pool_size; |
| 2938 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2939 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 2940 | modified_config.ice_check_interval_strong_connectivity = |
| 2941 | configuration.ice_check_interval_strong_connectivity; |
| 2942 | modified_config.ice_check_interval_weak_connectivity = |
| 2943 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2944 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 2945 | modified_config.ice_unwritable_min_checks = |
| 2946 | configuration.ice_unwritable_min_checks; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2947 | modified_config.stun_candidate_keepalive_interval = |
| 2948 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2949 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 2950 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 2951 | modified_config.active_reset_srtp_params = |
| 2952 | configuration.active_reset_srtp_params; |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 2953 | modified_config.use_media_transport = configuration.use_media_transport; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2954 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2955 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2956 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2957 | } |
| 2958 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 2959 | // Validate the modified configuration. |
| 2960 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 2961 | if (!validate_error.ok()) { |
| 2962 | return SafeSetError(std::move(validate_error), error); |
| 2963 | } |
| 2964 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2965 | // Note that this isn't possible through chromium, since it's an unsigned |
| 2966 | // short in WebIDL. |
| 2967 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 03:34:17 -0700 | [diff] [blame] | 2968 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2969 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 2970 | } |
| 2971 | |
| 2972 | // Parse ICE servers before hopping to network thread. |
| 2973 | cricket::ServerAddresses stun_servers; |
| 2974 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 2975 | RTCErrorType parse_error = |
| 2976 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 2977 | if (parse_error != RTCErrorType::NONE) { |
| 2978 | return SafeSetError(parse_error, error); |
| 2979 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2980 | // Note if STUN or TURN servers were supplied. |
| 2981 | if (!stun_servers.empty()) { |
| 2982 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 2983 | } |
| 2984 | if (!turn_servers.empty()) { |
| 2985 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 2986 | } |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2987 | |
| 2988 | // In theory this shouldn't fail. |
| 2989 | if (!network_thread()->Invoke<bool>( |
| 2990 | RTC_FROM_HERE, |
| 2991 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 2992 | stun_servers, turn_servers, modified_config.type, |
| 2993 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2994 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2995 | modified_config.turn_customizer, |
| 2996 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2997 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2998 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 2999 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3000 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3001 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 3002 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 3003 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3004 | if (modified_config.servers != configuration_.servers || |
| 3005 | modified_config.type != configuration_.type || |
| 3006 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3007 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3008 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3009 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 3010 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Piotr (Peter) Slatala | 97fc11f | 2018-10-18 12:57:59 -0700 | [diff] [blame] | 3011 | transport_controller_->SetMediaTransportFactory( |
| 3012 | modified_config.use_media_transport ? factory_->media_transport_factory() |
| 3013 | : nullptr); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3014 | |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3015 | if (configuration_.active_reset_srtp_params != |
| 3016 | modified_config.active_reset_srtp_params) { |
| 3017 | transport_controller_->SetActiveResetSrtpParams( |
| 3018 | modified_config.active_reset_srtp_params); |
| 3019 | } |
| 3020 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3021 | configuration_ = modified_config; |
| 3022 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3025 | bool PeerConnection::AddIceCandidate( |
| 3026 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3027 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3028 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3029 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3030 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3031 | return false; |
| 3032 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3033 | |
| 3034 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3035 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3036 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3037 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3038 | return false; |
| 3039 | } |
| 3040 | |
| 3041 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3042 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3043 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3044 | return false; |
| 3045 | } |
| 3046 | |
| 3047 | bool valid = false; |
| 3048 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 3049 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3050 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3051 | return false; |
| 3052 | } |
| 3053 | |
| 3054 | // Add this candidate to the remote session description. |
| 3055 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3056 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3057 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3058 | return false; |
| 3059 | } |
| 3060 | |
| 3061 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3062 | bool result = UseCandidate(ice_candidate); |
| 3063 | if (result) { |
| 3064 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 3065 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 3066 | } else { |
| 3067 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 3068 | } |
| 3069 | return result; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3070 | } else { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3071 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3072 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3073 | return true; |
| 3074 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3077 | bool PeerConnection::RemoveIceCandidates( |
| 3078 | const std::vector<cricket::Candidate>& candidates) { |
| 3079 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3080 | if (IsClosed()) { |
| 3081 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 3082 | return false; |
| 3083 | } |
| 3084 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3085 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3086 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 3087 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3088 | return false; |
| 3089 | } |
| 3090 | |
| 3091 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3092 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3093 | return false; |
| 3094 | } |
| 3095 | |
| 3096 | size_t number_removed = |
| 3097 | mutable_remote_description()->RemoveCandidates(candidates); |
| 3098 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3099 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3100 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3101 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3102 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3106 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 3107 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3108 | RTC_LOG(LS_ERROR) |
| 3109 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 3110 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3111 | } |
| 3112 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3113 | } |
| 3114 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3115 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3116 | if (!worker_thread()->IsCurrent()) { |
| 3117 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3118 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3119 | } |
| 3120 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3121 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 3122 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 3123 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3124 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3125 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3126 | "min_bitrate_bps <= 0"); |
| 3127 | } |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3128 | if (has_start) { |
| 3129 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3130 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3131 | "start_bitrate_bps < min_bitrate_bps"); |
| 3132 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3133 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3134 | "curent_bitrate_bps < 0"); |
| 3135 | } |
| 3136 | } |
| 3137 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3138 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3139 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3140 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3141 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3142 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3143 | "max_bitrate_bps < min_bitrate_bps"); |
| 3144 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3145 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3146 | "max_bitrate_bps < 0"); |
| 3147 | } |
| 3148 | } |
| 3149 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3150 | RTC_DCHECK(call_.get()); |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3151 | call_->GetTransportControllerSend()->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3152 | |
| 3153 | return RTCError::OK(); |
| 3154 | } |
| 3155 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3156 | void PeerConnection::SetBitrateAllocationStrategy( |
| 3157 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 3158 | bitrate_allocation_strategy) { |
| 3159 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 3160 | if (!worker_thread->IsCurrent()) { |
| 3161 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 3162 | bitrate_allocation_strategy.release(); |
| 3163 | auto functor = [this, strategy_raw]() { |
| 3164 | call_->SetBitrateAllocationStrategy( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3165 | absl::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3166 | }; |
| 3167 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 3168 | return; |
| 3169 | } |
| 3170 | RTC_DCHECK(call_.get()); |
| 3171 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 3172 | } |
| 3173 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3174 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3175 | if (!worker_thread()->IsCurrent()) { |
| 3176 | worker_thread()->Invoke<void>( |
| 3177 | RTC_FROM_HERE, |
| 3178 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3179 | return; |
| 3180 | } |
| 3181 | auto audio_state = |
| 3182 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3183 | audio_state->SetPlayout(playout); |
| 3184 | } |
| 3185 | |
| 3186 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3187 | if (!worker_thread()->IsCurrent()) { |
| 3188 | worker_thread()->Invoke<void>( |
| 3189 | RTC_FROM_HERE, |
| 3190 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3191 | return; |
| 3192 | } |
| 3193 | auto audio_state = |
| 3194 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3195 | audio_state->SetRecording(recording); |
| 3196 | } |
| 3197 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3198 | std::unique_ptr<rtc::SSLCertificate> |
| 3199 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3200 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3201 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3202 | return nullptr; |
| 3203 | } |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 3204 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3205 | } |
| 3206 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3207 | std::unique_ptr<rtc::SSLCertChain> |
| 3208 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3209 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3210 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3211 | return nullptr; |
| 3212 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3213 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3214 | audio_transceiver->internal()->channel()->transport_name()); |
| 3215 | } |
| 3216 | |
| 3217 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3218 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3219 | for (auto transceiver : transceivers_) { |
| 3220 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3221 | return transceiver; |
| 3222 | } |
| 3223 | } |
| 3224 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3225 | } |
| 3226 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3227 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3228 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3229 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3230 | const size_t max_size = (max_size_bytes < 0) |
| 3231 | ? RtcEventLog::kUnlimitedOutput |
| 3232 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 3233 | return StartRtcEventLog( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3234 | absl::make_unique<RtcEventLogOutputFile>(file, max_size), |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3235 | webrtc::RtcEventLog::kImmediateOutput); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3236 | } |
| 3237 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3238 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3239 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3240 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3241 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3242 | bool operator()() { |
| 3243 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3244 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3245 | PeerConnection* const pc; |
| 3246 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3247 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3248 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3249 | return worker_thread()->Invoke<bool>( |
| 3250 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3254 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3255 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3256 | } |
| 3257 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3258 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3259 | return pending_local_description_ ? pending_local_description_.get() |
| 3260 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3264 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3265 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3268 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3269 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3270 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3274 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3275 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3279 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3280 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3284 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3285 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3286 | } |
| 3287 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3288 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3289 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3290 | // Update stats here so that we have the most recent stats for tracks and |
| 3291 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3292 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3293 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3294 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3295 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3296 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3297 | for (auto transceiver : transceivers_) { |
| 3298 | transceiver->Stop(); |
| 3299 | } |
Steve Anton | 25cfeb9 | 2018-04-26 11:44:00 -0700 | [diff] [blame] | 3300 | |
| 3301 | // Ensure that all asynchronous stats requests are completed before destroying |
| 3302 | // the transport controller below. |
| 3303 | if (stats_collector_) { |
| 3304 | stats_collector_->WaitForPendingRequest(); |
| 3305 | } |
| 3306 | |
| 3307 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 3308 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3309 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3310 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3311 | // The event log is used in the transport controller, which must be outlived |
| 3312 | // by the former. CreateOffer by the peer connection is implemented |
| 3313 | // asynchronously and if the peer connection is closed without resetting the |
| 3314 | // WebRTC session description factory, the session description factory would |
| 3315 | // call the transport controller. |
| 3316 | webrtc_session_desc_factory_.reset(); |
| 3317 | transport_controller_.reset(); |
| 3318 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3319 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3320 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3321 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3322 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3323 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3324 | call_.reset(); |
| 3325 | // The event log must outlive call (and any other object that uses it). |
| 3326 | event_log_.reset(); |
| 3327 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3328 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3329 | // The .h file says that observer can be discarded after close() returns. |
| 3330 | // Make sure this is true. |
| 3331 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3334 | void PeerConnection::OnMessage(rtc::Message* msg) { |
| 3335 | switch (msg->message_id) { |
| 3336 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3337 | SetSessionDescriptionMsg* param = |
| 3338 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3339 | param->observer->OnSuccess(); |
| 3340 | delete param; |
| 3341 | break; |
| 3342 | } |
| 3343 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3344 | SetSessionDescriptionMsg* param = |
| 3345 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3346 | param->observer->OnFailure(std::move(param->error)); |
| 3347 | delete param; |
| 3348 | break; |
| 3349 | } |
| 3350 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3351 | CreateSessionDescriptionMsg* param = |
| 3352 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 3353 | param->observer->OnFailure(std::move(param->error)); |
| 3354 | delete param; |
| 3355 | break; |
| 3356 | } |
| 3357 | case MSG_GETSTATS: { |
| 3358 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 3359 | StatsReports reports; |
| 3360 | stats_->GetStats(param->track, &reports); |
| 3361 | param->observer->OnComplete(reports); |
| 3362 | delete param; |
| 3363 | break; |
| 3364 | } |
| 3365 | case MSG_FREE_DATACHANNELS: { |
| 3366 | sctp_data_channels_to_free_.clear(); |
| 3367 | break; |
| 3368 | } |
| 3369 | case MSG_REPORT_USAGE_PATTERN: { |
| 3370 | ReportUsagePattern(); |
| 3371 | break; |
| 3372 | } |
| 3373 | default: |
| 3374 | RTC_NOTREACHED() << "Not implemented"; |
| 3375 | break; |
| 3376 | } |
| 3377 | } |
| 3378 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3379 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3380 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3381 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3382 | GetAudioTransceiver()->internal()->channel()); |
| 3383 | if (voice_channel) { |
| 3384 | return voice_channel->media_channel(); |
| 3385 | } else { |
| 3386 | return nullptr; |
| 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3391 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3392 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3393 | GetVideoTransceiver()->internal()->channel()); |
| 3394 | if (video_channel) { |
| 3395 | return video_channel->media_channel(); |
| 3396 | } else { |
| 3397 | return nullptr; |
| 3398 | } |
| 3399 | } |
| 3400 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3401 | void PeerConnection::CreateAudioReceiver( |
| 3402 | MediaStreamInterface* stream, |
| 3403 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3404 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3405 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3406 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3407 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3408 | auto* audio_receiver = new AudioRtpReceiver( |
| 3409 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3410 | audio_receiver->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3411 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3412 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3413 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3414 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3415 | Observer()->OnAddTrack(receiver, std::move(streams)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3416 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3419 | void PeerConnection::CreateVideoReceiver( |
| 3420 | MediaStreamInterface* stream, |
| 3421 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3422 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3423 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3424 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3425 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3426 | auto* video_receiver = new VideoRtpReceiver( |
| 3427 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3428 | video_receiver->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3429 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3430 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3431 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3432 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3433 | Observer()->OnAddTrack(receiver, std::move(streams)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3434 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3437 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3438 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3439 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3440 | const RtpSenderInfo& remote_sender_info) { |
| 3441 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3442 | if (!receiver) { |
| 3443 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3444 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3445 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3446 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3447 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3448 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3449 | } else { |
| 3450 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3451 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3452 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3455 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3456 | MediaStreamInterface* stream) { |
| 3457 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3458 | RTC_DCHECK(track); |
| 3459 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3460 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3461 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3462 | // We already have a sender for this track, so just change the stream_id |
| 3463 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3464 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3465 | return; |
| 3466 | } |
| 3467 | |
| 3468 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3469 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 3470 | {stream->id()}, {}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3471 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3472 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3473 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3474 | // which will connect the sender to the underlying transport. This can |
| 3475 | // occur if a local session description that contains the ID of the sender |
| 3476 | // is set before AddStream is called. It can also occur if the local |
| 3477 | // session description is not changed and RemoveStream is called, and |
| 3478 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3479 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3480 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3481 | if (sender_info) { |
| 3482 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3483 | } |
| 3484 | } |
| 3485 | |
| 3486 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3487 | // indefinitely, when we have unified plan SDP. |
| 3488 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3489 | MediaStreamInterface* stream) { |
| 3490 | RTC_DCHECK(!IsClosed()); |
| 3491 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3492 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3493 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3494 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3495 | return; |
| 3496 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3497 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3501 | MediaStreamInterface* stream) { |
| 3502 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3503 | RTC_DCHECK(track); |
| 3504 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3505 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3506 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3507 | // We already have a sender for this track, so just change the stream_id |
| 3508 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3509 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3510 | return; |
| 3511 | } |
| 3512 | |
| 3513 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3514 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 3515 | {stream->id()}, {}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3516 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3517 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3518 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3519 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3520 | if (sender_info) { |
| 3521 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3526 | MediaStreamInterface* stream) { |
| 3527 | RTC_DCHECK(!IsClosed()); |
| 3528 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3529 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3530 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3531 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3532 | return; |
| 3533 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3534 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3535 | } |
| 3536 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3537 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3538 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3539 | if (ice_connection_state_ == new_state) { |
| 3540 | return; |
| 3541 | } |
| 3542 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3543 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3544 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3545 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3546 | return; |
| 3547 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3548 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3549 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3550 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3551 | RTC_DCHECK(ice_connection_state_ != |
| 3552 | PeerConnectionInterface::kIceConnectionClosed); |
| 3553 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3554 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3555 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 3558 | void PeerConnection::SetStandardizedIceConnectionState( |
| 3559 | PeerConnectionInterface::IceConnectionState new_state) { |
| 3560 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3561 | if (standardized_ice_connection_state_ == new_state) |
| 3562 | return; |
| 3563 | if (IsClosed()) |
| 3564 | return; |
| 3565 | standardized_ice_connection_state_ = new_state; |
| 3566 | // TODO(jonasolsson): Pass this value on to OnIceConnectionChange instead of |
| 3567 | // the old one once disconnects are handled properly. |
| 3568 | } |
| 3569 | |
| 3570 | void PeerConnection::SetConnectionState( |
| 3571 | PeerConnectionInterface::PeerConnectionState new_state) { |
| 3572 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3573 | if (connection_state_ == new_state) |
| 3574 | return; |
| 3575 | if (IsClosed()) |
| 3576 | return; |
| 3577 | connection_state_ = new_state; |
| 3578 | Observer()->OnConnectionChange(new_state); |
| 3579 | } |
| 3580 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3581 | void PeerConnection::OnIceGatheringChange( |
| 3582 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3583 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3584 | if (IsClosed()) { |
| 3585 | return; |
| 3586 | } |
| 3587 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3588 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3591 | void PeerConnection::OnIceCandidate( |
| 3592 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3593 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3594 | if (IsClosed()) { |
| 3595 | return; |
| 3596 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3597 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 3598 | if (candidate->candidate().type() == LOCAL_PORT_TYPE && |
| 3599 | candidate->candidate().address().IsPrivateIP()) { |
| 3600 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 3601 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3602 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3605 | void PeerConnection::OnIceCandidatesRemoved( |
| 3606 | const std::vector<cricket::Candidate>& candidates) { |
| 3607 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3608 | if (IsClosed()) { |
| 3609 | return; |
| 3610 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3611 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3612 | } |
| 3613 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3614 | void PeerConnection::ChangeSignalingState( |
| 3615 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3616 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3617 | if (signaling_state_ == signaling_state) { |
| 3618 | return; |
| 3619 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3620 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3621 | << GetSignalingStateString(signaling_state_) |
| 3622 | << " New state: " |
| 3623 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3624 | signaling_state_ = signaling_state; |
| 3625 | if (signaling_state == kClosed) { |
| 3626 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3627 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 3628 | standardized_ice_connection_state_ = |
| 3629 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
| 3630 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 3631 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3632 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3633 | ice_gathering_state_ = kIceGatheringComplete; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3634 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3635 | } |
| 3636 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3637 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3640 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3641 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3642 | if (IsClosed()) { |
| 3643 | return; |
| 3644 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3645 | AddAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3646 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3647 | } |
| 3648 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3649 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3650 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3651 | if (IsClosed()) { |
| 3652 | return; |
| 3653 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3654 | RemoveAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3655 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3659 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3660 | if (IsClosed()) { |
| 3661 | return; |
| 3662 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3663 | AddVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3664 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3668 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3669 | if (IsClosed()) { |
| 3670 | return; |
| 3671 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3672 | RemoveVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3673 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3674 | } |
| 3675 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3676 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3677 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3678 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3679 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3680 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3681 | } |
| 3682 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3683 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3684 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3685 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3686 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3687 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3688 | msg->error = std::move(error); |
| 3689 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3690 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3694 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3695 | RTCError error) { |
| 3696 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3697 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 3698 | msg->error = std::move(error); |
| 3699 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3700 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3703 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3704 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3705 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3706 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3707 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3708 | if (IsUnifiedPlan()) { |
| 3709 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3710 | } else { |
| 3711 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3712 | } |
| 3713 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3714 | // Intentionally unset the data channel type for RTP data channel with the |
| 3715 | // second condition. Otherwise the RTP data channels would be successfully |
| 3716 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3717 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3718 | // people won't try to use them. |
| 3719 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3720 | session_options->data_channel_type = data_channel_type(); |
| 3721 | } |
| 3722 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3723 | // Apply ICE restart flag and renomination flag. |
| 3724 | for (auto& options : session_options->media_description_options) { |
| 3725 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3726 | options.transport_options.enable_ice_renomination = |
| 3727 | configuration_.enable_ice_renomination; |
| 3728 | } |
| 3729 | |
| 3730 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 3731 | session_options->crypto_options = GetCryptoOptions(); |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3732 | session_options->is_unified_plan = IsUnifiedPlan(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 3733 | session_options->pooled_ice_credentials = |
| 3734 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 3735 | RTC_FROM_HERE, |
| 3736 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 3737 | port_allocator_.get())); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3741 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3742 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3743 | // Figure out transceiver directional preferences. |
| 3744 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3745 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3746 | |
| 3747 | // By default, generate sendrecv/recvonly m= sections. |
| 3748 | bool recv_audio = true; |
| 3749 | bool recv_video = true; |
| 3750 | |
| 3751 | // By default, only offer a new m= section if we have media to send with it. |
| 3752 | bool offer_new_audio_description = send_audio; |
| 3753 | bool offer_new_video_description = send_video; |
| 3754 | bool offer_new_data_description = HasDataChannels(); |
| 3755 | |
| 3756 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3757 | if (offer_answer_options.offer_to_receive_audio != |
| 3758 | RTCOfferAnswerOptions::kUndefined) { |
| 3759 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3760 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3761 | offer_new_audio_description || |
| 3762 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3763 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3764 | if (offer_answer_options.offer_to_receive_video != |
| 3765 | RTCOfferAnswerOptions::kUndefined) { |
| 3766 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3767 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3768 | offer_new_video_description || |
| 3769 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3772 | absl::optional<size_t> audio_index; |
| 3773 | absl::optional<size_t> video_index; |
| 3774 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3775 | // If a current description exists, generate m= sections in the same order, |
| 3776 | // using the first audio/video/data section that appears and rejecting |
| 3777 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3778 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3779 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3780 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3781 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3782 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3783 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3784 | } |
| 3785 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3786 | // Add audio/video/data m= sections to the end if needed. |
| 3787 | if (!audio_index && offer_new_audio_description) { |
| 3788 | session_options->media_description_options.push_back( |
| 3789 | cricket::MediaDescriptionOptions( |
| 3790 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3791 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3792 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3793 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3794 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3795 | if (!video_index && offer_new_video_description) { |
| 3796 | session_options->media_description_options.push_back( |
| 3797 | cricket::MediaDescriptionOptions( |
| 3798 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3799 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3800 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3801 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3802 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3803 | if (!data_index && offer_new_data_description) { |
| 3804 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3805 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3806 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3810 | !audio_index ? nullptr |
| 3811 | : &session_options->media_description_options[*audio_index]; |
| 3812 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3813 | !video_index ? nullptr |
| 3814 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3815 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3816 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 3817 | video_media_description_options, |
| 3818 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | // Find a new MID that is not already in |used_mids|, then add it to |used_mids| |
| 3822 | // and return a reference to it. |
| 3823 | // Generated MIDs should be no more than 3 bytes long to take up less space in |
| 3824 | // the RTP packet. |
| 3825 | static const std::string& AllocateMid(std::set<std::string>* used_mids) { |
| 3826 | RTC_DCHECK(used_mids); |
| 3827 | // We're boring: just generate MIDs 0, 1, 2, ... |
| 3828 | size_t i = 0; |
| 3829 | std::set<std::string>::iterator it; |
| 3830 | bool inserted; |
| 3831 | do { |
| 3832 | std::string mid = rtc::ToString(i++); |
| 3833 | auto insert_result = used_mids->insert(mid); |
| 3834 | it = insert_result.first; |
| 3835 | inserted = insert_result.second; |
| 3836 | } while (!inserted); |
| 3837 | return *it; |
| 3838 | } |
| 3839 | |
| 3840 | static cricket::MediaDescriptionOptions |
| 3841 | GetMediaDescriptionOptionsForTransceiver( |
| 3842 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3843 | transceiver, |
| 3844 | const std::string& mid) { |
| 3845 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3846 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3847 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3848 | // This behavior is specified in JSEP. The gist is that: |
| 3849 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 3850 | // sendrecv. |
| 3851 | // 2. If the MSID is included, then it must be included in any subsequent |
| 3852 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 3853 | if (!transceiver->stopped() && |
| 3854 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 3855 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 3856 | cricket::SenderOptions sender_options; |
| 3857 | sender_options.track_id = transceiver->sender()->id(); |
| 3858 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 3859 | int num_send_encoding_layers = |
| 3860 | transceiver->sender()->init_send_encodings().size(); |
| 3861 | sender_options.num_sim_layers = |
| 3862 | !num_send_encoding_layers ? 1 : num_send_encoding_layers; |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3863 | media_description_options.sender_options.push_back(sender_options); |
| 3864 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3865 | return media_description_options; |
| 3866 | } |
| 3867 | |
| 3868 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 3869 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3870 | cricket::MediaSessionOptions* session_options) { |
| 3871 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3872 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3873 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3874 | const ContentInfos& local_contents = |
| 3875 | (local_description() ? local_description()->description()->contents() |
| 3876 | : ContentInfos()); |
| 3877 | const ContentInfos& remote_contents = |
| 3878 | (remote_description() ? remote_description()->description()->contents() |
| 3879 | : ContentInfos()); |
| 3880 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3881 | // slots first. |
| 3882 | std::queue<size_t> recycleable_mline_indices; |
| 3883 | // Track the MIDs used in previous offer/answer exchanges and the current |
| 3884 | // offer so that new, unique MIDs are generated. |
| 3885 | std::set<std::string> used_mids = seen_mids_; |
| 3886 | // First, go through each media section that exists in either the local or |
| 3887 | // remote description and generate a media section in this offer for the |
| 3888 | // associated transceiver. If a media section can be recycled, generate a |
| 3889 | // default, rejected media section here that can be later overwritten. |
| 3890 | for (size_t i = 0; |
| 3891 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3892 | // Either |local_content| or |remote_content| is non-null. |
| 3893 | const ContentInfo* local_content = |
| 3894 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3895 | const ContentInfo* remote_content = |
| 3896 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3897 | bool had_been_rejected = (local_content && local_content->rejected) || |
| 3898 | (remote_content && remote_content->rejected); |
| 3899 | const std::string& mid = |
| 3900 | (local_content ? local_content->name : remote_content->name); |
| 3901 | cricket::MediaType media_type = |
| 3902 | (local_content ? local_content->media_description()->type() |
| 3903 | : remote_content->media_description()->type()); |
| 3904 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3905 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3906 | auto transceiver = GetAssociatedTransceiver(mid); |
| 3907 | RTC_CHECK(transceiver); |
| 3908 | // A media section is considered eligible for recycling if it is marked as |
| 3909 | // rejected in either the local or remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3910 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3911 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3912 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 3913 | RtpTransceiverDirection::kInactive, |
| 3914 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3915 | recycleable_mline_indices.push(i); |
| 3916 | } else { |
| 3917 | session_options->media_description_options.push_back( |
| 3918 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 3919 | // CreateOffer shouldn't really cause any state changes in |
| 3920 | // PeerConnection, but we need a way to match new transceivers to new |
| 3921 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 3922 | // by recording the index of the media section generated for the |
| 3923 | // transceiver in the offer. |
| 3924 | transceiver->internal()->set_mline_index(i); |
| 3925 | } |
| 3926 | } else { |
| 3927 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3928 | RTC_CHECK(GetDataMid()); |
| 3929 | if (had_been_rejected || mid != *GetDataMid()) { |
| 3930 | session_options->media_description_options.push_back( |
| 3931 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 3932 | } else { |
| 3933 | session_options->media_description_options.push_back( |
| 3934 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 3935 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3936 | } |
| 3937 | } |
| 3938 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3939 | // and not associated). Reuse media sections marked as recyclable first, |
| 3940 | // otherwise append to the end of the offer. New media sections should be |
| 3941 | // added in the order they were added to the PeerConnection. |
| 3942 | for (auto transceiver : transceivers_) { |
| 3943 | if (transceiver->mid() || transceiver->stopped()) { |
| 3944 | continue; |
| 3945 | } |
| 3946 | size_t mline_index; |
| 3947 | if (!recycleable_mline_indices.empty()) { |
| 3948 | mline_index = recycleable_mline_indices.front(); |
| 3949 | recycleable_mline_indices.pop(); |
| 3950 | session_options->media_description_options[mline_index] = |
| 3951 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3952 | AllocateMid(&used_mids)); |
| 3953 | } else { |
| 3954 | mline_index = session_options->media_description_options.size(); |
| 3955 | session_options->media_description_options.push_back( |
| 3956 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3957 | AllocateMid(&used_mids))); |
| 3958 | } |
| 3959 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3960 | transceiver->internal()->set_mline_index(mline_index); |
| 3961 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3962 | // Lastly, add a m-section if we have local data channels and an m section |
| 3963 | // does not already exist. |
| 3964 | if (!GetDataMid() && HasDataChannels()) { |
| 3965 | session_options->media_description_options.push_back( |
| 3966 | GetMediaDescriptionOptionsForActiveData(AllocateMid(&used_mids))); |
| 3967 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3968 | } |
| 3969 | |
| 3970 | void PeerConnection::GetOptionsForAnswer( |
| 3971 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3972 | cricket::MediaSessionOptions* session_options) { |
| 3973 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3974 | |
| 3975 | if (IsUnifiedPlan()) { |
| 3976 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3977 | } else { |
| 3978 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3979 | } |
| 3980 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3981 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3982 | // the RTP data channels would be successfully negotiated by default and the |
| 3983 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3984 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 3985 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3986 | session_options->data_channel_type = data_channel_type(); |
| 3987 | } |
| 3988 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3989 | // Apply ICE renomination flag. |
| 3990 | for (auto& options : session_options->media_description_options) { |
| 3991 | options.transport_options.enable_ice_renomination = |
| 3992 | configuration_.enable_ice_renomination; |
| 3993 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 3994 | |
| 3995 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 3996 | session_options->crypto_options = GetCryptoOptions(); |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3997 | session_options->is_unified_plan = IsUnifiedPlan(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 3998 | session_options->pooled_ice_credentials = |
| 3999 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4000 | RTC_FROM_HERE, |
| 4001 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4002 | port_allocator_.get())); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4003 | } |
| 4004 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4005 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 4006 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4007 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4008 | // Figure out transceiver directional preferences. |
| 4009 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4010 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4011 | |
| 4012 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 4013 | // restricted by the direction in the offer. |
| 4014 | bool recv_audio = true; |
| 4015 | bool recv_video = true; |
| 4016 | |
| 4017 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4018 | if (offer_answer_options.offer_to_receive_audio != |
| 4019 | RTCOfferAnswerOptions::kUndefined) { |
| 4020 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 4021 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4022 | if (offer_answer_options.offer_to_receive_video != |
| 4023 | RTCOfferAnswerOptions::kUndefined) { |
| 4024 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4025 | } |
| 4026 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4027 | absl::optional<size_t> audio_index; |
| 4028 | absl::optional<size_t> video_index; |
| 4029 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 4030 | |
| 4031 | // Generate m= sections that match those in the offer. |
| 4032 | // Note that mediasession.cc will handle intersection our preferred |
| 4033 | // direction with the offered direction. |
| 4034 | GenerateMediaDescriptionOptions( |
| 4035 | remote_description(), |
| 4036 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4037 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 4038 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4039 | |
| 4040 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4041 | !audio_index ? nullptr |
| 4042 | : &session_options->media_description_options[*audio_index]; |
| 4043 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4044 | !video_index ? nullptr |
| 4045 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4046 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4047 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 4048 | video_media_description_options, |
| 4049 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4050 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 4051 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4052 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 4053 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4054 | cricket::MediaSessionOptions* session_options) { |
| 4055 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 4056 | // Answers) and 5.3.2 (Subsequent Answers). |
| 4057 | RTC_DCHECK(remote_description()); |
| 4058 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 4059 | for (const ContentInfo& content : |
| 4060 | remote_description()->description()->contents()) { |
| 4061 | cricket::MediaType media_type = content.media_description()->type(); |
| 4062 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4063 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4064 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 4065 | RTC_CHECK(transceiver); |
| 4066 | session_options->media_description_options.push_back( |
| 4067 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 4068 | } else { |
| 4069 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 4070 | // Reject all data sections if data channels are disabled. |
| 4071 | // Reject a data section if it has already been rejected. |
| 4072 | // Reject all data sections except for the first one. |
| 4073 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 4074 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4075 | session_options->media_description_options.push_back( |
| 4076 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 4077 | } else { |
| 4078 | session_options->media_description_options.push_back( |
| 4079 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 4080 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4081 | } |
| 4082 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4083 | } |
| 4084 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4085 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 4086 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4087 | RtpTransceiverDirection audio_direction, |
| 4088 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4089 | absl::optional<size_t>* audio_index, |
| 4090 | absl::optional<size_t>* video_index, |
| 4091 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4092 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4093 | for (const cricket::ContentInfo& content : |
| 4094 | session_desc->description()->contents()) { |
| 4095 | if (IsAudioContent(&content)) { |
| 4096 | // If we already have an audio m= section, reject this extra one. |
| 4097 | if (*audio_index) { |
| 4098 | session_options->media_description_options.push_back( |
| 4099 | cricket::MediaDescriptionOptions( |
| 4100 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4101 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4102 | } else { |
| 4103 | session_options->media_description_options.push_back( |
| 4104 | cricket::MediaDescriptionOptions( |
| 4105 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4106 | audio_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4107 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4108 | } |
| 4109 | } else if (IsVideoContent(&content)) { |
| 4110 | // If we already have an video m= section, reject this extra one. |
| 4111 | if (*video_index) { |
| 4112 | session_options->media_description_options.push_back( |
| 4113 | cricket::MediaDescriptionOptions( |
| 4114 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4115 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4116 | } else { |
| 4117 | session_options->media_description_options.push_back( |
| 4118 | cricket::MediaDescriptionOptions( |
| 4119 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4120 | video_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4121 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4122 | } |
| 4123 | } else { |
| 4124 | RTC_DCHECK(IsDataContent(&content)); |
| 4125 | // If we already have an data m= section, reject this extra one. |
| 4126 | if (*data_index) { |
| 4127 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4128 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4129 | } else { |
| 4130 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4131 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4132 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4133 | } |
| 4134 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4135 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4136 | } |
| 4137 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4138 | cricket::MediaDescriptionOptions |
| 4139 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 4140 | const std::string& mid) const { |
| 4141 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4142 | // expect sendrecv. |
| 4143 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4144 | RtpTransceiverDirection::kSendRecv, |
| 4145 | /*stopped=*/false); |
| 4146 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4147 | return options; |
| 4148 | } |
| 4149 | |
| 4150 | cricket::MediaDescriptionOptions |
| 4151 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4152 | const std::string& mid) const { |
| 4153 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4154 | RtpTransceiverDirection::kInactive, |
| 4155 | /*stopped=*/true); |
| 4156 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4157 | return options; |
| 4158 | } |
| 4159 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4160 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4161 | switch (data_channel_type_) { |
| 4162 | case cricket::DCT_RTP: |
| 4163 | if (!rtp_data_channel_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4164 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4165 | } |
| 4166 | return rtp_data_channel_->content_name(); |
| 4167 | case cricket::DCT_SCTP: |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4168 | return sctp_mid_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4169 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4170 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4171 | } |
| 4172 | } |
| 4173 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4174 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 4175 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4176 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4177 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 4178 | } |
| 4179 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4180 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4181 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4182 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4183 | cricket::MediaType media_type, |
| 4184 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4185 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4186 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4187 | std::vector<RtpSenderInfo>* current_senders = |
| 4188 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4189 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4190 | // 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] | 4191 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4192 | for (auto sender_it = current_senders->begin(); |
| 4193 | sender_it != current_senders->end(); |
| 4194 | /* incremented manually */) { |
| 4195 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4196 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4197 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4198 | std::string params_stream_id; |
| 4199 | if (params) { |
| 4200 | params_stream_id = |
| 4201 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 4202 | : kDefaultStreamId); |
| 4203 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4204 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4205 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4206 | // 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] | 4207 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4208 | sender_exists) { |
| 4209 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4210 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4211 | OnRemoteSenderRemoved(info, media_type); |
| 4212 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4213 | } |
| 4214 | } |
| 4215 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4216 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4217 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4218 | if (!params.has_ssrcs()) { |
| 4219 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 4220 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 4221 | // create a default. |
| 4222 | default_sender_needed = true; |
| 4223 | break; |
| 4224 | } |
| 4225 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4226 | // |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] | 4227 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4228 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 4229 | // not supported in Plan B, we just take the first here and create the |
| 4230 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4231 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4232 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 4233 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4234 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4235 | uint32_t ssrc = params.first_ssrc(); |
| 4236 | |
| 4237 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4238 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4239 | if (!stream) { |
| 4240 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4241 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4242 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4243 | remote_streams_->AddStream(stream); |
| 4244 | new_streams->AddStream(stream); |
| 4245 | } |
| 4246 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4247 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4248 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4249 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4250 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4251 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4252 | } |
| 4253 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4254 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4255 | // Add default sender if necessary. |
| 4256 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4257 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4258 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4259 | if (!default_stream) { |
| 4260 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4261 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4262 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4263 | remote_streams_->AddStream(default_stream); |
| 4264 | new_streams->AddStream(default_stream); |
| 4265 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4266 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4267 | ? kDefaultAudioSenderId |
| 4268 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4269 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4270 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4271 | if (!default_sender_info) { |
| 4272 | current_senders->push_back( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4273 | RtpSenderInfo(kDefaultStreamId, default_sender_id, 0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4274 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4275 | } |
| 4276 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4277 | } |
| 4278 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4279 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 4280 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4281 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 4282 | << " receiver for track_id=" << sender_info.sender_id |
| 4283 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4284 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4285 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4286 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4287 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4288 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4289 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4290 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 4291 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4292 | } |
| 4293 | } |
| 4294 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4295 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 4296 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4297 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 4298 | << " receiver for track_id=" << sender_info.sender_id |
| 4299 | << " and stream_id=" << sender_info.stream_id; |
| 4300 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4301 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4302 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4303 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4304 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4305 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 4306 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4307 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4308 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4309 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4310 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4311 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4312 | } |
| 4313 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4314 | // Stopping or destroying a VideoRtpReceiver will end the |
| 4315 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4316 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4317 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4318 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4319 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4320 | // There's no guarantee the track is still available, e.g. the track may |
| 4321 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4322 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4323 | } |
| 4324 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 4325 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4326 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4327 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4328 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4329 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4333 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4334 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4335 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4336 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4337 | streams_to_remove.push_back(stream); |
| 4338 | } |
| 4339 | } |
| 4340 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4341 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4342 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4343 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4344 | } |
| 4345 | } |
| 4346 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4347 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4348 | const std::vector<cricket::StreamParams>& streams, |
| 4349 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4350 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4351 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4352 | // 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] | 4353 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4354 | for (auto sender_it = current_senders->begin(); |
| 4355 | sender_it != current_senders->end(); |
| 4356 | /* incremented manually */) { |
| 4357 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4358 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4359 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4360 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4361 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4362 | OnLocalSenderRemoved(info, media_type); |
| 4363 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4364 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4365 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4366 | } |
| 4367 | } |
| 4368 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4369 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4370 | for (const cricket::StreamParams& params : streams) { |
| 4371 | // 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] | 4372 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4373 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4374 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4375 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4376 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4377 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4378 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4379 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4380 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4381 | } |
| 4382 | } |
| 4383 | } |
| 4384 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4385 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4386 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4387 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4388 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4389 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4390 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4391 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4392 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4393 | return; |
| 4394 | } |
| 4395 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4396 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4397 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4398 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4399 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4400 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4401 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4402 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4403 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4404 | } |
| 4405 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4406 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4407 | cricket::MediaType media_type) { |
| 4408 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4409 | if (!sender) { |
| 4410 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4411 | // SessionDescriptions has been renegotiated. |
| 4412 | return; |
| 4413 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4414 | |
| 4415 | // A sender has been removed from the SessionDescription but it's still |
| 4416 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4417 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4418 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4419 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4420 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4421 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4422 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4423 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4424 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4428 | const cricket::StreamParamsVec& streams) { |
| 4429 | std::vector<std::string> existing_channels; |
| 4430 | |
| 4431 | // Find new and active data channels. |
| 4432 | for (const cricket::StreamParams& params : streams) { |
| 4433 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4434 | // we use the same naming of data channels as we do for |
| 4435 | // MediaStreams and Tracks. |
| 4436 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4437 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4438 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4439 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4440 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4441 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4442 | continue; |
| 4443 | } |
| 4444 | // Set the SSRC the data channel should use for sending. |
| 4445 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4446 | existing_channels.push_back(data_channel_it->first); |
| 4447 | } |
| 4448 | |
| 4449 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4450 | } |
| 4451 | |
| 4452 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4453 | const cricket::StreamParamsVec& streams) { |
| 4454 | std::vector<std::string> existing_channels; |
| 4455 | |
| 4456 | // Find new and active data channels. |
| 4457 | for (const cricket::StreamParams& params : streams) { |
| 4458 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4459 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4460 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4461 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4462 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4463 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4464 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4465 | // This is a new data channel. |
| 4466 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4467 | } else { |
| 4468 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4469 | } |
| 4470 | existing_channels.push_back(label); |
| 4471 | } |
| 4472 | |
| 4473 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4474 | } |
| 4475 | |
| 4476 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4477 | const std::vector<std::string>& active_channels, |
| 4478 | bool is_local_update) { |
| 4479 | auto it = rtp_data_channels_.begin(); |
| 4480 | while (it != rtp_data_channels_.end()) { |
| 4481 | DataChannel* data_channel = it->second; |
| 4482 | if (std::find(active_channels.begin(), active_channels.end(), |
| 4483 | data_channel->label()) != active_channels.end()) { |
| 4484 | ++it; |
| 4485 | continue; |
| 4486 | } |
| 4487 | |
| 4488 | if (is_local_update) { |
| 4489 | data_channel->SetSendSsrc(0); |
| 4490 | } else { |
| 4491 | data_channel->RemotePeerRequestClose(); |
| 4492 | } |
| 4493 | |
| 4494 | if (data_channel->state() == DataChannel::kClosed) { |
| 4495 | rtp_data_channels_.erase(it); |
| 4496 | it = rtp_data_channels_.begin(); |
| 4497 | } else { |
| 4498 | ++it; |
| 4499 | } |
| 4500 | } |
| 4501 | } |
| 4502 | |
| 4503 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4504 | uint32_t remote_ssrc) { |
| 4505 | rtc::scoped_refptr<DataChannel> channel( |
| 4506 | InternalCreateDataChannel(label, nullptr)); |
| 4507 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4508 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4509 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4510 | return; |
| 4511 | } |
| 4512 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4513 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4514 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4515 | Observer()->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4519 | const std::string& label, |
| 4520 | const InternalDataChannelInit* config) { |
| 4521 | if (IsClosed()) { |
| 4522 | return nullptr; |
| 4523 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4524 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4525 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4526 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4527 | return nullptr; |
| 4528 | } |
| 4529 | InternalDataChannelInit new_config = |
| 4530 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4531 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4532 | if (new_config.id < 0) { |
| 4533 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4534 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4535 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4536 | RTC_LOG(LS_ERROR) |
| 4537 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4538 | return nullptr; |
| 4539 | } |
| 4540 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4541 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4542 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4543 | return nullptr; |
| 4544 | } |
| 4545 | } |
| 4546 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4547 | rtc::scoped_refptr<DataChannel> channel( |
| 4548 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4549 | if (!channel) { |
| 4550 | sid_allocator_.ReleaseSid(new_config.id); |
| 4551 | return nullptr; |
| 4552 | } |
| 4553 | |
| 4554 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4555 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4556 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4557 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4558 | return nullptr; |
| 4559 | } |
| 4560 | rtp_data_channels_[channel->label()] = channel; |
| 4561 | } else { |
| 4562 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 4563 | sctp_data_channels_.push_back(channel); |
| 4564 | channel->SignalClosed.connect(this, |
| 4565 | &PeerConnection::OnSctpDataChannelClosed); |
| 4566 | } |
| 4567 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4568 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4569 | return channel; |
| 4570 | } |
| 4571 | |
| 4572 | bool PeerConnection::HasDataChannels() const { |
| 4573 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4574 | } |
| 4575 | |
| 4576 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4577 | for (const auto& channel : sctp_data_channels_) { |
| 4578 | if (channel->id() < 0) { |
| 4579 | int sid; |
| 4580 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4581 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4582 | continue; |
| 4583 | } |
| 4584 | channel->SetSctpSid(sid); |
| 4585 | } |
| 4586 | } |
| 4587 | } |
| 4588 | |
| 4589 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4590 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4591 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4592 | ++it) { |
| 4593 | if (it->get() == channel) { |
| 4594 | if (channel->id() >= 0) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 4595 | // After the closing procedure is done, it's safe to use this ID for |
| 4596 | // another data channel. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4597 | sid_allocator_.ReleaseSid(channel->id()); |
| 4598 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4599 | // Since this method is triggered by a signal from the DataChannel, |
| 4600 | // we can't free it directly here; we need to free it asynchronously. |
| 4601 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4602 | sctp_data_channels_.erase(it); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4603 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4604 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4605 | return; |
| 4606 | } |
| 4607 | } |
| 4608 | } |
| 4609 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4610 | void PeerConnection::OnDataChannelDestroyed() { |
| 4611 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4612 | // DataChannel may callback to us and try to modify the list. |
| 4613 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4614 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4615 | for (const auto& kv : temp_rtp_dcs) { |
| 4616 | kv.second->OnTransportChannelDestroyed(); |
| 4617 | } |
| 4618 | |
| 4619 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4620 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4621 | for (const auto& channel : temp_sctp_dcs) { |
| 4622 | channel->OnTransportChannelDestroyed(); |
| 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | void PeerConnection::OnDataChannelOpenMessage( |
| 4627 | const std::string& label, |
| 4628 | const InternalDataChannelInit& config) { |
| 4629 | rtc::scoped_refptr<DataChannel> channel( |
| 4630 | InternalCreateDataChannel(label, &config)); |
| 4631 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4632 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4633 | return; |
| 4634 | } |
| 4635 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4636 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4637 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4638 | Observer()->OnDataChannel(std::move(proxy_channel)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 4639 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4640 | } |
| 4641 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4642 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4643 | PeerConnection::GetAudioTransceiver() const { |
| 4644 | // This method only works with Plan B SDP, where there is a single |
| 4645 | // audio/video transceiver. |
| 4646 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4647 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4648 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4649 | return transceiver; |
| 4650 | } |
| 4651 | } |
| 4652 | RTC_NOTREACHED(); |
| 4653 | return nullptr; |
| 4654 | } |
| 4655 | |
| 4656 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4657 | PeerConnection::GetVideoTransceiver() const { |
| 4658 | // This method only works with Plan B SDP, where there is a single |
| 4659 | // audio/video transceiver. |
| 4660 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4661 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4662 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4663 | return transceiver; |
| 4664 | } |
| 4665 | } |
| 4666 | RTC_NOTREACHED(); |
| 4667 | return nullptr; |
| 4668 | } |
| 4669 | |
| 4670 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4671 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4672 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4673 | switch (type) { |
| 4674 | case cricket::MEDIA_TYPE_AUDIO: |
| 4675 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4676 | case cricket::MEDIA_TYPE_VIDEO: |
| 4677 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4678 | case cricket::MEDIA_TYPE_DATA: |
| 4679 | return false; |
| 4680 | } |
| 4681 | RTC_NOTREACHED(); |
| 4682 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4683 | } |
| 4684 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4685 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4686 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 4687 | for (auto transceiver : transceivers_) { |
| 4688 | for (auto sender : transceiver->internal()->senders()) { |
| 4689 | if (sender->track() == track) { |
| 4690 | return sender; |
| 4691 | } |
| 4692 | } |
| 4693 | } |
| 4694 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4695 | } |
| 4696 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4697 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4698 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 4699 | for (auto transceiver : transceivers_) { |
| 4700 | for (auto sender : transceiver->internal()->senders()) { |
| 4701 | if (sender->id() == sender_id) { |
| 4702 | return sender; |
| 4703 | } |
| 4704 | } |
| 4705 | } |
| 4706 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4707 | } |
| 4708 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4709 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4710 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 4711 | for (auto transceiver : transceivers_) { |
| 4712 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4713 | if (receiver->id() == receiver_id) { |
| 4714 | return receiver; |
| 4715 | } |
| 4716 | } |
| 4717 | } |
| 4718 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4719 | } |
| 4720 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4721 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4722 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4723 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4724 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4725 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4726 | ? &remote_audio_sender_infos_ |
| 4727 | : &remote_video_sender_infos_; |
| 4728 | } |
| 4729 | |
| 4730 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4731 | cricket::MediaType media_type) { |
| 4732 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4733 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4734 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4735 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4736 | } |
| 4737 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4738 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4739 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4740 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4741 | const std::string sender_id) const { |
| 4742 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4743 | if (sender_info.stream_id == stream_id && |
| 4744 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4745 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4746 | } |
| 4747 | } |
| 4748 | return nullptr; |
| 4749 | } |
| 4750 | |
| 4751 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4752 | for (const auto& channel : sctp_data_channels_) { |
| 4753 | if (channel->id() == sid) { |
| 4754 | return channel; |
| 4755 | } |
| 4756 | } |
| 4757 | return nullptr; |
| 4758 | } |
| 4759 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4760 | bool PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 4761 | const cricket::ServerAddresses& stun_servers, |
| 4762 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4763 | const RTCConfiguration& configuration) { |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 4764 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4765 | // To handle both internal and externally created port allocator, we will |
| 4766 | // enable BUNDLE here. |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4767 | port_allocator_flags_ = port_allocator_->flags(); |
| 4768 | port_allocator_flags_ |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 4769 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 4770 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4771 | // If the disable-IPv6 flag was specified, we'll not override it |
| 4772 | // by experiment. |
| 4773 | if (configuration.disable_ipv6) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4774 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 4775 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 4776 | .find("Disabled") == 0) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4777 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4778 | } |
| 4779 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4780 | if (configuration.disable_ipv6_on_wifi) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4781 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4782 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4783 | } |
| 4784 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4785 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4786 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4787 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4788 | } |
| 4789 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4790 | if (configuration.candidate_network_policy == |
| 4791 | kCandidateNetworkPolicyLowCost) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4792 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4793 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4794 | } |
| 4795 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4796 | if (configuration.disable_link_local_networks) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4797 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4798 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 4799 | } |
| 4800 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4801 | port_allocator_->set_flags(port_allocator_flags_); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4802 | // No step delay is used while allocating ports. |
| 4803 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 4804 | port_allocator_->set_candidate_filter( |
| 4805 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 4806 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4807 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 4808 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 4809 | for (auto& turn_server : turn_servers_copy) { |
| 4810 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 4811 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4812 | // Call this last since it may create pooled allocator sessions using the |
| 4813 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4814 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 4815 | stun_servers, std::move(turn_servers_copy), |
| 4816 | configuration.ice_candidate_pool_size, configuration.prune_turn_ports, |
| 4817 | configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4818 | configuration.stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4819 | return true; |
| 4820 | } |
| 4821 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4822 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4823 | const cricket::ServerAddresses& stun_servers, |
| 4824 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 4825 | IceTransportsType type, |
| 4826 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4827 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4828 | webrtc::TurnCustomizer* turn_customizer, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4829 | absl::optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4830 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4831 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 4832 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 4833 | // size is not allowed, and changing the set of ICE servers will not result |
| 4834 | // in new candidates being gathered. |
| 4835 | if (local_description()) { |
| 4836 | port_allocator_->FreezeCandidatePool(); |
| 4837 | } |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 4838 | // Add the custom tls turn servers if they exist. |
| 4839 | auto turn_servers_copy = turn_servers; |
| 4840 | for (auto& turn_server : turn_servers_copy) { |
| 4841 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 4842 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4843 | // Call this last since it may create pooled allocator sessions using the |
| 4844 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 4845 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 4846 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
| 4847 | prune_turn_ports, turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4848 | } |
| 4849 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4850 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 4851 | return factory_->channel_manager(); |
| 4852 | } |
| 4853 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 4854 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4855 | std::unique_ptr<RtcEventLogOutput> output, |
| 4856 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4857 | if (!event_log_) { |
| 4858 | return false; |
| 4859 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4860 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4864 | if (event_log_) { |
| 4865 | event_log_->StopLogging(); |
| 4866 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4867 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4868 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4869 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 4870 | const std::string& content_name) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4871 | for (auto transceiver : transceivers_) { |
| 4872 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4873 | if (channel && channel->content_name() == content_name) { |
| 4874 | return channel; |
| 4875 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4876 | } |
| 4877 | if (rtp_data_channel() && |
| 4878 | rtp_data_channel()->content_name() == content_name) { |
| 4879 | return rtp_data_channel(); |
| 4880 | } |
| 4881 | return nullptr; |
| 4882 | } |
| 4883 | |
| 4884 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 4885 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4886 | RTC_LOG(LS_INFO) |
| 4887 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4888 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4889 | return false; |
| 4890 | } |
| 4891 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4892 | 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] | 4893 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4894 | return false; |
| 4895 | } |
| 4896 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4897 | auto dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
| 4898 | if (dtls_role) { |
| 4899 | *role = *dtls_role; |
| 4900 | return true; |
| 4901 | } |
| 4902 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 4906 | rtc::SSLRole* role) { |
| 4907 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4908 | RTC_LOG(LS_INFO) |
| 4909 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4910 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4911 | return false; |
| 4912 | } |
| 4913 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4914 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 4915 | if (dtls_role) { |
| 4916 | *role = *dtls_role; |
| 4917 | return true; |
| 4918 | } |
| 4919 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4920 | } |
| 4921 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4922 | void PeerConnection::SetSessionError(SessionError error, |
| 4923 | const std::string& error_desc) { |
| 4924 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4925 | if (error != session_error_) { |
| 4926 | session_error_ = error; |
| 4927 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4928 | } |
| 4929 | } |
| 4930 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4931 | RTCError PeerConnection::UpdateSessionState( |
| 4932 | SdpType type, |
| 4933 | cricket::ContentSource source, |
| 4934 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4935 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4936 | |
| 4937 | // If there's already a pending error then no state transition should happen. |
| 4938 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4939 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4940 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4941 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4942 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4943 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | // Update the signaling state according to the specified state machine (see |
| 4947 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4948 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4949 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4950 | ? PeerConnectionInterface::kHaveLocalOffer |
| 4951 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4952 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4953 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4954 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 4955 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4956 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4957 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4958 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 4959 | } |
| 4960 | |
| 4961 | // Update internal objects according to the session description's media |
| 4962 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4963 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4964 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 4965 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4966 | } |
| 4967 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4968 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4969 | } |
| 4970 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4971 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4972 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4973 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4974 | const SessionDescriptionInterface* sdesc = |
| 4975 | (source == cricket::CS_LOCAL ? local_description() |
| 4976 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4977 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4978 | |
| 4979 | // Push down the new SDP media section for each audio/video transceiver. |
| 4980 | for (auto transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4981 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4982 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4983 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4984 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4985 | continue; |
| 4986 | } |
| 4987 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4988 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4989 | if (!content_desc) { |
| 4990 | continue; |
| 4991 | } |
| 4992 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 4993 | bool success = (source == cricket::CS_LOCAL) |
| 4994 | ? channel->SetLocalContent(content_desc, type, &error) |
| 4995 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4996 | if (!success) { |
| 4997 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, std::move(error)); |
| 4998 | } |
| 4999 | } |
| 5000 | |
| 5001 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 5002 | if (rtp_data_channel_) { |
| 5003 | const ContentInfo* data_content = |
| 5004 | cricket::GetFirstDataContent(sdesc->description()); |
| 5005 | if (data_content && !data_content->rejected) { |
| 5006 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5007 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5008 | if (data_desc) { |
| 5009 | std::string error; |
| 5010 | bool success = |
| 5011 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5012 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5013 | : rtp_data_channel_->SetRemoteContent(data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5014 | if (!success) { |
| 5015 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5016 | std::move(error)); |
| 5017 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5018 | } |
| 5019 | } |
| 5020 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5021 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5022 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 5023 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 5024 | if (sctp_transport_ && local_description() && remote_description() && |
| 5025 | cricket::GetFirstDataContent(local_description()->description()) && |
| 5026 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5027 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5028 | RTC_FROM_HERE, |
| 5029 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5030 | if (!success) { |
| 5031 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5032 | "Failed to push down SCTP parameters."); |
| 5033 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5034 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5035 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5036 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5037 | } |
| 5038 | |
| 5039 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 5040 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5041 | RTC_DCHECK(local_description()); |
| 5042 | RTC_DCHECK(remote_description()); |
| 5043 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 5044 | // When we support "max-message-size", that would also be pushed down here. |
| 5045 | return sctp_transport_->Start( |
| 5046 | GetSctpPort(local_description()->description()), |
| 5047 | GetSctpPort(remote_description()->description())); |
| 5048 | } |
| 5049 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5050 | RTCError PeerConnection::PushdownTransportDescription( |
| 5051 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5052 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5053 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5054 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5055 | if (source == cricket::CS_LOCAL) { |
| 5056 | const SessionDescriptionInterface* sdesc = local_description(); |
| 5057 | RTC_DCHECK(sdesc); |
| 5058 | return transport_controller_->SetLocalDescription(type, |
| 5059 | sdesc->description()); |
| 5060 | } else { |
| 5061 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 5062 | RTC_DCHECK(sdesc); |
| 5063 | return transport_controller_->SetRemoteDescription(type, |
| 5064 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5065 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | bool PeerConnection::GetTransportDescription( |
| 5069 | const SessionDescription* description, |
| 5070 | const std::string& content_name, |
| 5071 | cricket::TransportDescription* tdesc) { |
| 5072 | if (!description || !tdesc) { |
| 5073 | return false; |
| 5074 | } |
| 5075 | const TransportInfo* transport_info = |
| 5076 | description->GetTransportInfoByName(content_name); |
| 5077 | if (!transport_info) { |
| 5078 | return false; |
| 5079 | } |
| 5080 | *tdesc = transport_info->description; |
| 5081 | return true; |
| 5082 | } |
| 5083 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5084 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 5085 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 5086 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5087 | switch (config.continual_gathering_policy) { |
| 5088 | case PeerConnectionInterface::GATHER_ONCE: |
| 5089 | gathering_policy = cricket::GATHER_ONCE; |
| 5090 | break; |
| 5091 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 5092 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 5093 | break; |
| 5094 | default: |
| 5095 | RTC_NOTREACHED(); |
| 5096 | gathering_policy = cricket::GATHER_ONCE; |
| 5097 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5098 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5099 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5100 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 5101 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5102 | ice_config.prioritize_most_likely_candidate_pairs = |
| 5103 | config.prioritize_most_likely_ice_candidate_pairs; |
| 5104 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5105 | RTCConfigurationToIceConfigOptionalInt( |
| 5106 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5107 | ice_config.continual_gathering_policy = gathering_policy; |
| 5108 | ice_config.presume_writable_when_fully_relayed = |
| 5109 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 5110 | ice_config.ice_check_interval_strong_connectivity = |
| 5111 | config.ice_check_interval_strong_connectivity; |
| 5112 | ice_config.ice_check_interval_weak_connectivity = |
| 5113 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5114 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5115 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5116 | ice_config.regather_all_networks_interval_range = |
| 5117 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5118 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5119 | return ice_config; |
| 5120 | } |
| 5121 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5122 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 5123 | std::string* track_id) { |
| 5124 | if (!local_description()) { |
| 5125 | return false; |
| 5126 | } |
| 5127 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 5128 | track_id); |
| 5129 | } |
| 5130 | |
| 5131 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 5132 | std::string* track_id) { |
| 5133 | if (!remote_description()) { |
| 5134 | return false; |
| 5135 | } |
| 5136 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 5137 | track_id); |
| 5138 | } |
| 5139 | |
| 5140 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 5141 | const rtc::CopyOnWriteBuffer& payload, |
| 5142 | cricket::SendDataResult* result) { |
| 5143 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5144 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5145 | "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5146 | return false; |
| 5147 | } |
| 5148 | return rtp_data_channel_ |
| 5149 | ? rtp_data_channel_->SendData(params, payload, result) |
| 5150 | : network_thread()->Invoke<bool>( |
| 5151 | RTC_FROM_HERE, |
| 5152 | Bind(&cricket::SctpTransportInternal::SendData, |
| 5153 | sctp_transport_.get(), params, payload, result)); |
| 5154 | } |
| 5155 | |
| 5156 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 5157 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 5158 | // Don't log an error here, because DataChannels are expected to call |
| 5159 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 5160 | // whether or not the underlying transport is ready. |
| 5161 | return false; |
| 5162 | } |
| 5163 | if (rtp_data_channel_) { |
| 5164 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 5165 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 5166 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 5167 | &DataChannel::OnDataReceived); |
| 5168 | } else { |
| 5169 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 5170 | &DataChannel::OnChannelReady); |
| 5171 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 5172 | &DataChannel::OnDataReceived); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5173 | SignalSctpClosingProcedureStartedRemotely.connect( |
| 5174 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5175 | SignalSctpClosingProcedureComplete.connect( |
| 5176 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5177 | } |
| 5178 | return true; |
| 5179 | } |
| 5180 | |
| 5181 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 5182 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5183 | RTC_LOG(LS_ERROR) |
| 5184 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 5185 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5186 | return; |
| 5187 | } |
| 5188 | if (rtp_data_channel_) { |
| 5189 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 5190 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 5191 | } else { |
| 5192 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 5193 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5194 | SignalSctpClosingProcedureStartedRemotely.disconnect(webrtc_data_channel); |
| 5195 | SignalSctpClosingProcedureComplete.disconnect(webrtc_data_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | void PeerConnection::AddSctpDataStream(int sid) { |
| 5200 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5201 | RTC_LOG(LS_ERROR) |
| 5202 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5203 | return; |
| 5204 | } |
| 5205 | network_thread()->Invoke<void>( |
| 5206 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 5207 | sctp_transport_.get(), sid)); |
| 5208 | } |
| 5209 | |
| 5210 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 5211 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5212 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5213 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5214 | return; |
| 5215 | } |
| 5216 | network_thread()->Invoke<void>( |
| 5217 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 5218 | sctp_transport_.get(), sid)); |
| 5219 | } |
| 5220 | |
| 5221 | bool PeerConnection::ReadyToSendData() const { |
| 5222 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 5223 | sctp_ready_to_send_data_; |
| 5224 | } |
| 5225 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5226 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5227 | if (sctp_mid_ && transport_controller_) { |
| 5228 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 5229 | if (dtls_transport) { |
| 5230 | return dtls_transport->transport_name(); |
| 5231 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5232 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5233 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5234 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5235 | } |
| 5236 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5237 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 5238 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5239 | network_thread()->Invoke<void>( |
| 5240 | RTC_FROM_HERE, |
| 5241 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 5242 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5243 | return candidate_states_list; |
| 5244 | } |
| 5245 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5246 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 5247 | const { |
| 5248 | std::map<std::string, std::string> transport_names_by_mid; |
| 5249 | for (auto transceiver : transceivers_) { |
| 5250 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5251 | if (channel) { |
| 5252 | transport_names_by_mid[channel->content_name()] = |
| 5253 | channel->transport_name(); |
| 5254 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5255 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5256 | if (rtp_data_channel_) { |
| 5257 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5258 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5259 | } |
| 5260 | if (sctp_transport_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5261 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5262 | RTC_DCHECK(transport_name); |
| 5263 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5264 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5265 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5266 | } |
| 5267 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5268 | std::map<std::string, cricket::TransportStats> |
| 5269 | PeerConnection::GetTransportStatsByNames( |
| 5270 | const std::set<std::string>& transport_names) { |
| 5271 | if (!network_thread()->IsCurrent()) { |
| 5272 | return network_thread() |
| 5273 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5274 | RTC_FROM_HERE, |
| 5275 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5276 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5277 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5278 | for (const std::string& transport_name : transport_names) { |
| 5279 | cricket::TransportStats transport_stats; |
| 5280 | bool success = |
| 5281 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5282 | if (success) { |
| 5283 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5284 | } else { |
| 5285 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5286 | << transport_name; |
| 5287 | } |
| 5288 | } |
| 5289 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | bool PeerConnection::GetLocalCertificate( |
| 5293 | const std::string& transport_name, |
| 5294 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5295 | if (!certificate) { |
| 5296 | return false; |
| 5297 | } |
| 5298 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 5299 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5300 | } |
| 5301 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5302 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5303 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5304 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 5308 | return data_channel_type_; |
| 5309 | } |
| 5310 | |
| 5311 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5312 | return pending_ice_restarts_.find(content_name) != |
| 5313 | pending_ice_restarts_.end(); |
| 5314 | } |
| 5315 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5316 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5317 | return transport_controller_->NeedsIceRestart(content_name); |
| 5318 | } |
| 5319 | |
| 5320 | void PeerConnection::OnCertificateReady( |
| 5321 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5322 | transport_controller_->SetLocalCertificate(certificate); |
| 5323 | } |
| 5324 | |
| 5325 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5326 | SetSessionError(SessionError::kTransport, |
| 5327 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | void PeerConnection::OnTransportControllerConnectionState( |
| 5331 | cricket::IceConnectionState state) { |
| 5332 | switch (state) { |
| 5333 | case cricket::kIceConnectionConnecting: |
| 5334 | // If the current state is Connected or Completed, then there were |
| 5335 | // writable channels but now there are not, so the next state must |
| 5336 | // be Disconnected. |
| 5337 | // kIceConnectionConnecting is currently used as the default, |
| 5338 | // un-connected state by the TransportController, so its only use is |
| 5339 | // detecting disconnections. |
| 5340 | if (ice_connection_state_ == |
| 5341 | PeerConnectionInterface::kIceConnectionConnected || |
| 5342 | ice_connection_state_ == |
| 5343 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5344 | SetIceConnectionState( |
| 5345 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5346 | } |
| 5347 | break; |
| 5348 | case cricket::kIceConnectionFailed: |
| 5349 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5350 | break; |
| 5351 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5352 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5353 | "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5354 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 5355 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5356 | break; |
| 5357 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5358 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5359 | "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5360 | if (ice_connection_state_ != |
| 5361 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5362 | // If jumping directly from "checking" to "connected", |
| 5363 | // signal "connected" first. |
| 5364 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5365 | } |
| 5366 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 5367 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5368 | ReportTransportStats(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5369 | break; |
| 5370 | default: |
| 5371 | RTC_NOTREACHED(); |
| 5372 | } |
| 5373 | } |
| 5374 | |
| 5375 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5376 | const std::string& transport_name, |
| 5377 | const cricket::Candidates& candidates) { |
| 5378 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5379 | int sdp_mline_index; |
| 5380 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5381 | RTC_LOG(LS_ERROR) |
| 5382 | << "OnTransportControllerCandidatesGathered: content name " |
| 5383 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5384 | return; |
| 5385 | } |
| 5386 | |
| 5387 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5388 | citer != candidates.end(); ++citer) { |
| 5389 | // Use transport_name as the candidate media id. |
| 5390 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5391 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5392 | if (local_description()) { |
| 5393 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5394 | } |
| 5395 | OnIceCandidate(std::move(candidate)); |
| 5396 | } |
| 5397 | } |
| 5398 | |
| 5399 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5400 | const std::vector<cricket::Candidate>& candidates) { |
| 5401 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5402 | // Sanity check. |
| 5403 | for (const cricket::Candidate& candidate : candidates) { |
| 5404 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5405 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5406 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5407 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5408 | return; |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | if (local_description()) { |
| 5413 | mutable_local_description()->RemoveCandidates(candidates); |
| 5414 | } |
| 5415 | OnIceCandidatesRemoved(candidates); |
| 5416 | } |
| 5417 | |
| 5418 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5419 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5420 | RTC_HISTOGRAM_ENUMERATION( |
| 5421 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 5422 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5423 | } |
| 5424 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5425 | void PeerConnection::EnableSending() { |
| 5426 | for (auto transceiver : transceivers_) { |
| 5427 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5428 | if (channel && !channel->enabled()) { |
| 5429 | channel->Enable(true); |
| 5430 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5431 | } |
| 5432 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5433 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5434 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5435 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | // Returns the media index for a local ice candidate given the content name. |
| 5439 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5440 | const std::string& content_name, |
| 5441 | int* sdp_mline_index) { |
| 5442 | if (!local_description() || !sdp_mline_index) { |
| 5443 | return false; |
| 5444 | } |
| 5445 | |
| 5446 | bool content_found = false; |
| 5447 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5448 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5449 | if (contents[index].name == content_name) { |
| 5450 | *sdp_mline_index = static_cast<int>(index); |
| 5451 | content_found = true; |
| 5452 | break; |
| 5453 | } |
| 5454 | } |
| 5455 | return content_found; |
| 5456 | } |
| 5457 | |
| 5458 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5459 | const SessionDescriptionInterface* remote_desc) { |
| 5460 | if (!remote_desc) { |
| 5461 | return true; |
| 5462 | } |
| 5463 | bool ret = true; |
| 5464 | |
| 5465 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5466 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5467 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5468 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5469 | bool valid = false; |
| 5470 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5471 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5472 | RTC_LOG(LS_INFO) |
| 5473 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5474 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5475 | } |
| 5476 | continue; |
| 5477 | } |
| 5478 | ret = UseCandidate(candidate); |
| 5479 | if (!ret) { |
| 5480 | break; |
| 5481 | } |
| 5482 | } |
| 5483 | } |
| 5484 | return ret; |
| 5485 | } |
| 5486 | |
| 5487 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5488 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5489 | size_t remote_content_size = |
| 5490 | remote_description()->description()->contents().size(); |
| 5491 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5492 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5493 | return false; |
| 5494 | } |
| 5495 | |
| 5496 | cricket::ContentInfo content = |
| 5497 | remote_description()->description()->contents()[mediacontent_index]; |
| 5498 | std::vector<cricket::Candidate> candidates; |
| 5499 | candidates.push_back(candidate->candidate()); |
| 5500 | // Invoking BaseSession method to handle remote candidates. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5501 | RTCError error = |
| 5502 | transport_controller_->AddRemoteCandidates(content.name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 5503 | if (error.ok()) { |
| 5504 | // Candidates successfully submitted for checking. |
| 5505 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5506 | ice_connection_state_ == |
| 5507 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5508 | // If state is New, then the session has just gotten its first remote ICE |
| 5509 | // candidates, so go to Checking. |
| 5510 | // If state is Disconnected, the session is re-using old candidates or |
| 5511 | // receiving additional ones, so go to Checking. |
| 5512 | // If state is Connected, stay Connected. |
| 5513 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5514 | // newly added transport, then the state actually _should_ move to |
| 5515 | // checking. Add a way to distinguish that case. |
| 5516 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5517 | } |
| 5518 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 5519 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5520 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5521 | } |
| 5522 | return true; |
| 5523 | } |
| 5524 | |
| 5525 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5526 | // Destroy video channel first since it may have a pointer to the |
| 5527 | // voice channel. |
| 5528 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5529 | if (!video_info || video_info->rejected) { |
| 5530 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5531 | } |
| 5532 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5533 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5534 | if (!audio_info || audio_info->rejected) { |
| 5535 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5539 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5540 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5541 | } |
| 5542 | } |
| 5543 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5544 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5545 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5546 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5547 | if (configuration_.bundle_policy == |
| 5548 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5549 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5550 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5551 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5552 | "max-bundle configured but session description " |
| 5553 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5554 | } |
| 5555 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5556 | return std::move(bundle_group); |
| 5557 | } |
| 5558 | |
| 5559 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5560 | // Creating the media channels. Transports should already have been created |
| 5561 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5562 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5563 | if (voice && !voice->rejected && |
| 5564 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5565 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5566 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5567 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5568 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5569 | } |
| 5570 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5571 | } |
| 5572 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5573 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5574 | if (video && !video->rejected && |
| 5575 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5576 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5577 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5578 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5579 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5580 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5581 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5582 | } |
| 5583 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5584 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5585 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 5586 | !rtp_data_channel_ && !sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5587 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5588 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5589 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5590 | } |
| 5591 | } |
| 5592 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5593 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5594 | } |
| 5595 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5596 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5597 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5598 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 5599 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 5600 | MediaTransportInterface* media_transport = GetMediaTransport(mid); |
| 5601 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5602 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 5603 | call_.get(), configuration_.media_config, rtp_transport, media_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 5604 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
| 5605 | audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5606 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5607 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5608 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5609 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5610 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5611 | voice_channel->SignalSentPacket.connect(this, |
| 5612 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5613 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5614 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5615 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5616 | } |
| 5617 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5618 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5619 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5620 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 5621 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 5622 | |
| 5623 | // TODO(sukhanov): Propagate media_transport to video channel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5624 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5625 | call_.get(), configuration_.media_config, rtp_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 5626 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
| 5627 | video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5628 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5629 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5630 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5631 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5632 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5633 | video_channel->SignalSentPacket.connect(this, |
| 5634 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5635 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5636 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5637 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5638 | } |
| 5639 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5640 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5641 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 5642 | if (sctp) { |
| 5643 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5644 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5645 | << "Trying to create SCTP transport, but didn't compile with " |
| 5646 | "SCTP support (HAVE_SCTP)"; |
| 5647 | return false; |
| 5648 | } |
| 5649 | if (!network_thread()->Invoke<bool>( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5650 | RTC_FROM_HERE, |
| 5651 | rtc::Bind(&PeerConnection::CreateSctpTransport_n, this, mid))) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5652 | return false; |
| 5653 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5654 | for (const auto& channel : sctp_data_channels_) { |
| 5655 | channel->OnTransportChannelCreated(); |
| 5656 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5657 | } else { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 5658 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5659 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5660 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 5661 | SrtpRequired(), GetCryptoOptions()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5662 | if (!rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5663 | return false; |
| 5664 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5665 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 5666 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 5667 | rtp_data_channel_->SignalSentPacket.connect( |
| 5668 | this, &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5669 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5670 | } |
| 5671 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5672 | return true; |
| 5673 | } |
| 5674 | |
| 5675 | Call::Stats PeerConnection::GetCallStats() { |
| 5676 | if (!worker_thread()->IsCurrent()) { |
| 5677 | return worker_thread()->Invoke<Call::Stats>( |
| 5678 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 5679 | } |
| 5680 | if (call_) { |
| 5681 | return call_->GetStats(); |
| 5682 | } else { |
| 5683 | return Call::Stats(); |
| 5684 | } |
| 5685 | } |
| 5686 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5687 | bool PeerConnection::CreateSctpTransport_n(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5688 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5689 | RTC_DCHECK(sctp_factory_); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 5690 | cricket::DtlsTransportInternal* dtls_transport = |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5691 | transport_controller_->GetDtlsTransport(mid); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 5692 | RTC_DCHECK(dtls_transport); |
| 5693 | sctp_transport_ = sctp_factory_->CreateSctpTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5694 | RTC_DCHECK(sctp_transport_); |
| 5695 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 5696 | sctp_transport_->SignalReadyToSendData.connect( |
| 5697 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 5698 | sctp_transport_->SignalDataReceived.connect( |
| 5699 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5700 | // TODO(deadbeef): All we do here is AsyncInvoke to fire the signal on |
| 5701 | // another thread. Would be nice if there was a helper class similar to |
| 5702 | // sigslot::repeater that did this for us, eliminating a bunch of boilerplate |
| 5703 | // code. |
| 5704 | sctp_transport_->SignalClosingProcedureStartedRemotely.connect( |
| 5705 | this, &PeerConnection::OnSctpClosingProcedureStartedRemotely_n); |
| 5706 | sctp_transport_->SignalClosingProcedureComplete.connect( |
| 5707 | this, &PeerConnection::OnSctpClosingProcedureComplete_n); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5708 | sctp_mid_ = mid; |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 5709 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5710 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | void PeerConnection::DestroySctpTransport_n() { |
| 5714 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5715 | sctp_transport_.reset(nullptr); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5716 | sctp_mid_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5717 | sctp_invoker_.reset(nullptr); |
| 5718 | sctp_ready_to_send_data_ = false; |
| 5719 | } |
| 5720 | |
| 5721 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 5722 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5723 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5724 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5725 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5726 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5727 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5728 | // pending tasks will be cleared. |
| 5729 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 5730 | OnSctpTransportReadyToSendData_s(true); |
| 5731 | }); |
| 5732 | } |
| 5733 | |
| 5734 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 5735 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5736 | sctp_ready_to_send_data_ = ready; |
| 5737 | SignalSctpReadyToSendData(ready); |
| 5738 | } |
| 5739 | |
| 5740 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 5741 | const cricket::ReceiveDataParams& params, |
| 5742 | const rtc::CopyOnWriteBuffer& payload) { |
| 5743 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5744 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5745 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5746 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5747 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5748 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5749 | // pending tasks will be cleared. |
| 5750 | sctp_invoker_->AsyncInvoke<void>( |
| 5751 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 5752 | OnSctpTransportDataReceived_s(params, payload); |
| 5753 | }); |
| 5754 | } |
| 5755 | |
| 5756 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 5757 | const cricket::ReceiveDataParams& params, |
| 5758 | const rtc::CopyOnWriteBuffer& payload) { |
| 5759 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5760 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 5761 | // Received OPEN message; parse and signal that a new data channel should |
| 5762 | // be created. |
| 5763 | std::string label; |
| 5764 | InternalDataChannelInit config; |
| 5765 | config.id = params.ssrc; |
| 5766 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5767 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 5768 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5769 | return; |
| 5770 | } |
| 5771 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5772 | OnDataChannelOpenMessage(label, config); |
| 5773 | } else { |
| 5774 | // Otherwise just forward the signal. |
| 5775 | SignalSctpDataReceived(params, payload); |
| 5776 | } |
| 5777 | } |
| 5778 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5779 | void PeerConnection::OnSctpClosingProcedureStartedRemotely_n(int sid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5780 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5781 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5782 | sctp_invoker_->AsyncInvoke<void>( |
| 5783 | RTC_FROM_HERE, signaling_thread(), |
| 5784 | rtc::Bind(&sigslot::signal1<int>::operator(), |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5785 | &SignalSctpClosingProcedureStartedRemotely, sid)); |
| 5786 | } |
| 5787 | |
| 5788 | void PeerConnection::OnSctpClosingProcedureComplete_n(int sid) { |
| 5789 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5790 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5791 | sctp_invoker_->AsyncInvoke<void>( |
| 5792 | RTC_FROM_HERE, signaling_thread(), |
| 5793 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 5794 | &SignalSctpClosingProcedureComplete, sid)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5795 | } |
| 5796 | |
| 5797 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 5798 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 5799 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 5800 | if (!bundle_enabled) |
| 5801 | return true; |
| 5802 | |
| 5803 | const cricket::ContentGroup* bundle_group = |
| 5804 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 5805 | RTC_DCHECK(bundle_group != NULL); |
| 5806 | |
| 5807 | const cricket::ContentInfos& contents = desc->contents(); |
| 5808 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 5809 | citer != contents.end(); ++citer) { |
| 5810 | const cricket::ContentInfo* content = (&*citer); |
| 5811 | RTC_DCHECK(content != NULL); |
| 5812 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 5813 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5814 | if (!HasRtcpMuxEnabled(content)) |
| 5815 | return false; |
| 5816 | } |
| 5817 | } |
| 5818 | // RTCP-MUX is enabled in all the contents. |
| 5819 | return true; |
| 5820 | } |
| 5821 | |
| 5822 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5823 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5824 | } |
| 5825 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5826 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5827 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5828 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5829 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5830 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5834 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5835 | } |
| 5836 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5837 | SdpType type = sdesc->GetType(); |
| 5838 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 5839 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5840 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 5841 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5842 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5843 | } |
| 5844 | |
| 5845 | // Verify crypto settings. |
| 5846 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5847 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 5848 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5849 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5850 | if (!crypto_error.ok()) { |
| 5851 | return crypto_error; |
| 5852 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5853 | } |
| 5854 | |
| 5855 | // Verify ice-ufrag and ice-pwd. |
| 5856 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5857 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5858 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5859 | } |
| 5860 | |
| 5861 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5862 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5863 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 5867 | // m-lines that do not rtcp-mux enabled. |
| 5868 | |
| 5869 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5870 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5871 | // With an answer we want to compare the new answer session description with |
| 5872 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5873 | const cricket::SessionDescription* offer_desc = |
| 5874 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 5875 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5876 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 5877 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 5878 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5879 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5880 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5881 | } |
| 5882 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5883 | // The re-offers should respect the order of m= sections in current |
| 5884 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5885 | // With a re-offer, either the current local or current remote descriptions |
| 5886 | // could be the most up to date, so we would like to check against both of |
| 5887 | // them if they exist. It could be the case that one of them has a 0 port |
| 5888 | // for a media section, but the other does not. This is important to check |
| 5889 | // against in the case that we are recycling an m= section. |
| 5890 | const cricket::SessionDescription* current_desc = nullptr; |
| 5891 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 5892 | if (local_description()) { |
| 5893 | current_desc = local_description()->description(); |
| 5894 | if (remote_description()) { |
| 5895 | secondary_current_desc = remote_description()->description(); |
| 5896 | } |
| 5897 | } else if (remote_description()) { |
| 5898 | current_desc = remote_description()->description(); |
| 5899 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5900 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5901 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 5902 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5903 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5904 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5905 | } |
| 5906 | } |
| 5907 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 5908 | if (IsUnifiedPlan()) { |
| 5909 | // Ensure that each audio and video media section has at most one |
| 5910 | // "StreamParams". This will return an error if receiving a session |
| 5911 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 5912 | // same type. With Unified Plan, there can only be at most one track per |
| 5913 | // media section. |
| 5914 | for (const ContentInfo& content : sdesc->description()->contents()) { |
| 5915 | const MediaContentDescription& desc = *content.description; |
| 5916 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 5917 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 5918 | desc.streams().size() > 1u) { |
| 5919 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5920 | "Media section has more than one track specified " |
| 5921 | "with a=ssrc lines which is not supported with " |
| 5922 | "Unified Plan."); |
| 5923 | } |
| 5924 | } |
| 5925 | } |
| 5926 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5927 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5928 | } |
| 5929 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5930 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5931 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5932 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5933 | return (state == PeerConnectionInterface::kStable) || |
| 5934 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5935 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5936 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5937 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 5938 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 5939 | } |
| 5940 | } |
| 5941 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5942 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5943 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5944 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5945 | return (state == PeerConnectionInterface::kStable) || |
| 5946 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5947 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5948 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5949 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 5950 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5951 | } |
| 5952 | } |
| 5953 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5954 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 5955 | switch (error) { |
| 5956 | case SessionError::kNone: |
| 5957 | return "ERROR_NONE"; |
| 5958 | case SessionError::kContent: |
| 5959 | return "ERROR_CONTENT"; |
| 5960 | case SessionError::kTransport: |
| 5961 | return "ERROR_TRANSPORT"; |
| 5962 | } |
| 5963 | RTC_NOTREACHED(); |
| 5964 | return ""; |
| 5965 | } |
| 5966 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5967 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 5968 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5969 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 5970 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 5971 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5972 | } |
| 5973 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 5974 | void PeerConnection::ReportSdpFormatReceived( |
| 5975 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 5976 | int num_audio_mlines = 0; |
| 5977 | int num_video_mlines = 0; |
| 5978 | int num_audio_tracks = 0; |
| 5979 | int num_video_tracks = 0; |
| 5980 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 5981 | cricket::MediaType media_type = content.media_description()->type(); |
| 5982 | int num_tracks = std::max( |
| 5983 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 5984 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 5985 | num_audio_mlines += 1; |
| 5986 | num_audio_tracks += num_tracks; |
| 5987 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5988 | num_video_mlines += 1; |
| 5989 | num_video_tracks += num_tracks; |
| 5990 | } |
| 5991 | } |
| 5992 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 5993 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 5994 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 5995 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 5996 | format = kSdpFormatReceivedComplexPlanB; |
| 5997 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 5998 | format = kSdpFormatReceivedSimple; |
| 5999 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6000 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 6001 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6002 | } |
| 6003 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6004 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 6005 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6006 | usage_event_accumulator_ |= static_cast<int>(event); |
| 6007 | } |
| 6008 | |
| 6009 | void PeerConnection::ReportUsagePattern() const { |
| 6010 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6011 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 6012 | usage_event_accumulator_, |
| 6013 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6014 | const int bad_bits = |
| 6015 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED) | |
| 6016 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 6017 | const int good_bits = |
| 6018 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED) | |
| 6019 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 6020 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 6021 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 6022 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6023 | // If called after close(), we can't report, because observer may have |
| 6024 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 6025 | if (observer_) { |
| 6026 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 6027 | } else { |
| 6028 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 6029 | << usage_event_accumulator_ |
| 6030 | << " observed after observer shutdown"; |
| 6031 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6032 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6033 | } |
| 6034 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6035 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 6036 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6037 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6038 | switch (answer.description()->msid_signaling()) { |
| 6039 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6040 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6041 | break; |
| 6042 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6043 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6044 | break; |
| 6045 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6046 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6047 | break; |
| 6048 | case cricket::kMsidSignalingMediaSection | |
| 6049 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6050 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6051 | break; |
| 6052 | default: |
| 6053 | RTC_NOTREACHED(); |
| 6054 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6055 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 6056 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6057 | } |
| 6058 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6059 | // We need to check the local/remote description for the Transport instead of |
| 6060 | // the session, because a new Transport added during renegotiation may have |
| 6061 | // them unset while the session has them set from the previous negotiation. |
| 6062 | // Not doing so may trigger the auto generation of transport description and |
| 6063 | // mess up DTLS identity information, ICE credential, etc. |
| 6064 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 6065 | const IceCandidateInterface* candidate, |
| 6066 | const SessionDescriptionInterface* remote_desc, |
| 6067 | bool* valid) { |
| 6068 | *valid = true; |
| 6069 | |
| 6070 | const SessionDescriptionInterface* current_remote_desc = |
| 6071 | remote_desc ? remote_desc : remote_description(); |
| 6072 | |
| 6073 | if (!current_remote_desc) { |
| 6074 | return false; |
| 6075 | } |
| 6076 | |
| 6077 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 6078 | size_t remote_content_size = |
| 6079 | current_remote_desc->description()->contents().size(); |
| 6080 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6081 | RTC_LOG(LS_ERROR) |
| 6082 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 6083 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6084 | |
| 6085 | *valid = false; |
| 6086 | return false; |
| 6087 | } |
| 6088 | |
| 6089 | cricket::ContentInfo content = |
| 6090 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 6091 | |
| 6092 | const std::string transport_name = GetTransportName(content.name); |
| 6093 | if (transport_name.empty()) { |
| 6094 | return false; |
| 6095 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6096 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6097 | } |
| 6098 | |
| 6099 | bool PeerConnection::SrtpRequired() const { |
| 6100 | return dtls_enabled_ || |
| 6101 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 6102 | } |
| 6103 | |
| 6104 | void PeerConnection::OnTransportControllerGatheringState( |
| 6105 | cricket::IceGatheringState state) { |
| 6106 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 6107 | if (state == cricket::kIceGatheringGathering) { |
| 6108 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 6109 | } else if (state == cricket::kIceGatheringComplete) { |
| 6110 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 6111 | } |
| 6112 | } |
| 6113 | |
| 6114 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6115 | std::map<std::string, std::set<cricket::MediaType>> |
| 6116 | media_types_by_transport_name; |
| 6117 | for (auto transceiver : transceivers_) { |
| 6118 | if (transceiver->internal()->channel()) { |
| 6119 | const std::string& transport_name = |
| 6120 | transceiver->internal()->channel()->transport_name(); |
| 6121 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 6122 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6123 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6124 | } |
| 6125 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6126 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 6127 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6128 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6129 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6130 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6131 | if (transport_name) { |
| 6132 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6133 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6134 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6135 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6136 | for (const auto& entry : media_types_by_transport_name) { |
| 6137 | const std::string& transport_name = entry.first; |
| 6138 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6139 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6140 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6141 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6142 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6143 | } |
| 6144 | } |
| 6145 | } |
| 6146 | // Walk through the ConnectionInfos to gather best connection usage |
| 6147 | // for IPv4 and IPv6. |
| 6148 | void PeerConnection::ReportBestConnectionState( |
| 6149 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6150 | for (const cricket::TransportChannelStats& channel_stats : |
| 6151 | stats.channel_stats) { |
| 6152 | for (const cricket::ConnectionInfo& connection_info : |
| 6153 | channel_stats.connection_infos) { |
| 6154 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6155 | continue; |
| 6156 | } |
| 6157 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6158 | const cricket::Candidate& local = connection_info.local_candidate; |
| 6159 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6160 | |
| 6161 | // Increment the counter for IceCandidatePairType. |
| 6162 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 6163 | (local.type() == RELAY_PORT_TYPE && |
| 6164 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6165 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 6166 | GetIceCandidatePairCounter(local, remote), |
| 6167 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6168 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6169 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 6170 | GetIceCandidatePairCounter(local, remote), |
| 6171 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6172 | } else { |
| 6173 | RTC_CHECK(0); |
| 6174 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6175 | |
| 6176 | // Increment the counter for IP type. |
| 6177 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6178 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6179 | kBestConnections_IPv4, |
| 6180 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6181 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6182 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6183 | kBestConnections_IPv6, |
| 6184 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6185 | } else { |
| 6186 | RTC_CHECK(0); |
| 6187 | } |
| 6188 | |
| 6189 | return; |
| 6190 | } |
| 6191 | } |
| 6192 | } |
| 6193 | |
| 6194 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6195 | const cricket::TransportStats& stats, |
| 6196 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6197 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 6198 | return; |
| 6199 | } |
| 6200 | |
| 6201 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 6202 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 6203 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 6204 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6205 | return; |
| 6206 | } |
| 6207 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6208 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6209 | for (cricket::MediaType media_type : media_types) { |
| 6210 | switch (media_type) { |
| 6211 | case cricket::MEDIA_TYPE_AUDIO: |
| 6212 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6213 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 6214 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6215 | break; |
| 6216 | case cricket::MEDIA_TYPE_VIDEO: |
| 6217 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6218 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 6219 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6220 | break; |
| 6221 | case cricket::MEDIA_TYPE_DATA: |
| 6222 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6223 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 6224 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6225 | break; |
| 6226 | default: |
| 6227 | RTC_NOTREACHED(); |
| 6228 | continue; |
| 6229 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6230 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6231 | } |
| 6232 | |
| 6233 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6234 | for (cricket::MediaType media_type : media_types) { |
| 6235 | switch (media_type) { |
| 6236 | case cricket::MEDIA_TYPE_AUDIO: |
| 6237 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6238 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 6239 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6240 | break; |
| 6241 | case cricket::MEDIA_TYPE_VIDEO: |
| 6242 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6243 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 6244 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6245 | break; |
| 6246 | case cricket::MEDIA_TYPE_DATA: |
| 6247 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6248 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 6249 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6250 | break; |
| 6251 | default: |
| 6252 | RTC_NOTREACHED(); |
| 6253 | continue; |
| 6254 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6255 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6256 | } |
| 6257 | } |
| 6258 | |
| 6259 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 6260 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 6261 | RTC_DCHECK(call_); |
| 6262 | call_->OnSentPacket(sent_packet); |
| 6263 | } |
| 6264 | |
| 6265 | const std::string PeerConnection::GetTransportName( |
| 6266 | const std::string& content_name) { |
| 6267 | cricket::BaseChannel* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6268 | if (channel) { |
| 6269 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6270 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6271 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6272 | RTC_DCHECK(sctp_mid_); |
| 6273 | if (content_name == *sctp_mid_) { |
| 6274 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6275 | } |
| 6276 | } |
| 6277 | // Return an empty string if failed to retrieve the transport name. |
| 6278 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6279 | } |
| 6280 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6281 | void PeerConnection::DestroyTransceiverChannel( |
| 6282 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 6283 | transceiver) { |
| 6284 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6285 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6286 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 6287 | if (channel) { |
| 6288 | transceiver->internal()->SetChannel(nullptr); |
| 6289 | DestroyBaseChannel(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6290 | } |
| 6291 | } |
| 6292 | |
| 6293 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6294 | if (rtp_data_channel_) { |
| 6295 | OnDataChannelDestroyed(); |
| 6296 | DestroyBaseChannel(rtp_data_channel_); |
| 6297 | rtp_data_channel_ = nullptr; |
| 6298 | } |
| 6299 | |
| 6300 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 6301 | // grab a reference to this PeerConnection. If this is called from the |
| 6302 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 6303 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 6304 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 6305 | |
| 6306 | if (sctp_transport_) { |
| 6307 | OnDataChannelDestroyed(); |
| 6308 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 6309 | [this] { DestroySctpTransport_n(); }); |
| 6310 | } |
| 6311 | } |
| 6312 | |
| 6313 | void PeerConnection::DestroyBaseChannel(cricket::BaseChannel* channel) { |
| 6314 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6315 | switch (channel->media_type()) { |
| 6316 | case cricket::MEDIA_TYPE_AUDIO: |
| 6317 | channel_manager()->DestroyVoiceChannel( |
| 6318 | static_cast<cricket::VoiceChannel*>(channel)); |
| 6319 | break; |
| 6320 | case cricket::MEDIA_TYPE_VIDEO: |
| 6321 | channel_manager()->DestroyVideoChannel( |
| 6322 | static_cast<cricket::VideoChannel*>(channel)); |
| 6323 | break; |
| 6324 | case cricket::MEDIA_TYPE_DATA: |
| 6325 | channel_manager()->DestroyRtpDataChannel( |
| 6326 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 6327 | break; |
| 6328 | default: |
| 6329 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 6330 | break; |
| 6331 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6332 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6333 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6334 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6335 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6336 | RtpTransportInternal* rtp_transport, |
| 6337 | cricket::DtlsTransportInternal* dtls_transport) { |
| 6338 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6339 | auto base_channel = GetChannel(mid); |
| 6340 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6341 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6342 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6343 | if (sctp_transport_ && mid == sctp_mid_) { |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6344 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6345 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6346 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6347 | } |
| 6348 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6349 | PeerConnectionObserver* PeerConnection::Observer() const { |
| 6350 | // In earlier production code, the pointer was not cleared on close, |
| 6351 | // which might have led to undefined behavior if the observer was not |
| 6352 | // deallocated, or strange crashes if it was. |
| 6353 | // We use CHECK in order to catch such behavior if it exists. |
| 6354 | // TODO(hta): Remove or replace with DCHECK if nothing is found. |
| 6355 | RTC_CHECK(observer_); |
| 6356 | return observer_; |
| 6357 | } |
| 6358 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame^] | 6359 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 6360 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 6361 | // after it has been removed. |
| 6362 | return configuration_.crypto_options.has_value() |
| 6363 | ? *configuration_.crypto_options |
| 6364 | : factory_->options().crypto_options; |
| 6365 | } |
| 6366 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 6367 | void PeerConnection::ClearStatsCache() { |
| 6368 | if (stats_collector_) { |
| 6369 | stats_collector_->ClearCachedStatsReport(); |
| 6370 | } |
| 6371 | } |
| 6372 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6373 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 6374 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 6375 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6376 | } |
| 6377 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6378 | } // namespace webrtc |