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."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 93 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 95 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 96 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 97 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 98 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 99 | // The length of RTCP CNAMEs. |
| 100 | static const int kRtcpCnameLength = 16; |
| 101 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 103 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 105 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | MSG_GETSTATS, |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 107 | MSG_FREE_DATACHANNELS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 110 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | explicit SetSessionDescriptionMsg( |
| 112 | webrtc::SetSessionDescriptionObserver* observer) |
| 113 | : observer(observer) { |
| 114 | } |
| 115 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 116 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 117 | RTCError error; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 120 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 121 | explicit CreateSessionDescriptionMsg( |
| 122 | webrtc::CreateSessionDescriptionObserver* observer) |
| 123 | : observer(observer) {} |
| 124 | |
| 125 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 126 | RTCError error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 129 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 130 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 131 | webrtc::MediaStreamTrackInterface* track) |
| 132 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 134 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 135 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 138 | // Check if we can send |new_stream| on a PeerConnection. |
| 139 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 140 | webrtc::MediaStreamInterface* new_stream) { |
| 141 | if (!new_stream || !current_streams) { |
| 142 | return false; |
| 143 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 144 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 145 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 146 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | return true; |
| 150 | } |
| 151 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 152 | // If the direction is "recvonly" or "inactive", treat the description |
| 153 | // as containing no streams. |
| 154 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 155 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 156 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 157 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 158 | ? desc->streams() |
| 159 | : std::vector<cricket::StreamParams>(); |
| 160 | } |
| 161 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 162 | bool IsValidOfferToReceiveMedia(int value) { |
| 163 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 164 | return (value >= Options::kUndefined) && |
| 165 | (value <= Options::kMaxOfferToReceiveMedia); |
| 166 | } |
| 167 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 168 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 169 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 170 | const std::vector<rtc::scoped_refptr< |
| 171 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 172 | cricket::MediaDescriptionOptions* audio_media_description_options, |
| 173 | cricket::MediaDescriptionOptions* video_media_description_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 174 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 175 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 176 | if (audio_media_description_options) { |
| 177 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 178 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 179 | } |
| 180 | } else { |
| 181 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 182 | if (video_media_description_options) { |
| 183 | video_media_description_options->AddVideoSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 184 | sender->id(), sender->internal()->stream_ids(), 1); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 187 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 188 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 189 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 190 | // Add options to |session_options| from |rtp_data_channels|. |
| 191 | void AddRtpDataChannelOptions( |
| 192 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 193 | rtp_data_channels, |
| 194 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 195 | if (!data_media_description_options) { |
| 196 | return; |
| 197 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 198 | // Check for data channels. |
| 199 | for (const auto& kv : rtp_data_channels) { |
| 200 | const DataChannel* channel = kv.second; |
| 201 | if (channel->state() == DataChannel::kConnecting || |
| 202 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 203 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 204 | // the data channel's label. |
| 205 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 206 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 211 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 212 | PeerConnectionInterface::IceTransportsType type) { |
| 213 | switch (type) { |
| 214 | case PeerConnectionInterface::kNone: |
| 215 | return cricket::CF_NONE; |
| 216 | case PeerConnectionInterface::kRelay: |
| 217 | return cricket::CF_RELAY; |
| 218 | case PeerConnectionInterface::kNoHost: |
| 219 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 220 | case PeerConnectionInterface::kAll: |
| 221 | return cricket::CF_ALL; |
| 222 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 223 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 224 | } |
| 225 | return cricket::CF_NONE; |
| 226 | } |
| 227 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 228 | // Helper to set an error and return from a method. |
| 229 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 230 | if (error) { |
| 231 | error->set_type(type); |
| 232 | } |
| 233 | return type == webrtc::RTCErrorType::NONE; |
| 234 | } |
| 235 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 236 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
| 237 | if (error_out) { |
| 238 | *error_out = std::move(error); |
| 239 | } |
| 240 | return error.ok(); |
| 241 | } |
| 242 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 243 | std::string GetSignalingStateString( |
| 244 | PeerConnectionInterface::SignalingState state) { |
| 245 | switch (state) { |
| 246 | case PeerConnectionInterface::kStable: |
| 247 | return "kStable"; |
| 248 | case PeerConnectionInterface::kHaveLocalOffer: |
| 249 | return "kHaveLocalOffer"; |
| 250 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 251 | return "kHavePrAnswer"; |
| 252 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 253 | return "kHaveRemoteOffer"; |
| 254 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 255 | return "kHaveRemotePrAnswer"; |
| 256 | case PeerConnectionInterface::kClosed: |
| 257 | return "kClosed"; |
| 258 | } |
| 259 | RTC_NOTREACHED(); |
| 260 | return ""; |
| 261 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 262 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 263 | IceCandidatePairType GetIceCandidatePairCounter( |
| 264 | const cricket::Candidate& local, |
| 265 | const cricket::Candidate& remote) { |
| 266 | const auto& l = local.type(); |
| 267 | const auto& r = remote.type(); |
| 268 | const auto& host = LOCAL_PORT_TYPE; |
| 269 | const auto& srflx = STUN_PORT_TYPE; |
| 270 | const auto& relay = RELAY_PORT_TYPE; |
| 271 | const auto& prflx = PRFLX_PORT_TYPE; |
| 272 | if (l == host && r == host) { |
| 273 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 274 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 275 | if (local_private) { |
| 276 | if (remote_private) { |
| 277 | return kIceCandidatePairHostPrivateHostPrivate; |
| 278 | } else { |
| 279 | return kIceCandidatePairHostPrivateHostPublic; |
| 280 | } |
| 281 | } else { |
| 282 | if (remote_private) { |
| 283 | return kIceCandidatePairHostPublicHostPrivate; |
| 284 | } else { |
| 285 | return kIceCandidatePairHostPublicHostPublic; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | if (l == host && r == srflx) |
| 290 | return kIceCandidatePairHostSrflx; |
| 291 | if (l == host && r == relay) |
| 292 | return kIceCandidatePairHostRelay; |
| 293 | if (l == host && r == prflx) |
| 294 | return kIceCandidatePairHostPrflx; |
| 295 | if (l == srflx && r == host) |
| 296 | return kIceCandidatePairSrflxHost; |
| 297 | if (l == srflx && r == srflx) |
| 298 | return kIceCandidatePairSrflxSrflx; |
| 299 | if (l == srflx && r == relay) |
| 300 | return kIceCandidatePairSrflxRelay; |
| 301 | if (l == srflx && r == prflx) |
| 302 | return kIceCandidatePairSrflxPrflx; |
| 303 | if (l == relay && r == host) |
| 304 | return kIceCandidatePairRelayHost; |
| 305 | if (l == relay && r == srflx) |
| 306 | return kIceCandidatePairRelaySrflx; |
| 307 | if (l == relay && r == relay) |
| 308 | return kIceCandidatePairRelayRelay; |
| 309 | if (l == relay && r == prflx) |
| 310 | return kIceCandidatePairRelayPrflx; |
| 311 | if (l == prflx && r == host) |
| 312 | return kIceCandidatePairPrflxHost; |
| 313 | if (l == prflx && r == srflx) |
| 314 | return kIceCandidatePairPrflxSrflx; |
| 315 | if (l == prflx && r == relay) |
| 316 | return kIceCandidatePairPrflxRelay; |
| 317 | return kIceCandidatePairMax; |
| 318 | } |
| 319 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 320 | // Logic to decide if an m= section can be recycled. This means that the new |
| 321 | // m= section is not rejected, but the old local or remote m= section is |
| 322 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 323 | // of the old remote and old local descriptions in no particular order. |
| 324 | // We need to check both the old local and remote because either |
| 325 | // could be the most current from the latest negotation. |
| 326 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 327 | const ContentInfo& content, |
| 328 | const ContentInfo* old_content_one, |
| 329 | const ContentInfo* old_content_two) { |
| 330 | return type == SdpType::kOffer && !content.rejected && |
| 331 | ((old_content_one && old_content_one->rejected) || |
| 332 | (old_content_two && old_content_two->rejected)); |
| 333 | } |
| 334 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 335 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 336 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 337 | // less than |current_desc|. In the case of checking an answer's |
| 338 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 339 | // local or remote. In the case of checking an offer's |new_desc| we |
| 340 | // check against the local and remote descriptions stored from the last |
| 341 | // negotiation, because either of these could be the most up to date for |
| 342 | // possible rejected m sections. These are the |current_desc| and |
| 343 | // |secondary_current_desc|. |
| 344 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 345 | const SessionDescription* secondary_current_desc, |
| 346 | const SessionDescription& new_desc, |
| 347 | const SdpType type) { |
| 348 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 352 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 353 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 354 | if (secondary_current_desc && |
| 355 | i < secondary_current_desc->contents().size()) { |
| 356 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 357 | } |
| 358 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 359 | ¤t_desc.contents()[i], |
| 360 | secondary_content_info)) { |
| 361 | // For new offer descriptions, if the media section can be recycled, it's |
| 362 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 363 | continue; |
| 364 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 365 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 369 | new_desc.contents()[i].media_description(); |
| 370 | const MediaContentDescription* current_desc_mdesc = |
| 371 | current_desc.contents()[i].media_description(); |
| 372 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | } |
| 376 | return true; |
| 377 | } |
| 378 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 379 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 380 | const SessionDescription& desc2) { |
| 381 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 384 | void NoteKeyProtocolAndMedia( |
| 385 | KeyExchangeProtocolType protocol_type, |
| 386 | cricket::MediaType media_type, |
| 387 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
| 388 | if (!uma_observer) |
| 389 | return; |
| 390 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocol, |
| 391 | protocol_type, |
| 392 | webrtc::kEnumCounterKeyProtocolMax); |
| 393 | static const std::map<std::pair<KeyExchangeProtocolType, cricket::MediaType>, |
| 394 | KeyExchangeProtocolMedia> |
| 395 | proto_media_counter_map = { |
| 396 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO}, |
| 397 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 398 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO}, |
| 399 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 400 | {{kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA}, |
| 401 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 402 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO}, |
| 403 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 404 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO}, |
| 405 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 406 | {{kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA}, |
| 407 | kEnumCounterKeyProtocolMediaTypeSdesData}}; |
| 408 | |
| 409 | auto it = proto_media_counter_map.find({protocol_type, media_type}); |
| 410 | if (it != proto_media_counter_map.end()) { |
| 411 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocolMediaType, |
| 412 | it->second, |
| 413 | kEnumCounterKeyProtocolMediaTypeMax); |
| 414 | } |
| 415 | } |
| 416 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 417 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 418 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 419 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 420 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 421 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 422 | // by Channel's |srtp_required| check. |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 423 | RTCError VerifyCrypto(const SessionDescription* desc, |
| 424 | bool dtls_enabled, |
| 425 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 426 | const cricket::ContentGroup* bundle = |
| 427 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 428 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 429 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 430 | continue; |
| 431 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 432 | // Note what media is used with each crypto protocol, for all sections. |
| 433 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 434 | : webrtc::kEnumCounterKeyProtocolSdes, |
| 435 | content_info.media_description()->type(), |
| 436 | uma_observer); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 437 | const std::string& mid = content_info.name; |
| 438 | if (bundle && bundle->HasContentName(mid) && |
| 439 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 440 | // This isn't the first media section in the BUNDLE group, so it's not |
| 441 | // required to have crypto attributes, since only the crypto attributes |
| 442 | // from the first section actually get used. |
| 443 | continue; |
| 444 | } |
| 445 | |
| 446 | // If the content isn't rejected or bundled into another m= section, crypto |
| 447 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 448 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 449 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 450 | if (!media || !tinfo) { |
| 451 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 452 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 453 | } |
| 454 | if (dtls_enabled) { |
| 455 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 456 | RTC_LOG(LS_WARNING) |
| 457 | << "Session description must have DTLS fingerprint if " |
| 458 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 459 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 460 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 461 | } |
| 462 | } else { |
| 463 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 464 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 465 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 466 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 470 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 474 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 475 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 476 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 477 | const cricket::ContentGroup* bundle = |
| 478 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 479 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 480 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 481 | continue; |
| 482 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 483 | const std::string& mid = content_info.name; |
| 484 | if (bundle && bundle->HasContentName(mid) && |
| 485 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 486 | // This isn't the first media section in the BUNDLE group, so it's not |
| 487 | // required to have ufrag/password, since only the ufrag/password from |
| 488 | // the first section actually get used. |
| 489 | continue; |
| 490 | } |
| 491 | |
| 492 | // If the content isn't rejected or bundled into another m= section, |
| 493 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 494 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 495 | if (!tinfo) { |
| 496 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 497 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | if (tinfo->description.ice_ufrag.empty() || |
| 501 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 502 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | bool GetTrackIdBySsrc(const SessionDescription* session_description, |
| 510 | uint32_t ssrc, |
| 511 | std::string* track_id) { |
| 512 | RTC_DCHECK(track_id != NULL); |
| 513 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 514 | const cricket::AudioContentDescription* audio_desc = |
| 515 | cricket::GetFirstAudioContentDescription(session_description); |
| 516 | if (audio_desc) { |
| 517 | const auto* found = cricket::GetStreamBySsrc(audio_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 518 | if (found) { |
| 519 | *track_id = found->id; |
| 520 | return true; |
| 521 | } |
| 522 | } |
| 523 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 524 | const cricket::VideoContentDescription* video_desc = |
| 525 | cricket::GetFirstVideoContentDescription(session_description); |
| 526 | if (video_desc) { |
| 527 | const auto* found = cricket::GetStreamBySsrc(video_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 528 | if (found) { |
| 529 | *track_id = found->id; |
| 530 | return true; |
| 531 | } |
| 532 | } |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | // Get the SCTP port out of a SessionDescription. |
| 537 | // Return -1 if not found. |
| 538 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 539 | const cricket::DataContentDescription* data_desc = |
| 540 | GetFirstDataContentDescription(session_description); |
| 541 | RTC_DCHECK(data_desc); |
| 542 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 543 | return -1; |
| 544 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 545 | std::string value; |
| 546 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 547 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 548 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 549 | if (!codec.Matches(match_pattern)) { |
| 550 | continue; |
| 551 | } |
| 552 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 553 | return rtc::FromString<int>(value); |
| 554 | } |
| 555 | } |
| 556 | return -1; |
| 557 | } |
| 558 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 559 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 560 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 561 | const SessionDescriptionInterface* new_desc, |
| 562 | const std::string& content_name) { |
| 563 | if (!old_desc) { |
| 564 | return false; |
| 565 | } |
| 566 | const SessionDescription* new_sd = new_desc->description(); |
| 567 | const SessionDescription* old_sd = old_desc->description(); |
| 568 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 569 | if (!cinfo || cinfo->rejected) { |
| 570 | return false; |
| 571 | } |
| 572 | // If the content isn't rejected, check if ufrag and password has changed. |
| 573 | const cricket::TransportDescription* new_transport_desc = |
| 574 | new_sd->GetTransportDescriptionByName(content_name); |
| 575 | const cricket::TransportDescription* old_transport_desc = |
| 576 | old_sd->GetTransportDescriptionByName(content_name); |
| 577 | if (!new_transport_desc || !old_transport_desc) { |
| 578 | // No transport description exists. This is not an ICE restart. |
| 579 | return false; |
| 580 | } |
| 581 | if (cricket::IceCredentialsChanged( |
| 582 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 583 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 584 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 585 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 586 | return true; |
| 587 | } |
| 588 | return false; |
| 589 | } |
| 590 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 591 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 592 | // from an RTCError. |
| 593 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 594 | SdpType type, |
| 595 | const RTCError& error) { |
| 596 | std::ostringstream oss; |
| 597 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 598 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
| 599 | return oss.str(); |
| 600 | } |
| 601 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 602 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 603 | std::string output = "streams=["; |
| 604 | const char* separator = ""; |
| 605 | for (const auto& stream_id : stream_ids) { |
| 606 | output.append(separator).append(stream_id); |
| 607 | separator = ", "; |
| 608 | } |
| 609 | output.append("]"); |
| 610 | return output; |
| 611 | } |
| 612 | |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 613 | rtc::Optional<int> RTCConfigurationToIceConfigOptionalInt( |
| 614 | int rtc_configuration_parameter) { |
| 615 | if (rtc_configuration_parameter == |
| 616 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
| 617 | return rtc::nullopt; |
| 618 | } |
| 619 | return rtc_configuration_parameter; |
| 620 | } |
| 621 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 622 | } // namespace |
| 623 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 624 | // Upon completion, posts a task to execute the callback of the |
| 625 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 626 | // point, the state of the peer connection might no longer reflect the effects |
| 627 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 628 | // modified during the post. |
| 629 | // TODO(hbos): Remove this class once we remove the version of |
| 630 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 631 | // SetSessionDescriptionObserver as an argument. |
| 632 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 633 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 634 | public: |
| 635 | SetRemoteDescriptionObserverAdapter( |
| 636 | rtc::scoped_refptr<PeerConnection> pc, |
| 637 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 638 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 639 | |
| 640 | // SetRemoteDescriptionObserverInterface implementation. |
| 641 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 642 | if (error.ok()) |
| 643 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 644 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 645 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | private: |
| 649 | rtc::scoped_refptr<PeerConnection> pc_; |
| 650 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 651 | }; |
| 652 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 653 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 654 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 655 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 656 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 657 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 658 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 659 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 660 | BundlePolicy bundle_policy; |
| 661 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 662 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 663 | int ice_candidate_pool_size; |
| 664 | bool disable_ipv6; |
| 665 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 666 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 667 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 668 | bool enable_rtp_data_channel; |
| 669 | rtc::Optional<int> screencast_min_bitrate; |
| 670 | rtc::Optional<bool> combined_audio_video_bwe; |
| 671 | rtc::Optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 672 | TcpCandidatePolicy tcp_candidate_policy; |
| 673 | CandidateNetworkPolicy candidate_network_policy; |
| 674 | int audio_jitter_buffer_max_packets; |
| 675 | bool audio_jitter_buffer_fast_accelerate; |
| 676 | int ice_connection_receiving_timeout; |
| 677 | int ice_backup_candidate_pair_ping_interval; |
| 678 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 679 | bool prioritize_most_likely_ice_candidate_pairs; |
| 680 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 681 | bool prune_turn_ports; |
| 682 | bool presume_writable_when_fully_relayed; |
| 683 | bool enable_ice_renomination; |
| 684 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 685 | rtc::Optional<int> ice_check_interval_strong_connectivity; |
| 686 | rtc::Optional<int> ice_check_interval_weak_connectivity; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 687 | rtc::Optional<int> ice_check_min_interval; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 688 | rtc::Optional<int> ice_unwritable_timeout; |
| 689 | rtc::Optional<int> ice_unwritable_min_checks; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 690 | rtc::Optional<int> stun_candidate_keepalive_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 691 | rtc::Optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 692 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 693 | SdpSemantics sdp_semantics; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 694 | rtc::Optional<rtc::AdapterType> network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 695 | }; |
| 696 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 697 | "Did you add something to RTCConfiguration and forget to " |
| 698 | "update operator==?"); |
| 699 | return type == o.type && servers == o.servers && |
| 700 | bundle_policy == o.bundle_policy && |
| 701 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 702 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 703 | candidate_network_policy == o.candidate_network_policy && |
| 704 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 705 | audio_jitter_buffer_fast_accelerate == |
| 706 | o.audio_jitter_buffer_fast_accelerate && |
| 707 | ice_connection_receiving_timeout == |
| 708 | o.ice_connection_receiving_timeout && |
| 709 | ice_backup_candidate_pair_ping_interval == |
| 710 | o.ice_backup_candidate_pair_ping_interval && |
| 711 | continual_gathering_policy == o.continual_gathering_policy && |
| 712 | certificates == o.certificates && |
| 713 | prioritize_most_likely_ice_candidate_pairs == |
| 714 | o.prioritize_most_likely_ice_candidate_pairs && |
| 715 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 716 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 717 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 718 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 719 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 720 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 721 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 722 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 723 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 724 | prune_turn_ports == o.prune_turn_ports && |
| 725 | presume_writable_when_fully_relayed == |
| 726 | o.presume_writable_when_fully_relayed && |
| 727 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 728 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 729 | ice_check_interval_strong_connectivity == |
| 730 | o.ice_check_interval_strong_connectivity && |
| 731 | ice_check_interval_weak_connectivity == |
| 732 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 733 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 734 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 735 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 736 | stun_candidate_keepalive_interval == |
| 737 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 738 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 739 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 740 | sdp_semantics == o.sdp_semantics && |
| 741 | network_preference == o.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 745 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 746 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 747 | } |
| 748 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 749 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 750 | std::string GenerateRtcpCname() { |
| 751 | std::string cname; |
| 752 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 753 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 754 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 755 | } |
| 756 | return cname; |
| 757 | } |
| 758 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 759 | bool ValidateOfferAnswerOptions( |
| 760 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 761 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 762 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 763 | } |
| 764 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 765 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 766 | // m= sections (in other words, nothing that involves a map/array). |
| 767 | void ExtractSharedMediaSessionOptions( |
| 768 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 769 | cricket::MediaSessionOptions* session_options) { |
| 770 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 771 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 772 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 773 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 774 | bool ConvertConstraintsToOfferAnswerOptions( |
| 775 | const MediaConstraintsInterface* constraints, |
| 776 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 777 | if (!constraints) { |
| 778 | return true; |
| 779 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 780 | |
| 781 | bool value = false; |
| 782 | size_t mandatory_constraints_satisfied = 0; |
| 783 | |
| 784 | if (FindConstraint(constraints, |
| 785 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 786 | &mandatory_constraints_satisfied)) { |
| 787 | offer_answer_options->offer_to_receive_audio = |
| 788 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 789 | kOfferToReceiveMediaTrue |
| 790 | : 0; |
| 791 | } |
| 792 | |
| 793 | if (FindConstraint(constraints, |
| 794 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 795 | &mandatory_constraints_satisfied)) { |
| 796 | offer_answer_options->offer_to_receive_video = |
| 797 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 798 | kOfferToReceiveMediaTrue |
| 799 | : 0; |
| 800 | } |
| 801 | if (FindConstraint(constraints, |
| 802 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 803 | &mandatory_constraints_satisfied)) { |
| 804 | offer_answer_options->voice_activity_detection = value; |
| 805 | } |
| 806 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 807 | &mandatory_constraints_satisfied)) { |
| 808 | offer_answer_options->use_rtp_mux = value; |
| 809 | } |
| 810 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 811 | &value, &mandatory_constraints_satisfied)) { |
| 812 | offer_answer_options->ice_restart = value; |
| 813 | } |
| 814 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 815 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 816 | } |
| 817 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 818 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 819 | std::unique_ptr<RtcEventLog> event_log, |
| 820 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 821 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 822 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 823 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 824 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 825 | remote_streams_(StreamCollection::Create()), |
| 826 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 827 | |
| 828 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 829 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 830 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 831 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 832 | // Need to stop transceivers before destroying the stats collector because |
| 833 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 834 | // stopping. |
| 835 | for (auto transceiver : transceivers_) { |
| 836 | transceiver->Stop(); |
| 837 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 838 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 839 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 840 | if (stats_collector_) { |
| 841 | stats_collector_->WaitForPendingRequest(); |
| 842 | stats_collector_ = nullptr; |
| 843 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 844 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 845 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 846 | // the last stats request can still read from the channels. |
| 847 | DestroyAllChannels(); |
| 848 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 849 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 850 | |
| 851 | webrtc_session_desc_factory_.reset(); |
| 852 | sctp_invoker_.reset(); |
| 853 | sctp_factory_.reset(); |
| 854 | transport_controller_.reset(); |
| 855 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 856 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 857 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 858 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 859 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 860 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 861 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 862 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 863 | event_log_.reset(); |
| 864 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 867 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 868 | // Destroy video channels first since they may have a pointer to a voice |
| 869 | // channel. |
| 870 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 871 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 872 | DestroyTransceiverChannel(transceiver); |
| 873 | } |
| 874 | } |
| 875 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 876 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 877 | DestroyTransceiverChannel(transceiver); |
| 878 | } |
| 879 | } |
| 880 | DestroyDataChannel(); |
| 881 | } |
| 882 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 883 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 884 | const PeerConnectionInterface::RTCConfiguration& configuration, |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 885 | std::unique_ptr<cricket::PortAllocator> allocator, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 886 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 887 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 888 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 889 | |
| 890 | RTCError config_error = ValidateConfiguration(configuration); |
| 891 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 892 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 893 | return false; |
| 894 | } |
| 895 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 896 | if (!allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 897 | RTC_LOG(LS_ERROR) |
| 898 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 899 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 900 | return false; |
| 901 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 902 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 903 | if (!observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 904 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 905 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 906 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 907 | return false; |
| 908 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 909 | observer_ = observer; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 910 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 911 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 912 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 913 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 914 | if (!network_thread()->Invoke<bool>( |
| 915 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, |
| 916 | this, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 917 | return false; |
| 918 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 919 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 920 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 921 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 922 | // RFC 3264: The numeric value of the session id and version in the |
| 923 | // o line MUST be representable with a "64 bit signed integer". |
| 924 | // Due to this constraint session id |session_id_| is max limited to |
| 925 | // LLONG_MAX. |
| 926 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 927 | JsepTransportController::Config config; |
| 928 | config.redetermine_role_on_ice_restart = |
| 929 | configuration.redetermine_role_on_ice_restart; |
| 930 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 931 | config.disable_encryption = options.disable_encryption; |
| 932 | config.bundle_policy = configuration.bundle_policy; |
| 933 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
| 934 | config.crypto_options = options.crypto_options; |
| 935 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 936 | config.enable_external_auth = true; |
| 937 | #endif |
| 938 | transport_controller_.reset(new JsepTransportController( |
| 939 | signaling_thread(), network_thread(), port_allocator_.get(), config)); |
| 940 | transport_controller_->SignalIceConnectionState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 941 | this, &PeerConnection::OnTransportControllerConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 942 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 943 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 944 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 945 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 946 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 947 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 948 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 949 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 950 | transport_controller_->SignalRtpTransportChanged.connect( |
| 951 | this, &PeerConnection::OnRtpTransportChanged); |
| 952 | transport_controller_->SignalDtlsTransportChanged.connect( |
| 953 | this, &PeerConnection::OnDtlsTransportChanged); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 954 | |
| 955 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 956 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 957 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 958 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 959 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 960 | configuration_ = configuration; |
| 961 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 962 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 963 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 964 | if (!configuration.certificates.empty()) { |
| 965 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 966 | // just picking the first one. The decision should be made based on the DTLS |
| 967 | // handshake. The DTLS negotiations need to know about all certificates. |
| 968 | certificate = configuration.certificates[0]; |
| 969 | } |
| 970 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 971 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 972 | |
| 973 | if (options.disable_encryption) { |
| 974 | dtls_enabled_ = false; |
| 975 | } else { |
| 976 | // Enable DTLS by default if we have an identity store or a certificate. |
| 977 | dtls_enabled_ = (cert_generator || certificate); |
| 978 | // |configuration| can override the default |dtls_enabled_| value. |
| 979 | if (configuration.enable_dtls_srtp) { |
| 980 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 985 | // It takes precendence over the disable_sctp_data_channels |
| 986 | // PeerConnectionFactoryInterface::Options. |
| 987 | if (configuration.enable_rtp_data_channel) { |
| 988 | data_channel_type_ = cricket::DCT_RTP; |
| 989 | } else { |
| 990 | // DTLS has to be enabled to use SCTP. |
| 991 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 992 | data_channel_type_ = cricket::DCT_SCTP; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | video_options_.screencast_min_bitrate_kbps = |
| 997 | configuration.screencast_min_bitrate; |
| 998 | audio_options_.combined_audio_video_bwe = |
| 999 | configuration.combined_audio_video_bwe; |
| 1000 | |
| 1001 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1002 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1003 | |
| 1004 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1005 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1006 | |
| 1007 | // Whether the certificate generator/certificate is null or not determines |
| 1008 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1009 | // the right instructions by clearing the variables if needed. |
| 1010 | if (!dtls_enabled_) { |
| 1011 | cert_generator.reset(); |
| 1012 | certificate = nullptr; |
| 1013 | } else if (certificate) { |
| 1014 | // Favor generated certificate over the certificate generator. |
| 1015 | cert_generator.reset(); |
| 1016 | } |
| 1017 | |
| 1018 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1019 | signaling_thread(), channel_manager(), this, session_id(), |
| 1020 | std::move(cert_generator), certificate)); |
| 1021 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1022 | this, &PeerConnection::OnCertificateReady); |
| 1023 | |
| 1024 | if (options.disable_encryption) { |
| 1025 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1026 | } |
| 1027 | |
| 1028 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 1029 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1030 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1031 | // Add default audio/video transceivers for Plan B SDP. |
| 1032 | if (!IsUnifiedPlan()) { |
| 1033 | transceivers_.push_back( |
| 1034 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1035 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1036 | transceivers_.push_back( |
| 1037 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1038 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1039 | } |
| 1040 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1041 | return true; |
| 1042 | } |
| 1043 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1044 | RTCError PeerConnection::ValidateConfiguration( |
| 1045 | const RTCConfiguration& config) const { |
| 1046 | if (config.ice_regather_interval_range && |
| 1047 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1048 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1049 | "ice_regather_interval_range specified but continual " |
| 1050 | "gathering policy is GATHER_ONCE"); |
| 1051 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1052 | auto result = |
| 1053 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1054 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1057 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1058 | PeerConnection::local_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1059 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1060 | "Plan SdpSemantics. Please use GetSenders " |
| 1061 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1062 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1065 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1066 | PeerConnection::remote_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1067 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1068 | "Plan SdpSemantics. Please use GetReceivers " |
| 1069 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1070 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1073 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1074 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1075 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1076 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1077 | if (IsClosed()) { |
| 1078 | return false; |
| 1079 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1080 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1081 | return false; |
| 1082 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1083 | |
| 1084 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1085 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1086 | observer->SignalAudioTrackAdded.connect(this, |
| 1087 | &PeerConnection::OnAudioTrackAdded); |
| 1088 | observer->SignalAudioTrackRemoved.connect( |
| 1089 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1090 | observer->SignalVideoTrackAdded.connect(this, |
| 1091 | &PeerConnection::OnVideoTrackAdded); |
| 1092 | observer->SignalVideoTrackRemoved.connect( |
| 1093 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1094 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1095 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1096 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1097 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1098 | } |
| 1099 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1100 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1103 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1104 | observer_->OnRenegotiationNeeded(); |
| 1105 | return true; |
| 1106 | } |
| 1107 | |
| 1108 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1109 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1110 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1111 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1112 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1113 | if (!IsClosed()) { |
| 1114 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1115 | RemoveAudioTrack(track.get(), local_stream); |
| 1116 | } |
| 1117 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1118 | RemoveVideoTrack(track.get(), local_stream); |
| 1119 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1120 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1121 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1122 | stream_observers_.erase( |
| 1123 | std::remove_if( |
| 1124 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1125 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1126 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1127 | }), |
| 1128 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1129 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1130 | if (IsClosed()) { |
| 1131 | return; |
| 1132 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1133 | observer_->OnRenegotiationNeeded(); |
| 1134 | } |
| 1135 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1136 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 1137 | MediaStreamTrackInterface* track, |
| 1138 | std::vector<MediaStreamInterface*> streams) { |
| 1139 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1140 | std::vector<std::string> stream_ids; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1141 | for (auto* stream : streams) { |
| 1142 | if (!stream) { |
| 1143 | RTC_LOG(LS_ERROR) << "Stream list has null element."; |
| 1144 | return nullptr; |
| 1145 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1146 | stream_ids.push_back(stream->id()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1147 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1148 | auto sender_or_error = AddTrack(track, stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1149 | if (!sender_or_error.ok()) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1150 | return nullptr; |
| 1151 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1152 | return sender_or_error.MoveValue(); |
| 1153 | } |
| 1154 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1155 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1156 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1157 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1158 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1159 | if (!track) { |
| 1160 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1161 | } |
| 1162 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1163 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1164 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1165 | "Track has invalid kind: " + track->kind()); |
| 1166 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1167 | if (IsClosed()) { |
| 1168 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1169 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1170 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1171 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1172 | LOG_AND_RETURN_ERROR( |
| 1173 | RTCErrorType::INVALID_PARAMETER, |
| 1174 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1175 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1176 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 1177 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1178 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1179 | if (sender_or_error.ok()) { |
| 1180 | observer_->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1181 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1182 | } |
| 1183 | return sender_or_error; |
| 1184 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1185 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1186 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1187 | PeerConnection::AddTrackPlanB( |
| 1188 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1189 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 1190 | if (stream_ids.size() > 1u) { |
| 1191 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1192 | "AddTrack with more than one stream is not " |
| 1193 | "supported with Plan B semantics."); |
| 1194 | } |
| 1195 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1196 | if (adjusted_stream_ids.empty()) { |
| 1197 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1198 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1199 | cricket::MediaType media_type = |
| 1200 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1201 | ? cricket::MEDIA_TYPE_AUDIO |
| 1202 | : cricket::MEDIA_TYPE_VIDEO); |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 1203 | auto new_sender = CreateSender(media_type, track, adjusted_stream_ids); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1204 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1205 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1206 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1207 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1208 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 1209 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1210 | if (sender_info) { |
| 1211 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1212 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1213 | } else { |
| 1214 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1215 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1216 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1217 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1218 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 1219 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1220 | if (sender_info) { |
| 1221 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1222 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1223 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1224 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1225 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1226 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1227 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1228 | PeerConnection::AddTrackUnifiedPlan( |
| 1229 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1230 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1231 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1232 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1233 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1234 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1235 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1236 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1237 | transceiver->internal()->set_direction( |
| 1238 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1239 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1240 | transceiver->internal()->set_direction( |
| 1241 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1242 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1243 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1244 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1245 | } else { |
| 1246 | cricket::MediaType media_type = |
| 1247 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1248 | ? cricket::MEDIA_TYPE_AUDIO |
| 1249 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1250 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1251 | << " transceiver in response to a call to AddTrack."; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1252 | auto sender = CreateSender(media_type, track, stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1253 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1254 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1255 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1256 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1257 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1258 | return transceiver->sender(); |
| 1259 | } |
| 1260 | |
| 1261 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1262 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1263 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1264 | RTC_DCHECK(track); |
| 1265 | for (auto transceiver : transceivers_) { |
| 1266 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1267 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1268 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1269 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1270 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1271 | return transceiver; |
| 1272 | } |
| 1273 | } |
| 1274 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1278 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1279 | return RemoveTrackInternal(sender).ok(); |
| 1280 | } |
| 1281 | |
| 1282 | RTCError PeerConnection::RemoveTrackInternal( |
| 1283 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1284 | if (!sender) { |
| 1285 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1286 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1287 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1288 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1289 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1290 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1291 | if (IsUnifiedPlan()) { |
| 1292 | auto transceiver = FindTransceiverBySender(sender); |
| 1293 | if (!transceiver || !sender->track()) { |
| 1294 | return RTCError::OK(); |
| 1295 | } |
| 1296 | sender->SetTrack(nullptr); |
| 1297 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1298 | transceiver->internal()->set_direction( |
| 1299 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1300 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1301 | transceiver->internal()->set_direction( |
| 1302 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1303 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1304 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1305 | bool removed; |
| 1306 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1307 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1308 | } else { |
| 1309 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1310 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1311 | } |
| 1312 | if (!removed) { |
| 1313 | LOG_AND_RETURN_ERROR( |
| 1314 | RTCErrorType::INVALID_PARAMETER, |
| 1315 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1316 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1317 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1318 | observer_->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1319 | return RTCError::OK(); |
| 1320 | } |
| 1321 | |
| 1322 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1323 | PeerConnection::FindTransceiverBySender( |
| 1324 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1325 | for (auto transceiver : transceivers_) { |
| 1326 | if (transceiver->sender() == sender) { |
| 1327 | return transceiver; |
| 1328 | } |
| 1329 | } |
| 1330 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1333 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1334 | PeerConnection::AddTransceiver( |
| 1335 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1336 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1337 | } |
| 1338 | |
| 1339 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1340 | PeerConnection::AddTransceiver( |
| 1341 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1342 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1343 | RTC_CHECK(IsUnifiedPlan()) |
| 1344 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1345 | if (!track) { |
| 1346 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1347 | } |
| 1348 | cricket::MediaType media_type; |
| 1349 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1350 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1351 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1352 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1353 | } else { |
| 1354 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1355 | "Track kind is not audio or video"); |
| 1356 | } |
| 1357 | return AddTransceiver(media_type, track, init); |
| 1358 | } |
| 1359 | |
| 1360 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1361 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1362 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1363 | } |
| 1364 | |
| 1365 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1366 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1367 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1368 | RTC_CHECK(IsUnifiedPlan()) |
| 1369 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1370 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1371 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1372 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1373 | "media type is not audio or video"); |
| 1374 | } |
| 1375 | return AddTransceiver(media_type, nullptr, init); |
| 1376 | } |
| 1377 | |
| 1378 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1379 | PeerConnection::AddTransceiver( |
| 1380 | cricket::MediaType media_type, |
| 1381 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1382 | const RtpTransceiverInit& init, |
| 1383 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1384 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1385 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1386 | if (track) { |
| 1387 | RTC_DCHECK_EQ(media_type, |
| 1388 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1389 | ? cricket::MEDIA_TYPE_AUDIO |
| 1390 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1391 | } |
| 1392 | |
| 1393 | // TODO(bugs.webrtc.org/7600): Verify init. |
| 1394 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1395 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1396 | << " transceiver in response to a call to AddTransceiver."; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 1397 | auto sender = CreateSender(media_type, track, init.stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1398 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1399 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1400 | transceiver->internal()->set_direction(init.direction); |
| 1401 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1402 | if (fire_callback) { |
| 1403 | observer_->OnRenegotiationNeeded(); |
| 1404 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1405 | |
| 1406 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1407 | } |
| 1408 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1409 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1410 | PeerConnection::CreateSender( |
| 1411 | cricket::MediaType media_type, |
| 1412 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1413 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1414 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1415 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1416 | RTC_DCHECK(!track || |
| 1417 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1418 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1419 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1420 | new AudioRtpSender(worker_thread(), |
| 1421 | static_cast<AudioTrackInterface*>(track.get()), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1422 | stream_ids, stats_.get())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1423 | } else { |
| 1424 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1425 | RTC_DCHECK(!track || |
| 1426 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1427 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1428 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1429 | new VideoRtpSender(worker_thread(), |
| 1430 | static_cast<VideoTrackInterface*>(track.get()), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1431 | stream_ids)); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1432 | } |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1433 | sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1434 | return sender; |
| 1435 | } |
| 1436 | |
| 1437 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1438 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1439 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1440 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1441 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1442 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1443 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 1444 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1445 | new AudioRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1446 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1447 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1448 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 1449 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1450 | new VideoRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1451 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1452 | return receiver; |
| 1453 | } |
| 1454 | |
| 1455 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1456 | PeerConnection::CreateAndAddTransceiver( |
| 1457 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1458 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1459 | receiver) { |
| 1460 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1461 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1462 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1463 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1464 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1465 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1468 | void PeerConnection::OnNegotiationNeeded() { |
| 1469 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1470 | RTC_DCHECK(!IsClosed()); |
| 1471 | observer_->OnRenegotiationNeeded(); |
| 1472 | } |
| 1473 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1474 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1475 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1476 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1477 | if (IsClosed()) { |
| 1478 | return nullptr; |
| 1479 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1480 | if (!track) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1481 | RTC_LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1482 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1483 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1484 | auto track_sender = FindSenderForTrack(track); |
| 1485 | if (!track_sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1486 | RTC_LOG(LS_ERROR) << "CreateDtmfSender called with a non-added track."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1487 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1490 | return track_sender->GetDtmfSender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1493 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1494 | const std::string& kind, |
| 1495 | const std::string& stream_id) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1496 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1497 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1498 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1499 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1500 | if (IsClosed()) { |
| 1501 | return nullptr; |
| 1502 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1503 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 1504 | // Internally we need to have one stream with Plan B semantics, so we |
| 1505 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1506 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 1507 | if (stream_id.empty()) { |
| 1508 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1509 | RTC_LOG(LS_INFO) |
| 1510 | << "No stream_id specified for sender. Generated stream ID: " |
| 1511 | << stream_ids[0]; |
| 1512 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1513 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1516 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1517 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1518 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1519 | auto* audio_sender = |
| 1520 | new AudioRtpSender(worker_thread(), nullptr, stream_ids, stats_.get()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1521 | audio_sender->SetVoiceMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1522 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1523 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1524 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1525 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1526 | auto* video_sender = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1527 | new VideoRtpSender(worker_thread(), nullptr, stream_ids); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1528 | video_sender->SetVideoMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1529 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1530 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1531 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1532 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1533 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1534 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1535 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1536 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1537 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1540 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1541 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1542 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1543 | for (auto sender : GetSendersInternal()) { |
| 1544 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1545 | } |
| 1546 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1549 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1550 | PeerConnection::GetSendersInternal() const { |
| 1551 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1552 | all_senders; |
| 1553 | for (auto transceiver : transceivers_) { |
| 1554 | auto senders = transceiver->internal()->senders(); |
| 1555 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1556 | } |
| 1557 | return all_senders; |
| 1558 | } |
| 1559 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1560 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1561 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1562 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1563 | for (const auto& receiver : GetReceiversInternal()) { |
| 1564 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1565 | } |
| 1566 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1569 | std::vector< |
| 1570 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1571 | PeerConnection::GetReceiversInternal() const { |
| 1572 | std::vector< |
| 1573 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1574 | all_receivers; |
| 1575 | for (auto transceiver : transceivers_) { |
| 1576 | auto receivers = transceiver->internal()->receivers(); |
| 1577 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1578 | receivers.end()); |
| 1579 | } |
| 1580 | return all_receivers; |
| 1581 | } |
| 1582 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1583 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1584 | PeerConnection::GetTransceivers() const { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1585 | RTC_CHECK(IsUnifiedPlan()) |
| 1586 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1587 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
| 1588 | for (auto transceiver : transceivers_) { |
| 1589 | all_transceivers.push_back(transceiver); |
| 1590 | } |
| 1591 | return all_transceivers; |
| 1592 | } |
| 1593 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1594 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1595 | MediaStreamTrackInterface* track, |
| 1596 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1597 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1598 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1599 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1600 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1601 | return false; |
| 1602 | } |
| 1603 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1604 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1605 | // The StatsCollector is used to tell if a track is valid because it may |
| 1606 | // remember tracks that the PeerConnection previously removed. |
| 1607 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1608 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1609 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1610 | return false; |
| 1611 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1612 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1613 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1614 | return true; |
| 1615 | } |
| 1616 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1617 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1618 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1619 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1620 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1621 | stats_collector_->GetStatsReport(callback); |
| 1622 | } |
| 1623 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1624 | void PeerConnection::GetStats( |
| 1625 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1626 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1627 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1628 | RTC_DCHECK(callback); |
| 1629 | RTC_DCHECK(stats_collector_); |
| 1630 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1631 | if (selector) { |
| 1632 | for (const auto& proxy_transceiver : transceivers_) { |
| 1633 | for (const auto& proxy_sender : |
| 1634 | proxy_transceiver->internal()->senders()) { |
| 1635 | if (proxy_sender == selector) { |
| 1636 | internal_sender = proxy_sender->internal(); |
| 1637 | break; |
| 1638 | } |
| 1639 | } |
| 1640 | if (internal_sender) |
| 1641 | break; |
| 1642 | } |
| 1643 | } |
| 1644 | // If there is no |internal_sender|Â then |selector| is either null or does not |
| 1645 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1646 | // PeerConnection). This means that "all the stats objects representing the |
| 1647 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1648 | // produces an empty stats report. |
| 1649 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1650 | } |
| 1651 | |
| 1652 | void PeerConnection::GetStats( |
| 1653 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1654 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1655 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1656 | RTC_DCHECK(callback); |
| 1657 | RTC_DCHECK(stats_collector_); |
| 1658 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1659 | if (selector) { |
| 1660 | for (const auto& proxy_transceiver : transceivers_) { |
| 1661 | for (const auto& proxy_receiver : |
| 1662 | proxy_transceiver->internal()->receivers()) { |
| 1663 | if (proxy_receiver == selector) { |
| 1664 | internal_receiver = proxy_receiver->internal(); |
| 1665 | break; |
| 1666 | } |
| 1667 | } |
| 1668 | if (internal_receiver) |
| 1669 | break; |
| 1670 | } |
| 1671 | } |
| 1672 | // If there is no |internal_receiver|Â then |selector| is either null or does |
| 1673 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1674 | // the PeerConnection). This means that "all the stats objects representing |
| 1675 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1676 | // selector produces an empty stats report. |
| 1677 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1678 | } |
| 1679 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1680 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1681 | return signaling_state_; |
| 1682 | } |
| 1683 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1684 | PeerConnectionInterface::IceConnectionState |
| 1685 | PeerConnection::ice_connection_state() { |
| 1686 | return ice_connection_state_; |
| 1687 | } |
| 1688 | |
| 1689 | PeerConnectionInterface::IceGatheringState |
| 1690 | PeerConnection::ice_gathering_state() { |
| 1691 | return ice_gathering_state_; |
| 1692 | } |
| 1693 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1694 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1695 | PeerConnection::CreateDataChannel( |
| 1696 | const std::string& label, |
| 1697 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1698 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1699 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1700 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1701 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1702 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1703 | if (config) { |
| 1704 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1705 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1706 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1707 | InternalCreateDataChannel(label, internal_config.get())); |
| 1708 | if (!channel.get()) { |
| 1709 | return nullptr; |
| 1710 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1711 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1712 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1713 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1714 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1715 | observer_->OnRenegotiationNeeded(); |
| 1716 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1717 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1718 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1719 | } |
| 1720 | |
| 1721 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1722 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1723 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1724 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1725 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1726 | // Always create an offer even if |ConvertConstraintsToOfferAnswerOptions| |
| 1727 | // returns false for now. Because |ConvertConstraintsToOfferAnswerOptions| |
| 1728 | // compares the mandatory fields parsed with the mandatory fields added in the |
| 1729 | // |constraints| and some downstream applications might create offers with |
| 1730 | // mandatory fields which would not be parsed in the helper method. For |
| 1731 | // example, in Chromium/remoting, |kEnableDtlsSrtp| is added to the |
| 1732 | // |constraints| as a mandatory field but it is not parsed. |
| 1733 | ConvertConstraintsToOfferAnswerOptions(constraints, &offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1734 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1735 | CreateOffer(observer, offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1739 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1740 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1741 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1742 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1743 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1744 | return; |
| 1745 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1746 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1747 | if (IsClosed()) { |
| 1748 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1749 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1750 | PostCreateSessionDescriptionFailure( |
| 1751 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1752 | return; |
| 1753 | } |
| 1754 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1755 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1756 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1757 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1758 | PostCreateSessionDescriptionFailure( |
| 1759 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1760 | return; |
| 1761 | } |
| 1762 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1763 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1764 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1765 | if (IsUnifiedPlan()) { |
| 1766 | RTCError error = HandleLegacyOfferOptions(options); |
| 1767 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1768 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1769 | return; |
| 1770 | } |
| 1771 | } |
| 1772 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1773 | cricket::MediaSessionOptions session_options; |
| 1774 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1775 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1778 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1779 | const RTCOfferAnswerOptions& options) { |
| 1780 | RTC_DCHECK(IsUnifiedPlan()); |
| 1781 | |
| 1782 | if (options.offer_to_receive_audio == 0) { |
| 1783 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1784 | cricket::MEDIA_TYPE_AUDIO); |
| 1785 | } else if (options.offer_to_receive_audio == 1) { |
| 1786 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1787 | } else if (options.offer_to_receive_audio > 1) { |
| 1788 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1789 | "offer_to_receive_audio > 1 is not supported."); |
| 1790 | } |
| 1791 | |
| 1792 | if (options.offer_to_receive_video == 0) { |
| 1793 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1794 | cricket::MEDIA_TYPE_VIDEO); |
| 1795 | } else if (options.offer_to_receive_video == 1) { |
| 1796 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1797 | } else if (options.offer_to_receive_video > 1) { |
| 1798 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1799 | "offer_to_receive_video > 1 is not supported."); |
| 1800 | } |
| 1801 | |
| 1802 | return RTCError::OK(); |
| 1803 | } |
| 1804 | |
| 1805 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1806 | cricket::MediaType media_type) { |
| 1807 | for (auto transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1808 | RtpTransceiverDirection new_direction = |
| 1809 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 1810 | if (new_direction != transceiver->direction()) { |
| 1811 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 1812 | << " transceiver (MID=" |
| 1813 | << transceiver->mid().value_or("<not set>") << ") from " |
| 1814 | << RtpTransceiverDirectionToString( |
| 1815 | transceiver->direction()) |
| 1816 | << " to " |
| 1817 | << RtpTransceiverDirectionToString(new_direction) |
| 1818 | << " since CreateOffer specified offer_to_receive=0"; |
| 1819 | transceiver->internal()->set_direction(new_direction); |
| 1820 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1825 | cricket::MediaType media_type) { |
| 1826 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1827 | RTC_LOG(LS_INFO) |
| 1828 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 1829 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1830 | RtpTransceiverInit init; |
| 1831 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1832 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1837 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1838 | std::vector< |
| 1839 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1840 | receiving_transceivers; |
| 1841 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1842 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1843 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1844 | receiving_transceivers.push_back(transceiver); |
| 1845 | } |
| 1846 | } |
| 1847 | return receiving_transceivers; |
| 1848 | } |
| 1849 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1850 | void PeerConnection::CreateAnswer( |
| 1851 | CreateSessionDescriptionObserver* observer, |
| 1852 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1853 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1854 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1855 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1856 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1857 | return; |
| 1858 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1859 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1860 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1861 | if (!ConvertConstraintsToOfferAnswerOptions(constraints, |
| 1862 | &offer_answer_options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1863 | std::string error = "CreateAnswer called with invalid constraints."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1864 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1865 | PostCreateSessionDescriptionFailure( |
| 1866 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1867 | return; |
| 1868 | } |
| 1869 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1870 | CreateAnswer(observer, offer_answer_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1874 | const RTCOfferAnswerOptions& options) { |
| 1875 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1876 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1877 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1878 | return; |
| 1879 | } |
| 1880 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1881 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1882 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1883 | std::string error = |
| 1884 | "PeerConnection cannot create an answer in a state other than " |
| 1885 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1886 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1887 | PostCreateSessionDescriptionFailure( |
| 1888 | observer, RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1889 | return; |
| 1890 | } |
| 1891 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1892 | // The remote description should be set if we're in the right state. |
| 1893 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1894 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1895 | if (IsUnifiedPlan()) { |
| 1896 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1897 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1898 | "supported with Unified Plan semantics. Use the " |
| 1899 | "RtpTransceiver API instead."; |
| 1900 | } |
| 1901 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1902 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1903 | "supported with Unified Plan semantics. Use the " |
| 1904 | "RtpTransceiver API instead."; |
| 1905 | } |
| 1906 | } |
| 1907 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1908 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1909 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1910 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1911 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | void PeerConnection::SetLocalDescription( |
| 1915 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1916 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1917 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1918 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1919 | // The SetLocalDescription contract is that we take ownership of the session |
| 1920 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 1921 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 1922 | // description as a unique_ptr argument to formalize this agreement. |
| 1923 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 1924 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1925 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1926 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1927 | return; |
| 1928 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1929 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1930 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1931 | PostSetSessionDescriptionFailure( |
| 1932 | observer, |
| 1933 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1934 | return; |
| 1935 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1936 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1937 | // If a session error has occurred the PeerConnection is in a possibly |
| 1938 | // inconsistent state so fail right away. |
| 1939 | if (session_error() != SessionError::kNone) { |
| 1940 | std::string error_message = GetSessionErrorMsg(); |
| 1941 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1942 | PostSetSessionDescriptionFailure( |
| 1943 | observer, |
| 1944 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1945 | return; |
| 1946 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1947 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1948 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1949 | if (!error.ok()) { |
| 1950 | std::string error_message = GetSetDescriptionErrorMessage( |
| 1951 | cricket::CS_LOCAL, desc->GetType(), error); |
| 1952 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1953 | PostSetSessionDescriptionFailure( |
| 1954 | observer, |
| 1955 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1956 | return; |
| 1957 | } |
| 1958 | |
| 1959 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 1960 | // which may destroy it before returning. |
| 1961 | const SdpType type = desc->GetType(); |
| 1962 | |
| 1963 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1964 | // |desc| may be destroyed at this point. |
| 1965 | |
| 1966 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1967 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 1968 | // inconsistent state, so act conservatively here and set the session error |
| 1969 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 1970 | SetSessionError(SessionError::kContent, error.message()); |
| 1971 | std::string error_message = |
| 1972 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 1973 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1974 | PostSetSessionDescriptionFailure( |
| 1975 | observer, |
| 1976 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1977 | return; |
| 1978 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1979 | RTC_DCHECK(local_description()); |
| 1980 | |
| 1981 | PostSetSessionDescriptionSuccess(observer); |
| 1982 | |
| 1983 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1984 | // size is not allowed, and changing the set of ICE servers will not result |
| 1985 | // in new candidates being gathered. |
| 1986 | port_allocator_->FreezeCandidatePool(); |
| 1987 | |
| 1988 | // MaybeStartGathering needs to be called after posting |
| 1989 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1990 | // before signaling that SetLocalDescription completed. |
| 1991 | transport_controller_->MaybeStartGathering(); |
| 1992 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1993 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1994 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1995 | // MaybeStartGathering... |
| 1996 | network_thread()->Invoke<void>( |
| 1997 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1998 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 1999 | // Make UMA notes about what was agreed to. |
| 2000 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | RTCError PeerConnection::ApplyLocalDescription( |
| 2005 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2006 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2007 | RTC_DCHECK(desc); |
| 2008 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2009 | // Update stats here so that we have the most recent stats for tracks and |
| 2010 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2011 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2012 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2013 | // Take a reference to the old local description since it's used below to |
| 2014 | // compare against the new local description. When setting the new local |
| 2015 | // description, grab ownership of the replaced session description in case it |
| 2016 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2017 | // of the method. |
| 2018 | const SessionDescriptionInterface* old_local_description = |
| 2019 | local_description(); |
| 2020 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2021 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2022 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2023 | replaced_local_description = pending_local_description_ |
| 2024 | ? std::move(pending_local_description_) |
| 2025 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2026 | current_local_description_ = std::move(desc); |
| 2027 | pending_local_description_ = nullptr; |
| 2028 | current_remote_description_ = std::move(pending_remote_description_); |
| 2029 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2030 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2031 | pending_local_description_ = std::move(desc); |
| 2032 | } |
| 2033 | // The session description to apply now must be accessed by |
| 2034 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2035 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2036 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2037 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2038 | if (!error.ok()) { |
| 2039 | return error; |
| 2040 | } |
| 2041 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2042 | if (IsUnifiedPlan()) { |
| 2043 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2044 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2045 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2046 | if (!error.ok()) { |
| 2047 | return error; |
| 2048 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2049 | for (auto transceiver : transceivers_) { |
| 2050 | const ContentInfo* content = |
| 2051 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2052 | if (!content) { |
| 2053 | continue; |
| 2054 | } |
| 2055 | const MediaContentDescription* media_desc = content->media_description(); |
| 2056 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2057 | transceiver->internal()->set_current_direction(media_desc->direction()); |
| 2058 | } |
| 2059 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2060 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2061 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2062 | transceiver->Stop(); |
| 2063 | } |
| 2064 | } |
| 2065 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2066 | // Media channels will be created only when offer is set. These may use new |
| 2067 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2068 | if (type == SdpType::kOffer) { |
| 2069 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2070 | // description is applied. Restore back to old description. |
| 2071 | RTCError error = CreateChannels(*local_description()->description()); |
| 2072 | if (!error.ok()) { |
| 2073 | return error; |
| 2074 | } |
| 2075 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2076 | // Remove unused channels if MediaContentDescription is rejected. |
| 2077 | RemoveUnusedChannels(local_description()->description()); |
| 2078 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2079 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2080 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2081 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2082 | if (!error.ok()) { |
| 2083 | return error; |
| 2084 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2085 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2086 | if (remote_description()) { |
| 2087 | // Now that we have a local description, we can push down remote candidates. |
| 2088 | UseCandidatesInSessionDescription(remote_description()); |
| 2089 | } |
| 2090 | |
| 2091 | pending_ice_restarts_.clear(); |
| 2092 | if (session_error() != SessionError::kNone) { |
| 2093 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2094 | } |
| 2095 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2096 | // If setting the description decided our SSL role, allocate any necessary |
| 2097 | // SCTP sids. |
| 2098 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2099 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2100 | AllocateSctpSids(role); |
| 2101 | } |
| 2102 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2103 | if (IsUnifiedPlan()) { |
| 2104 | for (auto transceiver : transceivers_) { |
| 2105 | const ContentInfo* content = |
| 2106 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2107 | if (!content) { |
| 2108 | continue; |
| 2109 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2110 | const auto& streams = content->media_description()->streams(); |
| 2111 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2112 | transceiver->internal()->sender_internal()->set_stream_ids( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 2113 | streams[0].stream_ids()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2114 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2115 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2116 | } |
| 2117 | } |
| 2118 | } else { |
| 2119 | // Plan B semantics. |
| 2120 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2121 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2122 | // local session description. |
| 2123 | const cricket::ContentInfo* audio_content = |
| 2124 | GetFirstAudioContent(local_description()->description()); |
| 2125 | if (audio_content) { |
| 2126 | if (audio_content->rejected) { |
| 2127 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2128 | } else { |
| 2129 | const cricket::AudioContentDescription* audio_desc = |
| 2130 | audio_content->media_description()->as_audio(); |
| 2131 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2132 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2133 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2134 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2135 | const cricket::ContentInfo* video_content = |
| 2136 | GetFirstVideoContent(local_description()->description()); |
| 2137 | if (video_content) { |
| 2138 | if (video_content->rejected) { |
| 2139 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2140 | } else { |
| 2141 | const cricket::VideoContentDescription* video_desc = |
| 2142 | video_content->media_description()->as_video(); |
| 2143 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2144 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2145 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2149 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2150 | if (data_content) { |
| 2151 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2152 | data_content->media_description()->as_data(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2153 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2154 | cricket::kMediaProtocolRtpPrefix)) { |
| 2155 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 2156 | } |
| 2157 | } |
| 2158 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2159 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2163 | SetSessionDescriptionObserver* observer, |
| 2164 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2165 | SetRemoteDescription( |
| 2166 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2167 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2168 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2169 | } |
| 2170 | |
| 2171 | void PeerConnection::SetRemoteDescription( |
| 2172 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2173 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2174 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2175 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2176 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2177 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2178 | return; |
| 2179 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2180 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2181 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2182 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2183 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2184 | return; |
| 2185 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2186 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2187 | // If a session error has occurred the PeerConnection is in a possibly |
| 2188 | // inconsistent state so fail right away. |
| 2189 | if (session_error() != SessionError::kNone) { |
| 2190 | std::string error_message = GetSessionErrorMsg(); |
| 2191 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2192 | observer->OnSetRemoteDescriptionComplete( |
| 2193 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2194 | return; |
| 2195 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2196 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2197 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2198 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2199 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2200 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2201 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2202 | observer->OnSetRemoteDescriptionComplete( |
| 2203 | RTCError(error.type(), std::move(error_message))); |
| 2204 | return; |
| 2205 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2206 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2207 | // Grab the description type before moving ownership to |
| 2208 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2209 | const SdpType type = desc->GetType(); |
| 2210 | |
| 2211 | error = ApplyRemoteDescription(std::move(desc)); |
| 2212 | // |desc| may be destroyed at this point. |
| 2213 | |
| 2214 | if (!error.ok()) { |
| 2215 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2216 | // inconsistent state, so act conservatively here and set the session error |
| 2217 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2218 | SetSessionError(SessionError::kContent, error.message()); |
| 2219 | std::string error_message = |
| 2220 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2221 | RTC_LOG(LS_ERROR) << error_message; |
| 2222 | observer->OnSetRemoteDescriptionComplete( |
| 2223 | RTCError(error.type(), std::move(error_message))); |
| 2224 | return; |
| 2225 | } |
| 2226 | RTC_DCHECK(remote_description()); |
| 2227 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 2228 | if (type == SdpType::kOffer) { |
| 2229 | // Report to UMA the format of the received offer. |
| 2230 | ReportSdpFormatReceived(*remote_description()); |
| 2231 | } |
| 2232 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2233 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2234 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2235 | // MaybeStartGathering... |
| 2236 | network_thread()->Invoke<void>( |
| 2237 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2238 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2239 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2240 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
| 2244 | } |
| 2245 | |
| 2246 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2247 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2248 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2249 | RTC_DCHECK(desc); |
| 2250 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2251 | // Update stats here so that we have the most recent stats for tracks and |
| 2252 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2253 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2254 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2255 | // Take a reference to the old remote description since it's used below to |
| 2256 | // compare against the new remote description. When setting the new remote |
| 2257 | // description, grab ownership of the replaced session description in case it |
| 2258 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2259 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2260 | const SessionDescriptionInterface* old_remote_description = |
| 2261 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2262 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2263 | SdpType type = desc->GetType(); |
| 2264 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2265 | replaced_remote_description = pending_remote_description_ |
| 2266 | ? std::move(pending_remote_description_) |
| 2267 | : std::move(current_remote_description_); |
| 2268 | current_remote_description_ = std::move(desc); |
| 2269 | pending_remote_description_ = nullptr; |
| 2270 | current_local_description_ = std::move(pending_local_description_); |
| 2271 | } else { |
| 2272 | replaced_remote_description = std::move(pending_remote_description_); |
| 2273 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2274 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2275 | // The session description to apply now must be accessed by |
| 2276 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2277 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2278 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2279 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2280 | if (!error.ok()) { |
| 2281 | return error; |
| 2282 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2283 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2284 | if (IsUnifiedPlan()) { |
| 2285 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2286 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2287 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2288 | if (!error.ok()) { |
| 2289 | return error; |
| 2290 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2291 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2292 | // Media channels will be created only when offer is set. These may use new |
| 2293 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2294 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2295 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2296 | // description is applied. Restore back to old description. |
| 2297 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2298 | if (!error.ok()) { |
| 2299 | return error; |
| 2300 | } |
| 2301 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2302 | // Remove unused channels if MediaContentDescription is rejected. |
| 2303 | RemoveUnusedChannels(remote_description()->description()); |
| 2304 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2305 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2306 | // NOTE: Candidates allocation will be initiated only when |
| 2307 | // SetLocalDescription is called. |
| 2308 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2309 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2310 | if (!error.ok()) { |
| 2311 | return error; |
| 2312 | } |
| 2313 | |
| 2314 | if (local_description() && |
| 2315 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2316 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2317 | } |
| 2318 | |
| 2319 | if (old_remote_description) { |
| 2320 | for (const cricket::ContentInfo& content : |
| 2321 | old_remote_description->description()->contents()) { |
| 2322 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2323 | // password that indicates the remote peer requests an ICE restart. |
| 2324 | // TODO(deadbeef): When we start storing both the current and pending |
| 2325 | // remote description, this should reset pending_ice_restarts and compare |
| 2326 | // against the current description. |
| 2327 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2328 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2329 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2330 | pending_ice_restarts_.insert(content.name); |
| 2331 | } |
| 2332 | } else { |
| 2333 | // We retain all received candidates only if ICE is not restarted. |
| 2334 | // When ICE is restarted, all previous candidates belong to an old |
| 2335 | // generation and should not be kept. |
| 2336 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2337 | // description should only contain candidates from the last set remote |
| 2338 | // description plus any candidates added since then. We should remove |
| 2339 | // this once we're sure it won't break anything. |
| 2340 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2341 | old_remote_description, content.name, mutable_remote_description()); |
| 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | if (session_error() != SessionError::kNone) { |
| 2347 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2348 | } |
| 2349 | |
| 2350 | // Set the the ICE connection state to connecting since the connection may |
| 2351 | // become writable with peer reflexive candidates before any remote candidate |
| 2352 | // is signaled. |
| 2353 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2354 | // is to have a new signal the indicates a change in checking state from the |
| 2355 | // transport and expose a new checking() member from transport that can be |
| 2356 | // read to determine the current checking state. The existing SignalConnecting |
| 2357 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2358 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2359 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2360 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2361 | } |
| 2362 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2363 | // If setting the description decided our SSL role, allocate any necessary |
| 2364 | // SCTP sids. |
| 2365 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2366 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2367 | AllocateSctpSids(role); |
| 2368 | } |
| 2369 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2370 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2371 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 2372 | receiving_transceivers; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2373 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2374 | for (auto transceiver : transceivers_) { |
| 2375 | const ContentInfo* content = |
| 2376 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2377 | if (!content) { |
| 2378 | continue; |
| 2379 | } |
| 2380 | const MediaContentDescription* media_desc = content->media_description(); |
| 2381 | RtpTransceiverDirection local_direction = |
| 2382 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 2383 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 "Set |
| 2384 | // the RTCSessionDescription: If direction is sendrecv or recvonly, and |
| 2385 | // transceiver's current direction is neither sendrecv nor recvonly, |
| 2386 | // process the addition of a remote track for the media description. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 2387 | std::vector<std::string> stream_ids; |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2388 | if (!media_desc->streams().empty()) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 2389 | // TODO(bugs.webrtc.org/7932): Currently stream ids are all populated |
| 2390 | // within StreamParams. When they are updated to be stored within the |
| 2391 | // MediaContentDescription, change the logic here. |
Steve Anton | 5a26a3a | 2018-02-28 11:38:47 -0800 | [diff] [blame] | 2392 | const cricket::StreamParams& stream_params = media_desc->streams()[0]; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 2393 | if (!stream_params.stream_ids().empty()) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 2394 | stream_ids = stream_params.stream_ids(); |
Steve Anton | 5a26a3a | 2018-02-28 11:38:47 -0800 | [diff] [blame] | 2395 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2396 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2397 | if (RtpTransceiverDirectionHasRecv(local_direction) && |
| 2398 | (!transceiver->current_direction() || |
| 2399 | !RtpTransceiverDirectionHasRecv( |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2400 | *transceiver->current_direction()))) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2401 | RTC_LOG(LS_INFO) << "Processing the addition of a new track for MID=" |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 2402 | << content->name << " (added to " |
| 2403 | << GetStreamIdsString(stream_ids) << ")."; |
| 2404 | |
| 2405 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 2406 | for (const std::string& stream_id : stream_ids) { |
| 2407 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2408 | remote_streams_->find(stream_id); |
| 2409 | if (!stream) { |
| 2410 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2411 | MediaStream::Create(stream_id)); |
| 2412 | remote_streams_->AddStream(stream); |
| 2413 | added_streams.push_back(stream); |
| 2414 | } |
| 2415 | media_streams.push_back(stream); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2416 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 2417 | transceiver->internal()->receiver_internal()->SetStreams(media_streams); |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2418 | receiving_transceivers.push_back(transceiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2419 | } |
| 2420 | // If direction is sendonly or inactive, and transceiver's current |
| 2421 | // direction is neither sendonly nor inactive, process the removal of a |
| 2422 | // remote track for the media description. |
| 2423 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2424 | (transceiver->current_direction() && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2425 | RtpTransceiverDirectionHasRecv(*transceiver->current_direction()))) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2426 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 2427 | << content->name; |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2428 | transceiver->internal()->receiver_internal()->SetStreams({}); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2429 | } |
| 2430 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2431 | transceiver->internal()->set_current_direction(local_direction); |
| 2432 | } |
| 2433 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2434 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2435 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2436 | transceiver->Stop(); |
| 2437 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2438 | if (!content->rejected && |
| 2439 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2440 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2441 | uint32_t ssrc = 0; |
| 2442 | if (!media_desc->streams().empty()) { |
| 2443 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2444 | } |
| 2445 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2446 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2447 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2448 | // Once all processing has finished, fire off callbacks. |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2449 | for (auto transceiver : receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2450 | stats_->AddTrack(transceiver->receiver()->track()); |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2451 | observer_->OnTrack(transceiver); |
| 2452 | observer_->OnAddTrack(transceiver->receiver(), |
| 2453 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2454 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2455 | for (auto stream : added_streams) { |
| 2456 | observer_->OnAddStream(stream); |
| 2457 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2458 | } |
| 2459 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2460 | const cricket::ContentInfo* audio_content = |
| 2461 | GetFirstAudioContent(remote_description()->description()); |
| 2462 | const cricket::ContentInfo* video_content = |
| 2463 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2464 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2465 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2466 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2467 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2468 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2469 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2470 | |
| 2471 | // Check if the descriptions include streams, just in case the peer supports |
| 2472 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2473 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2474 | (audio_desc && !audio_desc->streams().empty()) || |
| 2475 | (video_desc && !video_desc->streams().empty())) { |
| 2476 | remote_peer_supports_msid_ = true; |
| 2477 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2478 | |
| 2479 | // We wait to signal new streams until we finish processing the description, |
| 2480 | // since only at that point will new streams have all their tracks. |
| 2481 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2482 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2483 | if (!IsUnifiedPlan()) { |
| 2484 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2485 | // rejected media section, there is some cleanup logic that expects the |
| 2486 | // voice/ video channel to still be set. But in this method the voice/video |
| 2487 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2488 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2489 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2490 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2491 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2492 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2493 | // and MediaStreams. |
| 2494 | if (audio_content) { |
| 2495 | if (audio_content->rejected) { |
| 2496 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2497 | } else { |
| 2498 | bool default_audio_track_needed = |
| 2499 | !remote_peer_supports_msid_ && |
| 2500 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2501 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2502 | default_audio_track_needed, audio_desc->type(), |
| 2503 | new_streams); |
| 2504 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2505 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2506 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2507 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2508 | // and MediaStreams. |
| 2509 | if (video_content) { |
| 2510 | if (video_content->rejected) { |
| 2511 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2512 | } else { |
| 2513 | bool default_video_track_needed = |
| 2514 | !remote_peer_supports_msid_ && |
| 2515 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2516 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2517 | default_video_track_needed, video_desc->type(), |
| 2518 | new_streams); |
| 2519 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2520 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2521 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2522 | // Update the DataChannels with the information from the remote peer. |
| 2523 | if (data_desc) { |
| 2524 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2525 | cricket::kMediaProtocolRtpPrefix)) { |
| 2526 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2527 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2528 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2529 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2530 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 2531 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2532 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2533 | stats_->AddStream(new_stream); |
| 2534 | observer_->OnAddStream( |
| 2535 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2536 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2537 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2538 | UpdateEndedRemoteMediaStreams(); |
| 2539 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2540 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2541 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2544 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2545 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2546 | const SessionDescriptionInterface& new_session, |
| 2547 | const SessionDescriptionInterface* old_local_description, |
| 2548 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2549 | RTC_DCHECK(IsUnifiedPlan()); |
| 2550 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2551 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2552 | if (new_session.GetType() == SdpType::kOffer) { |
| 2553 | auto bundle_group_or_error = |
| 2554 | GetEarlyBundleGroup(*new_session.description()); |
| 2555 | if (!bundle_group_or_error.ok()) { |
| 2556 | return bundle_group_or_error.MoveError(); |
| 2557 | } |
| 2558 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2559 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2560 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2561 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2562 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2563 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2564 | cricket::MediaType media_type = new_content.media_description()->type(); |
| 2565 | seen_mids_.insert(new_content.name); |
| 2566 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2567 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2568 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2569 | if (old_local_description && |
| 2570 | i < old_local_description->description()->contents().size()) { |
| 2571 | old_local_content = |
| 2572 | &old_local_description->description()->contents()[i]; |
| 2573 | } |
| 2574 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2575 | if (old_remote_description && |
| 2576 | i < old_remote_description->description()->contents().size()) { |
| 2577 | old_remote_content = |
| 2578 | &old_remote_description->description()->contents()[i]; |
| 2579 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2580 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2581 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2582 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2583 | if (!transceiver_or_error.ok()) { |
| 2584 | return transceiver_or_error.MoveError(); |
| 2585 | } |
| 2586 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2587 | RTCError error = |
| 2588 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2589 | if (!error.ok()) { |
| 2590 | return error; |
| 2591 | } |
| 2592 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2593 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2594 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2595 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 2596 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2597 | continue; |
| 2598 | } |
| 2599 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2600 | if (!error.ok()) { |
| 2601 | return error; |
| 2602 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2603 | } else { |
| 2604 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2605 | "Unknown section type."); |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | return RTCError::OK(); |
| 2610 | } |
| 2611 | |
| 2612 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2613 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2614 | transceiver, |
| 2615 | const cricket::ContentInfo& content, |
| 2616 | const cricket::ContentGroup* bundle_group) { |
| 2617 | RTC_DCHECK(IsUnifiedPlan()); |
| 2618 | RTC_DCHECK(transceiver); |
| 2619 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 2620 | if (content.rejected) { |
| 2621 | if (channel) { |
| 2622 | transceiver->internal()->SetChannel(nullptr); |
| 2623 | DestroyBaseChannel(channel); |
| 2624 | } |
| 2625 | } else { |
| 2626 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2627 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2628 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2629 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2630 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2631 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2632 | } |
| 2633 | if (!channel) { |
| 2634 | LOG_AND_RETURN_ERROR( |
| 2635 | RTCErrorType::INTERNAL_ERROR, |
| 2636 | "Failed to create channel for mid=" + content.name); |
| 2637 | } |
| 2638 | transceiver->internal()->SetChannel(channel); |
| 2639 | } |
| 2640 | } |
| 2641 | return RTCError::OK(); |
| 2642 | } |
| 2643 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2644 | RTCError PeerConnection::UpdateDataChannel( |
| 2645 | cricket::ContentSource source, |
| 2646 | const cricket::ContentInfo& content, |
| 2647 | const cricket::ContentGroup* bundle_group) { |
| 2648 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2649 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2650 | // will take care of rejecting it. |
| 2651 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2652 | } |
| 2653 | if (content.rejected) { |
| 2654 | DestroyDataChannel(); |
| 2655 | } else { |
| 2656 | if (!rtp_data_channel_ && !sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2657 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2658 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2659 | "Failed to create data channel."); |
| 2660 | } |
| 2661 | } |
| 2662 | if (source == cricket::CS_REMOTE) { |
| 2663 | const MediaContentDescription* data_desc = content.media_description(); |
| 2664 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2665 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | return RTCError::OK(); |
| 2670 | } |
| 2671 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2672 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2673 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2674 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2675 | size_t mline_index, |
| 2676 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2677 | const ContentInfo* old_local_content, |
| 2678 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2679 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2680 | // If this is an offer then the m= section might be recycled. If the m= |
| 2681 | // section is being recycled (defined as: rejected in the current local or |
| 2682 | // remote description and not rejected in new description), dissociate the |
| 2683 | // currently associated RtpTransceiver by setting its mid property to null, |
| 2684 | // and discard the mapping between the transceiver and its m= section index. |
| 2685 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 2686 | old_remote_content)) { |
| 2687 | // We want to dissociate the transceiver that has the rejected mid. |
| 2688 | const std::string& old_mid = |
| 2689 | (old_local_content && old_local_content->rejected) |
| 2690 | ? old_local_content->name |
| 2691 | : old_remote_content->name; |
| 2692 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2693 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2694 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 2695 | << " since the media section is being recycled."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2696 | old_transceiver->internal()->set_mid(rtc::nullopt); |
| 2697 | old_transceiver->internal()->set_mline_index(rtc::nullopt); |
| 2698 | } |
| 2699 | } |
| 2700 | const MediaContentDescription* media_desc = content.media_description(); |
| 2701 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2702 | if (source == cricket::CS_LOCAL) { |
| 2703 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2704 | // mapping between transceivers and m= section indices established when |
| 2705 | // creating the offer. |
| 2706 | if (!transceiver) { |
| 2707 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2708 | } |
| 2709 | if (!transceiver) { |
| 2710 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2711 | "Unknown transceiver"); |
| 2712 | } |
| 2713 | } else { |
| 2714 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2715 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2716 | // of the same type... |
| 2717 | if (!transceiver && |
| 2718 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2719 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2720 | } |
| 2721 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2722 | // recvonly direction. |
| 2723 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2724 | RTC_LOG(LS_INFO) << "Adding " |
| 2725 | << cricket::MediaTypeToString(media_desc->type()) |
| 2726 | << " transceiver for MID=" << content.name |
| 2727 | << " at i=" << mline_index |
| 2728 | << " in response to the remote description."; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2729 | auto sender = |
| 2730 | CreateSender(media_desc->type(), nullptr, {rtc::CreateRandomUuid()}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2731 | std::string receiver_id; |
| 2732 | if (!media_desc->streams().empty()) { |
| 2733 | receiver_id = media_desc->streams()[0].id; |
| 2734 | } else { |
| 2735 | receiver_id = rtc::CreateRandomUuid(); |
| 2736 | } |
| 2737 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2738 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2739 | transceiver->internal()->set_direction( |
| 2740 | RtpTransceiverDirection::kRecvOnly); |
| 2741 | } |
| 2742 | } |
| 2743 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2744 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2745 | LOG_AND_RETURN_ERROR( |
| 2746 | RTCErrorType::INVALID_PARAMETER, |
| 2747 | "Transceiver type does not match media description type."); |
| 2748 | } |
| 2749 | // Associate the found or created RtpTransceiver with the m= section by |
| 2750 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2751 | // section, and establish a mapping between the transceiver and the index of |
| 2752 | // the m= section. |
| 2753 | transceiver->internal()->set_mid(content.name); |
| 2754 | transceiver->internal()->set_mline_index(mline_index); |
| 2755 | return std::move(transceiver); |
| 2756 | } |
| 2757 | |
| 2758 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2759 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 2760 | RTC_DCHECK(IsUnifiedPlan()); |
| 2761 | for (auto transceiver : transceivers_) { |
| 2762 | if (transceiver->mid() == mid) { |
| 2763 | return transceiver; |
| 2764 | } |
| 2765 | } |
| 2766 | return nullptr; |
| 2767 | } |
| 2768 | |
| 2769 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2770 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 2771 | RTC_DCHECK(IsUnifiedPlan()); |
| 2772 | for (auto transceiver : transceivers_) { |
| 2773 | if (transceiver->internal()->mline_index() == mline_index) { |
| 2774 | return transceiver; |
| 2775 | } |
| 2776 | } |
| 2777 | return nullptr; |
| 2778 | } |
| 2779 | |
| 2780 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2781 | PeerConnection::FindAvailableTransceiverToReceive( |
| 2782 | cricket::MediaType media_type) const { |
| 2783 | RTC_DCHECK(IsUnifiedPlan()); |
| 2784 | // From JSEP section 5.10 (Applying a Remote Description): |
| 2785 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 2786 | // the same type that were added to the PeerConnection by addTrack and are not |
| 2787 | // associated with any m= section and are not stopped, find the first such |
| 2788 | // RtpTransceiver. |
| 2789 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2790 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2791 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 2792 | !transceiver->stopped()) { |
| 2793 | return transceiver; |
| 2794 | } |
| 2795 | } |
| 2796 | return nullptr; |
| 2797 | } |
| 2798 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2799 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 2800 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2801 | transceiver, |
| 2802 | const SessionDescriptionInterface* sdesc) const { |
| 2803 | RTC_DCHECK(transceiver); |
| 2804 | RTC_DCHECK(sdesc); |
| 2805 | if (IsUnifiedPlan()) { |
| 2806 | if (!transceiver->internal()->mid()) { |
| 2807 | // This transceiver is not associated with a media section yet. |
| 2808 | return nullptr; |
| 2809 | } |
| 2810 | return sdesc->description()->GetContentByName( |
| 2811 | *transceiver->internal()->mid()); |
| 2812 | } else { |
| 2813 | // Plan B only allows at most one audio and one video section, so use the |
| 2814 | // first media section of that type. |
| 2815 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2816 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 2820 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 2821 | return configuration_; |
| 2822 | } |
| 2823 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2824 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 2825 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2826 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 2827 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2828 | if (local_description() && configuration.ice_candidate_pool_size != |
| 2829 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2830 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 2831 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2832 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2833 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2834 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2835 | // The simplest (and most future-compatible) way to tell if the config was |
| 2836 | // modified in an invalid way is to copy each property we do support |
| 2837 | // modifying, then use operator==. There are far more properties we don't |
| 2838 | // support modifying than those we do, and more could be added. |
| 2839 | RTCConfiguration modified_config = configuration_; |
| 2840 | modified_config.servers = configuration.servers; |
| 2841 | modified_config.type = configuration.type; |
| 2842 | modified_config.ice_candidate_pool_size = |
| 2843 | configuration.ice_candidate_pool_size; |
| 2844 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2845 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 2846 | modified_config.ice_check_interval_strong_connectivity = |
| 2847 | configuration.ice_check_interval_strong_connectivity; |
| 2848 | modified_config.ice_check_interval_weak_connectivity = |
| 2849 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2850 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 2851 | modified_config.ice_unwritable_min_checks = |
| 2852 | configuration.ice_unwritable_min_checks; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2853 | modified_config.stun_candidate_keepalive_interval = |
| 2854 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2855 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 2856 | modified_config.network_preference = configuration.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2857 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2858 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2859 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2860 | } |
| 2861 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 2862 | // Validate the modified configuration. |
| 2863 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 2864 | if (!validate_error.ok()) { |
| 2865 | return SafeSetError(std::move(validate_error), error); |
| 2866 | } |
| 2867 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2868 | // Note that this isn't possible through chromium, since it's an unsigned |
| 2869 | // short in WebIDL. |
| 2870 | if (configuration.ice_candidate_pool_size < 0 || |
| 2871 | configuration.ice_candidate_pool_size > UINT16_MAX) { |
| 2872 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 2873 | } |
| 2874 | |
| 2875 | // Parse ICE servers before hopping to network thread. |
| 2876 | cricket::ServerAddresses stun_servers; |
| 2877 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 2878 | RTCErrorType parse_error = |
| 2879 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 2880 | if (parse_error != RTCErrorType::NONE) { |
| 2881 | return SafeSetError(parse_error, error); |
| 2882 | } |
| 2883 | |
| 2884 | // In theory this shouldn't fail. |
| 2885 | if (!network_thread()->Invoke<bool>( |
| 2886 | RTC_FROM_HERE, |
| 2887 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 2888 | stun_servers, turn_servers, modified_config.type, |
| 2889 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2890 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2891 | modified_config.turn_customizer, |
| 2892 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2893 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2894 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 2895 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2896 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2897 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 2898 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 2899 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2900 | if (modified_config.servers != configuration_.servers || |
| 2901 | modified_config.type != configuration_.type || |
| 2902 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2903 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2904 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2905 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 2906 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2907 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2908 | configuration_ = modified_config; |
| 2909 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2912 | bool PeerConnection::AddIceCandidate( |
| 2913 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2914 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2915 | if (IsClosed()) { |
| 2916 | return false; |
| 2917 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2918 | |
| 2919 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2920 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2921 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2922 | return false; |
| 2923 | } |
| 2924 | |
| 2925 | if (!ice_candidate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2926 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2927 | return false; |
| 2928 | } |
| 2929 | |
| 2930 | bool valid = false; |
| 2931 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 2932 | if (!valid) { |
| 2933 | return false; |
| 2934 | } |
| 2935 | |
| 2936 | // Add this candidate to the remote session description. |
| 2937 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2938 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2939 | return false; |
| 2940 | } |
| 2941 | |
| 2942 | if (ready) { |
| 2943 | return UseCandidate(ice_candidate); |
| 2944 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2945 | RTC_LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2946 | return true; |
| 2947 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2950 | bool PeerConnection::RemoveIceCandidates( |
| 2951 | const std::vector<cricket::Candidate>& candidates) { |
| 2952 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2953 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2954 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2955 | "removed without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2956 | return false; |
| 2957 | } |
| 2958 | |
| 2959 | if (candidates.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2960 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2961 | return false; |
| 2962 | } |
| 2963 | |
| 2964 | size_t number_removed = |
| 2965 | mutable_remote_description()->RemoveCandidates(candidates); |
| 2966 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2967 | RTC_LOG(LS_ERROR) |
| 2968 | << "RemoveRemoteIceCandidates: Failed to remove candidates. " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2969 | "Requested " |
| 2970 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2971 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2975 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 2976 | if (!error.ok()) { |
| 2977 | RTC_LOG(LS_ERROR) << "Error when removing remote candidates: " |
| 2978 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2979 | } |
| 2980 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2983 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2984 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2985 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2986 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2987 | if (transport_controller()) { |
| 2988 | transport_controller()->SetMetricsObserver(uma_observer_); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 2991 | for (auto transceiver : transceivers_) { |
| 2992 | auto* channel = transceiver->internal()->channel(); |
| 2993 | if (channel) { |
| 2994 | channel->SetMetricsObserver(uma_observer_); |
| 2995 | } |
| 2996 | } |
| 2997 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2998 | // Send information about IPv4/IPv6 status. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2999 | if (uma_observer_) { |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 3000 | port_allocator_->SetMetricsObserver(uma_observer_); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 3001 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 3002 | uma_observer_->IncrementEnumCounter( |
| 3003 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 3004 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 3005 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 3006 | uma_observer_->IncrementEnumCounter( |
| 3007 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 3008 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 3009 | } |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 3010 | // Send information about the requested SDP semantics. |
| 3011 | switch (configuration_.sdp_semantics) { |
| 3012 | case SdpSemantics::kDefault: |
| 3013 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 3014 | kSdpSemanticRequestDefault, |
| 3015 | kSdpSemanticRequestMax); |
| 3016 | |
| 3017 | break; |
| 3018 | case SdpSemantics::kPlanB: |
| 3019 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 3020 | kSdpSemanticRequestPlanB, |
| 3021 | kSdpSemanticRequestMax); |
| 3022 | break; |
| 3023 | case SdpSemantics::kUnifiedPlan: |
| 3024 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 3025 | kSdpSemanticRequestUnifiedPlan, |
| 3026 | kSdpSemanticRequestMax); |
| 3027 | break; |
| 3028 | default: |
| 3029 | RTC_NOTREACHED(); |
| 3030 | } |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 3031 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3034 | RTCError PeerConnection::SetBitrate(const BitrateParameters& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3035 | if (!worker_thread()->IsCurrent()) { |
| 3036 | return worker_thread()->Invoke<RTCError>( |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3037 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::SetBitrate, this, bitrate)); |
| 3038 | } |
| 3039 | |
| 3040 | const bool has_min = static_cast<bool>(bitrate.min_bitrate_bps); |
| 3041 | const bool has_current = static_cast<bool>(bitrate.current_bitrate_bps); |
| 3042 | const bool has_max = static_cast<bool>(bitrate.max_bitrate_bps); |
| 3043 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3044 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3045 | "min_bitrate_bps <= 0"); |
| 3046 | } |
| 3047 | if (has_current) { |
| 3048 | if (has_min && *bitrate.current_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3049 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3050 | "current_bitrate_bps < min_bitrate_bps"); |
| 3051 | } else if (*bitrate.current_bitrate_bps < 0) { |
| 3052 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3053 | "curent_bitrate_bps < 0"); |
| 3054 | } |
| 3055 | } |
| 3056 | if (has_max) { |
| 3057 | if (has_current && |
| 3058 | *bitrate.max_bitrate_bps < *bitrate.current_bitrate_bps) { |
| 3059 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3060 | "max_bitrate_bps < current_bitrate_bps"); |
| 3061 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3062 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3063 | "max_bitrate_bps < min_bitrate_bps"); |
| 3064 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3065 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3066 | "max_bitrate_bps < 0"); |
| 3067 | } |
| 3068 | } |
| 3069 | |
Sebastian Jansson | fc8d26b | 2018-02-21 09:52:06 +0100 | [diff] [blame] | 3070 | BitrateConstraintsMask mask; |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3071 | mask.min_bitrate_bps = bitrate.min_bitrate_bps; |
| 3072 | mask.start_bitrate_bps = bitrate.current_bitrate_bps; |
| 3073 | mask.max_bitrate_bps = bitrate.max_bitrate_bps; |
| 3074 | |
| 3075 | RTC_DCHECK(call_.get()); |
Sebastian Jansson | 8f83b42 | 2018-02-21 13:07:13 +0100 | [diff] [blame] | 3076 | call_->GetTransportControllerSend()->SetClientBitratePreferences(mask); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3077 | |
| 3078 | return RTCError::OK(); |
| 3079 | } |
| 3080 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3081 | void PeerConnection::SetBitrateAllocationStrategy( |
| 3082 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 3083 | bitrate_allocation_strategy) { |
| 3084 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 3085 | if (!worker_thread->IsCurrent()) { |
| 3086 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 3087 | bitrate_allocation_strategy.release(); |
| 3088 | auto functor = [this, strategy_raw]() { |
| 3089 | call_->SetBitrateAllocationStrategy( |
| 3090 | rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
| 3091 | }; |
| 3092 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 3093 | return; |
| 3094 | } |
| 3095 | RTC_DCHECK(call_.get()); |
| 3096 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 3097 | } |
| 3098 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3099 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3100 | if (!worker_thread()->IsCurrent()) { |
| 3101 | worker_thread()->Invoke<void>( |
| 3102 | RTC_FROM_HERE, |
| 3103 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3104 | return; |
| 3105 | } |
| 3106 | auto audio_state = |
| 3107 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3108 | audio_state->SetPlayout(playout); |
| 3109 | } |
| 3110 | |
| 3111 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3112 | if (!worker_thread()->IsCurrent()) { |
| 3113 | worker_thread()->Invoke<void>( |
| 3114 | RTC_FROM_HERE, |
| 3115 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3116 | return; |
| 3117 | } |
| 3118 | auto audio_state = |
| 3119 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 3120 | audio_state->SetRecording(recording); |
| 3121 | } |
| 3122 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3123 | std::unique_ptr<rtc::SSLCertificate> |
| 3124 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3125 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3126 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3127 | return nullptr; |
| 3128 | } |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3129 | return chain->Get(0).GetUniqueReference(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3130 | } |
| 3131 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3132 | std::unique_ptr<rtc::SSLCertChain> |
| 3133 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3134 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3135 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3136 | return nullptr; |
| 3137 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3138 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3139 | audio_transceiver->internal()->channel()->transport_name()); |
| 3140 | } |
| 3141 | |
| 3142 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3143 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3144 | for (auto transceiver : transceivers_) { |
| 3145 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3146 | return transceiver; |
| 3147 | } |
| 3148 | } |
| 3149 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3150 | } |
| 3151 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3152 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3153 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3154 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3155 | const size_t max_size = (max_size_bytes < 0) |
| 3156 | ? RtcEventLog::kUnlimitedOutput |
| 3157 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 3158 | return StartRtcEventLog( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3159 | rtc::MakeUnique<RtcEventLogOutputFile>(file, max_size), |
| 3160 | webrtc::RtcEventLog::kImmediateOutput); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3161 | } |
| 3162 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3163 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3164 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3165 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3166 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3167 | bool operator()() { |
| 3168 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3169 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3170 | PeerConnection* const pc; |
| 3171 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3172 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3173 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3174 | return worker_thread()->Invoke<bool>( |
| 3175 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3179 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3180 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3181 | } |
| 3182 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3183 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3184 | return pending_local_description_ ? pending_local_description_.get() |
| 3185 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3189 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3190 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3193 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3194 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3195 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3199 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3200 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3204 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3205 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3209 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3210 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3211 | } |
| 3212 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3213 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3214 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3215 | // Update stats here so that we have the most recent stats for tracks and |
| 3216 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3217 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3218 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3219 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3220 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3221 | for (auto transceiver : transceivers_) { |
| 3222 | transceiver->Stop(); |
| 3223 | } |
| 3224 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3225 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3226 | // The event log is used in the transport controller, which must be outlived |
| 3227 | // by the former. CreateOffer by the peer connection is implemented |
| 3228 | // asynchronously and if the peer connection is closed without resetting the |
| 3229 | // WebRTC session description factory, the session description factory would |
| 3230 | // call the transport controller. |
| 3231 | webrtc_session_desc_factory_.reset(); |
| 3232 | transport_controller_.reset(); |
| 3233 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3234 | network_thread()->Invoke<void>( |
| 3235 | RTC_FROM_HERE, |
| 3236 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3237 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3238 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3239 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3240 | call_.reset(); |
| 3241 | // The event log must outlive call (and any other object that uses it). |
| 3242 | event_log_.reset(); |
| 3243 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 3246 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3247 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3248 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3249 | SetSessionDescriptionMsg* param = |
| 3250 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3251 | param->observer->OnSuccess(); |
| 3252 | delete param; |
| 3253 | break; |
| 3254 | } |
| 3255 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3256 | SetSessionDescriptionMsg* param = |
| 3257 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3258 | param->observer->OnFailure(std::move(param->error)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3259 | delete param; |
| 3260 | break; |
| 3261 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3262 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3263 | CreateSessionDescriptionMsg* param = |
| 3264 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3265 | param->observer->OnFailure(std::move(param->error)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3266 | delete param; |
| 3267 | break; |
| 3268 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3269 | case MSG_GETSTATS: { |
| 3270 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 3271 | StatsReports reports; |
| 3272 | stats_->GetStats(param->track, &reports); |
| 3273 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3274 | delete param; |
| 3275 | break; |
| 3276 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 3277 | case MSG_FREE_DATACHANNELS: { |
| 3278 | sctp_data_channels_to_free_.clear(); |
| 3279 | break; |
| 3280 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3281 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3282 | RTC_NOTREACHED() << "Not implemented"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3283 | break; |
| 3284 | } |
| 3285 | } |
| 3286 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3287 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3288 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3289 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3290 | GetAudioTransceiver()->internal()->channel()); |
| 3291 | if (voice_channel) { |
| 3292 | return voice_channel->media_channel(); |
| 3293 | } else { |
| 3294 | return nullptr; |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3299 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3300 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3301 | GetVideoTransceiver()->internal()->channel()); |
| 3302 | if (video_channel) { |
| 3303 | return video_channel->media_channel(); |
| 3304 | } else { |
| 3305 | return nullptr; |
| 3306 | } |
| 3307 | } |
| 3308 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3309 | void PeerConnection::CreateAudioReceiver( |
| 3310 | MediaStreamInterface* stream, |
| 3311 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3312 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3313 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3314 | auto* audio_receiver = new AudioRtpReceiver( |
| 3315 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3316 | audio_receiver->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3317 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3318 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3319 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3320 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3321 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3324 | void PeerConnection::CreateVideoReceiver( |
| 3325 | MediaStreamInterface* stream, |
| 3326 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3327 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3328 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3329 | auto* video_receiver = new VideoRtpReceiver( |
| 3330 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3331 | video_receiver->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3332 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3333 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3334 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3335 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3336 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3339 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3340 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3341 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3342 | const RtpSenderInfo& remote_sender_info) { |
| 3343 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3344 | if (!receiver) { |
| 3345 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3346 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3347 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3348 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3349 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3350 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3351 | } else { |
| 3352 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3353 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3354 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3357 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3358 | MediaStreamInterface* stream) { |
| 3359 | RTC_DCHECK(!IsClosed()); |
| 3360 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3361 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3362 | // We already have a sender for this track, so just change the stream_id |
| 3363 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 3364 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3365 | return; |
| 3366 | } |
| 3367 | |
| 3368 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3369 | auto new_sender = |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 3370 | CreateSender(cricket::MEDIA_TYPE_AUDIO, track, {stream->id()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3371 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3372 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3373 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3374 | // which will connect the sender to the underlying transport. This can |
| 3375 | // occur if a local session description that contains the ID of the sender |
| 3376 | // is set before AddStream is called. It can also occur if the local |
| 3377 | // session description is not changed and RemoveStream is called, and |
| 3378 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3379 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3380 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3381 | if (sender_info) { |
| 3382 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3387 | // indefinitely, when we have unified plan SDP. |
| 3388 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3389 | MediaStreamInterface* stream) { |
| 3390 | RTC_DCHECK(!IsClosed()); |
| 3391 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3392 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3393 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3394 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3395 | return; |
| 3396 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3397 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3401 | MediaStreamInterface* stream) { |
| 3402 | RTC_DCHECK(!IsClosed()); |
| 3403 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3404 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3405 | // We already have a sender for this track, so just change the stream_id |
| 3406 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 3407 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3408 | return; |
| 3409 | } |
| 3410 | |
| 3411 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3412 | auto new_sender = |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 3413 | CreateSender(cricket::MEDIA_TYPE_VIDEO, track, {stream->id()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3414 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3415 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3416 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3417 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3418 | if (sender_info) { |
| 3419 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3424 | MediaStreamInterface* stream) { |
| 3425 | RTC_DCHECK(!IsClosed()); |
| 3426 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3427 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3428 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3429 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3430 | return; |
| 3431 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3432 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3433 | } |
| 3434 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3435 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3436 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3437 | if (ice_connection_state_ == new_state) { |
| 3438 | return; |
| 3439 | } |
| 3440 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3441 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3442 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3443 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3444 | return; |
| 3445 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3446 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3447 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3448 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3449 | RTC_DCHECK(ice_connection_state_ != |
| 3450 | PeerConnectionInterface::kIceConnectionClosed); |
| 3451 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3452 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3453 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | void PeerConnection::OnIceGatheringChange( |
| 3457 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3458 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3459 | if (IsClosed()) { |
| 3460 | return; |
| 3461 | } |
| 3462 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3463 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3466 | void PeerConnection::OnIceCandidate( |
| 3467 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3468 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3469 | if (IsClosed()) { |
| 3470 | return; |
| 3471 | } |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3472 | observer_->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3475 | void PeerConnection::OnIceCandidatesRemoved( |
| 3476 | const std::vector<cricket::Candidate>& candidates) { |
| 3477 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3478 | if (IsClosed()) { |
| 3479 | return; |
| 3480 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3481 | observer_->OnIceCandidatesRemoved(candidates); |
| 3482 | } |
| 3483 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3484 | void PeerConnection::ChangeSignalingState( |
| 3485 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3486 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3487 | if (signaling_state_ == signaling_state) { |
| 3488 | return; |
| 3489 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3490 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3491 | << GetSignalingStateString(signaling_state_) |
| 3492 | << " New state: " |
| 3493 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3494 | signaling_state_ = signaling_state; |
| 3495 | if (signaling_state == kClosed) { |
| 3496 | ice_connection_state_ = kIceConnectionClosed; |
| 3497 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 3498 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3499 | ice_gathering_state_ = kIceGatheringComplete; |
| 3500 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 3501 | } |
| 3502 | } |
| 3503 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3506 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3507 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3508 | if (IsClosed()) { |
| 3509 | return; |
| 3510 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3511 | AddAudioTrack(track, stream); |
| 3512 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3513 | } |
| 3514 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3515 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3516 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3517 | if (IsClosed()) { |
| 3518 | return; |
| 3519 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3520 | RemoveAudioTrack(track, stream); |
| 3521 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3522 | } |
| 3523 | |
| 3524 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3525 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3526 | if (IsClosed()) { |
| 3527 | return; |
| 3528 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3529 | AddVideoTrack(track, stream); |
| 3530 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3534 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3535 | if (IsClosed()) { |
| 3536 | return; |
| 3537 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3538 | RemoveVideoTrack(track, stream); |
| 3539 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3540 | } |
| 3541 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3542 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3543 | SetSessionDescriptionObserver* observer) { |
| 3544 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3545 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3546 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 3547 | } |
| 3548 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3549 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3550 | SetSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3551 | RTCError&& error) { |
| 3552 | RTC_DCHECK(!error.ok()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3553 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3554 | msg->error = std::move(error); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3555 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3556 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3560 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3561 | RTCError error) { |
| 3562 | RTC_DCHECK(!error.ok()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3563 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 3564 | msg->error = std::move(error); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3565 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3566 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3567 | } |
| 3568 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3569 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3570 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3571 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3572 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3573 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3574 | if (IsUnifiedPlan()) { |
| 3575 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3576 | } else { |
| 3577 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3578 | } |
| 3579 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3580 | // Intentionally unset the data channel type for RTP data channel with the |
| 3581 | // second condition. Otherwise the RTP data channels would be successfully |
| 3582 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3583 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3584 | // people won't try to use them. |
| 3585 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3586 | session_options->data_channel_type = data_channel_type(); |
| 3587 | } |
| 3588 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3589 | // Apply ICE restart flag and renomination flag. |
| 3590 | for (auto& options : session_options->media_description_options) { |
| 3591 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3592 | options.transport_options.enable_ice_renomination = |
| 3593 | configuration_.enable_ice_renomination; |
| 3594 | } |
| 3595 | |
| 3596 | session_options->rtcp_cname = rtcp_cname_; |
| 3597 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3598 | session_options->is_unified_plan = IsUnifiedPlan(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3602 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3603 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3604 | // Figure out transceiver directional preferences. |
| 3605 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3606 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3607 | |
| 3608 | // By default, generate sendrecv/recvonly m= sections. |
| 3609 | bool recv_audio = true; |
| 3610 | bool recv_video = true; |
| 3611 | |
| 3612 | // By default, only offer a new m= section if we have media to send with it. |
| 3613 | bool offer_new_audio_description = send_audio; |
| 3614 | bool offer_new_video_description = send_video; |
| 3615 | bool offer_new_data_description = HasDataChannels(); |
| 3616 | |
| 3617 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3618 | if (offer_answer_options.offer_to_receive_audio != |
| 3619 | RTCOfferAnswerOptions::kUndefined) { |
| 3620 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3621 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3622 | offer_new_audio_description || |
| 3623 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3624 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3625 | if (offer_answer_options.offer_to_receive_video != |
| 3626 | RTCOfferAnswerOptions::kUndefined) { |
| 3627 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3628 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3629 | offer_new_video_description || |
| 3630 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | rtc::Optional<size_t> audio_index; |
| 3634 | rtc::Optional<size_t> video_index; |
| 3635 | rtc::Optional<size_t> data_index; |
| 3636 | // If a current description exists, generate m= sections in the same order, |
| 3637 | // using the first audio/video/data section that appears and rejecting |
| 3638 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3639 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3640 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3641 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3642 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3643 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3644 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3645 | } |
| 3646 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3647 | // Add audio/video/data m= sections to the end if needed. |
| 3648 | if (!audio_index && offer_new_audio_description) { |
| 3649 | session_options->media_description_options.push_back( |
| 3650 | cricket::MediaDescriptionOptions( |
| 3651 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3652 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3653 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3654 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3655 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3656 | if (!video_index && offer_new_video_description) { |
| 3657 | session_options->media_description_options.push_back( |
| 3658 | cricket::MediaDescriptionOptions( |
| 3659 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3660 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3661 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3662 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3663 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3664 | if (!data_index && offer_new_data_description) { |
| 3665 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3666 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3667 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3671 | !audio_index ? nullptr |
| 3672 | : &session_options->media_description_options[*audio_index]; |
| 3673 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3674 | !video_index ? nullptr |
| 3675 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3676 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3677 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3678 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | // Find a new MID that is not already in |used_mids|, then add it to |used_mids| |
| 3682 | // and return a reference to it. |
| 3683 | // Generated MIDs should be no more than 3 bytes long to take up less space in |
| 3684 | // the RTP packet. |
| 3685 | static const std::string& AllocateMid(std::set<std::string>* used_mids) { |
| 3686 | RTC_DCHECK(used_mids); |
| 3687 | // We're boring: just generate MIDs 0, 1, 2, ... |
| 3688 | size_t i = 0; |
| 3689 | std::set<std::string>::iterator it; |
| 3690 | bool inserted; |
| 3691 | do { |
| 3692 | std::string mid = rtc::ToString(i++); |
| 3693 | auto insert_result = used_mids->insert(mid); |
| 3694 | it = insert_result.first; |
| 3695 | inserted = insert_result.second; |
| 3696 | } while (!inserted); |
| 3697 | return *it; |
| 3698 | } |
| 3699 | |
| 3700 | static cricket::MediaDescriptionOptions |
| 3701 | GetMediaDescriptionOptionsForTransceiver( |
| 3702 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3703 | transceiver, |
| 3704 | const std::string& mid) { |
| 3705 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3706 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3707 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3708 | // This behavior is specified in JSEP. The gist is that: |
| 3709 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 3710 | // sendrecv. |
| 3711 | // 2. If the MSID is included, then it must be included in any subsequent |
| 3712 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 3713 | if (!transceiver->stopped() && |
| 3714 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 3715 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 3716 | cricket::SenderOptions sender_options; |
| 3717 | sender_options.track_id = transceiver->sender()->id(); |
| 3718 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 3719 | // TODO(bugs.webrtc.org/7600): Set num_sim_layers to the number of encodings |
| 3720 | // set in the RTP parameters when the transceiver was added. |
| 3721 | sender_options.num_sim_layers = 1; |
| 3722 | media_description_options.sender_options.push_back(sender_options); |
| 3723 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3724 | return media_description_options; |
| 3725 | } |
| 3726 | |
| 3727 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 3728 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3729 | cricket::MediaSessionOptions* session_options) { |
| 3730 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3731 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3732 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3733 | const ContentInfos& local_contents = |
| 3734 | (local_description() ? local_description()->description()->contents() |
| 3735 | : ContentInfos()); |
| 3736 | const ContentInfos& remote_contents = |
| 3737 | (remote_description() ? remote_description()->description()->contents() |
| 3738 | : ContentInfos()); |
| 3739 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3740 | // slots first. |
| 3741 | std::queue<size_t> recycleable_mline_indices; |
| 3742 | // Track the MIDs used in previous offer/answer exchanges and the current |
| 3743 | // offer so that new, unique MIDs are generated. |
| 3744 | std::set<std::string> used_mids = seen_mids_; |
| 3745 | // First, go through each media section that exists in either the local or |
| 3746 | // remote description and generate a media section in this offer for the |
| 3747 | // associated transceiver. If a media section can be recycled, generate a |
| 3748 | // default, rejected media section here that can be later overwritten. |
| 3749 | for (size_t i = 0; |
| 3750 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3751 | // Either |local_content| or |remote_content| is non-null. |
| 3752 | const ContentInfo* local_content = |
| 3753 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3754 | const ContentInfo* remote_content = |
| 3755 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3756 | bool had_been_rejected = (local_content && local_content->rejected) || |
| 3757 | (remote_content && remote_content->rejected); |
| 3758 | const std::string& mid = |
| 3759 | (local_content ? local_content->name : remote_content->name); |
| 3760 | cricket::MediaType media_type = |
| 3761 | (local_content ? local_content->media_description()->type() |
| 3762 | : remote_content->media_description()->type()); |
| 3763 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3764 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3765 | auto transceiver = GetAssociatedTransceiver(mid); |
| 3766 | RTC_CHECK(transceiver); |
| 3767 | // A media section is considered eligible for recycling if it is marked as |
| 3768 | // rejected in either the local or remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3769 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3770 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3771 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 3772 | RtpTransceiverDirection::kInactive, |
| 3773 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3774 | recycleable_mline_indices.push(i); |
| 3775 | } else { |
| 3776 | session_options->media_description_options.push_back( |
| 3777 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 3778 | // CreateOffer shouldn't really cause any state changes in |
| 3779 | // PeerConnection, but we need a way to match new transceivers to new |
| 3780 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 3781 | // by recording the index of the media section generated for the |
| 3782 | // transceiver in the offer. |
| 3783 | transceiver->internal()->set_mline_index(i); |
| 3784 | } |
| 3785 | } else { |
| 3786 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3787 | RTC_CHECK(GetDataMid()); |
| 3788 | if (had_been_rejected || mid != *GetDataMid()) { |
| 3789 | session_options->media_description_options.push_back( |
| 3790 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 3791 | } else { |
| 3792 | session_options->media_description_options.push_back( |
| 3793 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 3794 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3795 | } |
| 3796 | } |
| 3797 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3798 | // and not associated). Reuse media sections marked as recyclable first, |
| 3799 | // otherwise append to the end of the offer. New media sections should be |
| 3800 | // added in the order they were added to the PeerConnection. |
| 3801 | for (auto transceiver : transceivers_) { |
| 3802 | if (transceiver->mid() || transceiver->stopped()) { |
| 3803 | continue; |
| 3804 | } |
| 3805 | size_t mline_index; |
| 3806 | if (!recycleable_mline_indices.empty()) { |
| 3807 | mline_index = recycleable_mline_indices.front(); |
| 3808 | recycleable_mline_indices.pop(); |
| 3809 | session_options->media_description_options[mline_index] = |
| 3810 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3811 | AllocateMid(&used_mids)); |
| 3812 | } else { |
| 3813 | mline_index = session_options->media_description_options.size(); |
| 3814 | session_options->media_description_options.push_back( |
| 3815 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3816 | AllocateMid(&used_mids))); |
| 3817 | } |
| 3818 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3819 | transceiver->internal()->set_mline_index(mline_index); |
| 3820 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3821 | // Lastly, add a m-section if we have local data channels and an m section |
| 3822 | // does not already exist. |
| 3823 | if (!GetDataMid() && HasDataChannels()) { |
| 3824 | session_options->media_description_options.push_back( |
| 3825 | GetMediaDescriptionOptionsForActiveData(AllocateMid(&used_mids))); |
| 3826 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | void PeerConnection::GetOptionsForAnswer( |
| 3830 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3831 | cricket::MediaSessionOptions* session_options) { |
| 3832 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3833 | |
| 3834 | if (IsUnifiedPlan()) { |
| 3835 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3836 | } else { |
| 3837 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3838 | } |
| 3839 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3840 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3841 | // the RTP data channels would be successfully negotiated by default and the |
| 3842 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3843 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 3844 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3845 | session_options->data_channel_type = data_channel_type(); |
| 3846 | } |
| 3847 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3848 | // Apply ICE renomination flag. |
| 3849 | for (auto& options : session_options->media_description_options) { |
| 3850 | options.transport_options.enable_ice_renomination = |
| 3851 | configuration_.enable_ice_renomination; |
| 3852 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 3853 | |
| 3854 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 3855 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3856 | session_options->is_unified_plan = IsUnifiedPlan(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3857 | } |
| 3858 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3859 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 3860 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3861 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3862 | // Figure out transceiver directional preferences. |
| 3863 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3864 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3865 | |
| 3866 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 3867 | // restricted by the direction in the offer. |
| 3868 | bool recv_audio = true; |
| 3869 | bool recv_video = true; |
| 3870 | |
| 3871 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3872 | if (offer_answer_options.offer_to_receive_audio != |
| 3873 | RTCOfferAnswerOptions::kUndefined) { |
| 3874 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 3875 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3876 | if (offer_answer_options.offer_to_receive_video != |
| 3877 | RTCOfferAnswerOptions::kUndefined) { |
| 3878 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | rtc::Optional<size_t> audio_index; |
| 3882 | rtc::Optional<size_t> video_index; |
| 3883 | rtc::Optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 3884 | |
| 3885 | // Generate m= sections that match those in the offer. |
| 3886 | // Note that mediasession.cc will handle intersection our preferred |
| 3887 | // direction with the offered direction. |
| 3888 | GenerateMediaDescriptionOptions( |
| 3889 | remote_description(), |
| 3890 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3891 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 3892 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3893 | |
| 3894 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3895 | !audio_index ? nullptr |
| 3896 | : &session_options->media_description_options[*audio_index]; |
| 3897 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3898 | !video_index ? nullptr |
| 3899 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3900 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3901 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3902 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3903 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 3904 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3905 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 3906 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3907 | cricket::MediaSessionOptions* session_options) { |
| 3908 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 3909 | // Answers) and 5.3.2 (Subsequent Answers). |
| 3910 | RTC_DCHECK(remote_description()); |
| 3911 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3912 | for (const ContentInfo& content : |
| 3913 | remote_description()->description()->contents()) { |
| 3914 | cricket::MediaType media_type = content.media_description()->type(); |
| 3915 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3916 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3917 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3918 | RTC_CHECK(transceiver); |
| 3919 | session_options->media_description_options.push_back( |
| 3920 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 3921 | } else { |
| 3922 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3923 | // Reject all data sections if data channels are disabled. |
| 3924 | // Reject a data section if it has already been rejected. |
| 3925 | // Reject all data sections except for the first one. |
| 3926 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 3927 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3928 | session_options->media_description_options.push_back( |
| 3929 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 3930 | } else { |
| 3931 | session_options->media_description_options.push_back( |
| 3932 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 3933 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3934 | } |
| 3935 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3936 | } |
| 3937 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3938 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 3939 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3940 | RtpTransceiverDirection audio_direction, |
| 3941 | RtpTransceiverDirection video_direction, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3942 | rtc::Optional<size_t>* audio_index, |
| 3943 | rtc::Optional<size_t>* video_index, |
| 3944 | rtc::Optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3945 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3946 | for (const cricket::ContentInfo& content : |
| 3947 | session_desc->description()->contents()) { |
| 3948 | if (IsAudioContent(&content)) { |
| 3949 | // If we already have an audio m= section, reject this extra one. |
| 3950 | if (*audio_index) { |
| 3951 | session_options->media_description_options.push_back( |
| 3952 | cricket::MediaDescriptionOptions( |
| 3953 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3954 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3955 | } else { |
| 3956 | session_options->media_description_options.push_back( |
| 3957 | cricket::MediaDescriptionOptions( |
| 3958 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3959 | audio_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3960 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3961 | } |
| 3962 | } else if (IsVideoContent(&content)) { |
| 3963 | // If we already have an video m= section, reject this extra one. |
| 3964 | if (*video_index) { |
| 3965 | session_options->media_description_options.push_back( |
| 3966 | cricket::MediaDescriptionOptions( |
| 3967 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3968 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3969 | } else { |
| 3970 | session_options->media_description_options.push_back( |
| 3971 | cricket::MediaDescriptionOptions( |
| 3972 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3973 | video_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3974 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3975 | } |
| 3976 | } else { |
| 3977 | RTC_DCHECK(IsDataContent(&content)); |
| 3978 | // If we already have an data m= section, reject this extra one. |
| 3979 | if (*data_index) { |
| 3980 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3981 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3982 | } else { |
| 3983 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3984 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3985 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3986 | } |
| 3987 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3988 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3989 | } |
| 3990 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3991 | cricket::MediaDescriptionOptions |
| 3992 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 3993 | const std::string& mid) const { |
| 3994 | // Direction for data sections is meaningless, but legacy endpoints might |
| 3995 | // expect sendrecv. |
| 3996 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3997 | RtpTransceiverDirection::kSendRecv, |
| 3998 | /*stopped=*/false); |
| 3999 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4000 | return options; |
| 4001 | } |
| 4002 | |
| 4003 | cricket::MediaDescriptionOptions |
| 4004 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4005 | const std::string& mid) const { |
| 4006 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4007 | RtpTransceiverDirection::kInactive, |
| 4008 | /*stopped=*/true); |
| 4009 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4010 | return options; |
| 4011 | } |
| 4012 | |
| 4013 | rtc::Optional<std::string> PeerConnection::GetDataMid() const { |
| 4014 | switch (data_channel_type_) { |
| 4015 | case cricket::DCT_RTP: |
| 4016 | if (!rtp_data_channel_) { |
| 4017 | return rtc::nullopt; |
| 4018 | } |
| 4019 | return rtp_data_channel_->content_name(); |
| 4020 | case cricket::DCT_SCTP: |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4021 | return sctp_mid_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4022 | default: |
| 4023 | return rtc::nullopt; |
| 4024 | } |
| 4025 | } |
| 4026 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4027 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 4028 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4029 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4030 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 4031 | } |
| 4032 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4033 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4034 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4035 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4036 | cricket::MediaType media_type, |
| 4037 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 4038 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4039 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4040 | std::vector<RtpSenderInfo>* current_senders = |
| 4041 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4042 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4043 | // 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] | 4044 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4045 | for (auto sender_it = current_senders->begin(); |
| 4046 | sender_it != current_senders->end(); |
| 4047 | /* incremented manually */) { |
| 4048 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4049 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4050 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 4051 | bool sender_exists = params && params->id == info.sender_id && |
| 4052 | params->first_stream_id() == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4053 | // 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] | 4054 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4055 | sender_exists) { |
| 4056 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4057 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4058 | OnRemoteSenderRemoved(info, media_type); |
| 4059 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4060 | } |
| 4061 | } |
| 4062 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4063 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4064 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4065 | // |params.id| is the sender id and the stream id uses the first of |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 4066 | // |params.stream_ids|. The remote description could come from a Unified |
| 4067 | // Plan endpoint, with multiple or no stream_ids() signalled. Since this is |
| 4068 | // not supported in Plan B, we just take the first here and create an empty |
| 4069 | // stream id if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4070 | const std::string& stream_id = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 4071 | (!params.stream_ids().empty() ? params.stream_ids()[0] |
| 4072 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4073 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4074 | uint32_t ssrc = params.first_ssrc(); |
| 4075 | |
| 4076 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4077 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4078 | if (!stream) { |
| 4079 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4080 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4081 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4082 | remote_streams_->AddStream(stream); |
| 4083 | new_streams->AddStream(stream); |
| 4084 | } |
| 4085 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4086 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4087 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4088 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4089 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4090 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4091 | } |
| 4092 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4093 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4094 | // Add default sender if necessary. |
| 4095 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4096 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4097 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4098 | if (!default_stream) { |
| 4099 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4100 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4101 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4102 | remote_streams_->AddStream(default_stream); |
| 4103 | new_streams->AddStream(default_stream); |
| 4104 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4105 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4106 | ? kDefaultAudioSenderId |
| 4107 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4108 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4109 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4110 | if (!default_sender_info) { |
| 4111 | current_senders->push_back( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4112 | RtpSenderInfo(kDefaultStreamId, default_sender_id, 0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4113 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4114 | } |
| 4115 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4116 | } |
| 4117 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4118 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 4119 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4120 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 4121 | << " receiver for track_id=" << sender_info.sender_id |
| 4122 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4123 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4124 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4125 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4126 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4127 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4128 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4129 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 4130 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4131 | } |
| 4132 | } |
| 4133 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4134 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 4135 | cricket::MediaType media_type) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4136 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4137 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4138 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4139 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4140 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 4141 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4142 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4143 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4144 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4145 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4146 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4147 | } |
| 4148 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4149 | // Stopping or destroying a VideoRtpReceiver will end the |
| 4150 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4151 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4152 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4153 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4154 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4155 | // There's no guarantee the track is still available, e.g. the track may |
| 4156 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4157 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4158 | } |
| 4159 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 4160 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4161 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4162 | if (receiver) { |
| 4163 | observer_->OnRemoveTrack(receiver); |
| 4164 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4168 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4169 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4170 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4171 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4172 | streams_to_remove.push_back(stream); |
| 4173 | } |
| 4174 | } |
| 4175 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4176 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4177 | remote_streams_->RemoveStream(stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4178 | observer_->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4179 | } |
| 4180 | } |
| 4181 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4182 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4183 | const std::vector<cricket::StreamParams>& streams, |
| 4184 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4185 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4186 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4187 | // 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] | 4188 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4189 | for (auto sender_it = current_senders->begin(); |
| 4190 | sender_it != current_senders->end(); |
| 4191 | /* incremented manually */) { |
| 4192 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4193 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4194 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4195 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4196 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4197 | OnLocalSenderRemoved(info, media_type); |
| 4198 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4199 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4200 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4204 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4205 | for (const cricket::StreamParams& params : streams) { |
| 4206 | // 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] | 4207 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4208 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4209 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4210 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4211 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4212 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4213 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4214 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4215 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4216 | } |
| 4217 | } |
| 4218 | } |
| 4219 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4220 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4221 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 4222 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4223 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4224 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4225 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4226 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4227 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4228 | return; |
| 4229 | } |
| 4230 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4231 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4232 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4233 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4234 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4235 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4236 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame^] | 4237 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4238 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4239 | } |
| 4240 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4241 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4242 | cricket::MediaType media_type) { |
| 4243 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4244 | if (!sender) { |
| 4245 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4246 | // SessionDescriptions has been renegotiated. |
| 4247 | return; |
| 4248 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4249 | |
| 4250 | // A sender has been removed from the SessionDescription but it's still |
| 4251 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4252 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4253 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4254 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4255 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4256 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4257 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4258 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4259 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4260 | } |
| 4261 | |
| 4262 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4263 | const cricket::StreamParamsVec& streams) { |
| 4264 | std::vector<std::string> existing_channels; |
| 4265 | |
| 4266 | // Find new and active data channels. |
| 4267 | for (const cricket::StreamParams& params : streams) { |
| 4268 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4269 | // we use the same naming of data channels as we do for |
| 4270 | // MediaStreams and Tracks. |
| 4271 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4272 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4273 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4274 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4275 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4276 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4277 | continue; |
| 4278 | } |
| 4279 | // Set the SSRC the data channel should use for sending. |
| 4280 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4281 | existing_channels.push_back(data_channel_it->first); |
| 4282 | } |
| 4283 | |
| 4284 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4285 | } |
| 4286 | |
| 4287 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4288 | const cricket::StreamParamsVec& streams) { |
| 4289 | std::vector<std::string> existing_channels; |
| 4290 | |
| 4291 | // Find new and active data channels. |
| 4292 | for (const cricket::StreamParams& params : streams) { |
| 4293 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4294 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4295 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4296 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4297 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4298 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4299 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4300 | // This is a new data channel. |
| 4301 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4302 | } else { |
| 4303 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4304 | } |
| 4305 | existing_channels.push_back(label); |
| 4306 | } |
| 4307 | |
| 4308 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4309 | } |
| 4310 | |
| 4311 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4312 | const std::vector<std::string>& active_channels, |
| 4313 | bool is_local_update) { |
| 4314 | auto it = rtp_data_channels_.begin(); |
| 4315 | while (it != rtp_data_channels_.end()) { |
| 4316 | DataChannel* data_channel = it->second; |
| 4317 | if (std::find(active_channels.begin(), active_channels.end(), |
| 4318 | data_channel->label()) != active_channels.end()) { |
| 4319 | ++it; |
| 4320 | continue; |
| 4321 | } |
| 4322 | |
| 4323 | if (is_local_update) { |
| 4324 | data_channel->SetSendSsrc(0); |
| 4325 | } else { |
| 4326 | data_channel->RemotePeerRequestClose(); |
| 4327 | } |
| 4328 | |
| 4329 | if (data_channel->state() == DataChannel::kClosed) { |
| 4330 | rtp_data_channels_.erase(it); |
| 4331 | it = rtp_data_channels_.begin(); |
| 4332 | } else { |
| 4333 | ++it; |
| 4334 | } |
| 4335 | } |
| 4336 | } |
| 4337 | |
| 4338 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4339 | uint32_t remote_ssrc) { |
| 4340 | rtc::scoped_refptr<DataChannel> channel( |
| 4341 | InternalCreateDataChannel(label, nullptr)); |
| 4342 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4343 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4344 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4345 | return; |
| 4346 | } |
| 4347 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4348 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4349 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4350 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4354 | const std::string& label, |
| 4355 | const InternalDataChannelInit* config) { |
| 4356 | if (IsClosed()) { |
| 4357 | return nullptr; |
| 4358 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4359 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4360 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4361 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4362 | return nullptr; |
| 4363 | } |
| 4364 | InternalDataChannelInit new_config = |
| 4365 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4366 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4367 | if (new_config.id < 0) { |
| 4368 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4369 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4370 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4371 | RTC_LOG(LS_ERROR) |
| 4372 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4373 | return nullptr; |
| 4374 | } |
| 4375 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4376 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4377 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4378 | return nullptr; |
| 4379 | } |
| 4380 | } |
| 4381 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4382 | rtc::scoped_refptr<DataChannel> channel( |
| 4383 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4384 | if (!channel) { |
| 4385 | sid_allocator_.ReleaseSid(new_config.id); |
| 4386 | return nullptr; |
| 4387 | } |
| 4388 | |
| 4389 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4390 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4391 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4392 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4393 | return nullptr; |
| 4394 | } |
| 4395 | rtp_data_channels_[channel->label()] = channel; |
| 4396 | } else { |
| 4397 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 4398 | sctp_data_channels_.push_back(channel); |
| 4399 | channel->SignalClosed.connect(this, |
| 4400 | &PeerConnection::OnSctpDataChannelClosed); |
| 4401 | } |
| 4402 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4403 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4404 | return channel; |
| 4405 | } |
| 4406 | |
| 4407 | bool PeerConnection::HasDataChannels() const { |
| 4408 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4409 | } |
| 4410 | |
| 4411 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4412 | for (const auto& channel : sctp_data_channels_) { |
| 4413 | if (channel->id() < 0) { |
| 4414 | int sid; |
| 4415 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4416 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4417 | continue; |
| 4418 | } |
| 4419 | channel->SetSctpSid(sid); |
| 4420 | } |
| 4421 | } |
| 4422 | } |
| 4423 | |
| 4424 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4425 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4426 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4427 | ++it) { |
| 4428 | if (it->get() == channel) { |
| 4429 | if (channel->id() >= 0) { |
| 4430 | sid_allocator_.ReleaseSid(channel->id()); |
| 4431 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4432 | // Since this method is triggered by a signal from the DataChannel, |
| 4433 | // we can't free it directly here; we need to free it asynchronously. |
| 4434 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4435 | sctp_data_channels_.erase(it); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 4436 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4437 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4438 | return; |
| 4439 | } |
| 4440 | } |
| 4441 | } |
| 4442 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4443 | void PeerConnection::OnDataChannelDestroyed() { |
| 4444 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4445 | // DataChannel may callback to us and try to modify the list. |
| 4446 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4447 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4448 | for (const auto& kv : temp_rtp_dcs) { |
| 4449 | kv.second->OnTransportChannelDestroyed(); |
| 4450 | } |
| 4451 | |
| 4452 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4453 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4454 | for (const auto& channel : temp_sctp_dcs) { |
| 4455 | channel->OnTransportChannelDestroyed(); |
| 4456 | } |
| 4457 | } |
| 4458 | |
| 4459 | void PeerConnection::OnDataChannelOpenMessage( |
| 4460 | const std::string& label, |
| 4461 | const InternalDataChannelInit& config) { |
| 4462 | rtc::scoped_refptr<DataChannel> channel( |
| 4463 | InternalCreateDataChannel(label, &config)); |
| 4464 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4465 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4466 | return; |
| 4467 | } |
| 4468 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4469 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4470 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4471 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4472 | } |
| 4473 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4474 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4475 | PeerConnection::GetAudioTransceiver() const { |
| 4476 | // This method only works with Plan B SDP, where there is a single |
| 4477 | // audio/video transceiver. |
| 4478 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4479 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4480 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4481 | return transceiver; |
| 4482 | } |
| 4483 | } |
| 4484 | RTC_NOTREACHED(); |
| 4485 | return nullptr; |
| 4486 | } |
| 4487 | |
| 4488 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4489 | PeerConnection::GetVideoTransceiver() const { |
| 4490 | // This method only works with Plan B SDP, where there is a single |
| 4491 | // audio/video transceiver. |
| 4492 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4493 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4494 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4495 | return transceiver; |
| 4496 | } |
| 4497 | } |
| 4498 | RTC_NOTREACHED(); |
| 4499 | return nullptr; |
| 4500 | } |
| 4501 | |
| 4502 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4503 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4504 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4505 | switch (type) { |
| 4506 | case cricket::MEDIA_TYPE_AUDIO: |
| 4507 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4508 | case cricket::MEDIA_TYPE_VIDEO: |
| 4509 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4510 | case cricket::MEDIA_TYPE_DATA: |
| 4511 | return false; |
| 4512 | } |
| 4513 | RTC_NOTREACHED(); |
| 4514 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4515 | } |
| 4516 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4517 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4518 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 4519 | for (auto transceiver : transceivers_) { |
| 4520 | for (auto sender : transceiver->internal()->senders()) { |
| 4521 | if (sender->track() == track) { |
| 4522 | return sender; |
| 4523 | } |
| 4524 | } |
| 4525 | } |
| 4526 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4529 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4530 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 4531 | for (auto transceiver : transceivers_) { |
| 4532 | for (auto sender : transceiver->internal()->senders()) { |
| 4533 | if (sender->id() == sender_id) { |
| 4534 | return sender; |
| 4535 | } |
| 4536 | } |
| 4537 | } |
| 4538 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4539 | } |
| 4540 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4541 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4542 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 4543 | for (auto transceiver : transceivers_) { |
| 4544 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4545 | if (receiver->id() == receiver_id) { |
| 4546 | return receiver; |
| 4547 | } |
| 4548 | } |
| 4549 | } |
| 4550 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4551 | } |
| 4552 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4553 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4554 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4555 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4556 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4557 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4558 | ? &remote_audio_sender_infos_ |
| 4559 | : &remote_video_sender_infos_; |
| 4560 | } |
| 4561 | |
| 4562 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4563 | cricket::MediaType media_type) { |
| 4564 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4565 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4566 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4567 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4568 | } |
| 4569 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4570 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4571 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4572 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4573 | const std::string sender_id) const { |
| 4574 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4575 | if (sender_info.stream_id == stream_id && |
| 4576 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4577 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4578 | } |
| 4579 | } |
| 4580 | return nullptr; |
| 4581 | } |
| 4582 | |
| 4583 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4584 | for (const auto& channel : sctp_data_channels_) { |
| 4585 | if (channel->id() == sid) { |
| 4586 | return channel; |
| 4587 | } |
| 4588 | } |
| 4589 | return nullptr; |
| 4590 | } |
| 4591 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4592 | bool PeerConnection::InitializePortAllocator_n( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4593 | const RTCConfiguration& configuration) { |
| 4594 | cricket::ServerAddresses stun_servers; |
| 4595 | std::vector<cricket::RelayServerConfig> turn_servers; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4596 | if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) != |
| 4597 | RTCErrorType::NONE) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4598 | return false; |
| 4599 | } |
| 4600 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 4601 | port_allocator_->Initialize(); |
| 4602 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4603 | // To handle both internal and externally created port allocator, we will |
| 4604 | // enable BUNDLE here. |
| 4605 | int portallocator_flags = port_allocator_->flags(); |
| 4606 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4607 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 4608 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4609 | // If the disable-IPv6 flag was specified, we'll not override it |
| 4610 | // by experiment. |
| 4611 | if (configuration.disable_ipv6) { |
| 4612 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 4613 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 4614 | .find("Disabled") == 0) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4615 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
| 4616 | } |
| 4617 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4618 | if (configuration.disable_ipv6_on_wifi) { |
| 4619 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4620 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4621 | } |
| 4622 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4623 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 4624 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4625 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4626 | } |
| 4627 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4628 | if (configuration.candidate_network_policy == |
| 4629 | kCandidateNetworkPolicyLowCost) { |
| 4630 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4631 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4632 | } |
| 4633 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4634 | if (configuration.disable_link_local_networks) { |
| 4635 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
| 4636 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 4637 | } |
| 4638 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4639 | port_allocator_->set_flags(portallocator_flags); |
| 4640 | // No step delay is used while allocating ports. |
| 4641 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 4642 | port_allocator_->set_candidate_filter( |
| 4643 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 4644 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4645 | |
| 4646 | // Call this last since it may create pooled allocator sessions using the |
| 4647 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4648 | port_allocator_->SetConfiguration( |
| 4649 | stun_servers, turn_servers, configuration.ice_candidate_pool_size, |
| 4650 | configuration.prune_turn_ports, configuration.turn_customizer, |
| 4651 | configuration.stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4652 | return true; |
| 4653 | } |
| 4654 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4655 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4656 | const cricket::ServerAddresses& stun_servers, |
| 4657 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 4658 | IceTransportsType type, |
| 4659 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4660 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4661 | webrtc::TurnCustomizer* turn_customizer, |
| 4662 | rtc::Optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4663 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4664 | ConvertIceTransportTypeToCandidateFilter(type)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4665 | // Call this last since it may create pooled allocator sessions using the |
| 4666 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 4667 | return port_allocator_->SetConfiguration( |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4668 | stun_servers, turn_servers, candidate_pool_size, prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4669 | turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4670 | } |
| 4671 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4672 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 4673 | return factory_->channel_manager(); |
| 4674 | } |
| 4675 | |
| 4676 | MetricsObserverInterface* PeerConnection::metrics_observer() const { |
| 4677 | return uma_observer_; |
| 4678 | } |
| 4679 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 4680 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4681 | std::unique_ptr<RtcEventLogOutput> output, |
| 4682 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4683 | if (!event_log_) { |
| 4684 | return false; |
| 4685 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4686 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4690 | if (event_log_) { |
| 4691 | event_log_->StopLogging(); |
| 4692 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4693 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4694 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4695 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 4696 | const std::string& content_name) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4697 | for (auto transceiver : transceivers_) { |
| 4698 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4699 | if (channel && channel->content_name() == content_name) { |
| 4700 | return channel; |
| 4701 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4702 | } |
| 4703 | if (rtp_data_channel() && |
| 4704 | rtp_data_channel()->content_name() == content_name) { |
| 4705 | return rtp_data_channel(); |
| 4706 | } |
| 4707 | return nullptr; |
| 4708 | } |
| 4709 | |
| 4710 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 4711 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4712 | RTC_LOG(LS_INFO) |
| 4713 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4714 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4715 | return false; |
| 4716 | } |
| 4717 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4718 | 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] | 4719 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4720 | return false; |
| 4721 | } |
| 4722 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4723 | auto dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
| 4724 | if (dtls_role) { |
| 4725 | *role = *dtls_role; |
| 4726 | return true; |
| 4727 | } |
| 4728 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 4732 | rtc::SSLRole* role) { |
| 4733 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4734 | RTC_LOG(LS_INFO) |
| 4735 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4736 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4737 | return false; |
| 4738 | } |
| 4739 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4740 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 4741 | if (dtls_role) { |
| 4742 | *role = *dtls_role; |
| 4743 | return true; |
| 4744 | } |
| 4745 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4746 | } |
| 4747 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4748 | void PeerConnection::SetSessionError(SessionError error, |
| 4749 | const std::string& error_desc) { |
| 4750 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4751 | if (error != session_error_) { |
| 4752 | session_error_ = error; |
| 4753 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4754 | } |
| 4755 | } |
| 4756 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4757 | RTCError PeerConnection::UpdateSessionState( |
| 4758 | SdpType type, |
| 4759 | cricket::ContentSource source, |
| 4760 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4761 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4762 | |
| 4763 | // If there's already a pending error then no state transition should happen. |
| 4764 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4765 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4766 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4767 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4768 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4769 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | // Update the signaling state according to the specified state machine (see |
| 4773 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4774 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4775 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4776 | ? PeerConnectionInterface::kHaveLocalOffer |
| 4777 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4778 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4779 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4780 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 4781 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4782 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4783 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4784 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 4785 | } |
| 4786 | |
| 4787 | // Update internal objects according to the session description's media |
| 4788 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4789 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4790 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 4791 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4792 | } |
| 4793 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4794 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4795 | } |
| 4796 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4797 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4798 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4799 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4800 | const SessionDescriptionInterface* sdesc = |
| 4801 | (source == cricket::CS_LOCAL ? local_description() |
| 4802 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4803 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4804 | |
| 4805 | // Push down the new SDP media section for each audio/video transceiver. |
| 4806 | for (auto transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4807 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4808 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4809 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4810 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4811 | continue; |
| 4812 | } |
| 4813 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4814 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4815 | if (!content_desc) { |
| 4816 | continue; |
| 4817 | } |
| 4818 | std::string error; |
| 4819 | bool success = |
| 4820 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4821 | ? channel->SetLocalContent(content_desc, type, &error) |
| 4822 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4823 | if (!success) { |
| 4824 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, std::move(error)); |
| 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 4829 | if (rtp_data_channel_) { |
| 4830 | const ContentInfo* data_content = |
| 4831 | cricket::GetFirstDataContent(sdesc->description()); |
| 4832 | if (data_content && !data_content->rejected) { |
| 4833 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4834 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4835 | if (data_desc) { |
| 4836 | std::string error; |
| 4837 | bool success = |
| 4838 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4839 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
| 4840 | : rtp_data_channel_->SetRemoteContent(data_desc, type, |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4841 | &error); |
| 4842 | if (!success) { |
| 4843 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4844 | std::move(error)); |
| 4845 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4846 | } |
| 4847 | } |
| 4848 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4849 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4850 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 4851 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 4852 | if (sctp_transport_ && local_description() && remote_description() && |
| 4853 | cricket::GetFirstDataContent(local_description()->description()) && |
| 4854 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4855 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4856 | RTC_FROM_HERE, |
| 4857 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4858 | if (!success) { |
| 4859 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4860 | "Failed to push down SCTP parameters."); |
| 4861 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4862 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4863 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4864 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 4868 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4869 | RTC_DCHECK(local_description()); |
| 4870 | RTC_DCHECK(remote_description()); |
| 4871 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 4872 | // When we support "max-message-size", that would also be pushed down here. |
| 4873 | return sctp_transport_->Start( |
| 4874 | GetSctpPort(local_description()->description()), |
| 4875 | GetSctpPort(remote_description()->description())); |
| 4876 | } |
| 4877 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4878 | RTCError PeerConnection::PushdownTransportDescription( |
| 4879 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4880 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4881 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4882 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4883 | if (source == cricket::CS_LOCAL) { |
| 4884 | const SessionDescriptionInterface* sdesc = local_description(); |
| 4885 | RTC_DCHECK(sdesc); |
| 4886 | return transport_controller_->SetLocalDescription(type, |
| 4887 | sdesc->description()); |
| 4888 | } else { |
| 4889 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 4890 | RTC_DCHECK(sdesc); |
| 4891 | return transport_controller_->SetRemoteDescription(type, |
| 4892 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4893 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | bool PeerConnection::GetTransportDescription( |
| 4897 | const SessionDescription* description, |
| 4898 | const std::string& content_name, |
| 4899 | cricket::TransportDescription* tdesc) { |
| 4900 | if (!description || !tdesc) { |
| 4901 | return false; |
| 4902 | } |
| 4903 | const TransportInfo* transport_info = |
| 4904 | description->GetTransportInfoByName(content_name); |
| 4905 | if (!transport_info) { |
| 4906 | return false; |
| 4907 | } |
| 4908 | *tdesc = transport_info->description; |
| 4909 | return true; |
| 4910 | } |
| 4911 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4912 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 4913 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 4914 | cricket::ContinualGatheringPolicy gathering_policy; |
| 4915 | // TODO(honghaiz): Add the third continual gathering policy in |
| 4916 | // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER. |
| 4917 | switch (config.continual_gathering_policy) { |
| 4918 | case PeerConnectionInterface::GATHER_ONCE: |
| 4919 | gathering_policy = cricket::GATHER_ONCE; |
| 4920 | break; |
| 4921 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 4922 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 4923 | break; |
| 4924 | default: |
| 4925 | RTC_NOTREACHED(); |
| 4926 | gathering_policy = cricket::GATHER_ONCE; |
| 4927 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4928 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4929 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 4930 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 4931 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4932 | ice_config.prioritize_most_likely_candidate_pairs = |
| 4933 | config.prioritize_most_likely_ice_candidate_pairs; |
| 4934 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 4935 | RTCConfigurationToIceConfigOptionalInt( |
| 4936 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4937 | ice_config.continual_gathering_policy = gathering_policy; |
| 4938 | ice_config.presume_writable_when_fully_relayed = |
| 4939 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 4940 | ice_config.ice_check_interval_strong_connectivity = |
| 4941 | config.ice_check_interval_strong_connectivity; |
| 4942 | ice_config.ice_check_interval_weak_connectivity = |
| 4943 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4944 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4945 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4946 | ice_config.regather_all_networks_interval_range = |
| 4947 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4948 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4949 | return ice_config; |
| 4950 | } |
| 4951 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4952 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 4953 | std::string* track_id) { |
| 4954 | if (!local_description()) { |
| 4955 | return false; |
| 4956 | } |
| 4957 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 4958 | track_id); |
| 4959 | } |
| 4960 | |
| 4961 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 4962 | std::string* track_id) { |
| 4963 | if (!remote_description()) { |
| 4964 | return false; |
| 4965 | } |
| 4966 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 4967 | track_id); |
| 4968 | } |
| 4969 | |
| 4970 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 4971 | const rtc::CopyOnWriteBuffer& payload, |
| 4972 | cricket::SendDataResult* result) { |
| 4973 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4974 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4975 | "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4976 | return false; |
| 4977 | } |
| 4978 | return rtp_data_channel_ |
| 4979 | ? rtp_data_channel_->SendData(params, payload, result) |
| 4980 | : network_thread()->Invoke<bool>( |
| 4981 | RTC_FROM_HERE, |
| 4982 | Bind(&cricket::SctpTransportInternal::SendData, |
| 4983 | sctp_transport_.get(), params, payload, result)); |
| 4984 | } |
| 4985 | |
| 4986 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 4987 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 4988 | // Don't log an error here, because DataChannels are expected to call |
| 4989 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 4990 | // whether or not the underlying transport is ready. |
| 4991 | return false; |
| 4992 | } |
| 4993 | if (rtp_data_channel_) { |
| 4994 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 4995 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 4996 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 4997 | &DataChannel::OnDataReceived); |
| 4998 | } else { |
| 4999 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 5000 | &DataChannel::OnChannelReady); |
| 5001 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 5002 | &DataChannel::OnDataReceived); |
| 5003 | SignalSctpStreamClosedRemotely.connect( |
| 5004 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
| 5005 | } |
| 5006 | return true; |
| 5007 | } |
| 5008 | |
| 5009 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 5010 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5011 | RTC_LOG(LS_ERROR) |
| 5012 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 5013 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5014 | return; |
| 5015 | } |
| 5016 | if (rtp_data_channel_) { |
| 5017 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 5018 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 5019 | } else { |
| 5020 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 5021 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
| 5022 | SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel); |
| 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | void PeerConnection::AddSctpDataStream(int sid) { |
| 5027 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5028 | RTC_LOG(LS_ERROR) |
| 5029 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5030 | return; |
| 5031 | } |
| 5032 | network_thread()->Invoke<void>( |
| 5033 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 5034 | sctp_transport_.get(), sid)); |
| 5035 | } |
| 5036 | |
| 5037 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 5038 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5039 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5040 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5041 | return; |
| 5042 | } |
| 5043 | network_thread()->Invoke<void>( |
| 5044 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 5045 | sctp_transport_.get(), sid)); |
| 5046 | } |
| 5047 | |
| 5048 | bool PeerConnection::ReadyToSendData() const { |
| 5049 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 5050 | sctp_ready_to_send_data_; |
| 5051 | } |
| 5052 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5053 | rtc::Optional<std::string> PeerConnection::sctp_transport_name() const { |
| 5054 | if (sctp_mid_ && transport_controller_) { |
| 5055 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 5056 | if (dtls_transport) { |
| 5057 | return dtls_transport->transport_name(); |
| 5058 | } |
| 5059 | return rtc::Optional<std::string>(); |
| 5060 | } |
| 5061 | return rtc::Optional<std::string>(); |
| 5062 | } |
| 5063 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5064 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 5065 | cricket::CandidateStatsList candidate_states_list; |
| 5066 | port_allocator_->GetCandidateStatsFromPooledSessions(&candidate_states_list); |
| 5067 | return candidate_states_list; |
| 5068 | } |
| 5069 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5070 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 5071 | const { |
| 5072 | std::map<std::string, std::string> transport_names_by_mid; |
| 5073 | for (auto transceiver : transceivers_) { |
| 5074 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5075 | if (channel) { |
| 5076 | transport_names_by_mid[channel->content_name()] = |
| 5077 | channel->transport_name(); |
| 5078 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5079 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5080 | if (rtp_data_channel_) { |
| 5081 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5082 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5083 | } |
| 5084 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5085 | rtc::Optional<std::string> transport_name = sctp_transport_name(); |
| 5086 | RTC_DCHECK(transport_name); |
| 5087 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5088 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5089 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5090 | } |
| 5091 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5092 | std::map<std::string, cricket::TransportStats> |
| 5093 | PeerConnection::GetTransportStatsByNames( |
| 5094 | const std::set<std::string>& transport_names) { |
| 5095 | if (!network_thread()->IsCurrent()) { |
| 5096 | return network_thread() |
| 5097 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5098 | RTC_FROM_HERE, |
| 5099 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5100 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5101 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5102 | for (const std::string& transport_name : transport_names) { |
| 5103 | cricket::TransportStats transport_stats; |
| 5104 | bool success = |
| 5105 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5106 | if (success) { |
| 5107 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5108 | } else { |
| 5109 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5110 | << transport_name; |
| 5111 | } |
| 5112 | } |
| 5113 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | bool PeerConnection::GetLocalCertificate( |
| 5117 | const std::string& transport_name, |
| 5118 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5119 | if (!certificate) { |
| 5120 | return false; |
| 5121 | } |
| 5122 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 5123 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5124 | } |
| 5125 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5126 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5127 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5128 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5129 | } |
| 5130 | |
| 5131 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 5132 | return data_channel_type_; |
| 5133 | } |
| 5134 | |
| 5135 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5136 | return pending_ice_restarts_.find(content_name) != |
| 5137 | pending_ice_restarts_.end(); |
| 5138 | } |
| 5139 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5140 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5141 | return transport_controller_->NeedsIceRestart(content_name); |
| 5142 | } |
| 5143 | |
| 5144 | void PeerConnection::OnCertificateReady( |
| 5145 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5146 | transport_controller_->SetLocalCertificate(certificate); |
| 5147 | } |
| 5148 | |
| 5149 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5150 | SetSessionError(SessionError::kTransport, |
| 5151 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5152 | } |
| 5153 | |
| 5154 | void PeerConnection::OnTransportControllerConnectionState( |
| 5155 | cricket::IceConnectionState state) { |
| 5156 | switch (state) { |
| 5157 | case cricket::kIceConnectionConnecting: |
| 5158 | // If the current state is Connected or Completed, then there were |
| 5159 | // writable channels but now there are not, so the next state must |
| 5160 | // be Disconnected. |
| 5161 | // kIceConnectionConnecting is currently used as the default, |
| 5162 | // un-connected state by the TransportController, so its only use is |
| 5163 | // detecting disconnections. |
| 5164 | if (ice_connection_state_ == |
| 5165 | PeerConnectionInterface::kIceConnectionConnected || |
| 5166 | ice_connection_state_ == |
| 5167 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5168 | SetIceConnectionState( |
| 5169 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5170 | } |
| 5171 | break; |
| 5172 | case cricket::kIceConnectionFailed: |
| 5173 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5174 | break; |
| 5175 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5176 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5177 | "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5178 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5179 | break; |
| 5180 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5181 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5182 | "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5183 | if (ice_connection_state_ != |
| 5184 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5185 | // If jumping directly from "checking" to "connected", |
| 5186 | // signal "connected" first. |
| 5187 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5188 | } |
| 5189 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 5190 | if (metrics_observer()) { |
| 5191 | ReportTransportStats(); |
| 5192 | } |
| 5193 | break; |
| 5194 | default: |
| 5195 | RTC_NOTREACHED(); |
| 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5200 | const std::string& transport_name, |
| 5201 | const cricket::Candidates& candidates) { |
| 5202 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5203 | int sdp_mline_index; |
| 5204 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5205 | RTC_LOG(LS_ERROR) |
| 5206 | << "OnTransportControllerCandidatesGathered: content name " |
| 5207 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5208 | return; |
| 5209 | } |
| 5210 | |
| 5211 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5212 | citer != candidates.end(); ++citer) { |
| 5213 | // Use transport_name as the candidate media id. |
| 5214 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5215 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5216 | if (local_description()) { |
| 5217 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5218 | } |
| 5219 | OnIceCandidate(std::move(candidate)); |
| 5220 | } |
| 5221 | } |
| 5222 | |
| 5223 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5224 | const std::vector<cricket::Candidate>& candidates) { |
| 5225 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5226 | // Sanity check. |
| 5227 | for (const cricket::Candidate& candidate : candidates) { |
| 5228 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5229 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5230 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5231 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5232 | return; |
| 5233 | } |
| 5234 | } |
| 5235 | |
| 5236 | if (local_description()) { |
| 5237 | mutable_local_description()->RemoveCandidates(candidates); |
| 5238 | } |
| 5239 | OnIceCandidatesRemoved(candidates); |
| 5240 | } |
| 5241 | |
| 5242 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5243 | rtc::SSLHandshakeError error) { |
| 5244 | if (metrics_observer()) { |
| 5245 | metrics_observer()->IncrementEnumCounter( |
| 5246 | webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error), |
| 5247 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
| 5248 | } |
| 5249 | } |
| 5250 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5251 | void PeerConnection::EnableSending() { |
| 5252 | for (auto transceiver : transceivers_) { |
| 5253 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5254 | if (channel && !channel->enabled()) { |
| 5255 | channel->Enable(true); |
| 5256 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5257 | } |
| 5258 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5259 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5260 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5261 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5262 | } |
| 5263 | |
| 5264 | // Returns the media index for a local ice candidate given the content name. |
| 5265 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5266 | const std::string& content_name, |
| 5267 | int* sdp_mline_index) { |
| 5268 | if (!local_description() || !sdp_mline_index) { |
| 5269 | return false; |
| 5270 | } |
| 5271 | |
| 5272 | bool content_found = false; |
| 5273 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5274 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5275 | if (contents[index].name == content_name) { |
| 5276 | *sdp_mline_index = static_cast<int>(index); |
| 5277 | content_found = true; |
| 5278 | break; |
| 5279 | } |
| 5280 | } |
| 5281 | return content_found; |
| 5282 | } |
| 5283 | |
| 5284 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5285 | const SessionDescriptionInterface* remote_desc) { |
| 5286 | if (!remote_desc) { |
| 5287 | return true; |
| 5288 | } |
| 5289 | bool ret = true; |
| 5290 | |
| 5291 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5292 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5293 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5294 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5295 | bool valid = false; |
| 5296 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5297 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5298 | RTC_LOG(LS_INFO) |
| 5299 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5300 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5301 | } |
| 5302 | continue; |
| 5303 | } |
| 5304 | ret = UseCandidate(candidate); |
| 5305 | if (!ret) { |
| 5306 | break; |
| 5307 | } |
| 5308 | } |
| 5309 | } |
| 5310 | return ret; |
| 5311 | } |
| 5312 | |
| 5313 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5314 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5315 | size_t remote_content_size = |
| 5316 | remote_description()->description()->contents().size(); |
| 5317 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5318 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5319 | return false; |
| 5320 | } |
| 5321 | |
| 5322 | cricket::ContentInfo content = |
| 5323 | remote_description()->description()->contents()[mediacontent_index]; |
| 5324 | std::vector<cricket::Candidate> candidates; |
| 5325 | candidates.push_back(candidate->candidate()); |
| 5326 | // Invoking BaseSession method to handle remote candidates. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5327 | RTCError error = |
| 5328 | transport_controller_->AddRemoteCandidates(content.name, candidates); |
| 5329 | if (error.ok()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5330 | // Candidates successfully submitted for checking. |
| 5331 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5332 | ice_connection_state_ == |
| 5333 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5334 | // If state is New, then the session has just gotten its first remote ICE |
| 5335 | // candidates, so go to Checking. |
| 5336 | // If state is Disconnected, the session is re-using old candidates or |
| 5337 | // receiving additional ones, so go to Checking. |
| 5338 | // If state is Connected, stay Connected. |
| 5339 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5340 | // newly added transport, then the state actually _should_ move to |
| 5341 | // checking. Add a way to distinguish that case. |
| 5342 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5343 | } |
| 5344 | // TODO(bemasc): If state is Completed, go back to Connected. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5345 | } else if (error.message()) { |
| 5346 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5347 | } |
| 5348 | return true; |
| 5349 | } |
| 5350 | |
| 5351 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5352 | // Destroy video channel first since it may have a pointer to the |
| 5353 | // voice channel. |
| 5354 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5355 | if (!video_info || video_info->rejected) { |
| 5356 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5357 | } |
| 5358 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5359 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5360 | if (!audio_info || audio_info->rejected) { |
| 5361 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5365 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5366 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5367 | } |
| 5368 | } |
| 5369 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5370 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5371 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5372 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5373 | if (configuration_.bundle_policy == |
| 5374 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5375 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5376 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5377 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5378 | "max-bundle configured but session description " |
| 5379 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5380 | } |
| 5381 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5382 | return std::move(bundle_group); |
| 5383 | } |
| 5384 | |
| 5385 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5386 | // Creating the media channels. Transports should already have been created |
| 5387 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5388 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5389 | if (voice && !voice->rejected && |
| 5390 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5391 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5392 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5393 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5394 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5395 | } |
| 5396 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5397 | } |
| 5398 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5399 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5400 | if (video && !video->rejected && |
| 5401 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5402 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5403 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5404 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5405 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5406 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5407 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5408 | } |
| 5409 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5410 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5411 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 5412 | !rtp_data_channel_ && !sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5413 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5414 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5415 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5416 | } |
| 5417 | } |
| 5418 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5419 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5420 | } |
| 5421 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5422 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5423 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5424 | const std::string& mid) { |
| 5425 | RtpTransportInternal* rtp_transport = |
| 5426 | transport_controller_->GetRtpTransport(mid); |
| 5427 | RTC_DCHECK(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5428 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5429 | call_.get(), configuration_.media_config, rtp_transport, |
| 5430 | signaling_thread(), mid, SrtpRequired(), |
| 5431 | factory_->options().crypto_options, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5432 | if (!voice_channel) { |
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 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5436 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5437 | voice_channel->SignalSentPacket.connect(this, |
| 5438 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5439 | voice_channel->SetRtpTransport(rtp_transport); |
| 5440 | if (factory_->options().disable_encryption) { |
| 5441 | voice_channel->DisableEncryption(true); |
| 5442 | } |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 5443 | if (uma_observer_) { |
| 5444 | voice_channel->SetMetricsObserver(uma_observer_); |
| 5445 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5446 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5447 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5448 | } |
| 5449 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5450 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5451 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5452 | const std::string& mid) { |
| 5453 | RtpTransportInternal* rtp_transport = |
| 5454 | transport_controller_->GetRtpTransport(mid); |
| 5455 | RTC_DCHECK(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5456 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5457 | call_.get(), configuration_.media_config, rtp_transport, |
| 5458 | signaling_thread(), mid, SrtpRequired(), |
| 5459 | factory_->options().crypto_options, video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5460 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5461 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5462 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5463 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5464 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5465 | video_channel->SignalSentPacket.connect(this, |
| 5466 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5467 | video_channel->SetRtpTransport(rtp_transport); |
| 5468 | if (factory_->options().disable_encryption) { |
| 5469 | video_channel->DisableEncryption(true); |
| 5470 | } |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 5471 | if (uma_observer_) { |
| 5472 | video_channel->SetMetricsObserver(uma_observer_); |
| 5473 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5474 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5475 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5476 | } |
| 5477 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5478 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5479 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 5480 | if (sctp) { |
| 5481 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5482 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5483 | << "Trying to create SCTP transport, but didn't compile with " |
| 5484 | "SCTP support (HAVE_SCTP)"; |
| 5485 | return false; |
| 5486 | } |
| 5487 | if (!network_thread()->Invoke<bool>( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5488 | RTC_FROM_HERE, |
| 5489 | rtc::Bind(&PeerConnection::CreateSctpTransport_n, this, mid))) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5490 | return false; |
| 5491 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5492 | for (const auto& channel : sctp_data_channels_) { |
| 5493 | channel->OnTransportChannelCreated(); |
| 5494 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5495 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5496 | RtpTransportInternal* rtp_transport = |
| 5497 | transport_controller_->GetRtpTransport(mid); |
| 5498 | RTC_DCHECK(rtp_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5499 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5500 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
| 5501 | SrtpRequired(), factory_->options().crypto_options); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5502 | if (!rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5503 | return false; |
| 5504 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5505 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 5506 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 5507 | rtp_data_channel_->SignalSentPacket.connect( |
| 5508 | this, &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5509 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
| 5510 | if (factory_->options().disable_encryption) { |
| 5511 | rtp_data_channel_->DisableEncryption(true); |
| 5512 | } |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 5513 | if (uma_observer_) { |
| 5514 | rtp_data_channel_->SetMetricsObserver(uma_observer_); |
| 5515 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5516 | } |
| 5517 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5518 | return true; |
| 5519 | } |
| 5520 | |
| 5521 | Call::Stats PeerConnection::GetCallStats() { |
| 5522 | if (!worker_thread()->IsCurrent()) { |
| 5523 | return worker_thread()->Invoke<Call::Stats>( |
| 5524 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 5525 | } |
| 5526 | if (call_) { |
| 5527 | return call_->GetStats(); |
| 5528 | } else { |
| 5529 | return Call::Stats(); |
| 5530 | } |
| 5531 | } |
| 5532 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5533 | bool PeerConnection::CreateSctpTransport_n(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5534 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5535 | RTC_DCHECK(sctp_factory_); |
| 5536 | cricket::DtlsTransportInternal* tc = |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5537 | transport_controller_->GetDtlsTransport(mid); |
| 5538 | RTC_DCHECK(tc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5539 | sctp_transport_ = sctp_factory_->CreateSctpTransport(tc); |
| 5540 | RTC_DCHECK(sctp_transport_); |
| 5541 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 5542 | sctp_transport_->SignalReadyToSendData.connect( |
| 5543 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 5544 | sctp_transport_->SignalDataReceived.connect( |
| 5545 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
| 5546 | sctp_transport_->SignalStreamClosedRemotely.connect( |
| 5547 | this, &PeerConnection::OnSctpStreamClosedRemotely_n); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5548 | sctp_mid_ = mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5549 | sctp_transport_->SetTransportChannel(tc); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5550 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5551 | } |
| 5552 | |
| 5553 | void PeerConnection::DestroySctpTransport_n() { |
| 5554 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5555 | sctp_transport_.reset(nullptr); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5556 | sctp_mid_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5557 | sctp_invoker_.reset(nullptr); |
| 5558 | sctp_ready_to_send_data_ = false; |
| 5559 | } |
| 5560 | |
| 5561 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 5562 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5563 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5564 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5565 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5566 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5567 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5568 | // pending tasks will be cleared. |
| 5569 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 5570 | OnSctpTransportReadyToSendData_s(true); |
| 5571 | }); |
| 5572 | } |
| 5573 | |
| 5574 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 5575 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5576 | sctp_ready_to_send_data_ = ready; |
| 5577 | SignalSctpReadyToSendData(ready); |
| 5578 | } |
| 5579 | |
| 5580 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 5581 | const cricket::ReceiveDataParams& params, |
| 5582 | const rtc::CopyOnWriteBuffer& payload) { |
| 5583 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5584 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5585 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5586 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5587 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5588 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5589 | // pending tasks will be cleared. |
| 5590 | sctp_invoker_->AsyncInvoke<void>( |
| 5591 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 5592 | OnSctpTransportDataReceived_s(params, payload); |
| 5593 | }); |
| 5594 | } |
| 5595 | |
| 5596 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 5597 | const cricket::ReceiveDataParams& params, |
| 5598 | const rtc::CopyOnWriteBuffer& payload) { |
| 5599 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5600 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 5601 | // Received OPEN message; parse and signal that a new data channel should |
| 5602 | // be created. |
| 5603 | std::string label; |
| 5604 | InternalDataChannelInit config; |
| 5605 | config.id = params.ssrc; |
| 5606 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5607 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 5608 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5609 | return; |
| 5610 | } |
| 5611 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5612 | OnDataChannelOpenMessage(label, config); |
| 5613 | } else { |
| 5614 | // Otherwise just forward the signal. |
| 5615 | SignalSctpDataReceived(params, payload); |
| 5616 | } |
| 5617 | } |
| 5618 | |
| 5619 | void PeerConnection::OnSctpStreamClosedRemotely_n(int sid) { |
| 5620 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5621 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5622 | sctp_invoker_->AsyncInvoke<void>( |
| 5623 | RTC_FROM_HERE, signaling_thread(), |
| 5624 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 5625 | &SignalSctpStreamClosedRemotely, sid)); |
| 5626 | } |
| 5627 | |
| 5628 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 5629 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 5630 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 5631 | if (!bundle_enabled) |
| 5632 | return true; |
| 5633 | |
| 5634 | const cricket::ContentGroup* bundle_group = |
| 5635 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 5636 | RTC_DCHECK(bundle_group != NULL); |
| 5637 | |
| 5638 | const cricket::ContentInfos& contents = desc->contents(); |
| 5639 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 5640 | citer != contents.end(); ++citer) { |
| 5641 | const cricket::ContentInfo* content = (&*citer); |
| 5642 | RTC_DCHECK(content != NULL); |
| 5643 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 5644 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5645 | if (!HasRtcpMuxEnabled(content)) |
| 5646 | return false; |
| 5647 | } |
| 5648 | } |
| 5649 | // RTCP-MUX is enabled in all the contents. |
| 5650 | return true; |
| 5651 | } |
| 5652 | |
| 5653 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5654 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5655 | } |
| 5656 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5657 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5658 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5659 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5660 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5661 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5665 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5666 | } |
| 5667 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5668 | SdpType type = sdesc->GetType(); |
| 5669 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 5670 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5671 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 5672 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5673 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5674 | } |
| 5675 | |
| 5676 | // Verify crypto settings. |
| 5677 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5678 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 5679 | dtls_enabled_) { |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 5680 | RTCError crypto_error = |
| 5681 | VerifyCrypto(sdesc->description(), dtls_enabled_, uma_observer_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5682 | if (!crypto_error.ok()) { |
| 5683 | return crypto_error; |
| 5684 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | // Verify ice-ufrag and ice-pwd. |
| 5688 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5689 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5690 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5691 | } |
| 5692 | |
| 5693 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5694 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5695 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5696 | } |
| 5697 | |
| 5698 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 5699 | // m-lines that do not rtcp-mux enabled. |
| 5700 | |
| 5701 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5702 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5703 | // With an answer we want to compare the new answer session description with |
| 5704 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5705 | const cricket::SessionDescription* offer_desc = |
| 5706 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 5707 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5708 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 5709 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 5710 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5711 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5712 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5713 | } |
| 5714 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5715 | // The re-offers should respect the order of m= sections in current |
| 5716 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5717 | // With a re-offer, either the current local or current remote descriptions |
| 5718 | // could be the most up to date, so we would like to check against both of |
| 5719 | // them if they exist. It could be the case that one of them has a 0 port |
| 5720 | // for a media section, but the other does not. This is important to check |
| 5721 | // against in the case that we are recycling an m= section. |
| 5722 | const cricket::SessionDescription* current_desc = nullptr; |
| 5723 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 5724 | if (local_description()) { |
| 5725 | current_desc = local_description()->description(); |
| 5726 | if (remote_description()) { |
| 5727 | secondary_current_desc = remote_description()->description(); |
| 5728 | } |
| 5729 | } else if (remote_description()) { |
| 5730 | current_desc = remote_description()->description(); |
| 5731 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5732 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5733 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 5734 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5735 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5736 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5737 | } |
| 5738 | } |
| 5739 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5740 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5741 | } |
| 5742 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5743 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5744 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5745 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5746 | return (state == PeerConnectionInterface::kStable) || |
| 5747 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5748 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5749 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5750 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 5751 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 5752 | } |
| 5753 | } |
| 5754 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5755 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5756 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5757 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5758 | return (state == PeerConnectionInterface::kStable) || |
| 5759 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5760 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5761 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5762 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 5763 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5764 | } |
| 5765 | } |
| 5766 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5767 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 5768 | switch (error) { |
| 5769 | case SessionError::kNone: |
| 5770 | return "ERROR_NONE"; |
| 5771 | case SessionError::kContent: |
| 5772 | return "ERROR_CONTENT"; |
| 5773 | case SessionError::kTransport: |
| 5774 | return "ERROR_TRANSPORT"; |
| 5775 | } |
| 5776 | RTC_NOTREACHED(); |
| 5777 | return ""; |
| 5778 | } |
| 5779 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5780 | std::string PeerConnection::GetSessionErrorMsg() { |
| 5781 | std::ostringstream desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5782 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 5783 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5784 | return desc.str(); |
| 5785 | } |
| 5786 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 5787 | void PeerConnection::ReportSdpFormatReceived( |
| 5788 | const SessionDescriptionInterface& remote_offer) { |
| 5789 | if (!uma_observer_) { |
| 5790 | return; |
| 5791 | } |
| 5792 | int num_audio_mlines = 0; |
| 5793 | int num_video_mlines = 0; |
| 5794 | int num_audio_tracks = 0; |
| 5795 | int num_video_tracks = 0; |
| 5796 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 5797 | cricket::MediaType media_type = content.media_description()->type(); |
| 5798 | int num_tracks = std::max( |
| 5799 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 5800 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 5801 | num_audio_mlines += 1; |
| 5802 | num_audio_tracks += num_tracks; |
| 5803 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5804 | num_video_mlines += 1; |
| 5805 | num_video_tracks += num_tracks; |
| 5806 | } |
| 5807 | } |
| 5808 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 5809 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 5810 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 5811 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 5812 | format = kSdpFormatReceivedComplexPlanB; |
| 5813 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 5814 | format = kSdpFormatReceivedSimple; |
| 5815 | } |
| 5816 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpFormatReceived, format, |
| 5817 | kSdpFormatReceivedMax); |
| 5818 | } |
| 5819 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 5820 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 5821 | const SessionDescriptionInterface& answer) { |
| 5822 | if (!uma_observer_) { |
| 5823 | return; |
| 5824 | } |
| 5825 | switch (answer.description()->msid_signaling()) { |
| 5826 | case 0: |
| 5827 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5828 | kSdpSemanticNegotiatedNone, |
| 5829 | kSdpSemanticNegotiatedMax); |
| 5830 | break; |
| 5831 | case cricket::kMsidSignalingMediaSection: |
| 5832 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5833 | kSdpSemanticNegotiatedUnifiedPlan, |
| 5834 | kSdpSemanticNegotiatedMax); |
| 5835 | break; |
| 5836 | case cricket::kMsidSignalingSsrcAttribute: |
| 5837 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5838 | kSdpSemanticNegotiatedPlanB, |
| 5839 | kSdpSemanticNegotiatedMax); |
| 5840 | break; |
| 5841 | case cricket::kMsidSignalingMediaSection | |
| 5842 | cricket::kMsidSignalingSsrcAttribute: |
| 5843 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 5844 | kSdpSemanticNegotiatedMixed, |
| 5845 | kSdpSemanticNegotiatedMax); |
| 5846 | break; |
| 5847 | default: |
| 5848 | RTC_NOTREACHED(); |
| 5849 | } |
| 5850 | } |
| 5851 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5852 | // We need to check the local/remote description for the Transport instead of |
| 5853 | // the session, because a new Transport added during renegotiation may have |
| 5854 | // them unset while the session has them set from the previous negotiation. |
| 5855 | // Not doing so may trigger the auto generation of transport description and |
| 5856 | // mess up DTLS identity information, ICE credential, etc. |
| 5857 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 5858 | const IceCandidateInterface* candidate, |
| 5859 | const SessionDescriptionInterface* remote_desc, |
| 5860 | bool* valid) { |
| 5861 | *valid = true; |
| 5862 | |
| 5863 | const SessionDescriptionInterface* current_remote_desc = |
| 5864 | remote_desc ? remote_desc : remote_description(); |
| 5865 | |
| 5866 | if (!current_remote_desc) { |
| 5867 | return false; |
| 5868 | } |
| 5869 | |
| 5870 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5871 | size_t remote_content_size = |
| 5872 | current_remote_desc->description()->contents().size(); |
| 5873 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5874 | RTC_LOG(LS_ERROR) |
| 5875 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 5876 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5877 | |
| 5878 | *valid = false; |
| 5879 | return false; |
| 5880 | } |
| 5881 | |
| 5882 | cricket::ContentInfo content = |
| 5883 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 5884 | |
| 5885 | const std::string transport_name = GetTransportName(content.name); |
| 5886 | if (transport_name.empty()) { |
| 5887 | return false; |
| 5888 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5889 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5890 | } |
| 5891 | |
| 5892 | bool PeerConnection::SrtpRequired() const { |
| 5893 | return dtls_enabled_ || |
| 5894 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 5895 | } |
| 5896 | |
| 5897 | void PeerConnection::OnTransportControllerGatheringState( |
| 5898 | cricket::IceGatheringState state) { |
| 5899 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5900 | if (state == cricket::kIceGatheringGathering) { |
| 5901 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 5902 | } else if (state == cricket::kIceGatheringComplete) { |
| 5903 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 5904 | } |
| 5905 | } |
| 5906 | |
| 5907 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5908 | std::map<std::string, std::set<cricket::MediaType>> |
| 5909 | media_types_by_transport_name; |
| 5910 | for (auto transceiver : transceivers_) { |
| 5911 | if (transceiver->internal()->channel()) { |
| 5912 | const std::string& transport_name = |
| 5913 | transceiver->internal()->channel()->transport_name(); |
| 5914 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5915 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5916 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5917 | } |
| 5918 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5919 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 5920 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5921 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5922 | |
| 5923 | rtc::Optional<std::string> transport_name = sctp_transport_name(); |
| 5924 | if (transport_name) { |
| 5925 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5926 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5927 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5928 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5929 | for (const auto& entry : media_types_by_transport_name) { |
| 5930 | const std::string& transport_name = entry.first; |
| 5931 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5932 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5933 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5934 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5935 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5936 | } |
| 5937 | } |
| 5938 | } |
| 5939 | // Walk through the ConnectionInfos to gather best connection usage |
| 5940 | // for IPv4 and IPv6. |
| 5941 | void PeerConnection::ReportBestConnectionState( |
| 5942 | const cricket::TransportStats& stats) { |
| 5943 | RTC_DCHECK(metrics_observer()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5944 | for (const cricket::TransportChannelStats& channel_stats : |
| 5945 | stats.channel_stats) { |
| 5946 | for (const cricket::ConnectionInfo& connection_info : |
| 5947 | channel_stats.connection_infos) { |
| 5948 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5949 | continue; |
| 5950 | } |
| 5951 | |
| 5952 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5953 | const cricket::Candidate& local = connection_info.local_candidate; |
| 5954 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5955 | |
| 5956 | // Increment the counter for IceCandidatePairType. |
| 5957 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 5958 | (local.type() == RELAY_PORT_TYPE && |
| 5959 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 5960 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 5961 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 5962 | type = kEnumCounterIceCandidatePairTypeUdp; |
| 5963 | } else { |
| 5964 | RTC_CHECK(0); |
| 5965 | } |
| 5966 | metrics_observer()->IncrementEnumCounter( |
| 5967 | type, GetIceCandidatePairCounter(local, remote), |
| 5968 | kIceCandidatePairMax); |
| 5969 | |
| 5970 | // Increment the counter for IP type. |
| 5971 | if (local.address().family() == AF_INET) { |
| 5972 | metrics_observer()->IncrementEnumCounter( |
| 5973 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 5974 | kPeerConnectionAddressFamilyCounter_Max); |
| 5975 | |
| 5976 | } else if (local.address().family() == AF_INET6) { |
| 5977 | metrics_observer()->IncrementEnumCounter( |
| 5978 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 5979 | kPeerConnectionAddressFamilyCounter_Max); |
| 5980 | } else { |
| 5981 | RTC_CHECK(0); |
| 5982 | } |
| 5983 | |
| 5984 | return; |
| 5985 | } |
| 5986 | } |
| 5987 | } |
| 5988 | |
| 5989 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5990 | const cricket::TransportStats& stats, |
| 5991 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5992 | RTC_DCHECK(metrics_observer()); |
| 5993 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 5994 | return; |
| 5995 | } |
| 5996 | |
| 5997 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 5998 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 5999 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 6000 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6001 | return; |
| 6002 | } |
| 6003 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6004 | for (cricket::MediaType media_type : media_types) { |
| 6005 | PeerConnectionEnumCounterType srtp_counter_type; |
| 6006 | PeerConnectionEnumCounterType ssl_counter_type; |
| 6007 | switch (media_type) { |
| 6008 | case cricket::MEDIA_TYPE_AUDIO: |
| 6009 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 6010 | ssl_counter_type = kEnumCounterAudioSslCipher; |
| 6011 | break; |
| 6012 | case cricket::MEDIA_TYPE_VIDEO: |
| 6013 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 6014 | ssl_counter_type = kEnumCounterVideoSslCipher; |
| 6015 | break; |
| 6016 | case cricket::MEDIA_TYPE_DATA: |
| 6017 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 6018 | ssl_counter_type = kEnumCounterDataSslCipher; |
| 6019 | break; |
| 6020 | default: |
| 6021 | RTC_NOTREACHED(); |
| 6022 | continue; |
| 6023 | } |
| 6024 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6025 | metrics_observer()->IncrementSparseEnumCounter(srtp_counter_type, |
| 6026 | srtp_crypto_suite); |
| 6027 | } |
| 6028 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6029 | metrics_observer()->IncrementSparseEnumCounter(ssl_counter_type, |
| 6030 | ssl_cipher_suite); |
| 6031 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6032 | } |
| 6033 | } |
| 6034 | |
| 6035 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 6036 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 6037 | RTC_DCHECK(call_); |
| 6038 | call_->OnSentPacket(sent_packet); |
| 6039 | } |
| 6040 | |
| 6041 | const std::string PeerConnection::GetTransportName( |
| 6042 | const std::string& content_name) { |
| 6043 | cricket::BaseChannel* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6044 | if (channel) { |
| 6045 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6046 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6047 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6048 | RTC_DCHECK(sctp_mid_); |
| 6049 | if (content_name == *sctp_mid_) { |
| 6050 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6051 | } |
| 6052 | } |
| 6053 | // Return an empty string if failed to retrieve the transport name. |
| 6054 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 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); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6091 | |
| 6092 | switch (channel->media_type()) { |
| 6093 | case cricket::MEDIA_TYPE_AUDIO: |
| 6094 | channel_manager()->DestroyVoiceChannel( |
| 6095 | static_cast<cricket::VoiceChannel*>(channel)); |
| 6096 | break; |
| 6097 | case cricket::MEDIA_TYPE_VIDEO: |
| 6098 | channel_manager()->DestroyVideoChannel( |
| 6099 | static_cast<cricket::VideoChannel*>(channel)); |
| 6100 | break; |
| 6101 | case cricket::MEDIA_TYPE_DATA: |
| 6102 | channel_manager()->DestroyRtpDataChannel( |
| 6103 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 6104 | break; |
| 6105 | default: |
| 6106 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 6107 | break; |
| 6108 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6109 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6110 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6111 | void PeerConnection::OnRtpTransportChanged( |
| 6112 | const std::string& mid, |
| 6113 | RtpTransportInternal* rtp_transport) { |
| 6114 | auto base_channel = GetChannel(mid); |
| 6115 | if (base_channel) { |
| 6116 | base_channel->SetRtpTransport(rtp_transport); |
| 6117 | } |
| 6118 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6119 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6120 | void PeerConnection::OnDtlsTransportChanged( |
| 6121 | const std::string& mid, |
| 6122 | cricket::DtlsTransportInternal* dtls_transport) { |
| 6123 | if (sctp_transport_) { |
| 6124 | RTC_DCHECK(mid == sctp_mid_); |
| 6125 | sctp_transport_->SetTransportChannel(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6126 | } |
| 6127 | } |
| 6128 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 6129 | void PeerConnection::ClearStatsCache() { |
| 6130 | if (stats_collector_) { |
| 6131 | stats_collector_->ClearCachedStatsReport(); |
| 6132 | } |
| 6133 | } |
| 6134 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6135 | } // namespace webrtc |