henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/peerconnection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 13 | #include <algorithm> |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 14 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 15 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 16 | #include <utility> |
| 17 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/jsepicecandidate.h" |
| 20 | #include "api/jsepsessiondescription.h" |
| 21 | #include "api/mediaconstraintsinterface.h" |
| 22 | #include "api/mediastreamproxy.h" |
| 23 | #include "api/mediastreamtrackproxy.h" |
| 24 | #include "call/call.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 25 | #include "logging/rtc_event_log/icelogger.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 26 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 28 | #include "media/sctp/sctptransport.h" |
| 29 | #include "pc/audiotrack.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 30 | #include "pc/channel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "pc/channelmanager.h" |
| 32 | #include "pc/dtmfsender.h" |
| 33 | #include "pc/mediastream.h" |
| 34 | #include "pc/mediastreamobserver.h" |
| 35 | #include "pc/remoteaudiosource.h" |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 36 | #include "pc/rtpmediautils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "pc/rtpreceiver.h" |
| 38 | #include "pc/rtpsender.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 39 | #include "pc/sctputils.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 40 | #include "pc/sdputils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "pc/streamcollection.h" |
| 42 | #include "pc/videocapturertracksource.h" |
| 43 | #include "pc/videotrack.h" |
| 44 | #include "rtc_base/bind.h" |
| 45 | #include "rtc_base/checks.h" |
| 46 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 47 | #include "rtc_base/numerics/safe_conversions.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 48 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 49 | #include "rtc_base/stringencode.h" |
| 50 | #include "rtc_base/stringutils.h" |
| 51 | #include "rtc_base/trace_event.h" |
| 52 | #include "system_wrappers/include/clock.h" |
| 53 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 55 | using cricket::ContentInfo; |
| 56 | using cricket::ContentInfos; |
| 57 | using cricket::MediaContentDescription; |
| 58 | using cricket::SessionDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 59 | using cricket::MediaProtocolType; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 60 | using cricket::TransportInfo; |
| 61 | |
| 62 | using cricket::LOCAL_PORT_TYPE; |
| 63 | using cricket::STUN_PORT_TYPE; |
| 64 | using cricket::RELAY_PORT_TYPE; |
| 65 | using cricket::PRFLX_PORT_TYPE; |
| 66 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 67 | namespace webrtc { |
| 68 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 69 | // Error messages |
| 70 | const char kBundleWithoutRtcpMux[] = |
| 71 | "rtcp-mux must be enabled when BUNDLE " |
| 72 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 73 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 74 | const char kInvalidSdp[] = "Invalid session description."; |
| 75 | const char kMlineMismatchInAnswer[] = |
| 76 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 77 | "answer."; |
| 78 | const char kMlineMismatchInSubsequentOffer[] = |
| 79 | "The order of m-lines in subsequent offer doesn't match order from " |
| 80 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 81 | const char kSdpWithoutDtlsFingerprint[] = |
| 82 | "Called with SDP without DTLS fingerprint."; |
| 83 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 84 | const char kSdpWithoutIceUfragPwd[] = |
| 85 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 86 | const char kSessionError[] = "Session error code: "; |
| 87 | const char kSessionErrorDesc[] = "Session error description: "; |
| 88 | const char kDtlsSrtpSetupFailureRtp[] = |
| 89 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 90 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 91 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
| 92 | const char kEnableBundleFailed[] = "Failed to enable BUNDLE."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 94 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 96 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 97 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 98 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 99 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 100 | // The length of RTCP CNAMEs. |
| 101 | static const int kRtcpCnameLength = 16; |
| 102 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 104 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 106 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | MSG_GETSTATS, |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 108 | MSG_FREE_DATACHANNELS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 111 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | explicit SetSessionDescriptionMsg( |
| 113 | webrtc::SetSessionDescriptionObserver* observer) |
| 114 | : observer(observer) { |
| 115 | } |
| 116 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 117 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | std::string error; |
| 119 | }; |
| 120 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 121 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 122 | explicit CreateSessionDescriptionMsg( |
| 123 | webrtc::CreateSessionDescriptionObserver* observer) |
| 124 | : observer(observer) {} |
| 125 | |
| 126 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 127 | std::string error; |
| 128 | }; |
| 129 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 130 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 131 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 132 | webrtc::MediaStreamTrackInterface* track) |
| 133 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 135 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 136 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 139 | // Check if we can send |new_stream| on a PeerConnection. |
| 140 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 141 | webrtc::MediaStreamInterface* new_stream) { |
| 142 | if (!new_stream || !current_streams) { |
| 143 | return false; |
| 144 | } |
| 145 | if (current_streams->find(new_stream->label()) != nullptr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 146 | RTC_LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() |
| 147 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 153 | // If the direction is "recvonly" or "inactive", treat the description |
| 154 | // as containing no streams. |
| 155 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 156 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 157 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 158 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 159 | ? desc->streams() |
| 160 | : std::vector<cricket::StreamParams>(); |
| 161 | } |
| 162 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 163 | bool IsValidOfferToReceiveMedia(int value) { |
| 164 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 165 | return (value >= Options::kUndefined) && |
| 166 | (value <= Options::kMaxOfferToReceiveMedia); |
| 167 | } |
| 168 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 169 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 170 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 171 | const std::vector<rtc::scoped_refptr< |
| 172 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 173 | cricket::MediaDescriptionOptions* audio_media_description_options, |
| 174 | cricket::MediaDescriptionOptions* video_media_description_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 175 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 176 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 177 | if (audio_media_description_options) { |
| 178 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 179 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 180 | } |
| 181 | } else { |
| 182 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 183 | if (video_media_description_options) { |
| 184 | video_media_description_options->AddVideoSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 185 | sender->id(), sender->internal()->stream_ids(), 1); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 188 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 189 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 190 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 191 | // Add options to |session_options| from |rtp_data_channels|. |
| 192 | void AddRtpDataChannelOptions( |
| 193 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 194 | rtp_data_channels, |
| 195 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 196 | if (!data_media_description_options) { |
| 197 | return; |
| 198 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 199 | // Check for data channels. |
| 200 | for (const auto& kv : rtp_data_channels) { |
| 201 | const DataChannel* channel = kv.second; |
| 202 | if (channel->state() == DataChannel::kConnecting || |
| 203 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 204 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 205 | // the data channel's label. |
| 206 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 207 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 212 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 213 | PeerConnectionInterface::IceTransportsType type) { |
| 214 | switch (type) { |
| 215 | case PeerConnectionInterface::kNone: |
| 216 | return cricket::CF_NONE; |
| 217 | case PeerConnectionInterface::kRelay: |
| 218 | return cricket::CF_RELAY; |
| 219 | case PeerConnectionInterface::kNoHost: |
| 220 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 221 | case PeerConnectionInterface::kAll: |
| 222 | return cricket::CF_ALL; |
| 223 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 224 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 225 | } |
| 226 | return cricket::CF_NONE; |
| 227 | } |
| 228 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 229 | // Helper to set an error and return from a method. |
| 230 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 231 | if (error) { |
| 232 | error->set_type(type); |
| 233 | } |
| 234 | return type == webrtc::RTCErrorType::NONE; |
| 235 | } |
| 236 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 237 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
| 238 | if (error_out) { |
| 239 | *error_out = std::move(error); |
| 240 | } |
| 241 | return error.ok(); |
| 242 | } |
| 243 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 244 | std::string GetSignalingStateString( |
| 245 | PeerConnectionInterface::SignalingState state) { |
| 246 | switch (state) { |
| 247 | case PeerConnectionInterface::kStable: |
| 248 | return "kStable"; |
| 249 | case PeerConnectionInterface::kHaveLocalOffer: |
| 250 | return "kHaveLocalOffer"; |
| 251 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 252 | return "kHavePrAnswer"; |
| 253 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 254 | return "kHaveRemoteOffer"; |
| 255 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 256 | return "kHaveRemotePrAnswer"; |
| 257 | case PeerConnectionInterface::kClosed: |
| 258 | return "kClosed"; |
| 259 | } |
| 260 | RTC_NOTREACHED(); |
| 261 | return ""; |
| 262 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 263 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 264 | IceCandidatePairType GetIceCandidatePairCounter( |
| 265 | const cricket::Candidate& local, |
| 266 | const cricket::Candidate& remote) { |
| 267 | const auto& l = local.type(); |
| 268 | const auto& r = remote.type(); |
| 269 | const auto& host = LOCAL_PORT_TYPE; |
| 270 | const auto& srflx = STUN_PORT_TYPE; |
| 271 | const auto& relay = RELAY_PORT_TYPE; |
| 272 | const auto& prflx = PRFLX_PORT_TYPE; |
| 273 | if (l == host && r == host) { |
| 274 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 275 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 276 | if (local_private) { |
| 277 | if (remote_private) { |
| 278 | return kIceCandidatePairHostPrivateHostPrivate; |
| 279 | } else { |
| 280 | return kIceCandidatePairHostPrivateHostPublic; |
| 281 | } |
| 282 | } else { |
| 283 | if (remote_private) { |
| 284 | return kIceCandidatePairHostPublicHostPrivate; |
| 285 | } else { |
| 286 | return kIceCandidatePairHostPublicHostPublic; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | if (l == host && r == srflx) |
| 291 | return kIceCandidatePairHostSrflx; |
| 292 | if (l == host && r == relay) |
| 293 | return kIceCandidatePairHostRelay; |
| 294 | if (l == host && r == prflx) |
| 295 | return kIceCandidatePairHostPrflx; |
| 296 | if (l == srflx && r == host) |
| 297 | return kIceCandidatePairSrflxHost; |
| 298 | if (l == srflx && r == srflx) |
| 299 | return kIceCandidatePairSrflxSrflx; |
| 300 | if (l == srflx && r == relay) |
| 301 | return kIceCandidatePairSrflxRelay; |
| 302 | if (l == srflx && r == prflx) |
| 303 | return kIceCandidatePairSrflxPrflx; |
| 304 | if (l == relay && r == host) |
| 305 | return kIceCandidatePairRelayHost; |
| 306 | if (l == relay && r == srflx) |
| 307 | return kIceCandidatePairRelaySrflx; |
| 308 | if (l == relay && r == relay) |
| 309 | return kIceCandidatePairRelayRelay; |
| 310 | if (l == relay && r == prflx) |
| 311 | return kIceCandidatePairRelayPrflx; |
| 312 | if (l == prflx && r == host) |
| 313 | return kIceCandidatePairPrflxHost; |
| 314 | if (l == prflx && r == srflx) |
| 315 | return kIceCandidatePairPrflxSrflx; |
| 316 | if (l == prflx && r == relay) |
| 317 | return kIceCandidatePairPrflxRelay; |
| 318 | return kIceCandidatePairMax; |
| 319 | } |
| 320 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 321 | // Logic to decide if an m= section can be recycled. This means that the new |
| 322 | // m= section is not rejected, but the old local or remote m= section is |
| 323 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 324 | // of the old remote and old local descriptions in no particular order. |
| 325 | // We need to check both the old local and remote because either |
| 326 | // could be the most current from the latest negotation. |
| 327 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 328 | const ContentInfo& content, |
| 329 | const ContentInfo* old_content_one, |
| 330 | const ContentInfo* old_content_two) { |
| 331 | return type == SdpType::kOffer && !content.rejected && |
| 332 | ((old_content_one && old_content_one->rejected) || |
| 333 | (old_content_two && old_content_two->rejected)); |
| 334 | } |
| 335 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 336 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 337 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 338 | // less than |current_desc|. In the case of checking an answer's |
| 339 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 340 | // local or remote. In the case of checking an offer's |new_desc| we |
| 341 | // check against the local and remote descriptions stored from the last |
| 342 | // negotiation, because either of these could be the most up to date for |
| 343 | // possible rejected m sections. These are the |current_desc| and |
| 344 | // |secondary_current_desc|. |
| 345 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 346 | const SessionDescription* secondary_current_desc, |
| 347 | const SessionDescription& new_desc, |
| 348 | const SdpType type) { |
| 349 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 353 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 354 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 355 | if (secondary_current_desc && |
| 356 | i < secondary_current_desc->contents().size()) { |
| 357 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 358 | } |
| 359 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 360 | ¤t_desc.contents()[i], |
| 361 | secondary_content_info)) { |
| 362 | // For new offer descriptions, if the media section can be recycled, it's |
| 363 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 364 | continue; |
| 365 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 366 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 367 | return false; |
| 368 | } |
| 369 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 370 | new_desc.contents()[i].media_description(); |
| 371 | const MediaContentDescription* current_desc_mdesc = |
| 372 | current_desc.contents()[i].media_description(); |
| 373 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 380 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 381 | const SessionDescription& desc2) { |
| 382 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 385 | void NoteKeyProtocolAndMedia( |
| 386 | KeyExchangeProtocolType protocol_type, |
| 387 | cricket::MediaType media_type, |
| 388 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
| 389 | if (!uma_observer) |
| 390 | return; |
| 391 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocol, |
| 392 | protocol_type, |
| 393 | webrtc::kEnumCounterKeyProtocolMax); |
| 394 | static const std::map<std::pair<KeyExchangeProtocolType, cricket::MediaType>, |
| 395 | KeyExchangeProtocolMedia> |
| 396 | proto_media_counter_map = { |
| 397 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO}, |
| 398 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 399 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO}, |
| 400 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 401 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA}, |
| 402 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 403 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO}, |
| 404 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 405 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO}, |
| 406 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 407 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA}, |
| 408 | kEnumCounterKeyProtocolMediaTypeSdesData}}; |
| 409 | |
| 410 | auto it = proto_media_counter_map.find({protocol_type, media_type}); |
| 411 | if (it != proto_media_counter_map.end()) { |
| 412 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocolMediaType, |
| 413 | it->second, |
| 414 | kEnumCounterKeyProtocolMediaTypeMax); |
| 415 | } |
| 416 | } |
| 417 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 418 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 419 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 420 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 421 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 422 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 423 | // by Channel's |srtp_required| check. |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 424 | RTCError VerifyCrypto(const SessionDescription* desc, |
| 425 | bool dtls_enabled, |
| 426 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 427 | const cricket::ContentGroup* bundle = |
| 428 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 429 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 430 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 431 | continue; |
| 432 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 433 | // Note what media is used with each crypto protocol, for all sections. |
| 434 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 435 | : webrtc::kEnumCounterKeyProtocolSdes, |
| 436 | content_info.media_description()->type(), |
| 437 | uma_observer); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 438 | const std::string& mid = content_info.name; |
| 439 | if (bundle && bundle->HasContentName(mid) && |
| 440 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 441 | // This isn't the first media section in the BUNDLE group, so it's not |
| 442 | // required to have crypto attributes, since only the crypto attributes |
| 443 | // from the first section actually get used. |
| 444 | continue; |
| 445 | } |
| 446 | |
| 447 | // If the content isn't rejected or bundled into another m= section, crypto |
| 448 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 449 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 450 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 451 | if (!media || !tinfo) { |
| 452 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 453 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 454 | } |
| 455 | if (dtls_enabled) { |
| 456 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 457 | RTC_LOG(LS_WARNING) |
| 458 | << "Session description must have DTLS fingerprint if " |
| 459 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 460 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 461 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 462 | } |
| 463 | } else { |
| 464 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 465 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 466 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 467 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 471 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 475 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 476 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 477 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 478 | const cricket::ContentGroup* bundle = |
| 479 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 480 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 481 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 482 | continue; |
| 483 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 484 | const std::string& mid = content_info.name; |
| 485 | if (bundle && bundle->HasContentName(mid) && |
| 486 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 487 | // This isn't the first media section in the BUNDLE group, so it's not |
| 488 | // required to have ufrag/password, since only the ufrag/password from |
| 489 | // the first section actually get used. |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | // If the content isn't rejected or bundled into another m= section, |
| 494 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 495 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 496 | if (!tinfo) { |
| 497 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 498 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 499 | return false; |
| 500 | } |
| 501 | if (tinfo->description.ice_ufrag.empty() || |
| 502 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 503 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | bool GetTrackIdBySsrc(const SessionDescription* session_description, |
| 511 | uint32_t ssrc, |
| 512 | std::string* track_id) { |
| 513 | RTC_DCHECK(track_id != NULL); |
| 514 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 515 | const cricket::AudioContentDescription* audio_desc = |
| 516 | cricket::GetFirstAudioContentDescription(session_description); |
| 517 | if (audio_desc) { |
| 518 | const auto* found = cricket::GetStreamBySsrc(audio_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 519 | if (found) { |
| 520 | *track_id = found->id; |
| 521 | return true; |
| 522 | } |
| 523 | } |
| 524 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 525 | const cricket::VideoContentDescription* video_desc = |
| 526 | cricket::GetFirstVideoContentDescription(session_description); |
| 527 | if (video_desc) { |
| 528 | const auto* found = cricket::GetStreamBySsrc(video_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 529 | if (found) { |
| 530 | *track_id = found->id; |
| 531 | return true; |
| 532 | } |
| 533 | } |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | // Get the SCTP port out of a SessionDescription. |
| 538 | // Return -1 if not found. |
| 539 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 540 | const cricket::DataContentDescription* data_desc = |
| 541 | GetFirstDataContentDescription(session_description); |
| 542 | RTC_DCHECK(data_desc); |
| 543 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 544 | return -1; |
| 545 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 546 | std::string value; |
| 547 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 548 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 549 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 550 | if (!codec.Matches(match_pattern)) { |
| 551 | continue; |
| 552 | } |
| 553 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 554 | return rtc::FromString<int>(value); |
| 555 | } |
| 556 | } |
| 557 | return -1; |
| 558 | } |
| 559 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 560 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 561 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 562 | const SessionDescriptionInterface* new_desc, |
| 563 | const std::string& content_name) { |
| 564 | if (!old_desc) { |
| 565 | return false; |
| 566 | } |
| 567 | const SessionDescription* new_sd = new_desc->description(); |
| 568 | const SessionDescription* old_sd = old_desc->description(); |
| 569 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 570 | if (!cinfo || cinfo->rejected) { |
| 571 | return false; |
| 572 | } |
| 573 | // If the content isn't rejected, check if ufrag and password has changed. |
| 574 | const cricket::TransportDescription* new_transport_desc = |
| 575 | new_sd->GetTransportDescriptionByName(content_name); |
| 576 | const cricket::TransportDescription* old_transport_desc = |
| 577 | old_sd->GetTransportDescriptionByName(content_name); |
| 578 | if (!new_transport_desc || !old_transport_desc) { |
| 579 | // No transport description exists. This is not an ICE restart. |
| 580 | return false; |
| 581 | } |
| 582 | if (cricket::IceCredentialsChanged( |
| 583 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 584 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 585 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 586 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 587 | return true; |
| 588 | } |
| 589 | return false; |
| 590 | } |
| 591 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 592 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 593 | // from an RTCError. |
| 594 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 595 | SdpType type, |
| 596 | const RTCError& error) { |
| 597 | std::ostringstream oss; |
| 598 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 599 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
| 600 | return oss.str(); |
| 601 | } |
| 602 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 603 | } // namespace |
| 604 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 605 | // Upon completion, posts a task to execute the callback of the |
| 606 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 607 | // point, the state of the peer connection might no longer reflect the effects |
| 608 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 609 | // modified during the post. |
| 610 | // TODO(hbos): Remove this class once we remove the version of |
| 611 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 612 | // SetSessionDescriptionObserver as an argument. |
| 613 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 614 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 615 | public: |
| 616 | SetRemoteDescriptionObserverAdapter( |
| 617 | rtc::scoped_refptr<PeerConnection> pc, |
| 618 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 619 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 620 | |
| 621 | // SetRemoteDescriptionObserverInterface implementation. |
| 622 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 623 | if (error.ok()) |
| 624 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 625 | else |
| 626 | pc_->PostSetSessionDescriptionFailure(wrapper_, error.message()); |
| 627 | } |
| 628 | |
| 629 | private: |
| 630 | rtc::scoped_refptr<PeerConnection> pc_; |
| 631 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 632 | }; |
| 633 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 634 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 635 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 636 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 637 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 638 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 639 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 640 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 641 | BundlePolicy bundle_policy; |
| 642 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 643 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 644 | int ice_candidate_pool_size; |
| 645 | bool disable_ipv6; |
| 646 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 647 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 648 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 649 | bool enable_rtp_data_channel; |
| 650 | rtc::Optional<int> screencast_min_bitrate; |
| 651 | rtc::Optional<bool> combined_audio_video_bwe; |
| 652 | rtc::Optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 653 | TcpCandidatePolicy tcp_candidate_policy; |
| 654 | CandidateNetworkPolicy candidate_network_policy; |
| 655 | int audio_jitter_buffer_max_packets; |
| 656 | bool audio_jitter_buffer_fast_accelerate; |
| 657 | int ice_connection_receiving_timeout; |
| 658 | int ice_backup_candidate_pair_ping_interval; |
| 659 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 660 | bool prioritize_most_likely_ice_candidate_pairs; |
| 661 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 662 | bool prune_turn_ports; |
| 663 | bool presume_writable_when_fully_relayed; |
| 664 | bool enable_ice_renomination; |
| 665 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame^] | 666 | rtc::Optional<int> ice_check_interval_strong_connectivity; |
| 667 | rtc::Optional<int> ice_check_interval_weak_connectivity; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 668 | rtc::Optional<int> ice_check_min_interval; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 669 | rtc::Optional<int> stun_candidate_keepalive_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 670 | rtc::Optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 671 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 672 | SdpSemantics sdp_semantics; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 673 | rtc::Optional<rtc::AdapterType> network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 674 | }; |
| 675 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 676 | "Did you add something to RTCConfiguration and forget to " |
| 677 | "update operator==?"); |
| 678 | return type == o.type && servers == o.servers && |
| 679 | bundle_policy == o.bundle_policy && |
| 680 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 681 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 682 | candidate_network_policy == o.candidate_network_policy && |
| 683 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 684 | audio_jitter_buffer_fast_accelerate == |
| 685 | o.audio_jitter_buffer_fast_accelerate && |
| 686 | ice_connection_receiving_timeout == |
| 687 | o.ice_connection_receiving_timeout && |
| 688 | ice_backup_candidate_pair_ping_interval == |
| 689 | o.ice_backup_candidate_pair_ping_interval && |
| 690 | continual_gathering_policy == o.continual_gathering_policy && |
| 691 | certificates == o.certificates && |
| 692 | prioritize_most_likely_ice_candidate_pairs == |
| 693 | o.prioritize_most_likely_ice_candidate_pairs && |
| 694 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 695 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 696 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 697 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 698 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 699 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 700 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 701 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 702 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 703 | prune_turn_ports == o.prune_turn_ports && |
| 704 | presume_writable_when_fully_relayed == |
| 705 | o.presume_writable_when_fully_relayed && |
| 706 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 707 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame^] | 708 | ice_check_interval_strong_connectivity == |
| 709 | o.ice_check_interval_strong_connectivity && |
| 710 | ice_check_interval_weak_connectivity == |
| 711 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 712 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 713 | stun_candidate_keepalive_interval == |
| 714 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 715 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 716 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 717 | sdp_semantics == o.sdp_semantics && |
| 718 | network_preference == o.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 722 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 723 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 724 | } |
| 725 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 726 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 727 | std::string GenerateRtcpCname() { |
| 728 | std::string cname; |
| 729 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 730 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 731 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 732 | } |
| 733 | return cname; |
| 734 | } |
| 735 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 736 | bool ValidateOfferAnswerOptions( |
| 737 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 738 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 739 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 740 | } |
| 741 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 742 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 743 | // m= sections (in other words, nothing that involves a map/array). |
| 744 | void ExtractSharedMediaSessionOptions( |
| 745 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 746 | cricket::MediaSessionOptions* session_options) { |
| 747 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 748 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 749 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 750 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 751 | bool ConvertConstraintsToOfferAnswerOptions( |
| 752 | const MediaConstraintsInterface* constraints, |
| 753 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 754 | if (!constraints) { |
| 755 | return true; |
| 756 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 757 | |
| 758 | bool value = false; |
| 759 | size_t mandatory_constraints_satisfied = 0; |
| 760 | |
| 761 | if (FindConstraint(constraints, |
| 762 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 763 | &mandatory_constraints_satisfied)) { |
| 764 | offer_answer_options->offer_to_receive_audio = |
| 765 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 766 | kOfferToReceiveMediaTrue |
| 767 | : 0; |
| 768 | } |
| 769 | |
| 770 | if (FindConstraint(constraints, |
| 771 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 772 | &mandatory_constraints_satisfied)) { |
| 773 | offer_answer_options->offer_to_receive_video = |
| 774 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 775 | kOfferToReceiveMediaTrue |
| 776 | : 0; |
| 777 | } |
| 778 | if (FindConstraint(constraints, |
| 779 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 780 | &mandatory_constraints_satisfied)) { |
| 781 | offer_answer_options->voice_activity_detection = value; |
| 782 | } |
| 783 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 784 | &mandatory_constraints_satisfied)) { |
| 785 | offer_answer_options->use_rtp_mux = value; |
| 786 | } |
| 787 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 788 | &value, &mandatory_constraints_satisfied)) { |
| 789 | offer_answer_options->ice_restart = value; |
| 790 | } |
| 791 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 792 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 793 | } |
| 794 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 795 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 796 | std::unique_ptr<RtcEventLog> event_log, |
| 797 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 798 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 799 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 800 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 801 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 802 | remote_streams_(StreamCollection::Create()), |
| 803 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 804 | |
| 805 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 806 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 807 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 808 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 809 | // Need to stop transceivers before destroying the stats collector because |
| 810 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 811 | // stopping. |
| 812 | for (auto transceiver : transceivers_) { |
| 813 | transceiver->Stop(); |
| 814 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 815 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 816 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 817 | if (stats_collector_) { |
| 818 | stats_collector_->WaitForPendingRequest(); |
| 819 | stats_collector_ = nullptr; |
| 820 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 821 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 822 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 823 | // the last stats request can still read from the channels. |
| 824 | DestroyAllChannels(); |
| 825 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 826 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 827 | |
| 828 | webrtc_session_desc_factory_.reset(); |
| 829 | sctp_invoker_.reset(); |
| 830 | sctp_factory_.reset(); |
| 831 | transport_controller_.reset(); |
| 832 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 833 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 834 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 835 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 836 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 837 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 838 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 839 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 840 | event_log_.reset(); |
| 841 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 844 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 845 | // Destroy video channels first since they may have a pointer to a voice |
| 846 | // channel. |
| 847 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 848 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 849 | DestroyTransceiverChannel(transceiver); |
| 850 | } |
| 851 | } |
| 852 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 853 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 854 | DestroyTransceiverChannel(transceiver); |
| 855 | } |
| 856 | } |
| 857 | DestroyDataChannel(); |
| 858 | } |
| 859 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 860 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 861 | const PeerConnectionInterface::RTCConfiguration& configuration, |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 862 | std::unique_ptr<cricket::PortAllocator> allocator, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 863 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 864 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 865 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 866 | |
| 867 | RTCError config_error = ValidateConfiguration(configuration); |
| 868 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 869 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 870 | return false; |
| 871 | } |
| 872 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 873 | if (!allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 874 | RTC_LOG(LS_ERROR) |
| 875 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 876 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 877 | return false; |
| 878 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 879 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 880 | if (!observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 881 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 882 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 883 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 884 | return false; |
| 885 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 886 | observer_ = observer; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 887 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 888 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 889 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 890 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 891 | if (!network_thread()->Invoke<bool>( |
| 892 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, |
| 893 | this, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 894 | return false; |
| 895 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 896 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 897 | // RFC 3264: The numeric value of the session id and version in the |
| 898 | // o line MUST be representable with a "64 bit signed integer". |
| 899 | // Due to this constraint session id |session_id_| is max limited to |
| 900 | // LLONG_MAX. |
| 901 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
| 902 | transport_controller_.reset(factory_->CreateTransportController( |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 903 | port_allocator_.get(), configuration.redetermine_role_on_ice_restart, |
| 904 | event_log_.get())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 905 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 906 | transport_controller_->SignalConnectionState.connect( |
| 907 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 908 | transport_controller_->SignalGatheringState.connect( |
| 909 | this, &PeerConnection::OnTransportControllerGatheringState); |
| 910 | transport_controller_->SignalCandidatesGathered.connect( |
| 911 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
| 912 | transport_controller_->SignalCandidatesRemoved.connect( |
| 913 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 914 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 915 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 916 | |
| 917 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 918 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 919 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 920 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 921 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 922 | configuration_ = configuration; |
| 923 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 924 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 925 | |
| 926 | transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version); |
| 927 | |
| 928 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 929 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 930 | if (!configuration.certificates.empty()) { |
| 931 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 932 | // just picking the first one. The decision should be made based on the DTLS |
| 933 | // handshake. The DTLS negotiations need to know about all certificates. |
| 934 | certificate = configuration.certificates[0]; |
| 935 | } |
| 936 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 937 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 938 | |
| 939 | if (options.disable_encryption) { |
| 940 | dtls_enabled_ = false; |
| 941 | } else { |
| 942 | // Enable DTLS by default if we have an identity store or a certificate. |
| 943 | dtls_enabled_ = (cert_generator || certificate); |
| 944 | // |configuration| can override the default |dtls_enabled_| value. |
| 945 | if (configuration.enable_dtls_srtp) { |
| 946 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 951 | // It takes precendence over the disable_sctp_data_channels |
| 952 | // PeerConnectionFactoryInterface::Options. |
| 953 | if (configuration.enable_rtp_data_channel) { |
| 954 | data_channel_type_ = cricket::DCT_RTP; |
| 955 | } else { |
| 956 | // DTLS has to be enabled to use SCTP. |
| 957 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 958 | data_channel_type_ = cricket::DCT_SCTP; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | video_options_.screencast_min_bitrate_kbps = |
| 963 | configuration.screencast_min_bitrate; |
| 964 | audio_options_.combined_audio_video_bwe = |
| 965 | configuration.combined_audio_video_bwe; |
| 966 | |
| 967 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 968 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 969 | |
| 970 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 971 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 972 | |
| 973 | // Whether the certificate generator/certificate is null or not determines |
| 974 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 975 | // the right instructions by clearing the variables if needed. |
| 976 | if (!dtls_enabled_) { |
| 977 | cert_generator.reset(); |
| 978 | certificate = nullptr; |
| 979 | } else if (certificate) { |
| 980 | // Favor generated certificate over the certificate generator. |
| 981 | cert_generator.reset(); |
| 982 | } |
| 983 | |
| 984 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 985 | signaling_thread(), channel_manager(), this, session_id(), |
| 986 | std::move(cert_generator), certificate)); |
| 987 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 988 | this, &PeerConnection::OnCertificateReady); |
| 989 | |
| 990 | if (options.disable_encryption) { |
| 991 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 992 | } |
| 993 | |
| 994 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 995 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 996 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 997 | // Add default audio/video transceivers for Plan B SDP. |
| 998 | if (!IsUnifiedPlan()) { |
| 999 | transceivers_.push_back( |
| 1000 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1001 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1002 | transceivers_.push_back( |
| 1003 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1004 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1005 | } |
| 1006 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1007 | return true; |
| 1008 | } |
| 1009 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1010 | RTCError PeerConnection::ValidateConfiguration( |
| 1011 | const RTCConfiguration& config) const { |
| 1012 | if (config.ice_regather_interval_range && |
| 1013 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1014 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1015 | "ice_regather_interval_range specified but continual " |
| 1016 | "gathering policy is GATHER_ONCE"); |
| 1017 | } |
| 1018 | return RTCError::OK(); |
| 1019 | } |
| 1020 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1021 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1022 | PeerConnection::local_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1023 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1024 | "Plan SdpSemantics. Please use GetSenders " |
| 1025 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1026 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1029 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1030 | PeerConnection::remote_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1031 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1032 | "Plan SdpSemantics. Please use GetReceivers " |
| 1033 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1034 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1037 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1038 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1039 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1040 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1041 | if (IsClosed()) { |
| 1042 | return false; |
| 1043 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1044 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1045 | return false; |
| 1046 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1047 | |
| 1048 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1049 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1050 | observer->SignalAudioTrackAdded.connect(this, |
| 1051 | &PeerConnection::OnAudioTrackAdded); |
| 1052 | observer->SignalAudioTrackRemoved.connect( |
| 1053 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1054 | observer->SignalVideoTrackAdded.connect(this, |
| 1055 | &PeerConnection::OnVideoTrackAdded); |
| 1056 | observer->SignalVideoTrackRemoved.connect( |
| 1057 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1058 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1059 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1060 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1061 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1062 | } |
| 1063 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1064 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1067 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1068 | observer_->OnRenegotiationNeeded(); |
| 1069 | return true; |
| 1070 | } |
| 1071 | |
| 1072 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1073 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1074 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1075 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1076 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1077 | if (!IsClosed()) { |
| 1078 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1079 | RemoveAudioTrack(track.get(), local_stream); |
| 1080 | } |
| 1081 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1082 | RemoveVideoTrack(track.get(), local_stream); |
| 1083 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1084 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1085 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1086 | stream_observers_.erase( |
| 1087 | std::remove_if( |
| 1088 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1089 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1090 | return observer->stream()->label().compare(local_stream->label()) == |
| 1091 | 0; |
| 1092 | }), |
| 1093 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1094 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1095 | if (IsClosed()) { |
| 1096 | return; |
| 1097 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1098 | observer_->OnRenegotiationNeeded(); |
| 1099 | } |
| 1100 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1101 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 1102 | MediaStreamTrackInterface* track, |
| 1103 | std::vector<MediaStreamInterface*> streams) { |
| 1104 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1105 | std::vector<std::string> stream_ids; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1106 | for (auto* stream : streams) { |
| 1107 | if (!stream) { |
| 1108 | RTC_LOG(LS_ERROR) << "Stream list has null element."; |
| 1109 | return nullptr; |
| 1110 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1111 | stream_ids.push_back(stream->label()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1112 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1113 | auto sender_or_error = AddTrack(track, stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1114 | if (!sender_or_error.ok()) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1115 | return nullptr; |
| 1116 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1117 | return sender_or_error.MoveValue(); |
| 1118 | } |
| 1119 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1120 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1121 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1122 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1123 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1124 | if (!track) { |
| 1125 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1126 | } |
| 1127 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1128 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1129 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1130 | "Track has invalid kind: " + track->kind()); |
| 1131 | } |
| 1132 | // TODO(bugs.webrtc.org/7932): Support adding a track to multiple streams. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1133 | if (stream_ids.size() > 1u) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1134 | LOG_AND_RETURN_ERROR( |
| 1135 | RTCErrorType::UNSUPPORTED_OPERATION, |
| 1136 | "AddTrack with more than one stream is not currently supported."); |
| 1137 | } |
| 1138 | if (IsClosed()) { |
| 1139 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1140 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1141 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1142 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1143 | LOG_AND_RETURN_ERROR( |
| 1144 | RTCErrorType::INVALID_PARAMETER, |
| 1145 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1146 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1147 | // TODO(bugs.webrtc.org/7933): MediaSession expects the sender to have exactly |
| 1148 | // one stream. AddTrackInternal will return an error if there is more than one |
| 1149 | // stream, but if the caller specifies none then we need to generate a random |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1150 | // stream id. |
| 1151 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1152 | if (stream_ids.empty()) { |
| 1153 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1154 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1155 | RTC_DCHECK_EQ(1, adjusted_stream_ids.size()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1156 | auto sender_or_error = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1157 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, adjusted_stream_ids) |
| 1158 | : AddTrackPlanB(track, adjusted_stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1159 | if (sender_or_error.ok()) { |
| 1160 | observer_->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1161 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1162 | } |
| 1163 | return sender_or_error; |
| 1164 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1165 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1166 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1167 | PeerConnection::AddTrackPlanB( |
| 1168 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1169 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1170 | cricket::MediaType media_type = |
| 1171 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1172 | ? cricket::MEDIA_TYPE_AUDIO |
| 1173 | : cricket::MEDIA_TYPE_VIDEO); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1174 | auto new_sender = CreateSender(media_type, track, stream_ids); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1175 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1176 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1177 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1178 | const RtpSenderInfo* sender_info = |
| 1179 | FindSenderInfo(local_audio_sender_infos_, |
| 1180 | new_sender->internal()->stream_id(), track->id()); |
| 1181 | if (sender_info) { |
| 1182 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1183 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1184 | } else { |
| 1185 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1186 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1187 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1188 | const RtpSenderInfo* sender_info = |
| 1189 | FindSenderInfo(local_video_sender_infos_, |
| 1190 | new_sender->internal()->stream_id(), track->id()); |
| 1191 | if (sender_info) { |
| 1192 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1193 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1194 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1195 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1196 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1197 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1198 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1199 | PeerConnection::AddTrackUnifiedPlan( |
| 1200 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1201 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1202 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1203 | if (transceiver) { |
| 1204 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1205 | transceiver->internal()->set_direction( |
| 1206 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1207 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1208 | transceiver->internal()->set_direction( |
| 1209 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1210 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1211 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1212 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1213 | } else { |
| 1214 | cricket::MediaType media_type = |
| 1215 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1216 | ? cricket::MEDIA_TYPE_AUDIO |
| 1217 | : cricket::MEDIA_TYPE_VIDEO); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1218 | auto sender = CreateSender(media_type, track, stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1219 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1220 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1221 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1222 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1223 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1224 | return transceiver->sender(); |
| 1225 | } |
| 1226 | |
| 1227 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1228 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1229 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1230 | RTC_DCHECK(track); |
| 1231 | for (auto transceiver : transceivers_) { |
| 1232 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1233 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1234 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1235 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1236 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1237 | return transceiver; |
| 1238 | } |
| 1239 | } |
| 1240 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1244 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1245 | return RemoveTrackInternal(sender).ok(); |
| 1246 | } |
| 1247 | |
| 1248 | RTCError PeerConnection::RemoveTrackInternal( |
| 1249 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1250 | if (!sender) { |
| 1251 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1252 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1253 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1254 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1255 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1256 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1257 | if (IsUnifiedPlan()) { |
| 1258 | auto transceiver = FindTransceiverBySender(sender); |
| 1259 | if (!transceiver || !sender->track()) { |
| 1260 | return RTCError::OK(); |
| 1261 | } |
| 1262 | sender->SetTrack(nullptr); |
| 1263 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1264 | transceiver->internal()->set_direction( |
| 1265 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1266 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1267 | transceiver->internal()->set_direction( |
| 1268 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1269 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1270 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1271 | bool removed; |
| 1272 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1273 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1274 | } else { |
| 1275 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1276 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1277 | } |
| 1278 | if (!removed) { |
| 1279 | LOG_AND_RETURN_ERROR( |
| 1280 | RTCErrorType::INVALID_PARAMETER, |
| 1281 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1282 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1283 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1284 | observer_->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1285 | return RTCError::OK(); |
| 1286 | } |
| 1287 | |
| 1288 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1289 | PeerConnection::FindTransceiverBySender( |
| 1290 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1291 | for (auto transceiver : transceivers_) { |
| 1292 | if (transceiver->sender() == sender) { |
| 1293 | return transceiver; |
| 1294 | } |
| 1295 | } |
| 1296 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1299 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1300 | PeerConnection::AddTransceiver( |
| 1301 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1302 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1303 | } |
| 1304 | |
| 1305 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1306 | PeerConnection::AddTransceiver( |
| 1307 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1308 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1309 | RTC_CHECK(IsUnifiedPlan()) |
| 1310 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1311 | if (!track) { |
| 1312 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1313 | } |
| 1314 | cricket::MediaType media_type; |
| 1315 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1316 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1317 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1318 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1319 | } else { |
| 1320 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1321 | "Track kind is not audio or video"); |
| 1322 | } |
| 1323 | return AddTransceiver(media_type, track, init); |
| 1324 | } |
| 1325 | |
| 1326 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1327 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1328 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1329 | } |
| 1330 | |
| 1331 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1332 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1333 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1334 | RTC_CHECK(IsUnifiedPlan()) |
| 1335 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1336 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1337 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1338 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1339 | "media type is not audio or video"); |
| 1340 | } |
| 1341 | return AddTransceiver(media_type, nullptr, init); |
| 1342 | } |
| 1343 | |
| 1344 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1345 | PeerConnection::AddTransceiver( |
| 1346 | cricket::MediaType media_type, |
| 1347 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1348 | const RtpTransceiverInit& init, |
| 1349 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1350 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1351 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1352 | if (track) { |
| 1353 | RTC_DCHECK_EQ(media_type, |
| 1354 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1355 | ? cricket::MEDIA_TYPE_AUDIO |
| 1356 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1357 | } |
| 1358 | |
| 1359 | // TODO(bugs.webrtc.org/7600): Verify init. |
| 1360 | |
Seth Hampson | 513449e | 2018-03-06 09:35:56 -0800 | [diff] [blame] | 1361 | if (init.stream_ids.size() > 1u) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1362 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1363 | "More than one stream is not yet supported."); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Seth Hampson | 513449e | 2018-03-06 09:35:56 -0800 | [diff] [blame] | 1366 | std::vector<std::string> stream_ids = { |
| 1367 | !init.stream_ids.empty() ? init.stream_ids[0] : rtc::CreateRandomUuid()}; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1368 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1369 | auto sender = CreateSender(media_type, track, stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1370 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1371 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1372 | transceiver->internal()->set_direction(init.direction); |
| 1373 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1374 | if (fire_callback) { |
| 1375 | observer_->OnRenegotiationNeeded(); |
| 1376 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1377 | |
| 1378 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1379 | } |
| 1380 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1381 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1382 | PeerConnection::CreateSender( |
| 1383 | cricket::MediaType media_type, |
| 1384 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1385 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1386 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1387 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1388 | RTC_DCHECK(!track || |
| 1389 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1390 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1391 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1392 | new AudioRtpSender(worker_thread(), |
| 1393 | static_cast<AudioTrackInterface*>(track.get()), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1394 | stream_ids, stats_.get())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1395 | } else { |
| 1396 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1397 | RTC_DCHECK(!track || |
| 1398 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1399 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1400 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1401 | new VideoRtpSender(worker_thread(), |
| 1402 | static_cast<VideoTrackInterface*>(track.get()), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1403 | stream_ids)); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1404 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1405 | sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1406 | return sender; |
| 1407 | } |
| 1408 | |
| 1409 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1410 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1411 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1412 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1413 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1414 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1415 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 1416 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1417 | new AudioRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1418 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1419 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1420 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 1421 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1422 | new VideoRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1423 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1424 | return receiver; |
| 1425 | } |
| 1426 | |
| 1427 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1428 | PeerConnection::CreateAndAddTransceiver( |
| 1429 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1430 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1431 | receiver) { |
| 1432 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1433 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1434 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1435 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1436 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1437 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1440 | void PeerConnection::OnNegotiationNeeded() { |
| 1441 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1442 | RTC_DCHECK(!IsClosed()); |
| 1443 | observer_->OnRenegotiationNeeded(); |
| 1444 | } |
| 1445 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1446 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1447 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1448 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1449 | if (IsClosed()) { |
| 1450 | return nullptr; |
| 1451 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1452 | if (!track) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1453 | RTC_LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1454 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1455 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1456 | auto track_sender = FindSenderForTrack(track); |
| 1457 | if (!track_sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1458 | RTC_LOG(LS_ERROR) << "CreateDtmfSender called with a non-added track."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1459 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1462 | return track_sender->GetDtmfSender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1465 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1466 | const std::string& kind, |
| 1467 | const std::string& stream_id) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1468 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1469 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1470 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1471 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1472 | if (IsClosed()) { |
| 1473 | return nullptr; |
| 1474 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1475 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1476 | std::vector<std::string> stream_ids; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1477 | if (!stream_id.empty()) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1478 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1481 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1482 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1483 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1484 | auto* audio_sender = |
| 1485 | new AudioRtpSender(worker_thread(), nullptr, stream_ids, stats_.get()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1486 | audio_sender->SetVoiceMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1487 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1488 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1489 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1490 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1491 | auto* video_sender = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1492 | new VideoRtpSender(worker_thread(), nullptr, stream_ids); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1493 | video_sender->SetVideoMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1494 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1495 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1496 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1497 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1498 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1499 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1500 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1501 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1502 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1505 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1506 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1507 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1508 | for (auto sender : GetSendersInternal()) { |
| 1509 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1510 | } |
| 1511 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1514 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1515 | PeerConnection::GetSendersInternal() const { |
| 1516 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1517 | all_senders; |
| 1518 | for (auto transceiver : transceivers_) { |
| 1519 | auto senders = transceiver->internal()->senders(); |
| 1520 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1521 | } |
| 1522 | return all_senders; |
| 1523 | } |
| 1524 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1525 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1526 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1527 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1528 | for (const auto& receiver : GetReceiversInternal()) { |
| 1529 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1530 | } |
| 1531 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1534 | std::vector< |
| 1535 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1536 | PeerConnection::GetReceiversInternal() const { |
| 1537 | std::vector< |
| 1538 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1539 | all_receivers; |
| 1540 | for (auto transceiver : transceivers_) { |
| 1541 | auto receivers = transceiver->internal()->receivers(); |
| 1542 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1543 | receivers.end()); |
| 1544 | } |
| 1545 | return all_receivers; |
| 1546 | } |
| 1547 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1548 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1549 | PeerConnection::GetTransceivers() const { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1550 | RTC_CHECK(IsUnifiedPlan()) |
| 1551 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1552 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
| 1553 | for (auto transceiver : transceivers_) { |
| 1554 | all_transceivers.push_back(transceiver); |
| 1555 | } |
| 1556 | return all_transceivers; |
| 1557 | } |
| 1558 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1559 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1560 | MediaStreamTrackInterface* track, |
| 1561 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1562 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1563 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1564 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1565 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1566 | return false; |
| 1567 | } |
| 1568 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1569 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1570 | // The StatsCollector is used to tell if a track is valid because it may |
| 1571 | // remember tracks that the PeerConnection previously removed. |
| 1572 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1573 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1574 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1575 | return false; |
| 1576 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1577 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1578 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1579 | return true; |
| 1580 | } |
| 1581 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1582 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
| 1583 | RTC_DCHECK(stats_collector_); |
| 1584 | stats_collector_->GetStatsReport(callback); |
| 1585 | } |
| 1586 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1587 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1588 | return signaling_state_; |
| 1589 | } |
| 1590 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1591 | PeerConnectionInterface::IceConnectionState |
| 1592 | PeerConnection::ice_connection_state() { |
| 1593 | return ice_connection_state_; |
| 1594 | } |
| 1595 | |
| 1596 | PeerConnectionInterface::IceGatheringState |
| 1597 | PeerConnection::ice_gathering_state() { |
| 1598 | return ice_gathering_state_; |
| 1599 | } |
| 1600 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1601 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1602 | PeerConnection::CreateDataChannel( |
| 1603 | const std::string& label, |
| 1604 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1605 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1606 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1607 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1608 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1609 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1610 | if (config) { |
| 1611 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1612 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1613 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1614 | InternalCreateDataChannel(label, internal_config.get())); |
| 1615 | if (!channel.get()) { |
| 1616 | return nullptr; |
| 1617 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1618 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1619 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1620 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1621 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1622 | observer_->OnRenegotiationNeeded(); |
| 1623 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1624 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1625 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1626 | } |
| 1627 | |
| 1628 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1629 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1630 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1631 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1632 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1633 | // Always create an offer even if |ConvertConstraintsToOfferAnswerOptions| |
| 1634 | // returns false for now. Because |ConvertConstraintsToOfferAnswerOptions| |
| 1635 | // compares the mandatory fields parsed with the mandatory fields added in the |
| 1636 | // |constraints| and some downstream applications might create offers with |
| 1637 | // mandatory fields which would not be parsed in the helper method. For |
| 1638 | // example, in Chromium/remoting, |kEnableDtlsSrtp| is added to the |
| 1639 | // |constraints| as a mandatory field but it is not parsed. |
| 1640 | ConvertConstraintsToOfferAnswerOptions(constraints, &offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1641 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1642 | CreateOffer(observer, offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1646 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1647 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1648 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1649 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1650 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1651 | return; |
| 1652 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1653 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1654 | if (IsClosed()) { |
| 1655 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1656 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1657 | PostCreateSessionDescriptionFailure(observer, error); |
| 1658 | return; |
| 1659 | } |
| 1660 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1661 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1662 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1663 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1664 | PostCreateSessionDescriptionFailure(observer, error); |
| 1665 | return; |
| 1666 | } |
| 1667 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1668 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1669 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1670 | if (IsUnifiedPlan()) { |
| 1671 | RTCError error = HandleLegacyOfferOptions(options); |
| 1672 | if (!error.ok()) { |
| 1673 | PostCreateSessionDescriptionFailure(observer, error.message()); |
| 1674 | return; |
| 1675 | } |
| 1676 | } |
| 1677 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1678 | cricket::MediaSessionOptions session_options; |
| 1679 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1680 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1683 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1684 | const RTCOfferAnswerOptions& options) { |
| 1685 | RTC_DCHECK(IsUnifiedPlan()); |
| 1686 | |
| 1687 | if (options.offer_to_receive_audio == 0) { |
| 1688 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1689 | cricket::MEDIA_TYPE_AUDIO); |
| 1690 | } else if (options.offer_to_receive_audio == 1) { |
| 1691 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1692 | } else if (options.offer_to_receive_audio > 1) { |
| 1693 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1694 | "offer_to_receive_audio > 1 is not supported."); |
| 1695 | } |
| 1696 | |
| 1697 | if (options.offer_to_receive_video == 0) { |
| 1698 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1699 | cricket::MEDIA_TYPE_VIDEO); |
| 1700 | } else if (options.offer_to_receive_video == 1) { |
| 1701 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1702 | } else if (options.offer_to_receive_video > 1) { |
| 1703 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1704 | "offer_to_receive_video > 1 is not supported."); |
| 1705 | } |
| 1706 | |
| 1707 | return RTCError::OK(); |
| 1708 | } |
| 1709 | |
| 1710 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1711 | cricket::MediaType media_type) { |
| 1712 | for (auto transceiver : GetReceivingTransceiversOfType(media_type)) { |
| 1713 | transceiver->internal()->set_direction( |
| 1714 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false)); |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1719 | cricket::MediaType media_type) { |
| 1720 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
| 1721 | RtpTransceiverInit init; |
| 1722 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1723 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1728 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1729 | std::vector< |
| 1730 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1731 | receiving_transceivers; |
| 1732 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1733 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1734 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1735 | receiving_transceivers.push_back(transceiver); |
| 1736 | } |
| 1737 | } |
| 1738 | return receiving_transceivers; |
| 1739 | } |
| 1740 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1741 | void PeerConnection::CreateAnswer( |
| 1742 | CreateSessionDescriptionObserver* observer, |
| 1743 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1744 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1745 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1746 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1747 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1748 | return; |
| 1749 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1750 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1751 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1752 | if (!ConvertConstraintsToOfferAnswerOptions(constraints, |
| 1753 | &offer_answer_options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1754 | std::string error = "CreateAnswer called with invalid constraints."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1755 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1756 | PostCreateSessionDescriptionFailure(observer, error); |
| 1757 | return; |
| 1758 | } |
| 1759 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1760 | CreateAnswer(observer, offer_answer_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1764 | const RTCOfferAnswerOptions& options) { |
| 1765 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1766 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1767 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1768 | return; |
| 1769 | } |
| 1770 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1771 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1772 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1773 | std::string error = |
| 1774 | "PeerConnection cannot create an answer in a state other than " |
| 1775 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1776 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1777 | PostCreateSessionDescriptionFailure(observer, error); |
| 1778 | return; |
| 1779 | } |
| 1780 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1781 | // The remote description should be set if we're in the right state. |
| 1782 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1783 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1784 | if (IsUnifiedPlan()) { |
| 1785 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1786 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1787 | "supported with Unified Plan semantics. Use the " |
| 1788 | "RtpTransceiver API instead."; |
| 1789 | } |
| 1790 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1791 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1792 | "supported with Unified Plan semantics. Use the " |
| 1793 | "RtpTransceiver API instead."; |
| 1794 | } |
| 1795 | } |
| 1796 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1797 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1798 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1799 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1800 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | void PeerConnection::SetLocalDescription( |
| 1804 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1805 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1806 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1807 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1808 | // The SetLocalDescription contract is that we take ownership of the session |
| 1809 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 1810 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 1811 | // description as a unique_ptr argument to formalize this agreement. |
| 1812 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 1813 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1814 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1815 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1816 | return; |
| 1817 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1818 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1819 | if (!desc) { |
| 1820 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1821 | return; |
| 1822 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1823 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1824 | // If a session error has occurred the PeerConnection is in a possibly |
| 1825 | // inconsistent state so fail right away. |
| 1826 | if (session_error() != SessionError::kNone) { |
| 1827 | std::string error_message = GetSessionErrorMsg(); |
| 1828 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
| 1829 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
| 1830 | return; |
| 1831 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1832 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1833 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1834 | if (!error.ok()) { |
| 1835 | std::string error_message = GetSetDescriptionErrorMessage( |
| 1836 | cricket::CS_LOCAL, desc->GetType(), error); |
| 1837 | RTC_LOG(LS_ERROR) << error_message; |
| 1838 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
| 1839 | return; |
| 1840 | } |
| 1841 | |
| 1842 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 1843 | // which may destroy it before returning. |
| 1844 | const SdpType type = desc->GetType(); |
| 1845 | |
| 1846 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1847 | // |desc| may be destroyed at this point. |
| 1848 | |
| 1849 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1850 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 1851 | // inconsistent state, so act conservatively here and set the session error |
| 1852 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 1853 | SetSessionError(SessionError::kContent, error.message()); |
| 1854 | std::string error_message = |
| 1855 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 1856 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1857 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1858 | return; |
| 1859 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1860 | RTC_DCHECK(local_description()); |
| 1861 | |
| 1862 | PostSetSessionDescriptionSuccess(observer); |
| 1863 | |
| 1864 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1865 | // size is not allowed, and changing the set of ICE servers will not result |
| 1866 | // in new candidates being gathered. |
| 1867 | port_allocator_->FreezeCandidatePool(); |
| 1868 | |
| 1869 | // MaybeStartGathering needs to be called after posting |
| 1870 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1871 | // before signaling that SetLocalDescription completed. |
| 1872 | transport_controller_->MaybeStartGathering(); |
| 1873 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1874 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1875 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1876 | // MaybeStartGathering... |
| 1877 | network_thread()->Invoke<void>( |
| 1878 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1879 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 1880 | // Make UMA notes about what was agreed to. |
| 1881 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | RTCError PeerConnection::ApplyLocalDescription( |
| 1886 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1887 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1888 | RTC_DCHECK(desc); |
| 1889 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1890 | // Update stats here so that we have the most recent stats for tracks and |
| 1891 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1892 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1893 | |
| 1894 | // Update the initial_offerer flag if this session is the initial_offerer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1895 | SdpType type = desc->GetType(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1896 | if (!initial_offerer_.has_value()) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1897 | initial_offerer_.emplace(type == SdpType::kOffer); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1898 | if (*initial_offerer_) { |
| 1899 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1900 | } else { |
| 1901 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1902 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1903 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1904 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1905 | // Take a reference to the old local description since it's used below to |
| 1906 | // compare against the new local description. When setting the new local |
| 1907 | // description, grab ownership of the replaced session description in case it |
| 1908 | // is the same as |old_local_description|, to keep it alive for the duration |
| 1909 | // of the method. |
| 1910 | const SessionDescriptionInterface* old_local_description = |
| 1911 | local_description(); |
| 1912 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1913 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1914 | replaced_local_description = pending_local_description_ |
| 1915 | ? std::move(pending_local_description_) |
| 1916 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1917 | current_local_description_ = std::move(desc); |
| 1918 | pending_local_description_ = nullptr; |
| 1919 | current_remote_description_ = std::move(pending_remote_description_); |
| 1920 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1921 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1922 | pending_local_description_ = std::move(desc); |
| 1923 | } |
| 1924 | // The session description to apply now must be accessed by |
| 1925 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1926 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1927 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1928 | if (IsUnifiedPlan()) { |
| 1929 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 1930 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 1931 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1932 | if (!error.ok()) { |
| 1933 | return error; |
| 1934 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1935 | for (auto transceiver : transceivers_) { |
| 1936 | const ContentInfo* content = |
| 1937 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 1938 | if (!content) { |
| 1939 | continue; |
| 1940 | } |
| 1941 | const MediaContentDescription* media_desc = content->media_description(); |
| 1942 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 1943 | transceiver->internal()->set_current_direction(media_desc->direction()); |
| 1944 | } |
| 1945 | if (content->rejected && !transceiver->stopped()) { |
| 1946 | transceiver->Stop(); |
| 1947 | } |
| 1948 | } |
| 1949 | } else { |
| 1950 | // Transport and Media channels will be created only when offer is set. |
| 1951 | if (type == SdpType::kOffer) { |
| 1952 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 1953 | // description is applied. Restore back to old description. |
| 1954 | RTCError error = CreateChannels(*local_description()->description()); |
| 1955 | if (!error.ok()) { |
| 1956 | return error; |
| 1957 | } |
| 1958 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1959 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1960 | // Remove unused channels if MediaContentDescription is rejected. |
| 1961 | RemoveUnusedChannels(local_description()->description()); |
| 1962 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1963 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1964 | RTCError error = UpdateSessionState(type, cricket::CS_LOCAL); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1965 | if (!error.ok()) { |
| 1966 | return error; |
| 1967 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1968 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1969 | if (remote_description()) { |
| 1970 | // Now that we have a local description, we can push down remote candidates. |
| 1971 | UseCandidatesInSessionDescription(remote_description()); |
| 1972 | } |
| 1973 | |
| 1974 | pending_ice_restarts_.clear(); |
| 1975 | if (session_error() != SessionError::kNone) { |
| 1976 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 1977 | } |
| 1978 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1979 | // If setting the description decided our SSL role, allocate any necessary |
| 1980 | // SCTP sids. |
| 1981 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1982 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1983 | AllocateSctpSids(role); |
| 1984 | } |
| 1985 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1986 | if (IsUnifiedPlan()) { |
| 1987 | for (auto transceiver : transceivers_) { |
| 1988 | const ContentInfo* content = |
| 1989 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 1990 | if (!content) { |
| 1991 | continue; |
| 1992 | } |
| 1993 | if (content->rejected && !transceiver->stopped()) { |
| 1994 | transceiver->Stop(); |
| 1995 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1996 | const auto& streams = content->media_description()->streams(); |
| 1997 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1998 | transceiver->internal()->sender_internal()->set_stream_ids( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1999 | streams[0].stream_ids()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2000 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2001 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | } else { |
| 2005 | // Plan B semantics. |
| 2006 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2007 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2008 | // local session description. |
| 2009 | const cricket::ContentInfo* audio_content = |
| 2010 | GetFirstAudioContent(local_description()->description()); |
| 2011 | if (audio_content) { |
| 2012 | if (audio_content->rejected) { |
| 2013 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2014 | } else { |
| 2015 | const cricket::AudioContentDescription* audio_desc = |
| 2016 | audio_content->media_description()->as_audio(); |
| 2017 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2018 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2019 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2020 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2021 | const cricket::ContentInfo* video_content = |
| 2022 | GetFirstVideoContent(local_description()->description()); |
| 2023 | if (video_content) { |
| 2024 | if (video_content->rejected) { |
| 2025 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2026 | } else { |
| 2027 | const cricket::VideoContentDescription* video_desc = |
| 2028 | video_content->media_description()->as_video(); |
| 2029 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2030 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2031 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2035 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2036 | if (data_content) { |
| 2037 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2038 | data_content->media_description()->as_data(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2039 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2040 | cricket::kMediaProtocolRtpPrefix)) { |
| 2041 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 2042 | } |
| 2043 | } |
| 2044 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2045 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2049 | SetSessionDescriptionObserver* observer, |
| 2050 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2051 | SetRemoteDescription( |
| 2052 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2053 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2054 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2055 | } |
| 2056 | |
| 2057 | void PeerConnection::SetRemoteDescription( |
| 2058 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2059 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2060 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2061 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2062 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2063 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2064 | return; |
| 2065 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2066 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2067 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2068 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2069 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2070 | return; |
| 2071 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2072 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2073 | // If a session error has occurred the PeerConnection is in a possibly |
| 2074 | // inconsistent state so fail right away. |
| 2075 | if (session_error() != SessionError::kNone) { |
| 2076 | std::string error_message = GetSessionErrorMsg(); |
| 2077 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2078 | observer->OnSetRemoteDescriptionComplete( |
| 2079 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2080 | return; |
| 2081 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2082 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2083 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2084 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2085 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2086 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2087 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2088 | observer->OnSetRemoteDescriptionComplete( |
| 2089 | RTCError(error.type(), std::move(error_message))); |
| 2090 | return; |
| 2091 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2092 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2093 | // Grab the description type before moving ownership to |
| 2094 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2095 | const SdpType type = desc->GetType(); |
| 2096 | |
| 2097 | error = ApplyRemoteDescription(std::move(desc)); |
| 2098 | // |desc| may be destroyed at this point. |
| 2099 | |
| 2100 | if (!error.ok()) { |
| 2101 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2102 | // inconsistent state, so act conservatively here and set the session error |
| 2103 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2104 | SetSessionError(SessionError::kContent, error.message()); |
| 2105 | std::string error_message = |
| 2106 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2107 | RTC_LOG(LS_ERROR) << error_message; |
| 2108 | observer->OnSetRemoteDescriptionComplete( |
| 2109 | RTCError(error.type(), std::move(error_message))); |
| 2110 | return; |
| 2111 | } |
| 2112 | RTC_DCHECK(remote_description()); |
| 2113 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 2114 | if (type == SdpType::kOffer) { |
| 2115 | // Report to UMA the format of the received offer. |
| 2116 | ReportSdpFormatReceived(*remote_description()); |
| 2117 | } |
| 2118 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2119 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2120 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2121 | // MaybeStartGathering... |
| 2122 | network_thread()->Invoke<void>( |
| 2123 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2124 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2125 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2126 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
| 2130 | } |
| 2131 | |
| 2132 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2133 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2134 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2135 | RTC_DCHECK(desc); |
| 2136 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2137 | // Update stats here so that we have the most recent stats for tracks and |
| 2138 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2139 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2140 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2141 | // Take a reference to the old remote description since it's used below to |
| 2142 | // compare against the new remote description. When setting the new remote |
| 2143 | // description, grab ownership of the replaced session description in case it |
| 2144 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2145 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2146 | const SessionDescriptionInterface* old_remote_description = |
| 2147 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2148 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2149 | SdpType type = desc->GetType(); |
| 2150 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2151 | replaced_remote_description = pending_remote_description_ |
| 2152 | ? std::move(pending_remote_description_) |
| 2153 | : std::move(current_remote_description_); |
| 2154 | current_remote_description_ = std::move(desc); |
| 2155 | pending_remote_description_ = nullptr; |
| 2156 | current_local_description_ = std::move(pending_local_description_); |
| 2157 | } else { |
| 2158 | replaced_remote_description = std::move(pending_remote_description_); |
| 2159 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2160 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2161 | // The session description to apply now must be accessed by |
| 2162 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2163 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2164 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2165 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2166 | if (IsUnifiedPlan()) { |
| 2167 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2168 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2169 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2170 | if (!error.ok()) { |
| 2171 | return error; |
| 2172 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2173 | } else { |
| 2174 | if (type == SdpType::kOffer) { |
| 2175 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2176 | // description is applied. Restore back to old description. |
| 2177 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2178 | if (!error.ok()) { |
| 2179 | return error; |
| 2180 | } |
| 2181 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2182 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2183 | // Remove unused channels if MediaContentDescription is rejected. |
| 2184 | RemoveUnusedChannels(remote_description()->description()); |
| 2185 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2186 | |
| 2187 | // NOTE: Candidates allocation will be initiated only when SetLocalDescription |
| 2188 | // is called. |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2189 | RTCError error = UpdateSessionState(type, cricket::CS_REMOTE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2190 | if (!error.ok()) { |
| 2191 | return error; |
| 2192 | } |
| 2193 | |
| 2194 | if (local_description() && |
| 2195 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2196 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2197 | } |
| 2198 | |
| 2199 | if (old_remote_description) { |
| 2200 | for (const cricket::ContentInfo& content : |
| 2201 | old_remote_description->description()->contents()) { |
| 2202 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2203 | // password that indicates the remote peer requests an ICE restart. |
| 2204 | // TODO(deadbeef): When we start storing both the current and pending |
| 2205 | // remote description, this should reset pending_ice_restarts and compare |
| 2206 | // against the current description. |
| 2207 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2208 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2209 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2210 | pending_ice_restarts_.insert(content.name); |
| 2211 | } |
| 2212 | } else { |
| 2213 | // We retain all received candidates only if ICE is not restarted. |
| 2214 | // When ICE is restarted, all previous candidates belong to an old |
| 2215 | // generation and should not be kept. |
| 2216 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2217 | // description should only contain candidates from the last set remote |
| 2218 | // description plus any candidates added since then. We should remove |
| 2219 | // this once we're sure it won't break anything. |
| 2220 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2221 | old_remote_description, content.name, mutable_remote_description()); |
| 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | if (session_error() != SessionError::kNone) { |
| 2227 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2228 | } |
| 2229 | |
| 2230 | // Set the the ICE connection state to connecting since the connection may |
| 2231 | // become writable with peer reflexive candidates before any remote candidate |
| 2232 | // is signaled. |
| 2233 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2234 | // is to have a new signal the indicates a change in checking state from the |
| 2235 | // transport and expose a new checking() member from transport that can be |
| 2236 | // read to determine the current checking state. The existing SignalConnecting |
| 2237 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2238 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2239 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2240 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2241 | } |
| 2242 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2243 | // If setting the description decided our SSL role, allocate any necessary |
| 2244 | // SCTP sids. |
| 2245 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2246 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2247 | AllocateSctpSids(role); |
| 2248 | } |
| 2249 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2250 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2251 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 2252 | receiving_transceivers; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2253 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2254 | for (auto transceiver : transceivers_) { |
| 2255 | const ContentInfo* content = |
| 2256 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2257 | if (!content) { |
| 2258 | continue; |
| 2259 | } |
| 2260 | const MediaContentDescription* media_desc = content->media_description(); |
| 2261 | RtpTransceiverDirection local_direction = |
| 2262 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 2263 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 "Set |
| 2264 | // the RTCSessionDescription: If direction is sendrecv or recvonly, and |
| 2265 | // transceiver's current direction is neither sendrecv nor recvonly, |
| 2266 | // process the addition of a remote track for the media description. |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2267 | // |
| 2268 | // Create a sync label in the case of an unsignalled msid. |
| 2269 | std::string sync_label = rtc::CreateRandomUuid(); |
| 2270 | if (!media_desc->streams().empty()) { |
Steve Anton | 5a26a3a | 2018-02-28 11:38:47 -0800 | [diff] [blame] | 2271 | const cricket::StreamParams& stream_params = media_desc->streams()[0]; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 2272 | if (!stream_params.stream_ids().empty()) { |
| 2273 | sync_label = stream_params.stream_ids()[0]; |
Steve Anton | 5a26a3a | 2018-02-28 11:38:47 -0800 | [diff] [blame] | 2274 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2275 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2276 | if (RtpTransceiverDirectionHasRecv(local_direction) && |
| 2277 | (!transceiver->current_direction() || |
| 2278 | !RtpTransceiverDirectionHasRecv( |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2279 | *transceiver->current_direction()))) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2280 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2281 | remote_streams_->find(sync_label); |
| 2282 | if (!stream) { |
| 2283 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2284 | MediaStream::Create(sync_label)); |
| 2285 | remote_streams_->AddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2286 | added_streams.push_back(stream); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2287 | } |
| 2288 | transceiver->internal()->receiver_internal()->SetStreams({stream}); |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2289 | receiving_transceivers.push_back(transceiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2290 | } |
| 2291 | // If direction is sendonly or inactive, and transceiver's current |
| 2292 | // direction is neither sendonly nor inactive, process the removal of a |
| 2293 | // remote track for the media description. |
| 2294 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2295 | (transceiver->current_direction() && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2296 | RtpTransceiverDirectionHasRecv(*transceiver->current_direction()))) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2297 | transceiver->internal()->receiver_internal()->SetStreams({}); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2298 | } |
| 2299 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2300 | transceiver->internal()->set_current_direction(local_direction); |
| 2301 | } |
| 2302 | if (content->rejected && !transceiver->stopped()) { |
| 2303 | transceiver->Stop(); |
| 2304 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2305 | if (!content->rejected && |
| 2306 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2307 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2308 | uint32_t ssrc = 0; |
| 2309 | if (!media_desc->streams().empty()) { |
| 2310 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2311 | } |
| 2312 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2313 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2314 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2315 | // Once all processing has finished, fire off callbacks. |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2316 | for (auto transceiver : receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2317 | stats_->AddTrack(transceiver->receiver()->track()); |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2318 | observer_->OnTrack(transceiver); |
| 2319 | observer_->OnAddTrack(transceiver->receiver(), |
| 2320 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2321 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2322 | for (auto stream : added_streams) { |
| 2323 | observer_->OnAddStream(stream); |
| 2324 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2325 | } |
| 2326 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2327 | const cricket::ContentInfo* audio_content = |
| 2328 | GetFirstAudioContent(remote_description()->description()); |
| 2329 | const cricket::ContentInfo* video_content = |
| 2330 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2331 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2332 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2333 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2334 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2335 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2336 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2337 | |
| 2338 | // Check if the descriptions include streams, just in case the peer supports |
| 2339 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2340 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2341 | (audio_desc && !audio_desc->streams().empty()) || |
| 2342 | (video_desc && !video_desc->streams().empty())) { |
| 2343 | remote_peer_supports_msid_ = true; |
| 2344 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2345 | |
| 2346 | // We wait to signal new streams until we finish processing the description, |
| 2347 | // since only at that point will new streams have all their tracks. |
| 2348 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2349 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2350 | if (!IsUnifiedPlan()) { |
| 2351 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2352 | // rejected media section, there is some cleanup logic that expects the |
| 2353 | // voice/ video channel to still be set. But in this method the voice/video |
| 2354 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2355 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2356 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2357 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2358 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2359 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2360 | // and MediaStreams. |
| 2361 | if (audio_content) { |
| 2362 | if (audio_content->rejected) { |
| 2363 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2364 | } else { |
| 2365 | bool default_audio_track_needed = |
| 2366 | !remote_peer_supports_msid_ && |
| 2367 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2368 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2369 | default_audio_track_needed, audio_desc->type(), |
| 2370 | new_streams); |
| 2371 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2372 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2373 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2374 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2375 | // and MediaStreams. |
| 2376 | if (video_content) { |
| 2377 | if (video_content->rejected) { |
| 2378 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2379 | } else { |
| 2380 | bool default_video_track_needed = |
| 2381 | !remote_peer_supports_msid_ && |
| 2382 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2383 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2384 | default_video_track_needed, video_desc->type(), |
| 2385 | new_streams); |
| 2386 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2387 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2388 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2389 | // Update the DataChannels with the information from the remote peer. |
| 2390 | if (data_desc) { |
| 2391 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2392 | cricket::kMediaProtocolRtpPrefix)) { |
| 2393 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2394 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2395 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2396 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2397 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 2398 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2399 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2400 | stats_->AddStream(new_stream); |
| 2401 | observer_->OnAddStream( |
| 2402 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2403 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2404 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2405 | UpdateEndedRemoteMediaStreams(); |
| 2406 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2407 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2408 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2409 | } |
| 2410 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2411 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2412 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2413 | const SessionDescriptionInterface& new_session, |
| 2414 | const SessionDescriptionInterface* old_local_description, |
| 2415 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2416 | RTC_DCHECK(IsUnifiedPlan()); |
| 2417 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2418 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2419 | if (new_session.GetType() == SdpType::kOffer) { |
| 2420 | auto bundle_group_or_error = |
| 2421 | GetEarlyBundleGroup(*new_session.description()); |
| 2422 | if (!bundle_group_or_error.ok()) { |
| 2423 | return bundle_group_or_error.MoveError(); |
| 2424 | } |
| 2425 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2426 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2427 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2428 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2429 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2430 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2431 | cricket::MediaType media_type = new_content.media_description()->type(); |
| 2432 | seen_mids_.insert(new_content.name); |
| 2433 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2434 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2435 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2436 | if (old_local_description && |
| 2437 | i < old_local_description->description()->contents().size()) { |
| 2438 | old_local_content = |
| 2439 | &old_local_description->description()->contents()[i]; |
| 2440 | } |
| 2441 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2442 | if (old_remote_description && |
| 2443 | i < old_remote_description->description()->contents().size()) { |
| 2444 | old_remote_content = |
| 2445 | &old_remote_description->description()->contents()[i]; |
| 2446 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2447 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2448 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2449 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2450 | if (!transceiver_or_error.ok()) { |
| 2451 | return transceiver_or_error.MoveError(); |
| 2452 | } |
| 2453 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2454 | RTCError error = |
| 2455 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2456 | if (!error.ok()) { |
| 2457 | return error; |
| 2458 | } |
| 2459 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2460 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2461 | // Ignore all but the first data section. |
| 2462 | continue; |
| 2463 | } |
| 2464 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2465 | if (!error.ok()) { |
| 2466 | return error; |
| 2467 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2468 | } else { |
| 2469 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2470 | "Unknown section type."); |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | return RTCError::OK(); |
| 2475 | } |
| 2476 | |
| 2477 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2478 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2479 | transceiver, |
| 2480 | const cricket::ContentInfo& content, |
| 2481 | const cricket::ContentGroup* bundle_group) { |
| 2482 | RTC_DCHECK(IsUnifiedPlan()); |
| 2483 | RTC_DCHECK(transceiver); |
| 2484 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 2485 | if (content.rejected) { |
| 2486 | if (channel) { |
| 2487 | transceiver->internal()->SetChannel(nullptr); |
| 2488 | DestroyBaseChannel(channel); |
| 2489 | } |
| 2490 | } else { |
| 2491 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2492 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2493 | channel = CreateVoiceChannel( |
| 2494 | content.name, |
| 2495 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2496 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2497 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2498 | channel = CreateVideoChannel( |
| 2499 | content.name, |
| 2500 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2501 | } |
| 2502 | if (!channel) { |
| 2503 | LOG_AND_RETURN_ERROR( |
| 2504 | RTCErrorType::INTERNAL_ERROR, |
| 2505 | "Failed to create channel for mid=" + content.name); |
| 2506 | } |
| 2507 | transceiver->internal()->SetChannel(channel); |
| 2508 | } |
| 2509 | } |
| 2510 | return RTCError::OK(); |
| 2511 | } |
| 2512 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2513 | RTCError PeerConnection::UpdateDataChannel( |
| 2514 | cricket::ContentSource source, |
| 2515 | const cricket::ContentInfo& content, |
| 2516 | const cricket::ContentGroup* bundle_group) { |
| 2517 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2518 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2519 | // will take care of rejecting it. |
| 2520 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2521 | } |
| 2522 | if (content.rejected) { |
| 2523 | DestroyDataChannel(); |
| 2524 | } else { |
| 2525 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 2526 | if (!CreateDataChannel(content.name, GetTransportNameForMediaSection( |
| 2527 | content.name, bundle_group))) { |
| 2528 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2529 | "Failed to create data channel."); |
| 2530 | } |
| 2531 | } |
| 2532 | if (source == cricket::CS_REMOTE) { |
| 2533 | const MediaContentDescription* data_desc = content.media_description(); |
| 2534 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2535 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2536 | } |
| 2537 | } |
| 2538 | } |
| 2539 | return RTCError::OK(); |
| 2540 | } |
| 2541 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2542 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2543 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2544 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2545 | size_t mline_index, |
| 2546 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2547 | const ContentInfo* old_local_content, |
| 2548 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2549 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2550 | // If this is an offer then the m= section might be recycled. If the m= |
| 2551 | // section is being recycled (defined as: rejected in the current local or |
| 2552 | // remote description and not rejected in new description), dissociate the |
| 2553 | // currently associated RtpTransceiver by setting its mid property to null, |
| 2554 | // and discard the mapping between the transceiver and its m= section index. |
| 2555 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 2556 | old_remote_content)) { |
| 2557 | // We want to dissociate the transceiver that has the rejected mid. |
| 2558 | const std::string& old_mid = |
| 2559 | (old_local_content && old_local_content->rejected) |
| 2560 | ? old_local_content->name |
| 2561 | : old_remote_content->name; |
| 2562 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2563 | if (old_transceiver) { |
| 2564 | old_transceiver->internal()->set_mid(rtc::nullopt); |
| 2565 | old_transceiver->internal()->set_mline_index(rtc::nullopt); |
| 2566 | } |
| 2567 | } |
| 2568 | const MediaContentDescription* media_desc = content.media_description(); |
| 2569 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2570 | if (source == cricket::CS_LOCAL) { |
| 2571 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2572 | // mapping between transceivers and m= section indices established when |
| 2573 | // creating the offer. |
| 2574 | if (!transceiver) { |
| 2575 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2576 | } |
| 2577 | if (!transceiver) { |
| 2578 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2579 | "Unknown transceiver"); |
| 2580 | } |
| 2581 | } else { |
| 2582 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2583 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2584 | // of the same type... |
| 2585 | if (!transceiver && |
| 2586 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2587 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2588 | } |
| 2589 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2590 | // recvonly direction. |
| 2591 | if (!transceiver) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2592 | auto sender = |
| 2593 | CreateSender(media_desc->type(), nullptr, {rtc::CreateRandomUuid()}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2594 | std::string receiver_id; |
| 2595 | if (!media_desc->streams().empty()) { |
| 2596 | receiver_id = media_desc->streams()[0].id; |
| 2597 | } else { |
| 2598 | receiver_id = rtc::CreateRandomUuid(); |
| 2599 | } |
| 2600 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2601 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2602 | transceiver->internal()->set_direction( |
| 2603 | RtpTransceiverDirection::kRecvOnly); |
| 2604 | } |
| 2605 | } |
| 2606 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2607 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2608 | LOG_AND_RETURN_ERROR( |
| 2609 | RTCErrorType::INVALID_PARAMETER, |
| 2610 | "Transceiver type does not match media description type."); |
| 2611 | } |
| 2612 | // Associate the found or created RtpTransceiver with the m= section by |
| 2613 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2614 | // section, and establish a mapping between the transceiver and the index of |
| 2615 | // the m= section. |
| 2616 | transceiver->internal()->set_mid(content.name); |
| 2617 | transceiver->internal()->set_mline_index(mline_index); |
| 2618 | return std::move(transceiver); |
| 2619 | } |
| 2620 | |
| 2621 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2622 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 2623 | RTC_DCHECK(IsUnifiedPlan()); |
| 2624 | for (auto transceiver : transceivers_) { |
| 2625 | if (transceiver->mid() == mid) { |
| 2626 | return transceiver; |
| 2627 | } |
| 2628 | } |
| 2629 | return nullptr; |
| 2630 | } |
| 2631 | |
| 2632 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2633 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 2634 | RTC_DCHECK(IsUnifiedPlan()); |
| 2635 | for (auto transceiver : transceivers_) { |
| 2636 | if (transceiver->internal()->mline_index() == mline_index) { |
| 2637 | return transceiver; |
| 2638 | } |
| 2639 | } |
| 2640 | return nullptr; |
| 2641 | } |
| 2642 | |
| 2643 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2644 | PeerConnection::FindAvailableTransceiverToReceive( |
| 2645 | cricket::MediaType media_type) const { |
| 2646 | RTC_DCHECK(IsUnifiedPlan()); |
| 2647 | // From JSEP section 5.10 (Applying a Remote Description): |
| 2648 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 2649 | // the same type that were added to the PeerConnection by addTrack and are not |
| 2650 | // associated with any m= section and are not stopped, find the first such |
| 2651 | // RtpTransceiver. |
| 2652 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2653 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2654 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 2655 | !transceiver->stopped()) { |
| 2656 | return transceiver; |
| 2657 | } |
| 2658 | } |
| 2659 | return nullptr; |
| 2660 | } |
| 2661 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2662 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 2663 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2664 | transceiver, |
| 2665 | const SessionDescriptionInterface* sdesc) const { |
| 2666 | RTC_DCHECK(transceiver); |
| 2667 | RTC_DCHECK(sdesc); |
| 2668 | if (IsUnifiedPlan()) { |
| 2669 | if (!transceiver->internal()->mid()) { |
| 2670 | // This transceiver is not associated with a media section yet. |
| 2671 | return nullptr; |
| 2672 | } |
| 2673 | return sdesc->description()->GetContentByName( |
| 2674 | *transceiver->internal()->mid()); |
| 2675 | } else { |
| 2676 | // Plan B only allows at most one audio and one video section, so use the |
| 2677 | // first media section of that type. |
| 2678 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2679 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2680 | } |
| 2681 | } |
| 2682 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 2683 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 2684 | return configuration_; |
| 2685 | } |
| 2686 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2687 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 2688 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2689 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 2690 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2691 | if (local_description() && configuration.ice_candidate_pool_size != |
| 2692 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2693 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 2694 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2695 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2696 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2697 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2698 | // The simplest (and most future-compatible) way to tell if the config was |
| 2699 | // modified in an invalid way is to copy each property we do support |
| 2700 | // modifying, then use operator==. There are far more properties we don't |
| 2701 | // support modifying than those we do, and more could be added. |
| 2702 | RTCConfiguration modified_config = configuration_; |
| 2703 | modified_config.servers = configuration.servers; |
| 2704 | modified_config.type = configuration.type; |
| 2705 | modified_config.ice_candidate_pool_size = |
| 2706 | configuration.ice_candidate_pool_size; |
| 2707 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2708 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame^] | 2709 | modified_config.ice_check_interval_strong_connectivity = |
| 2710 | configuration.ice_check_interval_strong_connectivity; |
| 2711 | modified_config.ice_check_interval_weak_connectivity = |
| 2712 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2713 | modified_config.stun_candidate_keepalive_interval = |
| 2714 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2715 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 2716 | modified_config.network_preference = configuration.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2717 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2718 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2719 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2720 | } |
| 2721 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 2722 | // Validate the modified configuration. |
| 2723 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 2724 | if (!validate_error.ok()) { |
| 2725 | return SafeSetError(std::move(validate_error), error); |
| 2726 | } |
| 2727 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2728 | // Note that this isn't possible through chromium, since it's an unsigned |
| 2729 | // short in WebIDL. |
| 2730 | if (configuration.ice_candidate_pool_size < 0 || |
| 2731 | configuration.ice_candidate_pool_size > UINT16_MAX) { |
| 2732 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 2733 | } |
| 2734 | |
| 2735 | // Parse ICE servers before hopping to network thread. |
| 2736 | cricket::ServerAddresses stun_servers; |
| 2737 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 2738 | RTCErrorType parse_error = |
| 2739 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 2740 | if (parse_error != RTCErrorType::NONE) { |
| 2741 | return SafeSetError(parse_error, error); |
| 2742 | } |
| 2743 | |
| 2744 | // In theory this shouldn't fail. |
| 2745 | if (!network_thread()->Invoke<bool>( |
| 2746 | RTC_FROM_HERE, |
| 2747 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 2748 | stun_servers, turn_servers, modified_config.type, |
| 2749 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2750 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2751 | modified_config.turn_customizer, |
| 2752 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2753 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2754 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 2755 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2756 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2757 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 2758 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 2759 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2760 | if (modified_config.servers != configuration_.servers || |
| 2761 | modified_config.type != configuration_.type || |
| 2762 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2763 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2764 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2765 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 2766 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2767 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2768 | configuration_ = modified_config; |
| 2769 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2772 | bool PeerConnection::AddIceCandidate( |
| 2773 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2774 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2775 | if (IsClosed()) { |
| 2776 | return false; |
| 2777 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2778 | |
| 2779 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2780 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2781 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2782 | return false; |
| 2783 | } |
| 2784 | |
| 2785 | if (!ice_candidate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2786 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2787 | return false; |
| 2788 | } |
| 2789 | |
| 2790 | bool valid = false; |
| 2791 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 2792 | if (!valid) { |
| 2793 | return false; |
| 2794 | } |
| 2795 | |
| 2796 | // Add this candidate to the remote session description. |
| 2797 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2798 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2799 | return false; |
| 2800 | } |
| 2801 | |
| 2802 | if (ready) { |
| 2803 | return UseCandidate(ice_candidate); |
| 2804 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2805 | RTC_LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2806 | return true; |
| 2807 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2810 | bool PeerConnection::RemoveIceCandidates( |
| 2811 | const std::vector<cricket::Candidate>& candidates) { |
| 2812 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2813 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2814 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2815 | "removed without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2816 | return false; |
| 2817 | } |
| 2818 | |
| 2819 | if (candidates.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2820 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2821 | return false; |
| 2822 | } |
| 2823 | |
| 2824 | size_t number_removed = |
| 2825 | mutable_remote_description()->RemoveCandidates(candidates); |
| 2826 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2827 | RTC_LOG(LS_ERROR) |
| 2828 | << "RemoveRemoteIceCandidates: Failed to remove candidates. " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2829 | "Requested " |
| 2830 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2831 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | // Remove the candidates from the transport controller. |
| 2835 | std::string error; |
| 2836 | bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error); |
| 2837 | if (!res && !error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2838 | RTC_LOG(LS_ERROR) << "Error when removing remote candidates: " << error; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2839 | } |
| 2840 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2843 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2844 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2845 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2846 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2847 | if (transport_controller()) { |
| 2848 | transport_controller()->SetMetricsObserver(uma_observer_); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2851 | // Send information about IPv4/IPv6 status. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2852 | if (uma_observer_) { |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 2853 | port_allocator_->SetMetricsObserver(uma_observer_); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2854 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2855 | uma_observer_->IncrementEnumCounter( |
| 2856 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 2857 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 2858 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2859 | uma_observer_->IncrementEnumCounter( |
| 2860 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 2861 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2862 | } |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2863 | // Send information about the requested SDP semantics. |
| 2864 | switch (configuration_.sdp_semantics) { |
| 2865 | case SdpSemantics::kDefault: |
| 2866 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2867 | kSdpSemanticRequestDefault, |
| 2868 | kSdpSemanticRequestMax); |
| 2869 | |
| 2870 | break; |
| 2871 | case SdpSemantics::kPlanB: |
| 2872 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2873 | kSdpSemanticRequestPlanB, |
| 2874 | kSdpSemanticRequestMax); |
| 2875 | break; |
| 2876 | case SdpSemantics::kUnifiedPlan: |
| 2877 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2878 | kSdpSemanticRequestUnifiedPlan, |
| 2879 | kSdpSemanticRequestMax); |
| 2880 | break; |
| 2881 | default: |
| 2882 | RTC_NOTREACHED(); |
| 2883 | } |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2884 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2887 | RTCError PeerConnection::SetBitrate(const BitrateParameters& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2888 | if (!worker_thread()->IsCurrent()) { |
| 2889 | return worker_thread()->Invoke<RTCError>( |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2890 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::SetBitrate, this, bitrate)); |
| 2891 | } |
| 2892 | |
| 2893 | const bool has_min = static_cast<bool>(bitrate.min_bitrate_bps); |
| 2894 | const bool has_current = static_cast<bool>(bitrate.current_bitrate_bps); |
| 2895 | const bool has_max = static_cast<bool>(bitrate.max_bitrate_bps); |
| 2896 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 2897 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2898 | "min_bitrate_bps <= 0"); |
| 2899 | } |
| 2900 | if (has_current) { |
| 2901 | if (has_min && *bitrate.current_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 2902 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2903 | "current_bitrate_bps < min_bitrate_bps"); |
| 2904 | } else if (*bitrate.current_bitrate_bps < 0) { |
| 2905 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2906 | "curent_bitrate_bps < 0"); |
| 2907 | } |
| 2908 | } |
| 2909 | if (has_max) { |
| 2910 | if (has_current && |
| 2911 | *bitrate.max_bitrate_bps < *bitrate.current_bitrate_bps) { |
| 2912 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2913 | "max_bitrate_bps < current_bitrate_bps"); |
| 2914 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 2915 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2916 | "max_bitrate_bps < min_bitrate_bps"); |
| 2917 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 2918 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2919 | "max_bitrate_bps < 0"); |
| 2920 | } |
| 2921 | } |
| 2922 | |
Sebastian Jansson | fc8d26b | 2018-02-21 09:52:06 +0100 | [diff] [blame] | 2923 | BitrateConstraintsMask mask; |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2924 | mask.min_bitrate_bps = bitrate.min_bitrate_bps; |
| 2925 | mask.start_bitrate_bps = bitrate.current_bitrate_bps; |
| 2926 | mask.max_bitrate_bps = bitrate.max_bitrate_bps; |
| 2927 | |
| 2928 | RTC_DCHECK(call_.get()); |
Sebastian Jansson | 8f83b42 | 2018-02-21 13:07:13 +0100 | [diff] [blame] | 2929 | call_->GetTransportControllerSend()->SetClientBitratePreferences(mask); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2930 | |
| 2931 | return RTCError::OK(); |
| 2932 | } |
| 2933 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 2934 | void PeerConnection::SetBitrateAllocationStrategy( |
| 2935 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 2936 | bitrate_allocation_strategy) { |
| 2937 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 2938 | if (!worker_thread->IsCurrent()) { |
| 2939 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 2940 | bitrate_allocation_strategy.release(); |
| 2941 | auto functor = [this, strategy_raw]() { |
| 2942 | call_->SetBitrateAllocationStrategy( |
| 2943 | rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
| 2944 | }; |
| 2945 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 2946 | return; |
| 2947 | } |
| 2948 | RTC_DCHECK(call_.get()); |
| 2949 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 2950 | } |
| 2951 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 2952 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 2953 | if (!worker_thread()->IsCurrent()) { |
| 2954 | worker_thread()->Invoke<void>( |
| 2955 | RTC_FROM_HERE, |
| 2956 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 2957 | return; |
| 2958 | } |
| 2959 | auto audio_state = |
| 2960 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 2961 | audio_state->SetPlayout(playout); |
| 2962 | } |
| 2963 | |
| 2964 | void PeerConnection::SetAudioRecording(bool recording) { |
| 2965 | if (!worker_thread()->IsCurrent()) { |
| 2966 | worker_thread()->Invoke<void>( |
| 2967 | RTC_FROM_HERE, |
| 2968 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 2969 | return; |
| 2970 | } |
| 2971 | auto audio_state = |
| 2972 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 2973 | audio_state->SetRecording(recording); |
| 2974 | } |
| 2975 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2976 | std::unique_ptr<rtc::SSLCertificate> |
| 2977 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 2978 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 2979 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2980 | return nullptr; |
| 2981 | } |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 2982 | return chain->Get(0).GetUniqueReference(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2983 | } |
| 2984 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 2985 | std::unique_ptr<rtc::SSLCertChain> |
| 2986 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 2987 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 2988 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 2989 | return nullptr; |
| 2990 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 2991 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 2992 | audio_transceiver->internal()->channel()->transport_name()); |
| 2993 | } |
| 2994 | |
| 2995 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2996 | PeerConnection::GetFirstAudioTransceiver() const { |
| 2997 | for (auto transceiver : transceivers_) { |
| 2998 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 2999 | return transceiver; |
| 3000 | } |
| 3001 | } |
| 3002 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3003 | } |
| 3004 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3005 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3006 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3007 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3008 | const size_t max_size = (max_size_bytes < 0) |
| 3009 | ? RtcEventLog::kUnlimitedOutput |
| 3010 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 3011 | return StartRtcEventLog( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3012 | rtc::MakeUnique<RtcEventLogOutputFile>(file, max_size), |
| 3013 | webrtc::RtcEventLog::kImmediateOutput); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3014 | } |
| 3015 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3016 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3017 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3018 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3019 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3020 | bool operator()() { |
| 3021 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3022 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3023 | PeerConnection* const pc; |
| 3024 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3025 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3026 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3027 | return worker_thread()->Invoke<bool>( |
| 3028 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3032 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3033 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3034 | } |
| 3035 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3036 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3037 | return pending_local_description_ ? pending_local_description_.get() |
| 3038 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3042 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3043 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3046 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3047 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3048 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3052 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3053 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3057 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3058 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3062 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3063 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3064 | } |
| 3065 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3066 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3067 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3068 | // Update stats here so that we have the most recent stats for tracks and |
| 3069 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3070 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3071 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3072 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3073 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3074 | for (auto transceiver : transceivers_) { |
| 3075 | transceiver->Stop(); |
| 3076 | } |
| 3077 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3078 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3079 | // The event log is used in the transport controller, which must be outlived |
| 3080 | // by the former. CreateOffer by the peer connection is implemented |
| 3081 | // asynchronously and if the peer connection is closed without resetting the |
| 3082 | // WebRTC session description factory, the session description factory would |
| 3083 | // call the transport controller. |
| 3084 | webrtc_session_desc_factory_.reset(); |
| 3085 | transport_controller_.reset(); |
| 3086 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3087 | network_thread()->Invoke<void>( |
| 3088 | RTC_FROM_HERE, |
| 3089 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3090 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3091 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3092 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3093 | call_.reset(); |
| 3094 | // The event log must outlive call (and any other object that uses it). |
| 3095 | event_log_.reset(); |
| 3096 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 3099 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3100 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3101 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3102 | SetSessionDescriptionMsg* param = |
| 3103 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3104 | param->observer->OnSuccess(); |
| 3105 | delete param; |
| 3106 | break; |
| 3107 | } |
| 3108 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3109 | SetSessionDescriptionMsg* param = |
| 3110 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3111 | param->observer->OnFailure(param->error); |
| 3112 | delete param; |
| 3113 | break; |
| 3114 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3115 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3116 | CreateSessionDescriptionMsg* param = |
| 3117 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 3118 | param->observer->OnFailure(param->error); |
| 3119 | delete param; |
| 3120 | break; |
| 3121 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3122 | case MSG_GETSTATS: { |
| 3123 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 3124 | StatsReports reports; |
| 3125 | stats_->GetStats(param->track, &reports); |
| 3126 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3127 | delete param; |
| 3128 | break; |
| 3129 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 3130 | case MSG_FREE_DATACHANNELS: { |
| 3131 | sctp_data_channels_to_free_.clear(); |
| 3132 | break; |
| 3133 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3134 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3135 | RTC_NOTREACHED() << "Not implemented"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3136 | break; |
| 3137 | } |
| 3138 | } |
| 3139 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3140 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3141 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3142 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3143 | GetAudioTransceiver()->internal()->channel()); |
| 3144 | if (voice_channel) { |
| 3145 | return voice_channel->media_channel(); |
| 3146 | } else { |
| 3147 | return nullptr; |
| 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3152 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3153 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3154 | GetVideoTransceiver()->internal()->channel()); |
| 3155 | if (video_channel) { |
| 3156 | return video_channel->media_channel(); |
| 3157 | } else { |
| 3158 | return nullptr; |
| 3159 | } |
| 3160 | } |
| 3161 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3162 | void PeerConnection::CreateAudioReceiver( |
| 3163 | MediaStreamInterface* stream, |
| 3164 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3165 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3166 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3167 | auto* audio_receiver = new AudioRtpReceiver( |
| 3168 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3169 | audio_receiver->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3170 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3171 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3172 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3173 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3174 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3177 | void PeerConnection::CreateVideoReceiver( |
| 3178 | MediaStreamInterface* stream, |
| 3179 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3180 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3181 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3182 | auto* video_receiver = new VideoRtpReceiver( |
| 3183 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3184 | video_receiver->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3185 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3186 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3187 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3188 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3189 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3192 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3193 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3194 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3195 | const RtpSenderInfo& remote_sender_info) { |
| 3196 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3197 | if (!receiver) { |
| 3198 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3199 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3200 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3201 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3202 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3203 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3204 | } else { |
| 3205 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3206 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3207 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3210 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3211 | MediaStreamInterface* stream) { |
| 3212 | RTC_DCHECK(!IsClosed()); |
| 3213 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3214 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3215 | // We already have a sender for this track, so just change the stream_id |
| 3216 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3217 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3218 | return; |
| 3219 | } |
| 3220 | |
| 3221 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3222 | auto new_sender = |
| 3223 | CreateSender(cricket::MEDIA_TYPE_AUDIO, track, {stream->label()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3224 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3225 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3226 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3227 | // which will connect the sender to the underlying transport. This can |
| 3228 | // occur if a local session description that contains the ID of the sender |
| 3229 | // is set before AddStream is called. It can also occur if the local |
| 3230 | // session description is not changed and RemoveStream is called, and |
| 3231 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3232 | const RtpSenderInfo* sender_info = |
| 3233 | FindSenderInfo(local_audio_sender_infos_, stream->label(), track->id()); |
| 3234 | if (sender_info) { |
| 3235 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3240 | // indefinitely, when we have unified plan SDP. |
| 3241 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3242 | MediaStreamInterface* stream) { |
| 3243 | RTC_DCHECK(!IsClosed()); |
| 3244 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3245 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3246 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3247 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3248 | return; |
| 3249 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3250 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3254 | MediaStreamInterface* stream) { |
| 3255 | RTC_DCHECK(!IsClosed()); |
| 3256 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3257 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3258 | // We already have a sender for this track, so just change the stream_id |
| 3259 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3260 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3261 | return; |
| 3262 | } |
| 3263 | |
| 3264 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3265 | auto new_sender = |
| 3266 | CreateSender(cricket::MEDIA_TYPE_VIDEO, track, {stream->label()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3267 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3268 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3269 | const RtpSenderInfo* sender_info = |
| 3270 | FindSenderInfo(local_video_sender_infos_, stream->label(), track->id()); |
| 3271 | if (sender_info) { |
| 3272 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3277 | MediaStreamInterface* stream) { |
| 3278 | RTC_DCHECK(!IsClosed()); |
| 3279 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3280 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3281 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3282 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3283 | return; |
| 3284 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3285 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3286 | } |
| 3287 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3288 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3289 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3290 | if (ice_connection_state_ == new_state) { |
| 3291 | return; |
| 3292 | } |
| 3293 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3294 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3295 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3296 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3297 | return; |
| 3298 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3299 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3300 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3301 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3302 | RTC_DCHECK(ice_connection_state_ != |
| 3303 | PeerConnectionInterface::kIceConnectionClosed); |
| 3304 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3305 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3306 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | void PeerConnection::OnIceGatheringChange( |
| 3310 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3311 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3312 | if (IsClosed()) { |
| 3313 | return; |
| 3314 | } |
| 3315 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3316 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3319 | void PeerConnection::OnIceCandidate( |
| 3320 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3321 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3322 | if (IsClosed()) { |
| 3323 | return; |
| 3324 | } |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3325 | observer_->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3328 | void PeerConnection::OnIceCandidatesRemoved( |
| 3329 | const std::vector<cricket::Candidate>& candidates) { |
| 3330 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3331 | if (IsClosed()) { |
| 3332 | return; |
| 3333 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3334 | observer_->OnIceCandidatesRemoved(candidates); |
| 3335 | } |
| 3336 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3337 | void PeerConnection::ChangeSignalingState( |
| 3338 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3339 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3340 | if (signaling_state_ == signaling_state) { |
| 3341 | return; |
| 3342 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3343 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3344 | << GetSignalingStateString(signaling_state_) |
| 3345 | << " New state: " |
| 3346 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3347 | signaling_state_ = signaling_state; |
| 3348 | if (signaling_state == kClosed) { |
| 3349 | ice_connection_state_ = kIceConnectionClosed; |
| 3350 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 3351 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3352 | ice_gathering_state_ = kIceGatheringComplete; |
| 3353 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 3354 | } |
| 3355 | } |
| 3356 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3359 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3360 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3361 | if (IsClosed()) { |
| 3362 | return; |
| 3363 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3364 | AddAudioTrack(track, stream); |
| 3365 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3366 | } |
| 3367 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3368 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3369 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3370 | if (IsClosed()) { |
| 3371 | return; |
| 3372 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3373 | RemoveAudioTrack(track, stream); |
| 3374 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3378 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3379 | if (IsClosed()) { |
| 3380 | return; |
| 3381 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3382 | AddVideoTrack(track, stream); |
| 3383 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3387 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3388 | if (IsClosed()) { |
| 3389 | return; |
| 3390 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3391 | RemoveVideoTrack(track, stream); |
| 3392 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3393 | } |
| 3394 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3395 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3396 | SetSessionDescriptionObserver* observer) { |
| 3397 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3398 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3399 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 3400 | } |
| 3401 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3402 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3403 | SetSessionDescriptionObserver* observer, |
| 3404 | const std::string& error) { |
| 3405 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3406 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3407 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3408 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3412 | CreateSessionDescriptionObserver* observer, |
| 3413 | const std::string& error) { |
| 3414 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 3415 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3416 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3417 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3418 | } |
| 3419 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3420 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3421 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3422 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3423 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3424 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3425 | if (IsUnifiedPlan()) { |
| 3426 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3427 | } else { |
| 3428 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3429 | } |
| 3430 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3431 | // Intentionally unset the data channel type for RTP data channel with the |
| 3432 | // second condition. Otherwise the RTP data channels would be successfully |
| 3433 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3434 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3435 | // people won't try to use them. |
| 3436 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3437 | session_options->data_channel_type = data_channel_type(); |
| 3438 | } |
| 3439 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3440 | // Apply ICE restart flag and renomination flag. |
| 3441 | for (auto& options : session_options->media_description_options) { |
| 3442 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3443 | options.transport_options.enable_ice_renomination = |
| 3444 | configuration_.enable_ice_renomination; |
| 3445 | } |
| 3446 | |
| 3447 | session_options->rtcp_cname = rtcp_cname_; |
| 3448 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3449 | session_options->is_unified_plan = IsUnifiedPlan(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3453 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3454 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3455 | // Figure out transceiver directional preferences. |
| 3456 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3457 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3458 | |
| 3459 | // By default, generate sendrecv/recvonly m= sections. |
| 3460 | bool recv_audio = true; |
| 3461 | bool recv_video = true; |
| 3462 | |
| 3463 | // By default, only offer a new m= section if we have media to send with it. |
| 3464 | bool offer_new_audio_description = send_audio; |
| 3465 | bool offer_new_video_description = send_video; |
| 3466 | bool offer_new_data_description = HasDataChannels(); |
| 3467 | |
| 3468 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3469 | if (offer_answer_options.offer_to_receive_audio != |
| 3470 | RTCOfferAnswerOptions::kUndefined) { |
| 3471 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3472 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3473 | offer_new_audio_description || |
| 3474 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3475 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3476 | if (offer_answer_options.offer_to_receive_video != |
| 3477 | RTCOfferAnswerOptions::kUndefined) { |
| 3478 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3479 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3480 | offer_new_video_description || |
| 3481 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | rtc::Optional<size_t> audio_index; |
| 3485 | rtc::Optional<size_t> video_index; |
| 3486 | rtc::Optional<size_t> data_index; |
| 3487 | // If a current description exists, generate m= sections in the same order, |
| 3488 | // using the first audio/video/data section that appears and rejecting |
| 3489 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3490 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3491 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3492 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3493 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3494 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3495 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3498 | // Add audio/video/data m= sections to the end if needed. |
| 3499 | if (!audio_index && offer_new_audio_description) { |
| 3500 | session_options->media_description_options.push_back( |
| 3501 | cricket::MediaDescriptionOptions( |
| 3502 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3503 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3504 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3505 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3506 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3507 | if (!video_index && offer_new_video_description) { |
| 3508 | session_options->media_description_options.push_back( |
| 3509 | cricket::MediaDescriptionOptions( |
| 3510 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3511 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3512 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3513 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3514 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3515 | if (!data_index && offer_new_data_description) { |
| 3516 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3517 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3518 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3522 | !audio_index ? nullptr |
| 3523 | : &session_options->media_description_options[*audio_index]; |
| 3524 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3525 | !video_index ? nullptr |
| 3526 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3527 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3528 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3529 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | // Find a new MID that is not already in |used_mids|, then add it to |used_mids| |
| 3533 | // and return a reference to it. |
| 3534 | // Generated MIDs should be no more than 3 bytes long to take up less space in |
| 3535 | // the RTP packet. |
| 3536 | static const std::string& AllocateMid(std::set<std::string>* used_mids) { |
| 3537 | RTC_DCHECK(used_mids); |
| 3538 | // We're boring: just generate MIDs 0, 1, 2, ... |
| 3539 | size_t i = 0; |
| 3540 | std::set<std::string>::iterator it; |
| 3541 | bool inserted; |
| 3542 | do { |
| 3543 | std::string mid = rtc::ToString(i++); |
| 3544 | auto insert_result = used_mids->insert(mid); |
| 3545 | it = insert_result.first; |
| 3546 | inserted = insert_result.second; |
| 3547 | } while (!inserted); |
| 3548 | return *it; |
| 3549 | } |
| 3550 | |
| 3551 | static cricket::MediaDescriptionOptions |
| 3552 | GetMediaDescriptionOptionsForTransceiver( |
| 3553 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3554 | transceiver, |
| 3555 | const std::string& mid) { |
| 3556 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3557 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3558 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3559 | // This behavior is specified in JSEP. The gist is that: |
| 3560 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 3561 | // sendrecv. |
| 3562 | // 2. If the MSID is included, then it must be included in any subsequent |
| 3563 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 3564 | if (!transceiver->stopped() && |
| 3565 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 3566 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 3567 | cricket::SenderOptions sender_options; |
| 3568 | sender_options.track_id = transceiver->sender()->id(); |
| 3569 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 3570 | // TODO(bugs.webrtc.org/7600): Set num_sim_layers to the number of encodings |
| 3571 | // set in the RTP parameters when the transceiver was added. |
| 3572 | sender_options.num_sim_layers = 1; |
| 3573 | media_description_options.sender_options.push_back(sender_options); |
| 3574 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3575 | return media_description_options; |
| 3576 | } |
| 3577 | |
| 3578 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 3579 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3580 | cricket::MediaSessionOptions* session_options) { |
| 3581 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3582 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3583 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3584 | const ContentInfos& local_contents = |
| 3585 | (local_description() ? local_description()->description()->contents() |
| 3586 | : ContentInfos()); |
| 3587 | const ContentInfos& remote_contents = |
| 3588 | (remote_description() ? remote_description()->description()->contents() |
| 3589 | : ContentInfos()); |
| 3590 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3591 | // slots first. |
| 3592 | std::queue<size_t> recycleable_mline_indices; |
| 3593 | // Track the MIDs used in previous offer/answer exchanges and the current |
| 3594 | // offer so that new, unique MIDs are generated. |
| 3595 | std::set<std::string> used_mids = seen_mids_; |
| 3596 | // First, go through each media section that exists in either the local or |
| 3597 | // remote description and generate a media section in this offer for the |
| 3598 | // associated transceiver. If a media section can be recycled, generate a |
| 3599 | // default, rejected media section here that can be later overwritten. |
| 3600 | for (size_t i = 0; |
| 3601 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3602 | // Either |local_content| or |remote_content| is non-null. |
| 3603 | const ContentInfo* local_content = |
| 3604 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3605 | const ContentInfo* remote_content = |
| 3606 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3607 | bool had_been_rejected = (local_content && local_content->rejected) || |
| 3608 | (remote_content && remote_content->rejected); |
| 3609 | const std::string& mid = |
| 3610 | (local_content ? local_content->name : remote_content->name); |
| 3611 | cricket::MediaType media_type = |
| 3612 | (local_content ? local_content->media_description()->type() |
| 3613 | : remote_content->media_description()->type()); |
| 3614 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3615 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3616 | auto transceiver = GetAssociatedTransceiver(mid); |
| 3617 | RTC_CHECK(transceiver); |
| 3618 | // A media section is considered eligible for recycling if it is marked as |
| 3619 | // rejected in either the local or remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3620 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3621 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3622 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 3623 | RtpTransceiverDirection::kInactive, |
| 3624 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3625 | recycleable_mline_indices.push(i); |
| 3626 | } else { |
| 3627 | session_options->media_description_options.push_back( |
| 3628 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 3629 | // CreateOffer shouldn't really cause any state changes in |
| 3630 | // PeerConnection, but we need a way to match new transceivers to new |
| 3631 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 3632 | // by recording the index of the media section generated for the |
| 3633 | // transceiver in the offer. |
| 3634 | transceiver->internal()->set_mline_index(i); |
| 3635 | } |
| 3636 | } else { |
| 3637 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3638 | RTC_CHECK(GetDataMid()); |
| 3639 | if (had_been_rejected || mid != *GetDataMid()) { |
| 3640 | session_options->media_description_options.push_back( |
| 3641 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 3642 | } else { |
| 3643 | session_options->media_description_options.push_back( |
| 3644 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 3645 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3646 | } |
| 3647 | } |
| 3648 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3649 | // and not associated). Reuse media sections marked as recyclable first, |
| 3650 | // otherwise append to the end of the offer. New media sections should be |
| 3651 | // added in the order they were added to the PeerConnection. |
| 3652 | for (auto transceiver : transceivers_) { |
| 3653 | if (transceiver->mid() || transceiver->stopped()) { |
| 3654 | continue; |
| 3655 | } |
| 3656 | size_t mline_index; |
| 3657 | if (!recycleable_mline_indices.empty()) { |
| 3658 | mline_index = recycleable_mline_indices.front(); |
| 3659 | recycleable_mline_indices.pop(); |
| 3660 | session_options->media_description_options[mline_index] = |
| 3661 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3662 | AllocateMid(&used_mids)); |
| 3663 | } else { |
| 3664 | mline_index = session_options->media_description_options.size(); |
| 3665 | session_options->media_description_options.push_back( |
| 3666 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3667 | AllocateMid(&used_mids))); |
| 3668 | } |
| 3669 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3670 | transceiver->internal()->set_mline_index(mline_index); |
| 3671 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3672 | // Lastly, add a m-section if we have local data channels and an m section |
| 3673 | // does not already exist. |
| 3674 | if (!GetDataMid() && HasDataChannels()) { |
| 3675 | session_options->media_description_options.push_back( |
| 3676 | GetMediaDescriptionOptionsForActiveData(AllocateMid(&used_mids))); |
| 3677 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | void PeerConnection::GetOptionsForAnswer( |
| 3681 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3682 | cricket::MediaSessionOptions* session_options) { |
| 3683 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3684 | |
| 3685 | if (IsUnifiedPlan()) { |
| 3686 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3687 | } else { |
| 3688 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3689 | } |
| 3690 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3691 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3692 | // the RTP data channels would be successfully negotiated by default and the |
| 3693 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3694 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 3695 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3696 | session_options->data_channel_type = data_channel_type(); |
| 3697 | } |
| 3698 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3699 | // Apply ICE renomination flag. |
| 3700 | for (auto& options : session_options->media_description_options) { |
| 3701 | options.transport_options.enable_ice_renomination = |
| 3702 | configuration_.enable_ice_renomination; |
| 3703 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 3704 | |
| 3705 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 3706 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3707 | session_options->is_unified_plan = IsUnifiedPlan(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3708 | } |
| 3709 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3710 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 3711 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3712 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3713 | // Figure out transceiver directional preferences. |
| 3714 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3715 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3716 | |
| 3717 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 3718 | // restricted by the direction in the offer. |
| 3719 | bool recv_audio = true; |
| 3720 | bool recv_video = true; |
| 3721 | |
| 3722 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3723 | if (offer_answer_options.offer_to_receive_audio != |
| 3724 | RTCOfferAnswerOptions::kUndefined) { |
| 3725 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 3726 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3727 | if (offer_answer_options.offer_to_receive_video != |
| 3728 | RTCOfferAnswerOptions::kUndefined) { |
| 3729 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | rtc::Optional<size_t> audio_index; |
| 3733 | rtc::Optional<size_t> video_index; |
| 3734 | rtc::Optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 3735 | |
| 3736 | // Generate m= sections that match those in the offer. |
| 3737 | // Note that mediasession.cc will handle intersection our preferred |
| 3738 | // direction with the offered direction. |
| 3739 | GenerateMediaDescriptionOptions( |
| 3740 | remote_description(), |
| 3741 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3742 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 3743 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3744 | |
| 3745 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3746 | !audio_index ? nullptr |
| 3747 | : &session_options->media_description_options[*audio_index]; |
| 3748 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3749 | !video_index ? nullptr |
| 3750 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3751 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3752 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3753 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3754 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 3755 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3756 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 3757 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3758 | cricket::MediaSessionOptions* session_options) { |
| 3759 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 3760 | // Answers) and 5.3.2 (Subsequent Answers). |
| 3761 | RTC_DCHECK(remote_description()); |
| 3762 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3763 | for (const ContentInfo& content : |
| 3764 | remote_description()->description()->contents()) { |
| 3765 | cricket::MediaType media_type = content.media_description()->type(); |
| 3766 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3767 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3768 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3769 | RTC_CHECK(transceiver); |
| 3770 | session_options->media_description_options.push_back( |
| 3771 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 3772 | } else { |
| 3773 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3774 | // Reject all data sections if data channels are disabled. |
| 3775 | // Reject a data section if it has already been rejected. |
| 3776 | // Reject all data sections except for the first one. |
| 3777 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 3778 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3779 | session_options->media_description_options.push_back( |
| 3780 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 3781 | } else { |
| 3782 | session_options->media_description_options.push_back( |
| 3783 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 3784 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3785 | } |
| 3786 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3787 | } |
| 3788 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3789 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 3790 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3791 | RtpTransceiverDirection audio_direction, |
| 3792 | RtpTransceiverDirection video_direction, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3793 | rtc::Optional<size_t>* audio_index, |
| 3794 | rtc::Optional<size_t>* video_index, |
| 3795 | rtc::Optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3796 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3797 | for (const cricket::ContentInfo& content : |
| 3798 | session_desc->description()->contents()) { |
| 3799 | if (IsAudioContent(&content)) { |
| 3800 | // If we already have an audio m= section, reject this extra one. |
| 3801 | if (*audio_index) { |
| 3802 | session_options->media_description_options.push_back( |
| 3803 | cricket::MediaDescriptionOptions( |
| 3804 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3805 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3806 | } else { |
| 3807 | session_options->media_description_options.push_back( |
| 3808 | cricket::MediaDescriptionOptions( |
| 3809 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3810 | audio_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3811 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3812 | } |
| 3813 | } else if (IsVideoContent(&content)) { |
| 3814 | // If we already have an video m= section, reject this extra one. |
| 3815 | if (*video_index) { |
| 3816 | session_options->media_description_options.push_back( |
| 3817 | cricket::MediaDescriptionOptions( |
| 3818 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3819 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3820 | } else { |
| 3821 | session_options->media_description_options.push_back( |
| 3822 | cricket::MediaDescriptionOptions( |
| 3823 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3824 | video_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3825 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3826 | } |
| 3827 | } else { |
| 3828 | RTC_DCHECK(IsDataContent(&content)); |
| 3829 | // If we already have an data m= section, reject this extra one. |
| 3830 | if (*data_index) { |
| 3831 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3832 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3833 | } else { |
| 3834 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3835 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3836 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3837 | } |
| 3838 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3839 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3840 | } |
| 3841 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3842 | cricket::MediaDescriptionOptions |
| 3843 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 3844 | const std::string& mid) const { |
| 3845 | // Direction for data sections is meaningless, but legacy endpoints might |
| 3846 | // expect sendrecv. |
| 3847 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3848 | RtpTransceiverDirection::kSendRecv, |
| 3849 | /*stopped=*/false); |
| 3850 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3851 | return options; |
| 3852 | } |
| 3853 | |
| 3854 | cricket::MediaDescriptionOptions |
| 3855 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 3856 | const std::string& mid) const { |
| 3857 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3858 | RtpTransceiverDirection::kInactive, |
| 3859 | /*stopped=*/true); |
| 3860 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3861 | return options; |
| 3862 | } |
| 3863 | |
| 3864 | rtc::Optional<std::string> PeerConnection::GetDataMid() const { |
| 3865 | switch (data_channel_type_) { |
| 3866 | case cricket::DCT_RTP: |
| 3867 | if (!rtp_data_channel_) { |
| 3868 | return rtc::nullopt; |
| 3869 | } |
| 3870 | return rtp_data_channel_->content_name(); |
| 3871 | case cricket::DCT_SCTP: |
| 3872 | return sctp_content_name_; |
| 3873 | default: |
| 3874 | return rtc::nullopt; |
| 3875 | } |
| 3876 | } |
| 3877 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3878 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 3879 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 3880 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3881 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 3882 | } |
| 3883 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3884 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3885 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3886 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3887 | cricket::MediaType media_type, |
| 3888 | StreamCollection* new_streams) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3889 | std::vector<RtpSenderInfo>* current_senders = |
| 3890 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3891 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3892 | // 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] | 3893 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3894 | for (auto sender_it = current_senders->begin(); |
| 3895 | sender_it != current_senders->end(); |
| 3896 | /* incremented manually */) { |
| 3897 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3898 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3899 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 3900 | bool sender_exists = params && params->id == info.sender_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3901 | // 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] | 3902 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3903 | sender_exists) { |
| 3904 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3905 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3906 | OnRemoteSenderRemoved(info, media_type); |
| 3907 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3908 | } |
| 3909 | } |
| 3910 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3911 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3912 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3913 | // |params.id| is the sender id and the stream id uses the first of |
| 3914 | // |params.stream_ids|. |
| 3915 | // TODO(bugs.webrtc.org/7932): Add support for multiple stream ids. |
| 3916 | const std::string& stream_id = |
| 3917 | (!params.stream_ids().empty() ? params.stream_ids()[0] : ""); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3918 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3919 | uint32_t ssrc = params.first_ssrc(); |
| 3920 | |
| 3921 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3922 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3923 | if (!stream) { |
| 3924 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3925 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3926 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3927 | remote_streams_->AddStream(stream); |
| 3928 | new_streams->AddStream(stream); |
| 3929 | } |
| 3930 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3931 | const RtpSenderInfo* sender_info = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3932 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3933 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3934 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3935 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3936 | } |
| 3937 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3938 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3939 | // Add default sender if necessary. |
| 3940 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3941 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3942 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3943 | if (!default_stream) { |
| 3944 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3945 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3946 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3947 | remote_streams_->AddStream(default_stream); |
| 3948 | new_streams->AddStream(default_stream); |
| 3949 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3950 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 3951 | ? kDefaultAudioSenderId |
| 3952 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3953 | const RtpSenderInfo* default_sender_info = |
| 3954 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3955 | if (!default_sender_info) { |
| 3956 | current_senders->push_back( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3957 | RtpSenderInfo(kDefaultStreamId, default_sender_id, 0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3958 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3959 | } |
| 3960 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3961 | } |
| 3962 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3963 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 3964 | cricket::MediaType media_type) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3965 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3966 | |
| 3967 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3968 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3969 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3970 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3971 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3972 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3973 | } |
| 3974 | } |
| 3975 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3976 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 3977 | cricket::MediaType media_type) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 3978 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3979 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3980 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3981 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3982 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 3983 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3984 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3985 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3986 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3987 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3988 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3989 | } |
| 3990 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3991 | // Stopping or destroying a VideoRtpReceiver will end the |
| 3992 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3993 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3994 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3995 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3996 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3997 | // There's no guarantee the track is still available, e.g. the track may |
| 3998 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3999 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4000 | } |
| 4001 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 4002 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4003 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4004 | if (receiver) { |
| 4005 | observer_->OnRemoveTrack(receiver); |
| 4006 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4010 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4011 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4012 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4013 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4014 | streams_to_remove.push_back(stream); |
| 4015 | } |
| 4016 | } |
| 4017 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4018 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4019 | remote_streams_->RemoveStream(stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4020 | observer_->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4024 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4025 | const std::vector<cricket::StreamParams>& streams, |
| 4026 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4027 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4028 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4029 | // 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] | 4030 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4031 | for (auto sender_it = current_senders->begin(); |
| 4032 | sender_it != current_senders->end(); |
| 4033 | /* incremented manually */) { |
| 4034 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4035 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4036 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4037 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4038 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4039 | OnLocalSenderRemoved(info, media_type); |
| 4040 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4041 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4042 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4043 | } |
| 4044 | } |
| 4045 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4046 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4047 | for (const cricket::StreamParams& params : streams) { |
| 4048 | // 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] | 4049 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4050 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4051 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4052 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4053 | const RtpSenderInfo* sender_info = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4054 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4055 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4056 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4057 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4058 | } |
| 4059 | } |
| 4060 | } |
| 4061 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4062 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4063 | cricket::MediaType media_type) { |
| 4064 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4065 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4066 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4067 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4068 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4069 | return; |
| 4070 | } |
| 4071 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4072 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4073 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4074 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4075 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4076 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4077 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4078 | sender->internal()->set_stream_id(sender_info.stream_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4079 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4080 | } |
| 4081 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4082 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4083 | cricket::MediaType media_type) { |
| 4084 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4085 | if (!sender) { |
| 4086 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4087 | // SessionDescriptions has been renegotiated. |
| 4088 | return; |
| 4089 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4090 | |
| 4091 | // A sender has been removed from the SessionDescription but it's still |
| 4092 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4093 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4094 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4095 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4096 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4097 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4098 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4099 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4100 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4104 | const cricket::StreamParamsVec& streams) { |
| 4105 | std::vector<std::string> existing_channels; |
| 4106 | |
| 4107 | // Find new and active data channels. |
| 4108 | for (const cricket::StreamParams& params : streams) { |
| 4109 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4110 | // we use the same naming of data channels as we do for |
| 4111 | // MediaStreams and Tracks. |
| 4112 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4113 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4114 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4115 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4116 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4117 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4118 | continue; |
| 4119 | } |
| 4120 | // Set the SSRC the data channel should use for sending. |
| 4121 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4122 | existing_channels.push_back(data_channel_it->first); |
| 4123 | } |
| 4124 | |
| 4125 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4126 | } |
| 4127 | |
| 4128 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4129 | const cricket::StreamParamsVec& streams) { |
| 4130 | std::vector<std::string> existing_channels; |
| 4131 | |
| 4132 | // Find new and active data channels. |
| 4133 | for (const cricket::StreamParams& params : streams) { |
| 4134 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4135 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4136 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4137 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4138 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4139 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4140 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4141 | // This is a new data channel. |
| 4142 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4143 | } else { |
| 4144 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4145 | } |
| 4146 | existing_channels.push_back(label); |
| 4147 | } |
| 4148 | |
| 4149 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4150 | } |
| 4151 | |
| 4152 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4153 | const std::vector<std::string>& active_channels, |
| 4154 | bool is_local_update) { |
| 4155 | auto it = rtp_data_channels_.begin(); |
| 4156 | while (it != rtp_data_channels_.end()) { |
| 4157 | DataChannel* data_channel = it->second; |
| 4158 | if (std::find(active_channels.begin(), active_channels.end(), |
| 4159 | data_channel->label()) != active_channels.end()) { |
| 4160 | ++it; |
| 4161 | continue; |
| 4162 | } |
| 4163 | |
| 4164 | if (is_local_update) { |
| 4165 | data_channel->SetSendSsrc(0); |
| 4166 | } else { |
| 4167 | data_channel->RemotePeerRequestClose(); |
| 4168 | } |
| 4169 | |
| 4170 | if (data_channel->state() == DataChannel::kClosed) { |
| 4171 | rtp_data_channels_.erase(it); |
| 4172 | it = rtp_data_channels_.begin(); |
| 4173 | } else { |
| 4174 | ++it; |
| 4175 | } |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4180 | uint32_t remote_ssrc) { |
| 4181 | rtc::scoped_refptr<DataChannel> channel( |
| 4182 | InternalCreateDataChannel(label, nullptr)); |
| 4183 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4184 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4185 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4186 | return; |
| 4187 | } |
| 4188 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4189 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4190 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4191 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4195 | const std::string& label, |
| 4196 | const InternalDataChannelInit* config) { |
| 4197 | if (IsClosed()) { |
| 4198 | return nullptr; |
| 4199 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4200 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4201 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4202 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4203 | return nullptr; |
| 4204 | } |
| 4205 | InternalDataChannelInit new_config = |
| 4206 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4207 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4208 | if (new_config.id < 0) { |
| 4209 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4210 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4211 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4212 | RTC_LOG(LS_ERROR) |
| 4213 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4214 | return nullptr; |
| 4215 | } |
| 4216 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4217 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4218 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4219 | return nullptr; |
| 4220 | } |
| 4221 | } |
| 4222 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4223 | rtc::scoped_refptr<DataChannel> channel( |
| 4224 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4225 | if (!channel) { |
| 4226 | sid_allocator_.ReleaseSid(new_config.id); |
| 4227 | return nullptr; |
| 4228 | } |
| 4229 | |
| 4230 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4231 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4232 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4233 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4234 | return nullptr; |
| 4235 | } |
| 4236 | rtp_data_channels_[channel->label()] = channel; |
| 4237 | } else { |
| 4238 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 4239 | sctp_data_channels_.push_back(channel); |
| 4240 | channel->SignalClosed.connect(this, |
| 4241 | &PeerConnection::OnSctpDataChannelClosed); |
| 4242 | } |
| 4243 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4244 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4245 | return channel; |
| 4246 | } |
| 4247 | |
| 4248 | bool PeerConnection::HasDataChannels() const { |
| 4249 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4250 | } |
| 4251 | |
| 4252 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4253 | for (const auto& channel : sctp_data_channels_) { |
| 4254 | if (channel->id() < 0) { |
| 4255 | int sid; |
| 4256 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4257 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4258 | continue; |
| 4259 | } |
| 4260 | channel->SetSctpSid(sid); |
| 4261 | } |
| 4262 | } |
| 4263 | } |
| 4264 | |
| 4265 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4266 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4267 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4268 | ++it) { |
| 4269 | if (it->get() == channel) { |
| 4270 | if (channel->id() >= 0) { |
| 4271 | sid_allocator_.ReleaseSid(channel->id()); |
| 4272 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4273 | // Since this method is triggered by a signal from the DataChannel, |
| 4274 | // we can't free it directly here; we need to free it asynchronously. |
| 4275 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4276 | sctp_data_channels_.erase(it); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 4277 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4278 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4279 | return; |
| 4280 | } |
| 4281 | } |
| 4282 | } |
| 4283 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4284 | void PeerConnection::OnDataChannelDestroyed() { |
| 4285 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4286 | // DataChannel may callback to us and try to modify the list. |
| 4287 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4288 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4289 | for (const auto& kv : temp_rtp_dcs) { |
| 4290 | kv.second->OnTransportChannelDestroyed(); |
| 4291 | } |
| 4292 | |
| 4293 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4294 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4295 | for (const auto& channel : temp_sctp_dcs) { |
| 4296 | channel->OnTransportChannelDestroyed(); |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | void PeerConnection::OnDataChannelOpenMessage( |
| 4301 | const std::string& label, |
| 4302 | const InternalDataChannelInit& config) { |
| 4303 | rtc::scoped_refptr<DataChannel> channel( |
| 4304 | InternalCreateDataChannel(label, &config)); |
| 4305 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4306 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4307 | return; |
| 4308 | } |
| 4309 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4310 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4311 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4312 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4313 | } |
| 4314 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4315 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4316 | PeerConnection::GetAudioTransceiver() const { |
| 4317 | // This method only works with Plan B SDP, where there is a single |
| 4318 | // audio/video transceiver. |
| 4319 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4320 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4321 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4322 | return transceiver; |
| 4323 | } |
| 4324 | } |
| 4325 | RTC_NOTREACHED(); |
| 4326 | return nullptr; |
| 4327 | } |
| 4328 | |
| 4329 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4330 | PeerConnection::GetVideoTransceiver() const { |
| 4331 | // This method only works with Plan B SDP, where there is a single |
| 4332 | // audio/video transceiver. |
| 4333 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4334 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4335 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4336 | return transceiver; |
| 4337 | } |
| 4338 | } |
| 4339 | RTC_NOTREACHED(); |
| 4340 | return nullptr; |
| 4341 | } |
| 4342 | |
| 4343 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4344 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4345 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4346 | switch (type) { |
| 4347 | case cricket::MEDIA_TYPE_AUDIO: |
| 4348 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4349 | case cricket::MEDIA_TYPE_VIDEO: |
| 4350 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4351 | case cricket::MEDIA_TYPE_DATA: |
| 4352 | return false; |
| 4353 | } |
| 4354 | RTC_NOTREACHED(); |
| 4355 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4356 | } |
| 4357 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4358 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4359 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 4360 | for (auto transceiver : transceivers_) { |
| 4361 | for (auto sender : transceiver->internal()->senders()) { |
| 4362 | if (sender->track() == track) { |
| 4363 | return sender; |
| 4364 | } |
| 4365 | } |
| 4366 | } |
| 4367 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4368 | } |
| 4369 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4370 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4371 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 4372 | for (auto transceiver : transceivers_) { |
| 4373 | for (auto sender : transceiver->internal()->senders()) { |
| 4374 | if (sender->id() == sender_id) { |
| 4375 | return sender; |
| 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4380 | } |
| 4381 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4382 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4383 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 4384 | for (auto transceiver : transceivers_) { |
| 4385 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4386 | if (receiver->id() == receiver_id) { |
| 4387 | return receiver; |
| 4388 | } |
| 4389 | } |
| 4390 | } |
| 4391 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4394 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4395 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4396 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4397 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4398 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4399 | ? &remote_audio_sender_infos_ |
| 4400 | : &remote_video_sender_infos_; |
| 4401 | } |
| 4402 | |
| 4403 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4404 | cricket::MediaType media_type) { |
| 4405 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4406 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4407 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4408 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4409 | } |
| 4410 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4411 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4412 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4413 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4414 | const std::string sender_id) const { |
| 4415 | for (const RtpSenderInfo& sender_info : infos) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4416 | if (sender_info.stream_id == stream_id && |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4417 | sender_info.sender_id == sender_id) { |
| 4418 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4419 | } |
| 4420 | } |
| 4421 | return nullptr; |
| 4422 | } |
| 4423 | |
| 4424 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4425 | for (const auto& channel : sctp_data_channels_) { |
| 4426 | if (channel->id() == sid) { |
| 4427 | return channel; |
| 4428 | } |
| 4429 | } |
| 4430 | return nullptr; |
| 4431 | } |
| 4432 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4433 | bool PeerConnection::InitializePortAllocator_n( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4434 | const RTCConfiguration& configuration) { |
| 4435 | cricket::ServerAddresses stun_servers; |
| 4436 | std::vector<cricket::RelayServerConfig> turn_servers; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4437 | if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) != |
| 4438 | RTCErrorType::NONE) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4439 | return false; |
| 4440 | } |
| 4441 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 4442 | port_allocator_->Initialize(); |
| 4443 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4444 | // To handle both internal and externally created port allocator, we will |
| 4445 | // enable BUNDLE here. |
| 4446 | int portallocator_flags = port_allocator_->flags(); |
| 4447 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4448 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 4449 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4450 | // If the disable-IPv6 flag was specified, we'll not override it |
| 4451 | // by experiment. |
| 4452 | if (configuration.disable_ipv6) { |
| 4453 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 4454 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 4455 | .find("Disabled") == 0) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4456 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
| 4457 | } |
| 4458 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4459 | if (configuration.disable_ipv6_on_wifi) { |
| 4460 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4461 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4464 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 4465 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4466 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4467 | } |
| 4468 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4469 | if (configuration.candidate_network_policy == |
| 4470 | kCandidateNetworkPolicyLowCost) { |
| 4471 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4472 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4473 | } |
| 4474 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4475 | if (configuration.disable_link_local_networks) { |
| 4476 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
| 4477 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 4478 | } |
| 4479 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4480 | port_allocator_->set_flags(portallocator_flags); |
| 4481 | // No step delay is used while allocating ports. |
| 4482 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 4483 | port_allocator_->set_candidate_filter( |
| 4484 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 4485 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4486 | |
| 4487 | // Call this last since it may create pooled allocator sessions using the |
| 4488 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4489 | port_allocator_->SetConfiguration( |
| 4490 | stun_servers, turn_servers, configuration.ice_candidate_pool_size, |
| 4491 | configuration.prune_turn_ports, configuration.turn_customizer, |
| 4492 | configuration.stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4493 | return true; |
| 4494 | } |
| 4495 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4496 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4497 | const cricket::ServerAddresses& stun_servers, |
| 4498 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 4499 | IceTransportsType type, |
| 4500 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4501 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4502 | webrtc::TurnCustomizer* turn_customizer, |
| 4503 | rtc::Optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4504 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4505 | ConvertIceTransportTypeToCandidateFilter(type)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4506 | // Call this last since it may create pooled allocator sessions using the |
| 4507 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 4508 | return port_allocator_->SetConfiguration( |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4509 | stun_servers, turn_servers, candidate_pool_size, prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4510 | turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4511 | } |
| 4512 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4513 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 4514 | return factory_->channel_manager(); |
| 4515 | } |
| 4516 | |
| 4517 | MetricsObserverInterface* PeerConnection::metrics_observer() const { |
| 4518 | return uma_observer_; |
| 4519 | } |
| 4520 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 4521 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4522 | std::unique_ptr<RtcEventLogOutput> output, |
| 4523 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4524 | if (!event_log_) { |
| 4525 | return false; |
| 4526 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4527 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4531 | if (event_log_) { |
| 4532 | event_log_->StopLogging(); |
| 4533 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4534 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4535 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4536 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 4537 | const std::string& content_name) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4538 | for (auto transceiver : transceivers_) { |
| 4539 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4540 | if (channel && channel->content_name() == content_name) { |
| 4541 | return channel; |
| 4542 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4543 | } |
| 4544 | if (rtp_data_channel() && |
| 4545 | rtp_data_channel()->content_name() == content_name) { |
| 4546 | return rtp_data_channel(); |
| 4547 | } |
| 4548 | return nullptr; |
| 4549 | } |
| 4550 | |
| 4551 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 4552 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4553 | RTC_LOG(LS_INFO) |
| 4554 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4555 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4556 | return false; |
| 4557 | } |
| 4558 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4559 | 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] | 4560 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4561 | return false; |
| 4562 | } |
| 4563 | |
| 4564 | return transport_controller_->GetSslRole(*sctp_transport_name_, role); |
| 4565 | } |
| 4566 | |
| 4567 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 4568 | rtc::SSLRole* role) { |
| 4569 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4570 | RTC_LOG(LS_INFO) |
| 4571 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4572 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4573 | return false; |
| 4574 | } |
| 4575 | |
| 4576 | return transport_controller_->GetSslRole(GetTransportName(content_name), |
| 4577 | role); |
| 4578 | } |
| 4579 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4580 | void PeerConnection::SetSessionError(SessionError error, |
| 4581 | const std::string& error_desc) { |
| 4582 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4583 | if (error != session_error_) { |
| 4584 | session_error_ = error; |
| 4585 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4586 | } |
| 4587 | } |
| 4588 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4589 | RTCError PeerConnection::UpdateSessionState(SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4590 | cricket::ContentSource source) { |
| 4591 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4592 | |
| 4593 | // If there's already a pending error then no state transition should happen. |
| 4594 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4595 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4596 | |
| 4597 | // If this is an answer then we know whether to BUNDLE or not. If both the |
| 4598 | // local and remote side have agreed to BUNDLE, go ahead and enable it. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4599 | if (type == SdpType::kAnswer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4600 | const cricket::ContentGroup* local_bundle = |
| 4601 | local_description()->description()->GetGroupByName( |
| 4602 | cricket::GROUP_TYPE_BUNDLE); |
| 4603 | const cricket::ContentGroup* remote_bundle = |
| 4604 | remote_description()->description()->GetGroupByName( |
| 4605 | cricket::GROUP_TYPE_BUNDLE); |
| 4606 | if (local_bundle && remote_bundle) { |
| 4607 | // The answerer decides the transport to bundle on. |
| 4608 | const cricket::ContentGroup* answer_bundle = |
| 4609 | (source == cricket::CS_LOCAL ? local_bundle : remote_bundle); |
| 4610 | if (!EnableBundle(*answer_bundle)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4611 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4612 | kEnableBundleFailed); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4613 | } |
| 4614 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4615 | } |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4616 | |
| 4617 | // Only push down the transport description after potentially enabling BUNDLE; |
| 4618 | // we don't want to push down a description on a transport about to be |
| 4619 | // destroyed. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4620 | RTCError error = PushdownTransportDescription(source, type); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4621 | if (!error.ok()) { |
| 4622 | return error; |
| 4623 | } |
| 4624 | |
| 4625 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4626 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4627 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | // Update the signaling state according to the specified state machine (see |
| 4631 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4632 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4633 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4634 | ? PeerConnectionInterface::kHaveLocalOffer |
| 4635 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4636 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4637 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4638 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 4639 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4640 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4641 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4642 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 4643 | } |
| 4644 | |
| 4645 | // Update internal objects according to the session description's media |
| 4646 | // descriptions. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4647 | error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4648 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 4649 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4650 | } |
| 4651 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4652 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4653 | } |
| 4654 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4655 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4656 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4657 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4658 | const SessionDescriptionInterface* sdesc = |
| 4659 | (source == cricket::CS_LOCAL ? local_description() |
| 4660 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4661 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4662 | |
| 4663 | // Push down the new SDP media section for each audio/video transceiver. |
| 4664 | for (auto transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4665 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4666 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4667 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4668 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4669 | continue; |
| 4670 | } |
| 4671 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4672 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4673 | if (!content_desc) { |
| 4674 | continue; |
| 4675 | } |
| 4676 | std::string error; |
| 4677 | bool success = |
| 4678 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4679 | ? channel->SetLocalContent(content_desc, type, &error) |
| 4680 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4681 | if (!success) { |
| 4682 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, std::move(error)); |
| 4683 | } |
| 4684 | } |
| 4685 | |
| 4686 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 4687 | if (rtp_data_channel_) { |
| 4688 | const ContentInfo* data_content = |
| 4689 | cricket::GetFirstDataContent(sdesc->description()); |
| 4690 | if (data_content && !data_content->rejected) { |
| 4691 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4692 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4693 | if (data_desc) { |
| 4694 | std::string error; |
| 4695 | bool success = |
| 4696 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4697 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
| 4698 | : rtp_data_channel_->SetRemoteContent(data_desc, type, |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4699 | &error); |
| 4700 | if (!success) { |
| 4701 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4702 | std::move(error)); |
| 4703 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4704 | } |
| 4705 | } |
| 4706 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4707 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4708 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 4709 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 4710 | if (sctp_transport_ && local_description() && remote_description() && |
| 4711 | cricket::GetFirstDataContent(local_description()->description()) && |
| 4712 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4713 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4714 | RTC_FROM_HERE, |
| 4715 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4716 | if (!success) { |
| 4717 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4718 | "Failed to push down SCTP parameters."); |
| 4719 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4720 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4721 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4722 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 4726 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4727 | RTC_DCHECK(local_description()); |
| 4728 | RTC_DCHECK(remote_description()); |
| 4729 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 4730 | // When we support "max-message-size", that would also be pushed down here. |
| 4731 | return sctp_transport_->Start( |
| 4732 | GetSctpPort(local_description()->description()), |
| 4733 | GetSctpPort(remote_description()->description())); |
| 4734 | } |
| 4735 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4736 | RTCError PeerConnection::PushdownTransportDescription( |
| 4737 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4738 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4739 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4740 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4741 | const SessionDescriptionInterface* sdesc = |
| 4742 | (source == cricket::CS_LOCAL ? local_description() |
| 4743 | : remote_description()); |
| 4744 | RTC_DCHECK(sdesc); |
| 4745 | for (const cricket::TransportInfo& tinfo : |
| 4746 | sdesc->description()->transport_infos()) { |
| 4747 | std::string error; |
| 4748 | bool success; |
| 4749 | if (source == cricket::CS_LOCAL) { |
| 4750 | success = transport_controller_->SetLocalTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4751 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4752 | } else { |
| 4753 | success = transport_controller_->SetRemoteTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4754 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4755 | } |
| 4756 | if (!success) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4757 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4758 | "Failed to push down transport description for " + |
| 4759 | tinfo.content_name + ": " + error); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4760 | } |
| 4761 | } |
| 4762 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4763 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | bool PeerConnection::GetTransportDescription( |
| 4767 | const SessionDescription* description, |
| 4768 | const std::string& content_name, |
| 4769 | cricket::TransportDescription* tdesc) { |
| 4770 | if (!description || !tdesc) { |
| 4771 | return false; |
| 4772 | } |
| 4773 | const TransportInfo* transport_info = |
| 4774 | description->GetTransportInfoByName(content_name); |
| 4775 | if (!transport_info) { |
| 4776 | return false; |
| 4777 | } |
| 4778 | *tdesc = transport_info->description; |
| 4779 | return true; |
| 4780 | } |
| 4781 | |
| 4782 | bool PeerConnection::EnableBundle(const cricket::ContentGroup& bundle) { |
| 4783 | const std::string* first_content_name = bundle.FirstContentName(); |
| 4784 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4785 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4786 | return false; |
| 4787 | } |
| 4788 | const std::string& transport_name = *first_content_name; |
| 4789 | |
| 4790 | auto maybe_set_transport = [this, bundle, |
| 4791 | transport_name](cricket::BaseChannel* ch) { |
| 4792 | if (!ch || !bundle.HasContentName(ch->content_name())) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4793 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | std::string old_transport_name = ch->transport_name(); |
| 4797 | if (old_transport_name == transport_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4798 | RTC_LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name() |
| 4799 | << " on " << transport_name << "."; |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4800 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 4804 | transport_controller_->CreateDtlsTransport( |
| 4805 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4806 | bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr); |
| 4807 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 4808 | if (need_rtcp) { |
| 4809 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 4810 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4811 | } |
| 4812 | |
| 4813 | ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4814 | RTC_LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on " |
| 4815 | << transport_name << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4816 | transport_controller_->DestroyDtlsTransport( |
| 4817 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4818 | // If the channel needs rtcp, it means that the channel used to have a |
| 4819 | // rtcp transport which needs to be deleted now. |
| 4820 | if (need_rtcp) { |
| 4821 | transport_controller_->DestroyDtlsTransport( |
| 4822 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4823 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4824 | }; |
| 4825 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4826 | for (auto transceiver : transceivers_) { |
| 4827 | maybe_set_transport(transceiver->internal()->channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4828 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4829 | maybe_set_transport(rtp_data_channel_); |
| 4830 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4831 | // For SCTP, transport creation/deletion happens here instead of in the |
| 4832 | // object itself. |
| 4833 | if (sctp_transport_) { |
| 4834 | RTC_DCHECK(sctp_transport_name_); |
| 4835 | RTC_DCHECK(sctp_content_name_); |
| 4836 | if (transport_name != *sctp_transport_name_ && |
| 4837 | bundle.HasContentName(*sctp_content_name_)) { |
| 4838 | network_thread()->Invoke<void>( |
| 4839 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::ChangeSctpTransport_n, this, |
| 4840 | transport_name)); |
| 4841 | } |
| 4842 | } |
| 4843 | |
| 4844 | return true; |
| 4845 | } |
| 4846 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4847 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 4848 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 4849 | cricket::ContinualGatheringPolicy gathering_policy; |
| 4850 | // TODO(honghaiz): Add the third continual gathering policy in |
| 4851 | // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER. |
| 4852 | switch (config.continual_gathering_policy) { |
| 4853 | case PeerConnectionInterface::GATHER_ONCE: |
| 4854 | gathering_policy = cricket::GATHER_ONCE; |
| 4855 | break; |
| 4856 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 4857 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 4858 | break; |
| 4859 | default: |
| 4860 | RTC_NOTREACHED(); |
| 4861 | gathering_policy = cricket::GATHER_ONCE; |
| 4862 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4863 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4864 | cricket::IceConfig ice_config; |
| 4865 | ice_config.receiving_timeout = config.ice_connection_receiving_timeout; |
| 4866 | ice_config.prioritize_most_likely_candidate_pairs = |
| 4867 | config.prioritize_most_likely_ice_candidate_pairs; |
| 4868 | ice_config.backup_connection_ping_interval = |
| 4869 | config.ice_backup_candidate_pair_ping_interval; |
| 4870 | ice_config.continual_gathering_policy = gathering_policy; |
| 4871 | ice_config.presume_writable_when_fully_relayed = |
| 4872 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame^] | 4873 | ice_config.ice_check_interval_strong_connectivity = |
| 4874 | config.ice_check_interval_strong_connectivity; |
| 4875 | ice_config.ice_check_interval_weak_connectivity = |
| 4876 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4877 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4878 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4879 | ice_config.regather_all_networks_interval_range = |
| 4880 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4881 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4882 | return ice_config; |
| 4883 | } |
| 4884 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4885 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 4886 | std::string* track_id) { |
| 4887 | if (!local_description()) { |
| 4888 | return false; |
| 4889 | } |
| 4890 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 4891 | track_id); |
| 4892 | } |
| 4893 | |
| 4894 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 4895 | std::string* track_id) { |
| 4896 | if (!remote_description()) { |
| 4897 | return false; |
| 4898 | } |
| 4899 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 4900 | track_id); |
| 4901 | } |
| 4902 | |
| 4903 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 4904 | const rtc::CopyOnWriteBuffer& payload, |
| 4905 | cricket::SendDataResult* result) { |
| 4906 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4907 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4908 | "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4909 | return false; |
| 4910 | } |
| 4911 | return rtp_data_channel_ |
| 4912 | ? rtp_data_channel_->SendData(params, payload, result) |
| 4913 | : network_thread()->Invoke<bool>( |
| 4914 | RTC_FROM_HERE, |
| 4915 | Bind(&cricket::SctpTransportInternal::SendData, |
| 4916 | sctp_transport_.get(), params, payload, result)); |
| 4917 | } |
| 4918 | |
| 4919 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 4920 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 4921 | // Don't log an error here, because DataChannels are expected to call |
| 4922 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 4923 | // whether or not the underlying transport is ready. |
| 4924 | return false; |
| 4925 | } |
| 4926 | if (rtp_data_channel_) { |
| 4927 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 4928 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 4929 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 4930 | &DataChannel::OnDataReceived); |
| 4931 | } else { |
| 4932 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 4933 | &DataChannel::OnChannelReady); |
| 4934 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 4935 | &DataChannel::OnDataReceived); |
| 4936 | SignalSctpStreamClosedRemotely.connect( |
| 4937 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
| 4938 | } |
| 4939 | return true; |
| 4940 | } |
| 4941 | |
| 4942 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 4943 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4944 | RTC_LOG(LS_ERROR) |
| 4945 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 4946 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4947 | return; |
| 4948 | } |
| 4949 | if (rtp_data_channel_) { |
| 4950 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 4951 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 4952 | } else { |
| 4953 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 4954 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
| 4955 | SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel); |
| 4956 | } |
| 4957 | } |
| 4958 | |
| 4959 | void PeerConnection::AddSctpDataStream(int sid) { |
| 4960 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4961 | RTC_LOG(LS_ERROR) |
| 4962 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4963 | return; |
| 4964 | } |
| 4965 | network_thread()->Invoke<void>( |
| 4966 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 4967 | sctp_transport_.get(), sid)); |
| 4968 | } |
| 4969 | |
| 4970 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 4971 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4972 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4973 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4974 | return; |
| 4975 | } |
| 4976 | network_thread()->Invoke<void>( |
| 4977 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 4978 | sctp_transport_.get(), sid)); |
| 4979 | } |
| 4980 | |
| 4981 | bool PeerConnection::ReadyToSendData() const { |
| 4982 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 4983 | sctp_ready_to_send_data_; |
| 4984 | } |
| 4985 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 4986 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 4987 | cricket::CandidateStatsList candidate_states_list; |
| 4988 | port_allocator_->GetCandidateStatsFromPooledSessions(&candidate_states_list); |
| 4989 | return candidate_states_list; |
| 4990 | } |
| 4991 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4992 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 4993 | const { |
| 4994 | std::map<std::string, std::string> transport_names_by_mid; |
| 4995 | for (auto transceiver : transceivers_) { |
| 4996 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4997 | if (channel) { |
| 4998 | transport_names_by_mid[channel->content_name()] = |
| 4999 | channel->transport_name(); |
| 5000 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5001 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5002 | if (rtp_data_channel_) { |
| 5003 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5004 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5005 | } |
| 5006 | if (sctp_transport_) { |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5007 | transport_names_by_mid[*sctp_content_name_] = *sctp_transport_name_; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5008 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5009 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5010 | } |
| 5011 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5012 | std::map<std::string, cricket::TransportStats> |
| 5013 | PeerConnection::GetTransportStatsByNames( |
| 5014 | const std::set<std::string>& transport_names) { |
| 5015 | if (!network_thread()->IsCurrent()) { |
| 5016 | return network_thread() |
| 5017 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5018 | RTC_FROM_HERE, |
| 5019 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5020 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5021 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5022 | for (const std::string& transport_name : transport_names) { |
| 5023 | cricket::TransportStats transport_stats; |
| 5024 | bool success = |
| 5025 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5026 | if (success) { |
| 5027 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5028 | } else { |
| 5029 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5030 | << transport_name; |
| 5031 | } |
| 5032 | } |
| 5033 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5034 | } |
| 5035 | |
| 5036 | bool PeerConnection::GetLocalCertificate( |
| 5037 | const std::string& transport_name, |
| 5038 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 5039 | return transport_controller_->GetLocalCertificate(transport_name, |
| 5040 | certificate); |
| 5041 | } |
| 5042 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5043 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5044 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5045 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5046 | } |
| 5047 | |
| 5048 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 5049 | return data_channel_type_; |
| 5050 | } |
| 5051 | |
| 5052 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5053 | return pending_ice_restarts_.find(content_name) != |
| 5054 | pending_ice_restarts_.end(); |
| 5055 | } |
| 5056 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5057 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5058 | return transport_controller_->NeedsIceRestart(content_name); |
| 5059 | } |
| 5060 | |
| 5061 | void PeerConnection::OnCertificateReady( |
| 5062 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5063 | transport_controller_->SetLocalCertificate(certificate); |
| 5064 | } |
| 5065 | |
| 5066 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5067 | SetSessionError(SessionError::kTransport, |
| 5068 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5069 | } |
| 5070 | |
| 5071 | void PeerConnection::OnTransportControllerConnectionState( |
| 5072 | cricket::IceConnectionState state) { |
| 5073 | switch (state) { |
| 5074 | case cricket::kIceConnectionConnecting: |
| 5075 | // If the current state is Connected or Completed, then there were |
| 5076 | // writable channels but now there are not, so the next state must |
| 5077 | // be Disconnected. |
| 5078 | // kIceConnectionConnecting is currently used as the default, |
| 5079 | // un-connected state by the TransportController, so its only use is |
| 5080 | // detecting disconnections. |
| 5081 | if (ice_connection_state_ == |
| 5082 | PeerConnectionInterface::kIceConnectionConnected || |
| 5083 | ice_connection_state_ == |
| 5084 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5085 | SetIceConnectionState( |
| 5086 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5087 | } |
| 5088 | break; |
| 5089 | case cricket::kIceConnectionFailed: |
| 5090 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5091 | break; |
| 5092 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5093 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5094 | "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5095 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5096 | break; |
| 5097 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5098 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5099 | "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5100 | if (ice_connection_state_ != |
| 5101 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5102 | // If jumping directly from "checking" to "connected", |
| 5103 | // signal "connected" first. |
| 5104 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5105 | } |
| 5106 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 5107 | if (metrics_observer()) { |
| 5108 | ReportTransportStats(); |
| 5109 | } |
| 5110 | break; |
| 5111 | default: |
| 5112 | RTC_NOTREACHED(); |
| 5113 | } |
| 5114 | } |
| 5115 | |
| 5116 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5117 | const std::string& transport_name, |
| 5118 | const cricket::Candidates& candidates) { |
| 5119 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5120 | int sdp_mline_index; |
| 5121 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5122 | RTC_LOG(LS_ERROR) |
| 5123 | << "OnTransportControllerCandidatesGathered: content name " |
| 5124 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5125 | return; |
| 5126 | } |
| 5127 | |
| 5128 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5129 | citer != candidates.end(); ++citer) { |
| 5130 | // Use transport_name as the candidate media id. |
| 5131 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5132 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5133 | if (local_description()) { |
| 5134 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5135 | } |
| 5136 | OnIceCandidate(std::move(candidate)); |
| 5137 | } |
| 5138 | } |
| 5139 | |
| 5140 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5141 | const std::vector<cricket::Candidate>& candidates) { |
| 5142 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5143 | // Sanity check. |
| 5144 | for (const cricket::Candidate& candidate : candidates) { |
| 5145 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5146 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5147 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5148 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5149 | return; |
| 5150 | } |
| 5151 | } |
| 5152 | |
| 5153 | if (local_description()) { |
| 5154 | mutable_local_description()->RemoveCandidates(candidates); |
| 5155 | } |
| 5156 | OnIceCandidatesRemoved(candidates); |
| 5157 | } |
| 5158 | |
| 5159 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5160 | rtc::SSLHandshakeError error) { |
| 5161 | if (metrics_observer()) { |
| 5162 | metrics_observer()->IncrementEnumCounter( |
| 5163 | webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error), |
| 5164 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
| 5165 | } |
| 5166 | } |
| 5167 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5168 | void PeerConnection::EnableSending() { |
| 5169 | for (auto transceiver : transceivers_) { |
| 5170 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5171 | if (channel && !channel->enabled()) { |
| 5172 | channel->Enable(true); |
| 5173 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5174 | } |
| 5175 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5176 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5177 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5178 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | // Returns the media index for a local ice candidate given the content name. |
| 5182 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5183 | const std::string& content_name, |
| 5184 | int* sdp_mline_index) { |
| 5185 | if (!local_description() || !sdp_mline_index) { |
| 5186 | return false; |
| 5187 | } |
| 5188 | |
| 5189 | bool content_found = false; |
| 5190 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5191 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5192 | if (contents[index].name == content_name) { |
| 5193 | *sdp_mline_index = static_cast<int>(index); |
| 5194 | content_found = true; |
| 5195 | break; |
| 5196 | } |
| 5197 | } |
| 5198 | return content_found; |
| 5199 | } |
| 5200 | |
| 5201 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5202 | const SessionDescriptionInterface* remote_desc) { |
| 5203 | if (!remote_desc) { |
| 5204 | return true; |
| 5205 | } |
| 5206 | bool ret = true; |
| 5207 | |
| 5208 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5209 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5210 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5211 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5212 | bool valid = false; |
| 5213 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5214 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5215 | RTC_LOG(LS_INFO) |
| 5216 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5217 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5218 | } |
| 5219 | continue; |
| 5220 | } |
| 5221 | ret = UseCandidate(candidate); |
| 5222 | if (!ret) { |
| 5223 | break; |
| 5224 | } |
| 5225 | } |
| 5226 | } |
| 5227 | return ret; |
| 5228 | } |
| 5229 | |
| 5230 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5231 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5232 | size_t remote_content_size = |
| 5233 | remote_description()->description()->contents().size(); |
| 5234 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5235 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5236 | return false; |
| 5237 | } |
| 5238 | |
| 5239 | cricket::ContentInfo content = |
| 5240 | remote_description()->description()->contents()[mediacontent_index]; |
| 5241 | std::vector<cricket::Candidate> candidates; |
| 5242 | candidates.push_back(candidate->candidate()); |
| 5243 | // Invoking BaseSession method to handle remote candidates. |
| 5244 | std::string error; |
| 5245 | if (transport_controller_->AddRemoteCandidates(content.name, candidates, |
| 5246 | &error)) { |
| 5247 | // Candidates successfully submitted for checking. |
| 5248 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5249 | ice_connection_state_ == |
| 5250 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5251 | // If state is New, then the session has just gotten its first remote ICE |
| 5252 | // candidates, so go to Checking. |
| 5253 | // If state is Disconnected, the session is re-using old candidates or |
| 5254 | // receiving additional ones, so go to Checking. |
| 5255 | // If state is Connected, stay Connected. |
| 5256 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5257 | // newly added transport, then the state actually _should_ move to |
| 5258 | // checking. Add a way to distinguish that case. |
| 5259 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5260 | } |
| 5261 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 5262 | } else { |
| 5263 | if (!error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5264 | RTC_LOG(LS_WARNING) << error; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5265 | } |
| 5266 | } |
| 5267 | return true; |
| 5268 | } |
| 5269 | |
| 5270 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5271 | // Destroy video channel first since it may have a pointer to the |
| 5272 | // voice channel. |
| 5273 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5274 | if (!video_info || video_info->rejected) { |
| 5275 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5276 | } |
| 5277 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5278 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5279 | if (!audio_info || audio_info->rejected) { |
| 5280 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5284 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5285 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5286 | } |
| 5287 | } |
| 5288 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5289 | std::string PeerConnection::GetTransportNameForMediaSection( |
| 5290 | const std::string& mid, |
| 5291 | const cricket::ContentGroup* bundle_group) const { |
| 5292 | if (!bundle_group) { |
| 5293 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5294 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5295 | const std::string* first_content_name = bundle_group->FirstContentName(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5296 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5297 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5298 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5299 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5300 | if (!bundle_group->HasContentName(mid)) { |
| 5301 | RTC_LOG(LS_WARNING) << mid << " is not part of any bundle group"; |
| 5302 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5303 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5304 | RTC_LOG(LS_INFO) << "Bundling " << mid << " on " << *first_content_name; |
| 5305 | return *first_content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5306 | } |
| 5307 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5308 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5309 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5310 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5311 | if (configuration_.bundle_policy == |
| 5312 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5313 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5314 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5315 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5316 | "max-bundle configured but session description " |
| 5317 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5318 | } |
| 5319 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5320 | return std::move(bundle_group); |
| 5321 | } |
| 5322 | |
| 5323 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
| 5324 | auto bundle_group_or_error = GetEarlyBundleGroup(desc); |
| 5325 | if (!bundle_group_or_error.ok()) { |
| 5326 | return bundle_group_or_error.MoveError(); |
| 5327 | } |
| 5328 | const cricket::ContentGroup* bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5329 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5330 | // Creating the media channels and transport proxies. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5331 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5332 | if (voice && !voice->rejected && |
| 5333 | !GetAudioTransceiver()->internal()->channel()) { |
| 5334 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel( |
| 5335 | voice->name, |
| 5336 | GetTransportNameForMediaSection(voice->name, bundle_group)); |
| 5337 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5338 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5339 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5340 | } |
| 5341 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5342 | } |
| 5343 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5344 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5345 | if (video && !video->rejected && |
| 5346 | !GetVideoTransceiver()->internal()->channel()) { |
| 5347 | cricket::VideoChannel* video_channel = CreateVideoChannel( |
| 5348 | video->name, |
| 5349 | GetTransportNameForMediaSection(video->name, bundle_group)); |
| 5350 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5351 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5352 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5353 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5354 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5355 | } |
| 5356 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5357 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5358 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 5359 | !rtp_data_channel_ && !sctp_transport_) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5360 | if (!CreateDataChannel(data->name, GetTransportNameForMediaSection( |
| 5361 | data->name, bundle_group))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5362 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5363 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5364 | } |
| 5365 | } |
| 5366 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5367 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5368 | } |
| 5369 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5370 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5371 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
| 5372 | const std::string& mid, |
| 5373 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5374 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5375 | transport_controller_->CreateDtlsTransport( |
| 5376 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5377 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5378 | if (configuration_.rtcp_mux_policy != |
| 5379 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5380 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5381 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5382 | } |
| 5383 | |
| 5384 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
| 5385 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5386 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5387 | audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5388 | if (!voice_channel) { |
| 5389 | transport_controller_->DestroyDtlsTransport( |
| 5390 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5391 | if (rtcp_dtls_transport) { |
| 5392 | transport_controller_->DestroyDtlsTransport( |
| 5393 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5394 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5395 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5396 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5397 | voice_channel->SignalRtcpMuxFullyActive.connect( |
| 5398 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5399 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5400 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5401 | voice_channel->SignalSentPacket.connect(this, |
| 5402 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5403 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5404 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5405 | } |
| 5406 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5407 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5408 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
| 5409 | const std::string& mid, |
| 5410 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5411 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5412 | transport_controller_->CreateDtlsTransport( |
| 5413 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5414 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5415 | if (configuration_.rtcp_mux_policy != |
| 5416 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5417 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5418 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5419 | } |
| 5420 | |
| 5421 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
| 5422 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5423 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5424 | video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5425 | |
| 5426 | if (!video_channel) { |
| 5427 | transport_controller_->DestroyDtlsTransport( |
| 5428 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5429 | if (rtcp_dtls_transport) { |
| 5430 | transport_controller_->DestroyDtlsTransport( |
| 5431 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5432 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5433 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5434 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5435 | video_channel->SignalRtcpMuxFullyActive.connect( |
| 5436 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5437 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5438 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5439 | video_channel->SignalSentPacket.connect(this, |
| 5440 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5441 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5442 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5443 | } |
| 5444 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5445 | bool PeerConnection::CreateDataChannel(const std::string& mid, |
| 5446 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5447 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 5448 | if (sctp) { |
| 5449 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5450 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5451 | << "Trying to create SCTP transport, but didn't compile with " |
| 5452 | "SCTP support (HAVE_SCTP)"; |
| 5453 | return false; |
| 5454 | } |
| 5455 | if (!network_thread()->Invoke<bool>( |
| 5456 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::CreateSctpTransport_n, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5457 | this, mid, transport_name))) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5458 | return false; |
| 5459 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5460 | for (const auto& channel : sctp_data_channels_) { |
| 5461 | channel->OnTransportChannelCreated(); |
| 5462 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5463 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5464 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5465 | transport_controller_->CreateDtlsTransport( |
| 5466 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5467 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5468 | if (configuration_.rtcp_mux_policy != |
| 5469 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5470 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5471 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5472 | } |
| 5473 | |
| 5474 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 5475 | configuration_.media_config, rtp_dtls_transport, rtcp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5476 | signaling_thread(), mid, SrtpRequired()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5477 | |
| 5478 | if (!rtp_data_channel_) { |
| 5479 | transport_controller_->DestroyDtlsTransport( |
| 5480 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5481 | if (rtcp_dtls_transport) { |
| 5482 | transport_controller_->DestroyDtlsTransport( |
| 5483 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5484 | } |
| 5485 | return false; |
| 5486 | } |
| 5487 | |
| 5488 | rtp_data_channel_->SignalRtcpMuxFullyActive.connect( |
| 5489 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5490 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 5491 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 5492 | rtp_data_channel_->SignalSentPacket.connect( |
| 5493 | this, &PeerConnection::OnSentPacket_w); |
| 5494 | } |
| 5495 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5496 | return true; |
| 5497 | } |
| 5498 | |
| 5499 | Call::Stats PeerConnection::GetCallStats() { |
| 5500 | if (!worker_thread()->IsCurrent()) { |
| 5501 | return worker_thread()->Invoke<Call::Stats>( |
| 5502 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 5503 | } |
| 5504 | if (call_) { |
| 5505 | return call_->GetStats(); |
| 5506 | } else { |
| 5507 | return Call::Stats(); |
| 5508 | } |
| 5509 | } |
| 5510 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5511 | bool PeerConnection::CreateSctpTransport_n(const std::string& content_name, |
| 5512 | const std::string& transport_name) { |
| 5513 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5514 | RTC_DCHECK(sctp_factory_); |
| 5515 | cricket::DtlsTransportInternal* tc = |
| 5516 | transport_controller_->CreateDtlsTransport_n( |
| 5517 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5518 | sctp_transport_ = sctp_factory_->CreateSctpTransport(tc); |
| 5519 | RTC_DCHECK(sctp_transport_); |
| 5520 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 5521 | sctp_transport_->SignalReadyToSendData.connect( |
| 5522 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 5523 | sctp_transport_->SignalDataReceived.connect( |
| 5524 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
| 5525 | sctp_transport_->SignalStreamClosedRemotely.connect( |
| 5526 | this, &PeerConnection::OnSctpStreamClosedRemotely_n); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5527 | sctp_transport_name_ = transport_name; |
| 5528 | sctp_content_name_ = content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5529 | return true; |
| 5530 | } |
| 5531 | |
| 5532 | void PeerConnection::ChangeSctpTransport_n(const std::string& transport_name) { |
| 5533 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5534 | RTC_DCHECK(sctp_transport_); |
| 5535 | RTC_DCHECK(sctp_transport_name_); |
| 5536 | std::string old_sctp_transport_name = *sctp_transport_name_; |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5537 | sctp_transport_name_ = transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5538 | cricket::DtlsTransportInternal* tc = |
| 5539 | transport_controller_->CreateDtlsTransport_n( |
| 5540 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5541 | sctp_transport_->SetTransportChannel(tc); |
| 5542 | transport_controller_->DestroyDtlsTransport_n( |
| 5543 | old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5544 | } |
| 5545 | |
| 5546 | void PeerConnection::DestroySctpTransport_n() { |
| 5547 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5548 | sctp_transport_.reset(nullptr); |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 5549 | transport_controller_->DestroyDtlsTransport_n( |
| 5550 | *sctp_transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5551 | sctp_content_name_.reset(); |
| 5552 | sctp_transport_name_.reset(); |
| 5553 | sctp_invoker_.reset(nullptr); |
| 5554 | sctp_ready_to_send_data_ = false; |
| 5555 | } |
| 5556 | |
| 5557 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 5558 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5559 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5560 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5561 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5562 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5563 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5564 | // pending tasks will be cleared. |
| 5565 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 5566 | OnSctpTransportReadyToSendData_s(true); |
| 5567 | }); |
| 5568 | } |
| 5569 | |
| 5570 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 5571 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5572 | sctp_ready_to_send_data_ = ready; |
| 5573 | SignalSctpReadyToSendData(ready); |
| 5574 | } |
| 5575 | |
| 5576 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 5577 | const cricket::ReceiveDataParams& params, |
| 5578 | const rtc::CopyOnWriteBuffer& payload) { |
| 5579 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5580 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5581 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5582 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5583 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5584 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5585 | // pending tasks will be cleared. |
| 5586 | sctp_invoker_->AsyncInvoke<void>( |
| 5587 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 5588 | OnSctpTransportDataReceived_s(params, payload); |
| 5589 | }); |
| 5590 | } |
| 5591 | |
| 5592 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 5593 | const cricket::ReceiveDataParams& params, |
| 5594 | const rtc::CopyOnWriteBuffer& payload) { |
| 5595 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5596 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 5597 | // Received OPEN message; parse and signal that a new data channel should |
| 5598 | // be created. |
| 5599 | std::string label; |
| 5600 | InternalDataChannelInit config; |
| 5601 | config.id = params.ssrc; |
| 5602 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5603 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 5604 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5605 | return; |
| 5606 | } |
| 5607 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5608 | OnDataChannelOpenMessage(label, config); |
| 5609 | } else { |
| 5610 | // Otherwise just forward the signal. |
| 5611 | SignalSctpDataReceived(params, payload); |
| 5612 | } |
| 5613 | } |
| 5614 | |
| 5615 | void PeerConnection::OnSctpStreamClosedRemotely_n(int sid) { |
| 5616 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5617 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5618 | sctp_invoker_->AsyncInvoke<void>( |
| 5619 | RTC_FROM_HERE, signaling_thread(), |
| 5620 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 5621 | &SignalSctpStreamClosedRemotely, sid)); |
| 5622 | } |
| 5623 | |
| 5624 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 5625 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 5626 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 5627 | if (!bundle_enabled) |
| 5628 | return true; |
| 5629 | |
| 5630 | const cricket::ContentGroup* bundle_group = |
| 5631 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 5632 | RTC_DCHECK(bundle_group != NULL); |
| 5633 | |
| 5634 | const cricket::ContentInfos& contents = desc->contents(); |
| 5635 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 5636 | citer != contents.end(); ++citer) { |
| 5637 | const cricket::ContentInfo* content = (&*citer); |
| 5638 | RTC_DCHECK(content != NULL); |
| 5639 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 5640 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5641 | if (!HasRtcpMuxEnabled(content)) |
| 5642 | return false; |
| 5643 | } |
| 5644 | } |
| 5645 | // RTCP-MUX is enabled in all the contents. |
| 5646 | return true; |
| 5647 | } |
| 5648 | |
| 5649 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5650 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5651 | } |
| 5652 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5653 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5654 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5655 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5656 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5657 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5658 | } |
| 5659 | |
| 5660 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5661 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5662 | } |
| 5663 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5664 | SdpType type = sdesc->GetType(); |
| 5665 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 5666 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5667 | LOG_AND_RETURN_ERROR( |
| 5668 | RTCErrorType::INVALID_PARAMETER, |
| 5669 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5670 | } |
| 5671 | |
| 5672 | // Verify crypto settings. |
| 5673 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5674 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 5675 | dtls_enabled_) { |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 5676 | RTCError crypto_error = |
| 5677 | VerifyCrypto(sdesc->description(), dtls_enabled_, uma_observer_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5678 | if (!crypto_error.ok()) { |
| 5679 | return crypto_error; |
| 5680 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5681 | } |
| 5682 | |
| 5683 | // Verify ice-ufrag and ice-pwd. |
| 5684 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5685 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5686 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5687 | } |
| 5688 | |
| 5689 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5690 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5691 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 5695 | // m-lines that do not rtcp-mux enabled. |
| 5696 | |
| 5697 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5698 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5699 | // With an answer we want to compare the new answer session description with |
| 5700 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5701 | const cricket::SessionDescription* offer_desc = |
| 5702 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 5703 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5704 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 5705 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 5706 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5707 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5708 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5709 | } |
| 5710 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5711 | // The re-offers should respect the order of m= sections in current |
| 5712 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5713 | // With a re-offer, either the current local or current remote descriptions |
| 5714 | // could be the most up to date, so we would like to check against both of |
| 5715 | // them if they exist. It could be the case that one of them has a 0 port |
| 5716 | // for a media section, but the other does not. This is important to check |
| 5717 | // against in the case that we are recycling an m= section. |
| 5718 | const cricket::SessionDescription* current_desc = nullptr; |
| 5719 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 5720 | if (local_description()) { |
| 5721 | current_desc = local_description()->description(); |
| 5722 | if (remote_description()) { |
| 5723 | secondary_current_desc = remote_description()->description(); |
| 5724 | } |
| 5725 | } else if (remote_description()) { |
| 5726 | current_desc = remote_description()->description(); |
| 5727 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5728 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5729 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 5730 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5731 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5732 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5733 | } |
| 5734 | } |
| 5735 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5736 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5737 | } |
| 5738 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5739 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5740 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5741 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5742 | return (state == PeerConnectionInterface::kStable) || |
| 5743 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5744 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5745 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5746 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 5747 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 5748 | } |
| 5749 | } |
| 5750 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5751 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5752 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5753 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5754 | return (state == PeerConnectionInterface::kStable) || |
| 5755 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5756 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5757 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5758 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 5759 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5760 | } |
| 5761 | } |
| 5762 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5763 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 5764 | switch (error) { |
| 5765 | case SessionError::kNone: |
| 5766 | return "ERROR_NONE"; |
| 5767 | case SessionError::kContent: |
| 5768 | return "ERROR_CONTENT"; |
| 5769 | case SessionError::kTransport: |
| 5770 | return "ERROR_TRANSPORT"; |
| 5771 | } |
| 5772 | RTC_NOTREACHED(); |
| 5773 | return ""; |
| 5774 | } |
| 5775 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5776 | std::string PeerConnection::GetSessionErrorMsg() { |
| 5777 | std::ostringstream desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5778 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 5779 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5780 | return desc.str(); |
| 5781 | } |
| 5782 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 5783 | void PeerConnection::ReportSdpFormatReceived( |
| 5784 | const SessionDescriptionInterface& remote_offer) { |
| 5785 | if (!uma_observer_) { |
| 5786 | return; |
| 5787 | } |
| 5788 | int num_audio_mlines = 0; |
| 5789 | int num_video_mlines = 0; |
| 5790 | int num_audio_tracks = 0; |
| 5791 | int num_video_tracks = 0; |
| 5792 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 5793 | cricket::MediaType media_type = content.media_description()->type(); |
| 5794 | int num_tracks = std::max( |
| 5795 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 5796 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 5797 | num_audio_mlines += 1; |
| 5798 | num_audio_tracks += num_tracks; |
| 5799 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5800 | num_video_mlines += 1; |
| 5801 | num_video_tracks += num_tracks; |
| 5802 | } |
| 5803 | } |
| 5804 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 5805 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 5806 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 5807 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 5808 | format = kSdpFormatReceivedComplexPlanB; |
| 5809 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 5810 | format = kSdpFormatReceivedSimple; |
| 5811 | } |
| 5812 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpFormatReceived, format, |
| 5813 | kSdpFormatReceivedMax); |
| 5814 | } |
| 5815 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5816 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 5817 | const SessionDescriptionInterface& answer) { |
| 5818 | if (!uma_observer_) { |
| 5819 | return; |
| 5820 | } |
| 5821 | switch (answer.description()->msid_signaling()) { |
| 5822 | case 0: |
| 5823 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5824 | kSdpSemanticNegotiatedNone, |
| 5825 | kSdpSemanticNegotiatedMax); |
| 5826 | break; |
| 5827 | case cricket::kMsidSignalingMediaSection: |
| 5828 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5829 | kSdpSemanticNegotiatedUnifiedPlan, |
| 5830 | kSdpSemanticNegotiatedMax); |
| 5831 | break; |
| 5832 | case cricket::kMsidSignalingSsrcAttribute: |
| 5833 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5834 | kSdpSemanticNegotiatedPlanB, |
| 5835 | kSdpSemanticNegotiatedMax); |
| 5836 | break; |
| 5837 | case cricket::kMsidSignalingMediaSection | |
| 5838 | cricket::kMsidSignalingSsrcAttribute: |
| 5839 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5840 | kSdpSemanticNegotiatedMixed, |
| 5841 | kSdpSemanticNegotiatedMax); |
| 5842 | break; |
| 5843 | default: |
| 5844 | RTC_NOTREACHED(); |
| 5845 | } |
| 5846 | } |
| 5847 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5848 | // We need to check the local/remote description for the Transport instead of |
| 5849 | // the session, because a new Transport added during renegotiation may have |
| 5850 | // them unset while the session has them set from the previous negotiation. |
| 5851 | // Not doing so may trigger the auto generation of transport description and |
| 5852 | // mess up DTLS identity information, ICE credential, etc. |
| 5853 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 5854 | const IceCandidateInterface* candidate, |
| 5855 | const SessionDescriptionInterface* remote_desc, |
| 5856 | bool* valid) { |
| 5857 | *valid = true; |
| 5858 | |
| 5859 | const SessionDescriptionInterface* current_remote_desc = |
| 5860 | remote_desc ? remote_desc : remote_description(); |
| 5861 | |
| 5862 | if (!current_remote_desc) { |
| 5863 | return false; |
| 5864 | } |
| 5865 | |
| 5866 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5867 | size_t remote_content_size = |
| 5868 | current_remote_desc->description()->contents().size(); |
| 5869 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5870 | RTC_LOG(LS_ERROR) |
| 5871 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 5872 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5873 | |
| 5874 | *valid = false; |
| 5875 | return false; |
| 5876 | } |
| 5877 | |
| 5878 | cricket::ContentInfo content = |
| 5879 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 5880 | |
| 5881 | const std::string transport_name = GetTransportName(content.name); |
| 5882 | if (transport_name.empty()) { |
| 5883 | return false; |
| 5884 | } |
| 5885 | return transport_controller_->ReadyForRemoteCandidates(transport_name); |
| 5886 | } |
| 5887 | |
| 5888 | bool PeerConnection::SrtpRequired() const { |
| 5889 | return dtls_enabled_ || |
| 5890 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 5891 | } |
| 5892 | |
| 5893 | void PeerConnection::OnTransportControllerGatheringState( |
| 5894 | cricket::IceGatheringState state) { |
| 5895 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5896 | if (state == cricket::kIceGatheringGathering) { |
| 5897 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 5898 | } else if (state == cricket::kIceGatheringComplete) { |
| 5899 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 5900 | } |
| 5901 | } |
| 5902 | |
| 5903 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5904 | std::map<std::string, std::set<cricket::MediaType>> |
| 5905 | media_types_by_transport_name; |
| 5906 | for (auto transceiver : transceivers_) { |
| 5907 | if (transceiver->internal()->channel()) { |
| 5908 | const std::string& transport_name = |
| 5909 | transceiver->internal()->channel()->transport_name(); |
| 5910 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5911 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5912 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5913 | } |
| 5914 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5915 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 5916 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5917 | } |
| 5918 | if (sctp_transport_name_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5919 | media_types_by_transport_name[*sctp_transport_name_].insert( |
| 5920 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5921 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5922 | for (const auto& entry : media_types_by_transport_name) { |
| 5923 | const std::string& transport_name = entry.first; |
| 5924 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5925 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5926 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5927 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5928 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5929 | } |
| 5930 | } |
| 5931 | } |
| 5932 | // Walk through the ConnectionInfos to gather best connection usage |
| 5933 | // for IPv4 and IPv6. |
| 5934 | void PeerConnection::ReportBestConnectionState( |
| 5935 | const cricket::TransportStats& stats) { |
| 5936 | RTC_DCHECK(metrics_observer()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5937 | for (const cricket::TransportChannelStats& channel_stats : |
| 5938 | stats.channel_stats) { |
| 5939 | for (const cricket::ConnectionInfo& connection_info : |
| 5940 | channel_stats.connection_infos) { |
| 5941 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5942 | continue; |
| 5943 | } |
| 5944 | |
| 5945 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5946 | const cricket::Candidate& local = connection_info.local_candidate; |
| 5947 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5948 | |
| 5949 | // Increment the counter for IceCandidatePairType. |
| 5950 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 5951 | (local.type() == RELAY_PORT_TYPE && |
| 5952 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 5953 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 5954 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 5955 | type = kEnumCounterIceCandidatePairTypeUdp; |
| 5956 | } else { |
| 5957 | RTC_CHECK(0); |
| 5958 | } |
| 5959 | metrics_observer()->IncrementEnumCounter( |
| 5960 | type, GetIceCandidatePairCounter(local, remote), |
| 5961 | kIceCandidatePairMax); |
| 5962 | |
| 5963 | // Increment the counter for IP type. |
| 5964 | if (local.address().family() == AF_INET) { |
| 5965 | metrics_observer()->IncrementEnumCounter( |
| 5966 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 5967 | kPeerConnectionAddressFamilyCounter_Max); |
| 5968 | |
| 5969 | } else if (local.address().family() == AF_INET6) { |
| 5970 | metrics_observer()->IncrementEnumCounter( |
| 5971 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 5972 | kPeerConnectionAddressFamilyCounter_Max); |
| 5973 | } else { |
| 5974 | RTC_CHECK(0); |
| 5975 | } |
| 5976 | |
| 5977 | return; |
| 5978 | } |
| 5979 | } |
| 5980 | } |
| 5981 | |
| 5982 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5983 | const cricket::TransportStats& stats, |
| 5984 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5985 | RTC_DCHECK(metrics_observer()); |
| 5986 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 5987 | return; |
| 5988 | } |
| 5989 | |
| 5990 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 5991 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 5992 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 5993 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 5994 | return; |
| 5995 | } |
| 5996 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5997 | for (cricket::MediaType media_type : media_types) { |
| 5998 | PeerConnectionEnumCounterType srtp_counter_type; |
| 5999 | PeerConnectionEnumCounterType ssl_counter_type; |
| 6000 | switch (media_type) { |
| 6001 | case cricket::MEDIA_TYPE_AUDIO: |
| 6002 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 6003 | ssl_counter_type = kEnumCounterAudioSslCipher; |
| 6004 | break; |
| 6005 | case cricket::MEDIA_TYPE_VIDEO: |
| 6006 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 6007 | ssl_counter_type = kEnumCounterVideoSslCipher; |
| 6008 | break; |
| 6009 | case cricket::MEDIA_TYPE_DATA: |
| 6010 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 6011 | ssl_counter_type = kEnumCounterDataSslCipher; |
| 6012 | break; |
| 6013 | default: |
| 6014 | RTC_NOTREACHED(); |
| 6015 | continue; |
| 6016 | } |
| 6017 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6018 | metrics_observer()->IncrementSparseEnumCounter(srtp_counter_type, |
| 6019 | srtp_crypto_suite); |
| 6020 | } |
| 6021 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6022 | metrics_observer()->IncrementSparseEnumCounter(ssl_counter_type, |
| 6023 | ssl_cipher_suite); |
| 6024 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6025 | } |
| 6026 | } |
| 6027 | |
| 6028 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 6029 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 6030 | RTC_DCHECK(call_); |
| 6031 | call_->OnSentPacket(sent_packet); |
| 6032 | } |
| 6033 | |
| 6034 | const std::string PeerConnection::GetTransportName( |
| 6035 | const std::string& content_name) { |
| 6036 | cricket::BaseChannel* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6037 | if (channel) { |
| 6038 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6039 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6040 | if (sctp_transport_) { |
| 6041 | RTC_DCHECK(sctp_content_name_); |
| 6042 | RTC_DCHECK(sctp_transport_name_); |
| 6043 | if (content_name == *sctp_content_name_) { |
| 6044 | return *sctp_transport_name_; |
| 6045 | } |
| 6046 | } |
| 6047 | // Return an empty string if failed to retrieve the transport name. |
| 6048 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6049 | } |
| 6050 | |
| 6051 | void PeerConnection::DestroyRtcpTransport_n(const std::string& transport_name) { |
| 6052 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6053 | transport_controller_->DestroyDtlsTransport_n( |
| 6054 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 6055 | } |
| 6056 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6057 | void PeerConnection::DestroyTransceiverChannel( |
| 6058 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 6059 | transceiver) { |
| 6060 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6061 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6062 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 6063 | if (channel) { |
| 6064 | transceiver->internal()->SetChannel(nullptr); |
| 6065 | DestroyBaseChannel(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6066 | } |
| 6067 | } |
| 6068 | |
| 6069 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6070 | if (rtp_data_channel_) { |
| 6071 | OnDataChannelDestroyed(); |
| 6072 | DestroyBaseChannel(rtp_data_channel_); |
| 6073 | rtp_data_channel_ = nullptr; |
| 6074 | } |
| 6075 | |
| 6076 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 6077 | // grab a reference to this PeerConnection. If this is called from the |
| 6078 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 6079 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 6080 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 6081 | |
| 6082 | if (sctp_transport_) { |
| 6083 | OnDataChannelDestroyed(); |
| 6084 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 6085 | [this] { DestroySctpTransport_n(); }); |
| 6086 | } |
| 6087 | } |
| 6088 | |
| 6089 | void PeerConnection::DestroyBaseChannel(cricket::BaseChannel* channel) { |
| 6090 | RTC_DCHECK(channel); |
| 6091 | RTC_DCHECK(channel->rtp_dtls_transport()); |
| 6092 | |
| 6093 | // Need to cache these before destroying the base channel so that we do not |
| 6094 | // access uninitialized memory. |
| 6095 | const std::string transport_name = |
| 6096 | channel->rtp_dtls_transport()->transport_name(); |
| 6097 | const bool need_to_delete_rtcp = (channel->rtcp_dtls_transport() != nullptr); |
| 6098 | |
| 6099 | switch (channel->media_type()) { |
| 6100 | case cricket::MEDIA_TYPE_AUDIO: |
| 6101 | channel_manager()->DestroyVoiceChannel( |
| 6102 | static_cast<cricket::VoiceChannel*>(channel)); |
| 6103 | break; |
| 6104 | case cricket::MEDIA_TYPE_VIDEO: |
| 6105 | channel_manager()->DestroyVideoChannel( |
| 6106 | static_cast<cricket::VideoChannel*>(channel)); |
| 6107 | break; |
| 6108 | case cricket::MEDIA_TYPE_DATA: |
| 6109 | channel_manager()->DestroyRtpDataChannel( |
| 6110 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 6111 | break; |
| 6112 | default: |
| 6113 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 6114 | break; |
| 6115 | } |
| 6116 | |
| 6117 | // |channel| can no longer be used. |
| 6118 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6119 | transport_controller_->DestroyDtlsTransport( |
| 6120 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 6121 | if (need_to_delete_rtcp) { |
| 6122 | transport_controller_->DestroyDtlsTransport( |
| 6123 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 6124 | } |
| 6125 | } |
| 6126 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 6127 | void PeerConnection::ClearStatsCache() { |
| 6128 | if (stats_collector_) { |
| 6129 | stats_collector_->ClearCachedStatsReport(); |
| 6130 | } |
| 6131 | } |
| 6132 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6133 | } // namespace webrtc |