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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/peer_connection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 13 | #include <algorithm> |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 14 | #include <limits> |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 15 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 16 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 17 | #include <utility> |
| 18 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 20 | #include "absl/algorithm/container.h" |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 21 | #include "absl/memory/memory.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 21:02:22 +0100 | [diff] [blame] | 22 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "api/jsep_ice_candidate.h" |
| 24 | #include "api/jsep_session_description.h" |
| 25 | #include "api/media_stream_proxy.h" |
| 26 | #include "api/media_stream_track_proxy.h" |
| 27 | #include "api/uma_metrics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "call/call.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "logging/rtc_event_log/ice_logger.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 30 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "logging/rtc_event_log/rtc_event_log.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 32 | #include "media/sctp/sctp_transport.h" |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 33 | #include "pc/audio_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "pc/audio_track.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 35 | #include "pc/channel.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 36 | #include "pc/channel_manager.h" |
| 37 | #include "pc/dtmf_sender.h" |
| 38 | #include "pc/media_stream.h" |
| 39 | #include "pc/media_stream_observer.h" |
| 40 | #include "pc/remote_audio_source.h" |
| 41 | #include "pc/rtp_media_utils.h" |
| 42 | #include "pc/rtp_receiver.h" |
| 43 | #include "pc/rtp_sender.h" |
| 44 | #include "pc/sctp_utils.h" |
| 45 | #include "pc/sdp_utils.h" |
| 46 | #include "pc/stream_collection.h" |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 47 | #include "pc/video_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 48 | #include "pc/video_track.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 49 | #include "rtc_base/bind.h" |
| 50 | #include "rtc_base/checks.h" |
| 51 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 52 | #include "rtc_base/numerics/safe_conversions.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 53 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 54 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 55 | #include "rtc_base/trace_event.h" |
| 56 | #include "system_wrappers/include/clock.h" |
| 57 | #include "system_wrappers/include/field_trial.h" |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 58 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 60 | using cricket::ContentInfo; |
| 61 | using cricket::ContentInfos; |
| 62 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 63 | using cricket::MediaProtocolType; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 64 | using cricket::RidDescription; |
| 65 | using cricket::RidDirection; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 66 | using cricket::SessionDescription; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 67 | using cricket::SimulcastDescription; |
| 68 | using cricket::SimulcastLayer; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 69 | using cricket::SimulcastLayerList; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 70 | using cricket::StreamParams; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 71 | using cricket::TransportInfo; |
| 72 | |
| 73 | using cricket::LOCAL_PORT_TYPE; |
| 74 | using cricket::STUN_PORT_TYPE; |
| 75 | using cricket::RELAY_PORT_TYPE; |
| 76 | using cricket::PRFLX_PORT_TYPE; |
| 77 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 78 | namespace webrtc { |
| 79 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 80 | // Error messages |
| 81 | const char kBundleWithoutRtcpMux[] = |
| 82 | "rtcp-mux must be enabled when BUNDLE " |
| 83 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 84 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 85 | const char kInvalidSdp[] = "Invalid session description."; |
| 86 | const char kMlineMismatchInAnswer[] = |
| 87 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 88 | "answer."; |
| 89 | const char kMlineMismatchInSubsequentOffer[] = |
| 90 | "The order of m-lines in subsequent offer doesn't match order from " |
| 91 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 92 | const char kSdpWithoutDtlsFingerprint[] = |
| 93 | "Called with SDP without DTLS fingerprint."; |
| 94 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 95 | const char kSdpWithoutIceUfragPwd[] = |
| 96 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 97 | const char kSessionError[] = "Session error code: "; |
| 98 | const char kSessionErrorDesc[] = "Session error description: "; |
| 99 | const char kDtlsSrtpSetupFailureRtp[] = |
| 100 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 101 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 102 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 104 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 106 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 107 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 108 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 109 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 110 | // The length of RTCP CNAMEs. |
| 111 | static const int kRtcpCnameLength = 16; |
| 112 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 113 | enum { |
| 114 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 115 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 116 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 117 | MSG_GETSTATS, |
| 118 | MSG_FREE_DATACHANNELS, |
| 119 | MSG_REPORT_USAGE_PATTERN, |
| 120 | }; |
| 121 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 122 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 123 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 124 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 125 | explicit SetSessionDescriptionMsg( |
| 126 | webrtc::SetSessionDescriptionObserver* observer) |
| 127 | : observer(observer) {} |
| 128 | |
| 129 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 130 | RTCError error; |
| 131 | }; |
| 132 | |
| 133 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 134 | explicit CreateSessionDescriptionMsg( |
| 135 | webrtc::CreateSessionDescriptionObserver* observer) |
| 136 | : observer(observer) {} |
| 137 | |
| 138 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 139 | RTCError error; |
| 140 | }; |
| 141 | |
| 142 | struct GetStatsMsg : public rtc::MessageData { |
| 143 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 144 | webrtc::MediaStreamTrackInterface* track) |
| 145 | : observer(observer), track(track) {} |
| 146 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 147 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 148 | }; |
| 149 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 150 | // Check if we can send |new_stream| on a PeerConnection. |
| 151 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 152 | webrtc::MediaStreamInterface* new_stream) { |
| 153 | if (!new_stream || !current_streams) { |
| 154 | return false; |
| 155 | } |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 156 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 157 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 158 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 164 | // If the direction is "recvonly" or "inactive", treat the description |
| 165 | // as containing no streams. |
| 166 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 167 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 168 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 169 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 170 | ? desc->streams() |
| 171 | : std::vector<cricket::StreamParams>(); |
| 172 | } |
| 173 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 174 | bool IsValidOfferToReceiveMedia(int value) { |
| 175 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 176 | return (value >= Options::kUndefined) && |
| 177 | (value <= Options::kMaxOfferToReceiveMedia); |
| 178 | } |
| 179 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 180 | // Add options to |[audio/video]_media_description_options| from |senders|. |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 181 | void AddPlanBRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 182 | const std::vector<rtc::scoped_refptr< |
| 183 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 184 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 185 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 186 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 187 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 188 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 189 | if (audio_media_description_options) { |
| 190 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 191 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 192 | } |
| 193 | } else { |
| 194 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 195 | if (video_media_description_options) { |
| 196 | video_media_description_options->AddVideoSender( |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 197 | sender->id(), sender->internal()->stream_ids(), {}, |
| 198 | SimulcastLayerList(), num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 201 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 202 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 203 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 204 | // Add options to |session_options| from |rtp_data_channels|. |
| 205 | void AddRtpDataChannelOptions( |
| 206 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 207 | rtp_data_channels, |
| 208 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 209 | if (!data_media_description_options) { |
| 210 | return; |
| 211 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 212 | // Check for data channels. |
| 213 | for (const auto& kv : rtp_data_channels) { |
| 214 | const DataChannel* channel = kv.second; |
| 215 | if (channel->state() == DataChannel::kConnecting || |
| 216 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 217 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 218 | // the data channel's label. |
| 219 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 220 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 225 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 226 | PeerConnectionInterface::IceTransportsType type) { |
| 227 | switch (type) { |
| 228 | case PeerConnectionInterface::kNone: |
| 229 | return cricket::CF_NONE; |
| 230 | case PeerConnectionInterface::kRelay: |
| 231 | return cricket::CF_RELAY; |
| 232 | case PeerConnectionInterface::kNoHost: |
| 233 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 234 | case PeerConnectionInterface::kAll: |
| 235 | return cricket::CF_ALL; |
| 236 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 237 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 238 | } |
| 239 | return cricket::CF_NONE; |
| 240 | } |
| 241 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 242 | // Helper to set an error and return from a method. |
| 243 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 244 | if (error) { |
| 245 | error->set_type(type); |
| 246 | } |
| 247 | return type == webrtc::RTCErrorType::NONE; |
| 248 | } |
| 249 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 250 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 251 | bool ok = error.ok(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 252 | if (error_out) { |
| 253 | *error_out = std::move(error); |
| 254 | } |
Steve Anton | f820a5e | 2018-08-10 10:22:52 -0700 | [diff] [blame] | 255 | return ok; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 258 | std::string GetSignalingStateString( |
| 259 | PeerConnectionInterface::SignalingState state) { |
| 260 | switch (state) { |
| 261 | case PeerConnectionInterface::kStable: |
| 262 | return "kStable"; |
| 263 | case PeerConnectionInterface::kHaveLocalOffer: |
| 264 | return "kHaveLocalOffer"; |
| 265 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 266 | return "kHavePrAnswer"; |
| 267 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 268 | return "kHaveRemoteOffer"; |
| 269 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 270 | return "kHaveRemotePrAnswer"; |
| 271 | case PeerConnectionInterface::kClosed: |
| 272 | return "kClosed"; |
| 273 | } |
| 274 | RTC_NOTREACHED(); |
| 275 | return ""; |
| 276 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 277 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 278 | IceCandidatePairType GetIceCandidatePairCounter( |
| 279 | const cricket::Candidate& local, |
| 280 | const cricket::Candidate& remote) { |
| 281 | const auto& l = local.type(); |
| 282 | const auto& r = remote.type(); |
| 283 | const auto& host = LOCAL_PORT_TYPE; |
| 284 | const auto& srflx = STUN_PORT_TYPE; |
| 285 | const auto& relay = RELAY_PORT_TYPE; |
| 286 | const auto& prflx = PRFLX_PORT_TYPE; |
| 287 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 288 | bool local_hostname = |
| 289 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 290 | bool remote_hostname = !remote.address().hostname().empty() && |
| 291 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 292 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 293 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 294 | if (local_hostname) { |
| 295 | if (remote_hostname) { |
| 296 | return kIceCandidatePairHostNameHostName; |
| 297 | } else if (remote_private) { |
| 298 | return kIceCandidatePairHostNameHostPrivate; |
| 299 | } else { |
| 300 | return kIceCandidatePairHostNameHostPublic; |
| 301 | } |
| 302 | } else if (local_private) { |
| 303 | if (remote_hostname) { |
| 304 | return kIceCandidatePairHostPrivateHostName; |
| 305 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 306 | return kIceCandidatePairHostPrivateHostPrivate; |
| 307 | } else { |
| 308 | return kIceCandidatePairHostPrivateHostPublic; |
| 309 | } |
| 310 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 08:25:54 -0800 | [diff] [blame] | 311 | if (remote_hostname) { |
| 312 | return kIceCandidatePairHostPublicHostName; |
| 313 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 314 | return kIceCandidatePairHostPublicHostPrivate; |
| 315 | } else { |
| 316 | return kIceCandidatePairHostPublicHostPublic; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | if (l == host && r == srflx) |
| 321 | return kIceCandidatePairHostSrflx; |
| 322 | if (l == host && r == relay) |
| 323 | return kIceCandidatePairHostRelay; |
| 324 | if (l == host && r == prflx) |
| 325 | return kIceCandidatePairHostPrflx; |
| 326 | if (l == srflx && r == host) |
| 327 | return kIceCandidatePairSrflxHost; |
| 328 | if (l == srflx && r == srflx) |
| 329 | return kIceCandidatePairSrflxSrflx; |
| 330 | if (l == srflx && r == relay) |
| 331 | return kIceCandidatePairSrflxRelay; |
| 332 | if (l == srflx && r == prflx) |
| 333 | return kIceCandidatePairSrflxPrflx; |
| 334 | if (l == relay && r == host) |
| 335 | return kIceCandidatePairRelayHost; |
| 336 | if (l == relay && r == srflx) |
| 337 | return kIceCandidatePairRelaySrflx; |
| 338 | if (l == relay && r == relay) |
| 339 | return kIceCandidatePairRelayRelay; |
| 340 | if (l == relay && r == prflx) |
| 341 | return kIceCandidatePairRelayPrflx; |
| 342 | if (l == prflx && r == host) |
| 343 | return kIceCandidatePairPrflxHost; |
| 344 | if (l == prflx && r == srflx) |
| 345 | return kIceCandidatePairPrflxSrflx; |
| 346 | if (l == prflx && r == relay) |
| 347 | return kIceCandidatePairPrflxRelay; |
| 348 | return kIceCandidatePairMax; |
| 349 | } |
| 350 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 351 | // Logic to decide if an m= section can be recycled. This means that the new |
| 352 | // m= section is not rejected, but the old local or remote m= section is |
| 353 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 354 | // of the old remote and old local descriptions in no particular order. |
| 355 | // We need to check both the old local and remote because either |
| 356 | // could be the most current from the latest negotation. |
| 357 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 358 | const ContentInfo& content, |
| 359 | const ContentInfo* old_content_one, |
| 360 | const ContentInfo* old_content_two) { |
| 361 | return type == SdpType::kOffer && !content.rejected && |
| 362 | ((old_content_one && old_content_one->rejected) || |
| 363 | (old_content_two && old_content_two->rejected)); |
| 364 | } |
| 365 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 366 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 367 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 368 | // less than |current_desc|. In the case of checking an answer's |
| 369 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 370 | // local or remote. In the case of checking an offer's |new_desc| we |
| 371 | // check against the local and remote descriptions stored from the last |
| 372 | // negotiation, because either of these could be the most up to date for |
| 373 | // possible rejected m sections. These are the |current_desc| and |
| 374 | // |secondary_current_desc|. |
| 375 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 376 | const SessionDescription* secondary_current_desc, |
| 377 | const SessionDescription& new_desc, |
| 378 | const SdpType type) { |
| 379 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 380 | return false; |
| 381 | } |
| 382 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 383 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 384 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 385 | if (secondary_current_desc && |
| 386 | i < secondary_current_desc->contents().size()) { |
| 387 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 388 | } |
| 389 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 390 | ¤t_desc.contents()[i], |
| 391 | secondary_content_info)) { |
| 392 | // For new offer descriptions, if the media section can be recycled, it's |
| 393 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 394 | continue; |
| 395 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 396 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 397 | return false; |
| 398 | } |
| 399 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 400 | new_desc.contents()[i].media_description(); |
| 401 | const MediaContentDescription* current_desc_mdesc = |
| 402 | current_desc.contents()[i].media_description(); |
| 403 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 404 | return false; |
| 405 | } |
| 406 | } |
| 407 | return true; |
| 408 | } |
| 409 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 410 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 411 | const SessionDescription& desc2) { |
| 412 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 415 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 416 | cricket::MediaType media_type) { |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 417 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 418 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 419 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 420 | static constexpr struct { |
| 421 | KeyExchangeProtocolType protocol_type; |
| 422 | cricket::MediaType media_type; |
| 423 | KeyExchangeProtocolMedia protocol_media; |
| 424 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 425 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 426 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 427 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 428 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 429 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 430 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 431 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 432 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 433 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 434 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 435 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 436 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 437 | }; |
| 438 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 439 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 440 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 441 | |
Mirko Bonadei | ab49982 | 2018-09-11 10:43:20 +0200 | [diff] [blame] | 442 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 443 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 444 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 445 | i.protocol_media, |
| 446 | kEnumCounterKeyProtocolMediaTypeMax); |
| 447 | } |
Harald Alvestrand | f9d0f1d | 2018-03-02 14:15:26 +0100 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 451 | void NoteAddIceCandidateResult(int result) { |
| 452 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 453 | kAddIceCandidateMax); |
| 454 | } |
| 455 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 456 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 457 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 458 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 459 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 460 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 461 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 462 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 463 | const cricket::ContentGroup* bundle = |
| 464 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 465 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 466 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 467 | continue; |
| 468 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 10:56:14 +0100 | [diff] [blame] | 469 | // Note what media is used with each crypto protocol, for all sections. |
| 470 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 471 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 472 | content_info.media_description()->type()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 473 | const std::string& mid = content_info.name; |
| 474 | if (bundle && bundle->HasContentName(mid) && |
| 475 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 476 | // This isn't the first media section in the BUNDLE group, so it's not |
| 477 | // required to have crypto attributes, since only the crypto attributes |
| 478 | // from the first section actually get used. |
| 479 | continue; |
| 480 | } |
| 481 | |
| 482 | // If the content isn't rejected or bundled into another m= section, crypto |
| 483 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 484 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 485 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 486 | if (!media || !tinfo) { |
| 487 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 488 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 489 | } |
| 490 | if (dtls_enabled) { |
| 491 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 492 | RTC_LOG(LS_WARNING) |
| 493 | << "Session description must have DTLS fingerprint if " |
| 494 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 495 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 496 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 497 | } |
| 498 | } else { |
| 499 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 500 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 501 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 502 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 506 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 510 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 511 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 512 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 513 | const cricket::ContentGroup* bundle = |
| 514 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 515 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 516 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 517 | continue; |
| 518 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 519 | const std::string& mid = content_info.name; |
| 520 | if (bundle && bundle->HasContentName(mid) && |
| 521 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 522 | // This isn't the first media section in the BUNDLE group, so it's not |
| 523 | // required to have ufrag/password, since only the ufrag/password from |
| 524 | // the first section actually get used. |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | // If the content isn't rejected or bundled into another m= section, |
| 529 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 530 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 531 | if (!tinfo) { |
| 532 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 533 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 534 | return false; |
| 535 | } |
| 536 | if (tinfo->description.ice_ufrag.empty() || |
| 537 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 538 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | } |
| 542 | return true; |
| 543 | } |
| 544 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 545 | // Get the SCTP port out of a SessionDescription. |
| 546 | // Return -1 if not found. |
| 547 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 548 | const cricket::DataContentDescription* data_desc = |
| 549 | GetFirstDataContentDescription(session_description); |
| 550 | RTC_DCHECK(data_desc); |
| 551 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 552 | return -1; |
| 553 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 554 | std::string value; |
| 555 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 556 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 557 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 558 | if (!codec.Matches(match_pattern)) { |
| 559 | continue; |
| 560 | } |
| 561 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 562 | return rtc::FromString<int>(value); |
| 563 | } |
| 564 | } |
| 565 | return -1; |
| 566 | } |
| 567 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 568 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 569 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 570 | const SessionDescriptionInterface* new_desc, |
| 571 | const std::string& content_name) { |
| 572 | if (!old_desc) { |
| 573 | return false; |
| 574 | } |
| 575 | const SessionDescription* new_sd = new_desc->description(); |
| 576 | const SessionDescription* old_sd = old_desc->description(); |
| 577 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 578 | if (!cinfo || cinfo->rejected) { |
| 579 | return false; |
| 580 | } |
| 581 | // If the content isn't rejected, check if ufrag and password has changed. |
| 582 | const cricket::TransportDescription* new_transport_desc = |
| 583 | new_sd->GetTransportDescriptionByName(content_name); |
| 584 | const cricket::TransportDescription* old_transport_desc = |
| 585 | old_sd->GetTransportDescriptionByName(content_name); |
| 586 | if (!new_transport_desc || !old_transport_desc) { |
| 587 | // No transport description exists. This is not an ICE restart. |
| 588 | return false; |
| 589 | } |
| 590 | if (cricket::IceCredentialsChanged( |
| 591 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 592 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 593 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 594 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 595 | return true; |
| 596 | } |
| 597 | return false; |
| 598 | } |
| 599 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 600 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 601 | // from an RTCError. |
| 602 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 603 | SdpType type, |
| 604 | const RTCError& error) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 605 | rtc::StringBuilder oss; |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 606 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 607 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 608 | return oss.Release(); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 611 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 612 | std::string output = "streams=["; |
| 613 | const char* separator = ""; |
| 614 | for (const auto& stream_id : stream_ids) { |
| 615 | output.append(separator).append(stream_id); |
| 616 | separator = ", "; |
| 617 | } |
| 618 | output.append("]"); |
| 619 | return output; |
| 620 | } |
| 621 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 622 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 623 | int rtc_configuration_parameter) { |
| 624 | if (rtc_configuration_parameter == |
| 625 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 626 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 627 | } |
| 628 | return rtc_configuration_parameter; |
| 629 | } |
| 630 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 631 | cricket::DataMessageType ToCricketDataMessageType(DataMessageType type) { |
| 632 | switch (type) { |
| 633 | case DataMessageType::kText: |
| 634 | return cricket::DMT_TEXT; |
| 635 | case DataMessageType::kBinary: |
| 636 | return cricket::DMT_BINARY; |
| 637 | case DataMessageType::kControl: |
| 638 | return cricket::DMT_CONTROL; |
| 639 | default: |
| 640 | return cricket::DMT_NONE; |
| 641 | } |
| 642 | return cricket::DMT_NONE; |
| 643 | } |
| 644 | |
| 645 | DataMessageType ToWebrtcDataMessageType(cricket::DataMessageType type) { |
| 646 | switch (type) { |
| 647 | case cricket::DMT_TEXT: |
| 648 | return DataMessageType::kText; |
| 649 | case cricket::DMT_BINARY: |
| 650 | return DataMessageType::kBinary; |
| 651 | case cricket::DMT_CONTROL: |
| 652 | return DataMessageType::kControl; |
| 653 | case cricket::DMT_NONE: |
| 654 | default: |
| 655 | RTC_NOTREACHED(); |
| 656 | } |
| 657 | return DataMessageType::kControl; |
| 658 | } |
| 659 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 660 | } // namespace |
| 661 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 662 | // Upon completion, posts a task to execute the callback of the |
| 663 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 664 | // point, the state of the peer connection might no longer reflect the effects |
| 665 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 666 | // modified during the post. |
| 667 | // TODO(hbos): Remove this class once we remove the version of |
| 668 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 669 | // SetSessionDescriptionObserver as an argument. |
| 670 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 671 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 672 | public: |
| 673 | SetRemoteDescriptionObserverAdapter( |
| 674 | rtc::scoped_refptr<PeerConnection> pc, |
| 675 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 676 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 677 | |
| 678 | // SetRemoteDescriptionObserverInterface implementation. |
| 679 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 680 | if (error.ok()) |
| 681 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 682 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 683 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | private: |
| 687 | rtc::scoped_refptr<PeerConnection> pc_; |
| 688 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 689 | }; |
| 690 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 691 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 692 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 693 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 694 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 695 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 696 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 697 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 698 | BundlePolicy bundle_policy; |
| 699 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 700 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 701 | int ice_candidate_pool_size; |
| 702 | bool disable_ipv6; |
| 703 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 704 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 705 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 706 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 707 | absl::optional<int> screencast_min_bitrate; |
| 708 | absl::optional<bool> combined_audio_video_bwe; |
| 709 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 710 | TcpCandidatePolicy tcp_candidate_policy; |
| 711 | CandidateNetworkPolicy candidate_network_policy; |
| 712 | int audio_jitter_buffer_max_packets; |
| 713 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 714 | int audio_jitter_buffer_min_delay_ms; |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 715 | bool audio_jitter_buffer_enable_rtx_handling; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 716 | int ice_connection_receiving_timeout; |
| 717 | int ice_backup_candidate_pair_ping_interval; |
| 718 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 719 | bool prioritize_most_likely_ice_candidate_pairs; |
| 720 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 721 | bool prune_turn_ports; |
| 722 | bool presume_writable_when_fully_relayed; |
| 723 | bool enable_ice_renomination; |
| 724 | bool redetermine_role_on_ice_restart; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 725 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 726 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 727 | absl::optional<int> ice_check_min_interval; |
| 728 | absl::optional<int> ice_unwritable_timeout; |
| 729 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 730 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 731 | absl::optional<int> stun_candidate_keepalive_interval; |
| 732 | absl::optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 733 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 734 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 735 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 736 | bool active_reset_srtp_params; |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 737 | bool use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 738 | bool use_media_transport_for_data_channels; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 739 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 740 | bool offer_extmap_allow_mixed; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 741 | }; |
| 742 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 743 | "Did you add something to RTCConfiguration and forget to " |
| 744 | "update operator==?"); |
| 745 | return type == o.type && servers == o.servers && |
| 746 | bundle_policy == o.bundle_policy && |
| 747 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 748 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 749 | candidate_network_policy == o.candidate_network_policy && |
| 750 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 751 | audio_jitter_buffer_fast_accelerate == |
| 752 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 753 | audio_jitter_buffer_min_delay_ms == |
| 754 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 755 | audio_jitter_buffer_enable_rtx_handling == |
| 756 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 757 | ice_connection_receiving_timeout == |
| 758 | o.ice_connection_receiving_timeout && |
| 759 | ice_backup_candidate_pair_ping_interval == |
| 760 | o.ice_backup_candidate_pair_ping_interval && |
| 761 | continual_gathering_policy == o.continual_gathering_policy && |
| 762 | certificates == o.certificates && |
| 763 | prioritize_most_likely_ice_candidate_pairs == |
| 764 | o.prioritize_most_likely_ice_candidate_pairs && |
| 765 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 766 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 767 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 768 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 769 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 770 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 771 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 772 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 773 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 774 | prune_turn_ports == o.prune_turn_ports && |
| 775 | presume_writable_when_fully_relayed == |
| 776 | o.presume_writable_when_fully_relayed && |
| 777 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 778 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 779 | ice_check_interval_strong_connectivity == |
| 780 | o.ice_check_interval_strong_connectivity && |
| 781 | ice_check_interval_weak_connectivity == |
| 782 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 783 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 784 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 785 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 786 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 787 | stun_candidate_keepalive_interval == |
| 788 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 789 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 790 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 791 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 792 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 793 | active_reset_srtp_params == o.active_reset_srtp_params && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 794 | use_media_transport == o.use_media_transport && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 795 | use_media_transport_for_data_channels == |
| 796 | o.use_media_transport_for_data_channels && |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 797 | crypto_options == o.crypto_options && |
| 798 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 802 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 803 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 804 | } |
| 805 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 806 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 807 | std::string GenerateRtcpCname() { |
| 808 | std::string cname; |
| 809 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 810 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 811 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 812 | } |
| 813 | return cname; |
| 814 | } |
| 815 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 816 | bool ValidateOfferAnswerOptions( |
| 817 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 818 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 819 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 820 | } |
| 821 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 822 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 823 | // m= sections (in other words, nothing that involves a map/array). |
| 824 | void ExtractSharedMediaSessionOptions( |
| 825 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 826 | cricket::MediaSessionOptions* session_options) { |
| 827 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 828 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 829 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 830 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 831 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 832 | std::unique_ptr<RtcEventLog> event_log, |
| 833 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 834 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 835 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 836 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 837 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 838 | remote_streams_(StreamCollection::Create()), |
| 839 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 840 | |
| 841 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 842 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 843 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 844 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 845 | // Need to stop transceivers before destroying the stats collector because |
| 846 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 847 | // stopping. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 848 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 849 | transceiver->Stop(); |
| 850 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 851 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 852 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 853 | if (stats_collector_) { |
| 854 | stats_collector_->WaitForPendingRequest(); |
| 855 | stats_collector_ = nullptr; |
| 856 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 857 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 858 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 859 | // the last stats request can still read from the channels. |
| 860 | DestroyAllChannels(); |
| 861 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 862 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 863 | |
| 864 | webrtc_session_desc_factory_.reset(); |
| 865 | sctp_invoker_.reset(); |
| 866 | sctp_factory_.reset(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 867 | media_transport_invoker_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 868 | transport_controller_.reset(); |
| 869 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 870 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 871 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 872 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 873 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 874 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 875 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 876 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 877 | event_log_.reset(); |
| 878 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 881 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 882 | // Destroy video channels first since they may have a pointer to a voice |
| 883 | // channel. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 884 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 885 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 886 | DestroyTransceiverChannel(transceiver); |
| 887 | } |
| 888 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 889 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 890 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 891 | DestroyTransceiverChannel(transceiver); |
| 892 | } |
| 893 | } |
| 894 | DestroyDataChannel(); |
| 895 | } |
| 896 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 897 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 898 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 899 | PeerConnectionDependencies dependencies) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 900 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 901 | |
| 902 | RTCError config_error = ValidateConfiguration(configuration); |
| 903 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 904 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 905 | return false; |
| 906 | } |
| 907 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 908 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 909 | RTC_LOG(LS_ERROR) |
| 910 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 911 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 912 | return false; |
| 913 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 914 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 915 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 916 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 917 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 918 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 919 | return false; |
| 920 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 921 | |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 922 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 923 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 924 | port_allocator_ = std::move(dependencies.allocator); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 925 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 926 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 927 | cricket::ServerAddresses stun_servers; |
| 928 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 929 | |
| 930 | RTCErrorType parse_error = |
| 931 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 932 | if (parse_error != RTCErrorType::NONE) { |
| 933 | return false; |
| 934 | } |
| 935 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 936 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 937 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 938 | if (!network_thread()->Invoke<bool>( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 939 | RTC_FROM_HERE, |
| 940 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
| 941 | stun_servers, turn_servers, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 942 | return false; |
| 943 | } |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 944 | // If initialization was successful, note if STUN or TURN servers |
| 945 | // were supplied. |
| 946 | if (!stun_servers.empty()) { |
| 947 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 948 | } |
| 949 | if (!turn_servers.empty()) { |
| 950 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 951 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 952 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 953 | // Send information about IPv4/IPv6 status. |
| 954 | PeerConnectionAddressFamilyCounter address_family; |
| 955 | if (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
| 956 | address_family = kPeerConnection_IPv6; |
| 957 | } else { |
| 958 | address_family = kPeerConnection_IPv4; |
| 959 | } |
| 960 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 961 | kPeerConnectionAddressFamilyCounter_Max); |
| 962 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 963 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 964 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 965 | // RFC 3264: The numeric value of the session id and version in the |
| 966 | // o line MUST be representable with a "64 bit signed integer". |
| 967 | // Due to this constraint session id |session_id_| is max limited to |
| 968 | // LLONG_MAX. |
| 969 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 970 | JsepTransportController::Config config; |
| 971 | config.redetermine_role_on_ice_restart = |
| 972 | configuration.redetermine_role_on_ice_restart; |
| 973 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 974 | config.disable_encryption = options.disable_encryption; |
| 975 | config.bundle_policy = configuration.bundle_policy; |
| 976 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 977 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 978 | // stub. |
| 979 | config.crypto_options = configuration.crypto_options.has_value() |
| 980 | ? *configuration.crypto_options |
| 981 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 982 | config.transport_observer = this; |
Qingsi Wang | 7685e86 | 2018-06-11 20:15:46 -0700 | [diff] [blame] | 983 | config.event_log = event_log_.get(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 984 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 985 | config.enable_external_auth = true; |
| 986 | #endif |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 987 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 988 | |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 989 | if (configuration.use_media_transport || |
| 990 | configuration.use_media_transport_for_data_channels) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 991 | if (!factory_->media_transport_factory()) { |
| 992 | RTC_DCHECK(false) |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 993 | << "PeerConnecton is initialized with use_media_transport = true or " |
| 994 | << "use_media_transport_for_data_channels = true " |
| 995 | << "but media transport factory is not set in PeerConnectionFactory"; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 996 | return false; |
| 997 | } |
| 998 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 999 | config.use_media_transport_for_media = configuration.use_media_transport; |
| 1000 | config.use_media_transport_for_data_channels = |
| 1001 | configuration.use_media_transport_for_data_channels; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1002 | config.media_transport_factory = factory_->media_transport_factory(); |
| 1003 | } |
| 1004 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1005 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 1006 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 1007 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1008 | transport_controller_->SignalIceConnectionState.connect( |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1009 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 1010 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 1011 | this, &PeerConnection::SetStandardizedIceConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1012 | transport_controller_->SignalConnectionState.connect( |
| 1013 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1014 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1015 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1016 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1017 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1018 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1019 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 1020 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 1021 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 1022 | |
| 1023 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1024 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1025 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1026 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1027 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 1028 | configuration_ = configuration; |
| 1029 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1030 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 1031 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 1032 | if (!configuration.certificates.empty()) { |
| 1033 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 1034 | // just picking the first one. The decision should be made based on the DTLS |
| 1035 | // handshake. The DTLS negotiations need to know about all certificates. |
| 1036 | certificate = configuration.certificates[0]; |
| 1037 | } |
| 1038 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1039 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1040 | |
| 1041 | if (options.disable_encryption) { |
| 1042 | dtls_enabled_ = false; |
| 1043 | } else { |
| 1044 | // Enable DTLS by default if we have an identity store or a certificate. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1045 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1046 | // |configuration| can override the default |dtls_enabled_| value. |
| 1047 | if (configuration.enable_dtls_srtp) { |
| 1048 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 1052 | if (configuration.use_media_transport_for_data_channels) { |
| 1053 | if (configuration.enable_rtp_data_channel) { |
| 1054 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1055 | "use_media_transport_for_data_channels are " |
| 1056 | "incompatible and cannot both be set to true"; |
| 1057 | return false; |
| 1058 | } |
| 1059 | data_channel_type_ = cricket::DCT_MEDIA_TRANSPORT; |
| 1060 | } else if (configuration.enable_rtp_data_channel) { |
| 1061 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is |
| 1062 | // set. It takes precendence over the disable_sctp_data_channels |
| 1063 | // PeerConnectionFactoryInterface::Options. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1064 | data_channel_type_ = cricket::DCT_RTP; |
| 1065 | } else { |
| 1066 | // DTLS has to be enabled to use SCTP. |
| 1067 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 1068 | data_channel_type_ = cricket::DCT_SCTP; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | video_options_.screencast_min_bitrate_kbps = |
| 1073 | configuration.screencast_min_bitrate; |
| 1074 | audio_options_.combined_audio_video_bwe = |
| 1075 | configuration.combined_audio_video_bwe; |
| 1076 | |
| 1077 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1078 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1079 | |
| 1080 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 1081 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1082 | |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 1083 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 1084 | configuration.audio_jitter_buffer_min_delay_ms; |
| 1085 | |
Jakob Ivarsson | 53eae87 | 2019-01-10 15:58:36 +0100 | [diff] [blame] | 1086 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 1087 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 1088 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1089 | // Whether the certificate generator/certificate is null or not determines |
| 1090 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1091 | // the right instructions by clearing the variables if needed. |
| 1092 | if (!dtls_enabled_) { |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1093 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1094 | certificate = nullptr; |
| 1095 | } else if (certificate) { |
| 1096 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 1097 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1101 | signaling_thread(), channel_manager(), this, session_id(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1102 | std::move(dependencies.cert_generator), certificate, &ssrc_generator_)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1103 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1104 | this, &PeerConnection::OnCertificateReady); |
| 1105 | |
| 1106 | if (options.disable_encryption) { |
| 1107 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1108 | } |
| 1109 | |
| 1110 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 1111 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1112 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1113 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1114 | // Add default audio/video transceivers for Plan B SDP. |
| 1115 | if (!IsUnifiedPlan()) { |
| 1116 | transceivers_.push_back( |
| 1117 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1118 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1119 | transceivers_.push_back( |
| 1120 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1121 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1122 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 1123 | int delay_ms = |
| 1124 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1125 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1126 | MSG_REPORT_USAGE_PATTERN, nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1127 | return true; |
| 1128 | } |
| 1129 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1130 | RTCError PeerConnection::ValidateConfiguration( |
| 1131 | const RTCConfiguration& config) const { |
| 1132 | if (config.ice_regather_interval_range && |
| 1133 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1134 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1135 | "ice_regather_interval_range specified but continual " |
| 1136 | "gathering policy is GATHER_ONCE"); |
| 1137 | } |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 1138 | auto result = |
| 1139 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1140 | return result; |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1143 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1144 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1145 | "Plan SdpSemantics. Please use GetSenders " |
| 1146 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1147 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1150 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1151 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1152 | "Plan SdpSemantics. Please use GetReceivers " |
| 1153 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1154 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 1157 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1158 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1159 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1160 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1161 | if (IsClosed()) { |
| 1162 | return false; |
| 1163 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1164 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1165 | return false; |
| 1166 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1167 | |
| 1168 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1169 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1170 | observer->SignalAudioTrackAdded.connect(this, |
| 1171 | &PeerConnection::OnAudioTrackAdded); |
| 1172 | observer->SignalAudioTrackRemoved.connect( |
| 1173 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1174 | observer->SignalVideoTrackAdded.connect(this, |
| 1175 | &PeerConnection::OnVideoTrackAdded); |
| 1176 | observer->SignalVideoTrackRemoved.connect( |
| 1177 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1178 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1179 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1180 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1181 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1182 | } |
| 1183 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1184 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1187 | stats_->AddStream(local_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1188 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1189 | return true; |
| 1190 | } |
| 1191 | |
| 1192 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1193 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1194 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1195 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1196 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1197 | if (!IsClosed()) { |
| 1198 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1199 | RemoveAudioTrack(track.get(), local_stream); |
| 1200 | } |
| 1201 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1202 | RemoveVideoTrack(track.get(), local_stream); |
| 1203 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1204 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1205 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1206 | stream_observers_.erase( |
| 1207 | std::remove_if( |
| 1208 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1209 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 16:05:28 -0700 | [diff] [blame] | 1210 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1211 | }), |
| 1212 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1213 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1214 | if (IsClosed()) { |
| 1215 | return; |
| 1216 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1217 | Observer()->OnRenegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1220 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1221 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1222 | const std::vector<std::string>& stream_ids) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1223 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1224 | if (!track) { |
| 1225 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1226 | } |
| 1227 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1228 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1229 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1230 | "Track has invalid kind: " + track->kind()); |
| 1231 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1232 | if (IsClosed()) { |
| 1233 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1234 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1235 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1236 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1237 | LOG_AND_RETURN_ERROR( |
| 1238 | RTCErrorType::INVALID_PARAMETER, |
| 1239 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1240 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1241 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1242 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1243 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1244 | if (sender_or_error.ok()) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1245 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1246 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1247 | } |
| 1248 | return sender_or_error; |
| 1249 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1250 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1251 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1252 | PeerConnection::AddTrackPlanB( |
| 1253 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1254 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1255 | if (stream_ids.size() > 1u) { |
| 1256 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1257 | "AddTrack with more than one stream is not " |
| 1258 | "supported with Plan B semantics."); |
| 1259 | } |
| 1260 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1261 | if (adjusted_stream_ids.empty()) { |
| 1262 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1263 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1264 | cricket::MediaType media_type = |
| 1265 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1266 | ? cricket::MEDIA_TYPE_AUDIO |
| 1267 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1268 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1269 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1270 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1271 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1272 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1273 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1274 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1275 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1276 | if (sender_info) { |
| 1277 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1278 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1279 | } else { |
| 1280 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1281 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1282 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1283 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 1284 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1285 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1286 | if (sender_info) { |
| 1287 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1288 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1289 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1290 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1291 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1292 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1293 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1294 | PeerConnection::AddTrackUnifiedPlan( |
| 1295 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1296 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1297 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1298 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1299 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1300 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1301 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1302 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1303 | transceiver->internal()->set_direction( |
| 1304 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1305 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1306 | transceiver->internal()->set_direction( |
| 1307 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1308 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1309 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1310 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1311 | } else { |
| 1312 | cricket::MediaType media_type = |
| 1313 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1314 | ? cricket::MEDIA_TYPE_AUDIO |
| 1315 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1316 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1317 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1318 | std::string sender_id = track->id(); |
| 1319 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1320 | // This can happen if this is the second time AddTrack has created a sender |
| 1321 | // for this track. |
| 1322 | if (FindSenderById(sender_id)) { |
| 1323 | sender_id = rtc::CreateRandomUuid(); |
| 1324 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1325 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1326 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1327 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1328 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1329 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1330 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1331 | return transceiver->sender(); |
| 1332 | } |
| 1333 | |
| 1334 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1335 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1336 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1337 | RTC_DCHECK(track); |
| 1338 | for (auto transceiver : transceivers_) { |
| 1339 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1340 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1341 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 1342 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1343 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1344 | return transceiver; |
| 1345 | } |
| 1346 | } |
| 1347 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1351 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1352 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
Steve Anton | 24db573 | 2018-07-23 10:27:33 -0700 | [diff] [blame] | 1355 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1356 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1357 | if (!sender) { |
| 1358 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1359 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1360 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1361 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1362 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1363 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1364 | if (IsUnifiedPlan()) { |
| 1365 | auto transceiver = FindTransceiverBySender(sender); |
| 1366 | if (!transceiver || !sender->track()) { |
| 1367 | return RTCError::OK(); |
| 1368 | } |
| 1369 | sender->SetTrack(nullptr); |
| 1370 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1371 | transceiver->internal()->set_direction( |
| 1372 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1373 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1374 | transceiver->internal()->set_direction( |
| 1375 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1376 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1377 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1378 | bool removed; |
| 1379 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1380 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1381 | } else { |
| 1382 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1383 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1384 | } |
| 1385 | if (!removed) { |
| 1386 | LOG_AND_RETURN_ERROR( |
| 1387 | RTCErrorType::INVALID_PARAMETER, |
| 1388 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1389 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1390 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1391 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1392 | return RTCError::OK(); |
| 1393 | } |
| 1394 | |
| 1395 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1396 | PeerConnection::FindTransceiverBySender( |
| 1397 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1398 | for (auto transceiver : transceivers_) { |
| 1399 | if (transceiver->sender() == sender) { |
| 1400 | return transceiver; |
| 1401 | } |
| 1402 | } |
| 1403 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1404 | } |
| 1405 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1406 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1407 | PeerConnection::AddTransceiver( |
| 1408 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1409 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1410 | } |
| 1411 | |
| 1412 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1413 | PeerConnection::AddTransceiver( |
| 1414 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1415 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1416 | RTC_CHECK(IsUnifiedPlan()) |
| 1417 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1418 | if (!track) { |
| 1419 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1420 | } |
| 1421 | cricket::MediaType media_type; |
| 1422 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1423 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1424 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1425 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1426 | } else { |
| 1427 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1428 | "Track kind is not audio or video"); |
| 1429 | } |
| 1430 | return AddTransceiver(media_type, track, init); |
| 1431 | } |
| 1432 | |
| 1433 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1434 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1435 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1436 | } |
| 1437 | |
| 1438 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1439 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1440 | const RtpTransceiverInit& init) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1441 | RTC_CHECK(IsUnifiedPlan()) |
| 1442 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1443 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1444 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1445 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1446 | "media type is not audio or video"); |
| 1447 | } |
| 1448 | return AddTransceiver(media_type, nullptr, init); |
| 1449 | } |
| 1450 | |
| 1451 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1452 | PeerConnection::AddTransceiver( |
| 1453 | cricket::MediaType media_type, |
| 1454 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1455 | const RtpTransceiverInit& init, |
| 1456 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1457 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1458 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1459 | if (track) { |
| 1460 | RTC_DCHECK_EQ(media_type, |
| 1461 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1462 | ? cricket::MEDIA_TYPE_AUDIO |
| 1463 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1464 | } |
| 1465 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame^] | 1466 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1467 | [](const RtpEncodingParameters& encoding) { |
| 1468 | return !encoding.rid.empty(); |
| 1469 | }); |
| 1470 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-06 17:09:52 -0800 | [diff] [blame] | 1471 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame^] | 1472 | RTCErrorType::INVALID_PARAMETER, |
| 1473 | "RIDs must be provided for either all or none of the send encodings."); |
Emircan Uysaler | 7832343 | 2019-02-08 20:41:39 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame^] | 1476 | if (absl::c_any_of(init.send_encodings, |
| 1477 | [](const RtpEncodingParameters& encoding) { |
| 1478 | return encoding.ssrc.has_value(); |
| 1479 | })) { |
| 1480 | LOG_AND_RETURN_ERROR( |
| 1481 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1482 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | RtpParameters parameters; |
| 1486 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame^] | 1487 | |
| 1488 | // Encodings are dropped from the tail if too many are provided. |
| 1489 | if (parameters.encodings.size() > kMaxSimulcastStreams) { |
| 1490 | parameters.encodings.erase( |
| 1491 | parameters.encodings.begin() + kMaxSimulcastStreams, |
| 1492 | parameters.encodings.end()); |
| 1493 | } |
| 1494 | |
| 1495 | // Single RID should be removed. |
| 1496 | if (parameters.encodings.size() == 1 && |
| 1497 | !parameters.encodings[0].rid.empty()) { |
| 1498 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1499 | parameters.encodings[0].rid.clear(); |
| 1500 | } |
| 1501 | |
| 1502 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1503 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1504 | rtc::UniqueStringGenerator rid_generator; |
| 1505 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 1506 | encoding.rid = rid_generator(); |
| 1507 | } |
| 1508 | } |
| 1509 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1510 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1511 | LOG_AND_RETURN_ERROR( |
| 1512 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1513 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1514 | } |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1515 | |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 1516 | auto result = cricket::CheckRtpParametersValues(parameters); |
| 1517 | if (!result.ok()) { |
| 1518 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1519 | } |
| 1520 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1521 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1522 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1523 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1524 | // that sender ID is already in use. |
| 1525 | std::string sender_id = |
| 1526 | (track && !FindSenderById(track->id()) ? track->id() |
| 1527 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1528 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame^] | 1529 | parameters.encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1530 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1531 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1532 | transceiver->internal()->set_direction(init.direction); |
| 1533 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1534 | if (fire_callback) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1535 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1536 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1537 | |
| 1538 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1539 | } |
| 1540 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1541 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1542 | PeerConnection::CreateSender( |
| 1543 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1544 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1545 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1546 | const std::vector<std::string>& stream_ids, |
| 1547 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1548 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1549 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1550 | RTC_DCHECK(!track || |
| 1551 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1552 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1553 | signaling_thread(), |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1554 | new AudioRtpSender(worker_thread(), id, stats_.get())); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1555 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1556 | } else { |
| 1557 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1558 | RTC_DCHECK(!track || |
| 1559 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1560 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1561 | signaling_thread(), new VideoRtpSender(worker_thread(), id)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1562 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1563 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1564 | bool set_track_succeeded = sender->SetTrack(track); |
| 1565 | RTC_DCHECK(set_track_succeeded); |
| 1566 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 1567 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1568 | return sender; |
| 1569 | } |
| 1570 | |
| 1571 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1572 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1573 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1574 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1575 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1576 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1577 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1578 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1579 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1580 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1581 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1582 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1583 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 1584 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1585 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1586 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1587 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1588 | return receiver; |
| 1589 | } |
| 1590 | |
| 1591 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1592 | PeerConnection::CreateAndAddTransceiver( |
| 1593 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1594 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1595 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 11:13:50 -0700 | [diff] [blame] | 1596 | // Ensure that the new sender does not have an ID that is already in use by |
| 1597 | // another sender. |
| 1598 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1599 | // could be invalid, but should not cause a crash). |
| 1600 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1601 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1602 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1603 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1604 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1605 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1606 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1609 | void PeerConnection::OnNegotiationNeeded() { |
| 1610 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1611 | RTC_DCHECK(!IsClosed()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1612 | Observer()->OnRenegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 1613 | } |
| 1614 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1615 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1616 | const std::string& kind, |
| 1617 | const std::string& stream_id) { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1618 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1619 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1620 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1621 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1622 | if (IsClosed()) { |
| 1623 | return nullptr; |
| 1624 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1625 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1626 | // Internally we need to have one stream with Plan B semantics, so we |
| 1627 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1628 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 1629 | if (stream_id.empty()) { |
| 1630 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1631 | RTC_LOG(LS_INFO) |
| 1632 | << "No stream_id specified for sender. Generated stream ID: " |
| 1633 | << stream_ids[0]; |
| 1634 | } else { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 1635 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1636 | } |
| 1637 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1638 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1639 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1640 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1641 | auto* audio_sender = new AudioRtpSender( |
| 1642 | worker_thread(), rtc::CreateRandomUuid(), stats_.get()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1643 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1644 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1645 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1646 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1647 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1648 | auto* video_sender = |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1649 | new VideoRtpSender(worker_thread(), rtc::CreateRandomUuid()); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 1650 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1651 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1652 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1653 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1654 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1655 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1656 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1657 | } |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 1658 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1659 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1660 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1661 | } |
| 1662 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1663 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1664 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1665 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1666 | for (const auto& sender : GetSendersInternal()) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1667 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1668 | } |
| 1669 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1672 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1673 | PeerConnection::GetSendersInternal() const { |
| 1674 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1675 | all_senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1676 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1677 | auto senders = transceiver->internal()->senders(); |
| 1678 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1679 | } |
| 1680 | return all_senders; |
| 1681 | } |
| 1682 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1683 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1684 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1685 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1686 | for (const auto& receiver : GetReceiversInternal()) { |
| 1687 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1688 | } |
| 1689 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1692 | std::vector< |
| 1693 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1694 | PeerConnection::GetReceiversInternal() const { |
| 1695 | std::vector< |
| 1696 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1697 | all_receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1698 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1699 | auto receivers = transceiver->internal()->receivers(); |
| 1700 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1701 | receivers.end()); |
| 1702 | } |
| 1703 | return all_receivers; |
| 1704 | } |
| 1705 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1706 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1707 | PeerConnection::GetTransceivers() const { |
Steve Anton | fc85371 | 2018-03-01 13:48:58 -0800 | [diff] [blame] | 1708 | RTC_CHECK(IsUnifiedPlan()) |
| 1709 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1710 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1711 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1712 | all_transceivers.push_back(transceiver); |
| 1713 | } |
| 1714 | return all_transceivers; |
| 1715 | } |
| 1716 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1717 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1718 | MediaStreamTrackInterface* track, |
| 1719 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1720 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1721 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1722 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1723 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1724 | return false; |
| 1725 | } |
| 1726 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1727 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1728 | // The StatsCollector is used to tell if a track is valid because it may |
| 1729 | // remember tracks that the PeerConnection previously removed. |
| 1730 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1731 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1732 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1733 | return false; |
| 1734 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 1735 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 1736 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1737 | return true; |
| 1738 | } |
| 1739 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1740 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1741 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1742 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1743 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1744 | stats_collector_->GetStatsReport(callback); |
| 1745 | } |
| 1746 | |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1747 | void PeerConnection::GetStats( |
| 1748 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1749 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1750 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1751 | RTC_DCHECK(callback); |
| 1752 | RTC_DCHECK(stats_collector_); |
| 1753 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1754 | if (selector) { |
| 1755 | for (const auto& proxy_transceiver : transceivers_) { |
| 1756 | for (const auto& proxy_sender : |
| 1757 | proxy_transceiver->internal()->senders()) { |
| 1758 | if (proxy_sender == selector) { |
| 1759 | internal_sender = proxy_sender->internal(); |
| 1760 | break; |
| 1761 | } |
| 1762 | } |
| 1763 | if (internal_sender) |
| 1764 | break; |
| 1765 | } |
| 1766 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1767 | // If there is no |internal_sender| then |selector| is either null or does not |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1768 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1769 | // PeerConnection). This means that "all the stats objects representing the |
| 1770 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1771 | // produces an empty stats report. |
| 1772 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1773 | } |
| 1774 | |
| 1775 | void PeerConnection::GetStats( |
| 1776 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1777 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1778 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
| 1779 | RTC_DCHECK(callback); |
| 1780 | RTC_DCHECK(stats_collector_); |
| 1781 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1782 | if (selector) { |
| 1783 | for (const auto& proxy_transceiver : transceivers_) { |
| 1784 | for (const auto& proxy_receiver : |
| 1785 | proxy_transceiver->internal()->receivers()) { |
| 1786 | if (proxy_receiver == selector) { |
| 1787 | internal_receiver = proxy_receiver->internal(); |
| 1788 | break; |
| 1789 | } |
| 1790 | } |
| 1791 | if (internal_receiver) |
| 1792 | break; |
| 1793 | } |
| 1794 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 1795 | // If there is no |internal_receiver| then |selector| is either null or does |
Henrik Boström | 1df1bf8 | 2018-03-20 13:24:20 +0100 | [diff] [blame] | 1796 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1797 | // the PeerConnection). This means that "all the stats objects representing |
| 1798 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1799 | // selector produces an empty stats report. |
| 1800 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1801 | } |
| 1802 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1803 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1804 | return signaling_state_; |
| 1805 | } |
| 1806 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1807 | PeerConnectionInterface::IceConnectionState |
| 1808 | PeerConnection::ice_connection_state() { |
| 1809 | return ice_connection_state_; |
| 1810 | } |
| 1811 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1812 | PeerConnectionInterface::IceConnectionState |
| 1813 | PeerConnection::standardized_ice_connection_state() { |
| 1814 | return standardized_ice_connection_state_; |
| 1815 | } |
| 1816 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1817 | PeerConnectionInterface::PeerConnectionState |
| 1818 | PeerConnection::peer_connection_state() { |
| 1819 | return connection_state_; |
| 1820 | } |
| 1821 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1822 | PeerConnectionInterface::IceGatheringState |
| 1823 | PeerConnection::ice_gathering_state() { |
| 1824 | return ice_gathering_state_; |
| 1825 | } |
| 1826 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1827 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1828 | const std::string& label, |
| 1829 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1830 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1831 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1832 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1833 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1834 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1835 | if (config) { |
| 1836 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1837 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1838 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1839 | InternalCreateDataChannel(label, internal_config.get())); |
| 1840 | if (!channel.get()) { |
| 1841 | return nullptr; |
| 1842 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1843 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1844 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1845 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1846 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 1847 | Observer()->OnRenegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1848 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 1849 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1850 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1851 | } |
| 1852 | |
| 1853 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1854 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1855 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1856 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1857 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1858 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1859 | return; |
| 1860 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1861 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1862 | if (IsClosed()) { |
| 1863 | std::string error = "CreateOffer called when PeerConnection is closed."; |
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( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1866 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1867 | return; |
| 1868 | } |
| 1869 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1870 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1871 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1872 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1873 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1874 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1875 | return; |
| 1876 | } |
| 1877 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1878 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1879 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1880 | if (IsUnifiedPlan()) { |
| 1881 | RTCError error = HandleLegacyOfferOptions(options); |
| 1882 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1883 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1884 | return; |
| 1885 | } |
| 1886 | } |
| 1887 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1888 | cricket::MediaSessionOptions session_options; |
| 1889 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1890 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1893 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1894 | const RTCOfferAnswerOptions& options) { |
| 1895 | RTC_DCHECK(IsUnifiedPlan()); |
| 1896 | |
| 1897 | if (options.offer_to_receive_audio == 0) { |
| 1898 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1899 | cricket::MEDIA_TYPE_AUDIO); |
| 1900 | } else if (options.offer_to_receive_audio == 1) { |
| 1901 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1902 | } else if (options.offer_to_receive_audio > 1) { |
| 1903 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1904 | "offer_to_receive_audio > 1 is not supported."); |
| 1905 | } |
| 1906 | |
| 1907 | if (options.offer_to_receive_video == 0) { |
| 1908 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1909 | cricket::MEDIA_TYPE_VIDEO); |
| 1910 | } else if (options.offer_to_receive_video == 1) { |
| 1911 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1912 | } else if (options.offer_to_receive_video > 1) { |
| 1913 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1914 | "offer_to_receive_video > 1 is not supported."); |
| 1915 | } |
| 1916 | |
| 1917 | return RTCError::OK(); |
| 1918 | } |
| 1919 | |
| 1920 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1921 | cricket::MediaType media_type) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1922 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1923 | RtpTransceiverDirection new_direction = |
| 1924 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 1925 | if (new_direction != transceiver->direction()) { |
| 1926 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 1927 | << " transceiver (MID=" |
| 1928 | << transceiver->mid().value_or("<not set>") << ") from " |
| 1929 | << RtpTransceiverDirectionToString( |
| 1930 | transceiver->direction()) |
| 1931 | << " to " |
| 1932 | << RtpTransceiverDirectionToString(new_direction) |
| 1933 | << " since CreateOffer specified offer_to_receive=0"; |
| 1934 | transceiver->internal()->set_direction(new_direction); |
| 1935 | } |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1940 | cricket::MediaType media_type) { |
| 1941 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 1942 | RTC_LOG(LS_INFO) |
| 1943 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 1944 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1945 | RtpTransceiverInit init; |
| 1946 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1947 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1952 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1953 | std::vector< |
| 1954 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1955 | receiving_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 1956 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1957 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1958 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1959 | receiving_transceivers.push_back(transceiver); |
| 1960 | } |
| 1961 | } |
| 1962 | return receiving_transceivers; |
| 1963 | } |
| 1964 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1965 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1966 | const RTCOfferAnswerOptions& options) { |
| 1967 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1968 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1969 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1970 | return; |
| 1971 | } |
| 1972 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1973 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1974 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1975 | std::string error = |
| 1976 | "PeerConnection cannot create an answer in a state other than " |
| 1977 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1978 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 1979 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 15:02:10 +0200 | [diff] [blame] | 1980 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1981 | return; |
| 1982 | } |
| 1983 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1984 | // The remote description should be set if we're in the right state. |
| 1985 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1986 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1987 | if (IsUnifiedPlan()) { |
| 1988 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1989 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1990 | "supported with Unified Plan semantics. Use the " |
| 1991 | "RtpTransceiver API instead."; |
| 1992 | } |
| 1993 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1994 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1995 | "supported with Unified Plan semantics. Use the " |
| 1996 | "RtpTransceiver API instead."; |
| 1997 | } |
| 1998 | } |
| 1999 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2000 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2001 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 2002 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2003 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | void PeerConnection::SetLocalDescription( |
| 2007 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2008 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2009 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2010 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2011 | // The SetLocalDescription contract is that we take ownership of the session |
| 2012 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 2013 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 2014 | // description as a unique_ptr argument to formalize this agreement. |
| 2015 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 2016 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2017 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2018 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2021 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2022 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2023 | PostSetSessionDescriptionFailure( |
| 2024 | observer, |
| 2025 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2026 | return; |
| 2027 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2028 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2029 | // If a session error has occurred the PeerConnection is in a possibly |
| 2030 | // inconsistent state so fail right away. |
| 2031 | if (session_error() != SessionError::kNone) { |
| 2032 | std::string error_message = GetSessionErrorMsg(); |
| 2033 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2034 | PostSetSessionDescriptionFailure( |
| 2035 | observer, |
| 2036 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2037 | return; |
| 2038 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2039 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2040 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 2041 | if (!error.ok()) { |
| 2042 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2043 | cricket::CS_LOCAL, desc->GetType(), error); |
| 2044 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2045 | PostSetSessionDescriptionFailure( |
| 2046 | observer, |
| 2047 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2048 | return; |
| 2049 | } |
| 2050 | |
| 2051 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 2052 | // which may destroy it before returning. |
| 2053 | const SdpType type = desc->GetType(); |
| 2054 | |
| 2055 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2056 | // |desc| may be destroyed at this point. |
| 2057 | |
| 2058 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2059 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 2060 | // inconsistent state, so act conservatively here and set the session error |
| 2061 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2062 | SetSessionError(SessionError::kContent, error.message()); |
| 2063 | std::string error_message = |
| 2064 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 2065 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 2066 | PostSetSessionDescriptionFailure( |
| 2067 | observer, |
| 2068 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2069 | return; |
| 2070 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2071 | RTC_DCHECK(local_description()); |
| 2072 | |
| 2073 | PostSetSessionDescriptionSuccess(observer); |
| 2074 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 2075 | // MaybeStartGathering needs to be called after posting |
| 2076 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 2077 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2078 | transport_controller_->MaybeStartGathering(); |
| 2079 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2080 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2081 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2082 | // MaybeStartGathering... |
| 2083 | network_thread()->Invoke<void>( |
| 2084 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2085 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2086 | // Make UMA notes about what was agreed to. |
| 2087 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2088 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2089 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | RTCError PeerConnection::ApplyLocalDescription( |
| 2093 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2094 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2095 | RTC_DCHECK(desc); |
| 2096 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2097 | // Update stats here so that we have the most recent stats for tracks and |
| 2098 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2099 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2100 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2101 | // Take a reference to the old local description since it's used below to |
| 2102 | // compare against the new local description. When setting the new local |
| 2103 | // description, grab ownership of the replaced session description in case it |
| 2104 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2105 | // of the method. |
| 2106 | const SessionDescriptionInterface* old_local_description = |
| 2107 | local_description(); |
| 2108 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2109 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2110 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2111 | replaced_local_description = pending_local_description_ |
| 2112 | ? std::move(pending_local_description_) |
| 2113 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2114 | current_local_description_ = std::move(desc); |
| 2115 | pending_local_description_ = nullptr; |
| 2116 | current_remote_description_ = std::move(pending_remote_description_); |
| 2117 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2118 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2119 | pending_local_description_ = std::move(desc); |
| 2120 | } |
| 2121 | // The session description to apply now must be accessed by |
| 2122 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2123 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2124 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2125 | if (!is_caller_) { |
| 2126 | if (remote_description()) { |
| 2127 | // Remote description was applied first, so this PC is the callee. |
| 2128 | is_caller_ = false; |
| 2129 | } else { |
| 2130 | // Local description is applied first, so this PC is the caller. |
| 2131 | is_caller_ = true; |
| 2132 | } |
| 2133 | } |
| 2134 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2135 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2136 | if (!error.ok()) { |
| 2137 | return error; |
| 2138 | } |
| 2139 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2140 | if (IsUnifiedPlan()) { |
| 2141 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2142 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2143 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2144 | if (!error.ok()) { |
| 2145 | return error; |
| 2146 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2147 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2148 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2149 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2150 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 2151 | // Note that code paths that don't set MID won't be able to use |
| 2152 | // information about DTLS transports. |
| 2153 | if (transceiver->mid()) { |
| 2154 | auto dtls_transport = |
| 2155 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2156 | transceiver->internal()->sender_internal()->set_transport( |
| 2157 | dtls_transport); |
| 2158 | transceiver->internal()->receiver_internal()->set_transport( |
| 2159 | dtls_transport); |
| 2160 | } |
| 2161 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2162 | const ContentInfo* content = |
| 2163 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2164 | if (!content) { |
| 2165 | continue; |
| 2166 | } |
| 2167 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2168 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2169 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2170 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2171 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2172 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2173 | // "recvonly", process the removal of a remote track for the media |
| 2174 | // description, given transceiver, removeList, and muteTracks. |
| 2175 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2176 | (transceiver->internal()->fired_direction() && |
| 2177 | RtpTransceiverDirectionHasRecv( |
| 2178 | *transceiver->internal()->fired_direction()))) { |
| 2179 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2180 | &removed_streams); |
| 2181 | } |
| 2182 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2183 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2184 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2185 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2186 | } |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2187 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2188 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2189 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2190 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2191 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2192 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2193 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2194 | } |
| 2195 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2196 | // Media channels will be created only when offer is set. These may use new |
| 2197 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2198 | if (type == SdpType::kOffer) { |
| 2199 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2200 | // description is applied. Restore back to old description. |
| 2201 | RTCError error = CreateChannels(*local_description()->description()); |
| 2202 | if (!error.ok()) { |
| 2203 | return error; |
| 2204 | } |
| 2205 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2206 | // Remove unused channels if MediaContentDescription is rejected. |
| 2207 | RemoveUnusedChannels(local_description()->description()); |
| 2208 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2209 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2210 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2211 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2212 | if (!error.ok()) { |
| 2213 | return error; |
| 2214 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2215 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2216 | if (remote_description()) { |
| 2217 | // Now that we have a local description, we can push down remote candidates. |
| 2218 | UseCandidatesInSessionDescription(remote_description()); |
| 2219 | } |
| 2220 | |
| 2221 | pending_ice_restarts_.clear(); |
| 2222 | if (session_error() != SessionError::kNone) { |
| 2223 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2224 | } |
| 2225 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2226 | // If setting the description decided our SSL role, allocate any necessary |
| 2227 | // SCTP sids. |
| 2228 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2229 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2230 | AllocateSctpSids(role); |
| 2231 | } |
| 2232 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2233 | if (IsUnifiedPlan()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2234 | for (const auto& transceiver : transceivers_) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2235 | const ContentInfo* content = |
| 2236 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2237 | if (!content) { |
| 2238 | continue; |
| 2239 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2240 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2241 | if (content->rejected || !channel || channel->local_streams().empty()) { |
Steve Anton | 60b6c1d | 2018-06-13 11:32:27 -0700 | [diff] [blame] | 2242 | // 0 is a special value meaning "this sender has no associated send |
| 2243 | // stream". Need to call this so the sender won't attempt to configure |
| 2244 | // a no longer existing stream and run into DCHECKs in the lower |
| 2245 | // layers. |
| 2246 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2247 | } else { |
| 2248 | // Get the StreamParams from the channel which could generate SSRCs. |
| 2249 | const std::vector<StreamParams>& streams = channel->local_streams(); |
| 2250 | transceiver->internal()->sender_internal()->set_stream_ids( |
| 2251 | streams[0].stream_ids()); |
| 2252 | transceiver->internal()->sender_internal()->SetSsrc( |
| 2253 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2254 | } |
| 2255 | } |
| 2256 | } else { |
| 2257 | // Plan B semantics. |
| 2258 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2259 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2260 | // local session description. |
| 2261 | const cricket::ContentInfo* audio_content = |
| 2262 | GetFirstAudioContent(local_description()->description()); |
| 2263 | if (audio_content) { |
| 2264 | if (audio_content->rejected) { |
| 2265 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2266 | } else { |
| 2267 | const cricket::AudioContentDescription* audio_desc = |
| 2268 | audio_content->media_description()->as_audio(); |
| 2269 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2270 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2271 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2272 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2273 | const cricket::ContentInfo* video_content = |
| 2274 | GetFirstVideoContent(local_description()->description()); |
| 2275 | if (video_content) { |
| 2276 | if (video_content->rejected) { |
| 2277 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2278 | } else { |
| 2279 | const cricket::VideoContentDescription* video_desc = |
| 2280 | video_content->media_description()->as_video(); |
| 2281 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2282 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2283 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2287 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2288 | if (data_content) { |
| 2289 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2290 | data_content->media_description()->as_data(); |
Steve Anton | 68586e8 | 2018-12-13 17:41:25 -0800 | [diff] [blame] | 2291 | if (absl::StartsWith(data_desc->protocol(), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2292 | cricket::kMediaProtocolRtpPrefix)) { |
| 2293 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 2294 | } |
| 2295 | } |
| 2296 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2297 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2300 | // The SDP parser used to populate these values by default for the 'content |
| 2301 | // name' if an a=mid line was absent. |
| 2302 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 2303 | switch (media_type) { |
| 2304 | case cricket::MEDIA_TYPE_AUDIO: |
| 2305 | return cricket::CN_AUDIO; |
| 2306 | case cricket::MEDIA_TYPE_VIDEO: |
| 2307 | return cricket::CN_VIDEO; |
| 2308 | case cricket::MEDIA_TYPE_DATA: |
| 2309 | return cricket::CN_DATA; |
| 2310 | } |
| 2311 | RTC_NOTREACHED(); |
| 2312 | return ""; |
| 2313 | } |
| 2314 | |
| 2315 | void PeerConnection::FillInMissingRemoteMids( |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2316 | cricket::SessionDescription* new_remote_description) { |
| 2317 | RTC_DCHECK(new_remote_description); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2318 | const cricket::ContentInfos& local_contents = |
| 2319 | (local_description() ? local_description()->description()->contents() |
| 2320 | : cricket::ContentInfos()); |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2321 | const cricket::ContentInfos& remote_contents = |
| 2322 | (remote_description() ? remote_description()->description()->contents() |
| 2323 | : cricket::ContentInfos()); |
| 2324 | for (size_t i = 0; i < new_remote_description->contents().size(); ++i) { |
| 2325 | cricket::ContentInfo& content = new_remote_description->contents()[i]; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2326 | if (!content.name.empty()) { |
| 2327 | continue; |
| 2328 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2329 | std::string new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2330 | absl::string_view source_explanation; |
| 2331 | if (IsUnifiedPlan()) { |
| 2332 | if (i < local_contents.size()) { |
| 2333 | new_mid = local_contents[i].name; |
| 2334 | source_explanation = "from the matching local media section"; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2335 | } else if (i < remote_contents.size()) { |
| 2336 | new_mid = remote_contents[i].name; |
| 2337 | source_explanation = "from the matching previous remote media section"; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2338 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2339 | new_mid = mid_generator_(); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2340 | source_explanation = "generated just now"; |
| 2341 | } |
| 2342 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2343 | new_mid = std::string( |
| 2344 | GetDefaultMidForPlanB(content.media_description()->type())); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2345 | source_explanation = "to match pre-existing behavior"; |
| 2346 | } |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2347 | RTC_DCHECK(!new_mid.empty()); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2348 | content.name = new_mid; |
Steve Anton | d7180cc | 2019-02-07 10:44:53 -0800 | [diff] [blame] | 2349 | new_remote_description->transport_infos()[i].content_name = new_mid; |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2350 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 2351 | << " is missing an a=mid line. Filling in the value '" |
| 2352 | << new_mid << "' " << source_explanation << "."; |
| 2353 | } |
| 2354 | } |
| 2355 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2356 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 2357 | SetSessionDescriptionObserver* observer, |
| 2358 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2359 | SetRemoteDescription( |
| 2360 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2361 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2362 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2363 | } |
| 2364 | |
| 2365 | void PeerConnection::SetRemoteDescription( |
| 2366 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2367 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2368 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2369 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2370 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2371 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2372 | return; |
| 2373 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2374 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2375 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2376 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2377 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2378 | return; |
| 2379 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2380 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2381 | // If a session error has occurred the PeerConnection is in a possibly |
| 2382 | // inconsistent state so fail right away. |
| 2383 | if (session_error() != SessionError::kNone) { |
| 2384 | std::string error_message = GetSessionErrorMsg(); |
| 2385 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2386 | observer->OnSetRemoteDescriptionComplete( |
| 2387 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2388 | return; |
| 2389 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2390 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 2391 | if (desc->GetType() == SdpType::kOffer) { |
| 2392 | // Report to UMA the format of the received offer. |
| 2393 | ReportSdpFormatReceived(*desc); |
| 2394 | } |
| 2395 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2396 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 2397 | // points. |
| 2398 | FillInMissingRemoteMids(desc->description()); |
| 2399 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2400 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2401 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2402 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2403 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2404 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2405 | observer->OnSetRemoteDescriptionComplete( |
| 2406 | RTCError(error.type(), std::move(error_message))); |
| 2407 | return; |
| 2408 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2409 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2410 | // Grab the description type before moving ownership to |
| 2411 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2412 | const SdpType type = desc->GetType(); |
| 2413 | |
| 2414 | error = ApplyRemoteDescription(std::move(desc)); |
| 2415 | // |desc| may be destroyed at this point. |
| 2416 | |
| 2417 | if (!error.ok()) { |
| 2418 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2419 | // inconsistent state, so act conservatively here and set the session error |
| 2420 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2421 | SetSessionError(SessionError::kContent, error.message()); |
| 2422 | std::string error_message = |
| 2423 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2424 | RTC_LOG(LS_ERROR) << error_message; |
| 2425 | observer->OnSetRemoteDescriptionComplete( |
| 2426 | RTCError(error.type(), std::move(error_message))); |
| 2427 | return; |
| 2428 | } |
| 2429 | RTC_DCHECK(remote_description()); |
| 2430 | |
| 2431 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2432 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2433 | // MaybeStartGathering... |
| 2434 | network_thread()->Invoke<void>( |
| 2435 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2436 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2437 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 2438 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 2442 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2446 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2447 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2448 | RTC_DCHECK(desc); |
| 2449 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2450 | // Update stats here so that we have the most recent stats for tracks and |
| 2451 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2452 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2453 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2454 | // Take a reference to the old remote description since it's used below to |
| 2455 | // compare against the new remote description. When setting the new remote |
| 2456 | // description, grab ownership of the replaced session description in case it |
| 2457 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2458 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2459 | const SessionDescriptionInterface* old_remote_description = |
| 2460 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2461 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2462 | SdpType type = desc->GetType(); |
| 2463 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2464 | replaced_remote_description = pending_remote_description_ |
| 2465 | ? std::move(pending_remote_description_) |
| 2466 | : std::move(current_remote_description_); |
| 2467 | current_remote_description_ = std::move(desc); |
| 2468 | pending_remote_description_ = nullptr; |
| 2469 | current_local_description_ = std::move(pending_local_description_); |
| 2470 | } else { |
| 2471 | replaced_remote_description = std::move(pending_remote_description_); |
| 2472 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2473 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2474 | // The session description to apply now must be accessed by |
| 2475 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2476 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2477 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2478 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2479 | if (!error.ok()) { |
| 2480 | return error; |
| 2481 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2482 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2483 | if (IsUnifiedPlan()) { |
| 2484 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2485 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2486 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2487 | if (!error.ok()) { |
| 2488 | return error; |
| 2489 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2490 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2491 | // Media channels will be created only when offer is set. These may use new |
| 2492 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2493 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2494 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2495 | // description is applied. Restore back to old description. |
| 2496 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2497 | if (!error.ok()) { |
| 2498 | return error; |
| 2499 | } |
| 2500 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2501 | // Remove unused channels if MediaContentDescription is rejected. |
| 2502 | RemoveUnusedChannels(remote_description()->description()); |
| 2503 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2504 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2505 | // NOTE: Candidates allocation will be initiated only when |
| 2506 | // SetLocalDescription is called. |
| 2507 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2508 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2509 | if (!error.ok()) { |
| 2510 | return error; |
| 2511 | } |
| 2512 | |
| 2513 | if (local_description() && |
| 2514 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2515 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2516 | } |
| 2517 | |
| 2518 | if (old_remote_description) { |
| 2519 | for (const cricket::ContentInfo& content : |
| 2520 | old_remote_description->description()->contents()) { |
| 2521 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2522 | // password that indicates the remote peer requests an ICE restart. |
| 2523 | // TODO(deadbeef): When we start storing both the current and pending |
| 2524 | // remote description, this should reset pending_ice_restarts and compare |
| 2525 | // against the current description. |
| 2526 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2527 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2528 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2529 | pending_ice_restarts_.insert(content.name); |
| 2530 | } |
| 2531 | } else { |
| 2532 | // We retain all received candidates only if ICE is not restarted. |
| 2533 | // When ICE is restarted, all previous candidates belong to an old |
| 2534 | // generation and should not be kept. |
| 2535 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2536 | // description should only contain candidates from the last set remote |
| 2537 | // description plus any candidates added since then. We should remove |
| 2538 | // this once we're sure it won't break anything. |
| 2539 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2540 | old_remote_description, content.name, mutable_remote_description()); |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | if (session_error() != SessionError::kNone) { |
| 2546 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2547 | } |
| 2548 | |
| 2549 | // Set the the ICE connection state to connecting since the connection may |
| 2550 | // become writable with peer reflexive candidates before any remote candidate |
| 2551 | // is signaled. |
| 2552 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2553 | // is to have a new signal the indicates a change in checking state from the |
| 2554 | // transport and expose a new checking() member from transport that can be |
| 2555 | // read to determine the current checking state. The existing SignalConnecting |
| 2556 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2557 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 14:32:17 -0700 | [diff] [blame] | 2558 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2559 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2560 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2561 | } |
| 2562 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2563 | // If setting the description decided our SSL role, allocate any necessary |
| 2564 | // SCTP sids. |
| 2565 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2566 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2567 | AllocateSctpSids(role); |
| 2568 | } |
| 2569 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2570 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2571 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2572 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2573 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2574 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2575 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2576 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2577 | const ContentInfo* content = |
| 2578 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2579 | if (!content) { |
| 2580 | continue; |
| 2581 | } |
| 2582 | const MediaContentDescription* media_desc = content->media_description(); |
| 2583 | RtpTransceiverDirection local_direction = |
| 2584 | RtpTransceiverDirectionReversed(media_desc->direction()); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2585 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 2586 | // RTCSessionDescription: Set the associated remote streams given |
| 2587 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 2588 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 2589 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2590 | std::vector<std::string> stream_ids; |
| 2591 | if (!media_desc->streams().empty()) { |
| 2592 | // The remote description has signaled the stream IDs. |
| 2593 | stream_ids = media_desc->streams()[0].stream_ids(); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2594 | } |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2595 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 2596 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 2597 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 2598 | stream_ids, &added_streams, |
| 2599 | &removed_streams); |
| 2600 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 2601 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 2602 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 2603 | // process the addition of a remote track for the media description. |
| 2604 | if (!transceiver->fired_direction() || |
| 2605 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 2606 | RTC_LOG(LS_INFO) |
| 2607 | << "Processing the addition of a remote track for MID=" |
| 2608 | << content->name << "."; |
| 2609 | now_receiving_transceivers.push_back(transceiver); |
Henrik Boström | 5b14778 | 2018-12-04 11:25:05 +0100 | [diff] [blame] | 2610 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2611 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2612 | // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2613 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 2614 | // removal of a remote track for the media description, given transceiver, |
| 2615 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2616 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2617 | (transceiver->fired_direction() && |
| 2618 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 2619 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2620 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2621 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2622 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2623 | transceiver->internal()->set_fired_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2624 | // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2625 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2626 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2627 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2628 | // direction. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2629 | transceiver->internal()->set_current_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2630 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 2631 | if (transceiver->mid()) { |
| 2632 | auto dtls_transport = |
| 2633 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2634 | transceiver->internal()->sender_internal()->set_transport( |
| 2635 | dtls_transport); |
| 2636 | transceiver->internal()->receiver_internal()->set_transport( |
| 2637 | dtls_transport); |
| 2638 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2639 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 2640 | // 2.2.8.1.12: If the media description is rejected, and transceiver is |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2641 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2642 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2643 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2644 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2645 | transceiver->Stop(); |
| 2646 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2647 | if (!content->rejected && |
| 2648 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2649 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2650 | uint32_t ssrc = 0; |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 2651 | if (!media_desc->streams().empty() && |
| 2652 | media_desc->streams()[0].has_ssrcs()) { |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame] | 2653 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2654 | } |
| 2655 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2656 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2657 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2658 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2659 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2660 | for (const auto& transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 12:59:16 -0800 | [diff] [blame] | 2661 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2662 | observer->OnTrack(transceiver); |
| 2663 | observer->OnAddTrack(transceiver->receiver(), |
| 2664 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2665 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2666 | for (const auto& stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2667 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2668 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2669 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2670 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2671 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2672 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2673 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 15:30:18 -0700 | [diff] [blame] | 2674 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2675 | } |
| 2676 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2677 | const cricket::ContentInfo* audio_content = |
| 2678 | GetFirstAudioContent(remote_description()->description()); |
| 2679 | const cricket::ContentInfo* video_content = |
| 2680 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2681 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2682 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2683 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2684 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2685 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2686 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2687 | |
| 2688 | // Check if the descriptions include streams, just in case the peer supports |
| 2689 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2690 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2691 | (audio_desc && !audio_desc->streams().empty()) || |
| 2692 | (video_desc && !video_desc->streams().empty())) { |
| 2693 | remote_peer_supports_msid_ = true; |
| 2694 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2695 | |
| 2696 | // We wait to signal new streams until we finish processing the description, |
| 2697 | // since only at that point will new streams have all their tracks. |
| 2698 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2699 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2700 | if (!IsUnifiedPlan()) { |
| 2701 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2702 | // rejected media section, there is some cleanup logic that expects the |
| 2703 | // voice/ video channel to still be set. But in this method the voice/video |
| 2704 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2705 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2706 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2707 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2708 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2709 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2710 | // and MediaStreams. |
| 2711 | if (audio_content) { |
| 2712 | if (audio_content->rejected) { |
| 2713 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2714 | } else { |
| 2715 | bool default_audio_track_needed = |
| 2716 | !remote_peer_supports_msid_ && |
| 2717 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2718 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2719 | default_audio_track_needed, audio_desc->type(), |
| 2720 | new_streams); |
| 2721 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2722 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2723 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2724 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2725 | // and MediaStreams. |
| 2726 | if (video_content) { |
| 2727 | if (video_content->rejected) { |
| 2728 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2729 | } else { |
| 2730 | bool default_video_track_needed = |
| 2731 | !remote_peer_supports_msid_ && |
| 2732 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2733 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2734 | default_video_track_needed, video_desc->type(), |
| 2735 | new_streams); |
| 2736 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2737 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2738 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2739 | // Update the DataChannels with the information from the remote peer. |
| 2740 | if (data_desc) { |
Steve Anton | 68586e8 | 2018-12-13 17:41:25 -0800 | [diff] [blame] | 2741 | if (absl::StartsWith(data_desc->protocol(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2742 | cricket::kMediaProtocolRtpPrefix)) { |
| 2743 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2744 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2745 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2746 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2747 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2748 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2749 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2750 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2751 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 2752 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2753 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2754 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2755 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2756 | UpdateEndedRemoteMediaStreams(); |
| 2757 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2758 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2759 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2762 | void PeerConnection::SetAssociatedRemoteStreams( |
| 2763 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 2764 | const std::vector<std::string>& stream_ids, |
| 2765 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 2766 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2767 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 2768 | for (const std::string& stream_id : stream_ids) { |
| 2769 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2770 | remote_streams_->find(stream_id); |
| 2771 | if (!stream) { |
| 2772 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2773 | MediaStream::Create(stream_id)); |
| 2774 | remote_streams_->AddStream(stream); |
| 2775 | added_streams->push_back(stream); |
| 2776 | } |
| 2777 | media_streams.push_back(stream); |
| 2778 | } |
| 2779 | // Special case: "a=msid" missing, use random stream ID. |
| 2780 | if (media_streams.empty() && |
| 2781 | !(remote_description()->description()->msid_signaling() & |
| 2782 | cricket::kMsidSignalingMediaSection)) { |
| 2783 | if (!missing_msid_default_stream_) { |
| 2784 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 2785 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 2786 | added_streams->push_back(missing_msid_default_stream_); |
| 2787 | } |
| 2788 | media_streams.push_back(missing_msid_default_stream_); |
| 2789 | } |
| 2790 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 2791 | receiver->streams(); |
| 2792 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 2793 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 2794 | // update the stream-track relationships in a later step. We do this earlier, |
| 2795 | // changing the order of things, but the end-result is the same. |
| 2796 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 2797 | // instead. https://crbug.com/webrtc/9480 |
| 2798 | receiver->SetStreams(media_streams); |
| 2799 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 2800 | } |
| 2801 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2802 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 2803 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2804 | transceiver, |
| 2805 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 2806 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2807 | RTC_DCHECK(transceiver->mid()); |
| 2808 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 2809 | << *transceiver->mid(); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2810 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2811 | transceiver->internal()->receiver_internal()->streams(); |
| 2812 | // This will remove the remote track from the streams. |
| 2813 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 2814 | remove_list->push_back(transceiver); |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2815 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 2816 | } |
| 2817 | |
| 2818 | void PeerConnection::RemoveRemoteStreamsIfEmpty( |
| 2819 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 2820 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2821 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 2822 | // streams, see if the stream was removed by checking if this was the last |
| 2823 | // receiver with that stream ID. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 2824 | for (const auto& remote_stream : remote_streams) { |
Henrik Boström | afa07dd | 2018-12-20 11:06:02 +0100 | [diff] [blame] | 2825 | if (remote_stream->GetAudioTracks().empty() && |
| 2826 | remote_stream->GetVideoTracks().empty()) { |
| 2827 | remote_streams_->RemoveStream(remote_stream); |
| 2828 | removed_streams->push_back(remote_stream); |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2833 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2834 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2835 | const SessionDescriptionInterface& new_session, |
| 2836 | const SessionDescriptionInterface* old_local_description, |
| 2837 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2838 | RTC_DCHECK(IsUnifiedPlan()); |
| 2839 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2840 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2841 | if (new_session.GetType() == SdpType::kOffer) { |
| 2842 | auto bundle_group_or_error = |
| 2843 | GetEarlyBundleGroup(*new_session.description()); |
| 2844 | if (!bundle_group_or_error.ok()) { |
| 2845 | return bundle_group_or_error.MoveError(); |
| 2846 | } |
| 2847 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2848 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2849 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2850 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2851 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2852 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2853 | cricket::MediaType media_type = new_content.media_description()->type(); |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 2854 | mid_generator_.AddKnownId(new_content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2855 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2856 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2857 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2858 | if (old_local_description && |
| 2859 | i < old_local_description->description()->contents().size()) { |
| 2860 | old_local_content = |
| 2861 | &old_local_description->description()->contents()[i]; |
| 2862 | } |
| 2863 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2864 | if (old_remote_description && |
| 2865 | i < old_remote_description->description()->contents().size()) { |
| 2866 | old_remote_content = |
| 2867 | &old_remote_description->description()->contents()[i]; |
| 2868 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2869 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2870 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2871 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2872 | if (!transceiver_or_error.ok()) { |
| 2873 | return transceiver_or_error.MoveError(); |
| 2874 | } |
| 2875 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2876 | RTCError error = |
| 2877 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2878 | if (!error.ok()) { |
| 2879 | return error; |
| 2880 | } |
| 2881 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2882 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2883 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 2884 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 2885 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2886 | continue; |
| 2887 | } |
| 2888 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2889 | if (!error.ok()) { |
| 2890 | return error; |
| 2891 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2892 | } else { |
| 2893 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2894 | "Unknown section type."); |
| 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | return RTCError::OK(); |
| 2899 | } |
| 2900 | |
| 2901 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2902 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2903 | transceiver, |
| 2904 | const cricket::ContentInfo& content, |
| 2905 | const cricket::ContentGroup* bundle_group) { |
| 2906 | RTC_DCHECK(IsUnifiedPlan()); |
| 2907 | RTC_DCHECK(transceiver); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 2908 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2909 | if (content.rejected) { |
| 2910 | if (channel) { |
| 2911 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 2912 | DestroyChannelInterface(channel); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2913 | } |
| 2914 | } else { |
| 2915 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2916 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2917 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2918 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2919 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2920 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2921 | } |
| 2922 | if (!channel) { |
| 2923 | LOG_AND_RETURN_ERROR( |
| 2924 | RTCErrorType::INTERNAL_ERROR, |
| 2925 | "Failed to create channel for mid=" + content.name); |
| 2926 | } |
| 2927 | transceiver->internal()->SetChannel(channel); |
| 2928 | } |
| 2929 | } |
| 2930 | return RTCError::OK(); |
| 2931 | } |
| 2932 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2933 | RTCError PeerConnection::UpdateDataChannel( |
| 2934 | cricket::ContentSource source, |
| 2935 | const cricket::ContentInfo& content, |
| 2936 | const cricket::ContentGroup* bundle_group) { |
| 2937 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2938 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2939 | // will take care of rejecting it. |
| 2940 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2941 | } |
| 2942 | if (content.rejected) { |
| 2943 | DestroyDataChannel(); |
| 2944 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 2945 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 2946 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2947 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2948 | "Failed to create data channel."); |
| 2949 | } |
| 2950 | } |
| 2951 | if (source == cricket::CS_REMOTE) { |
| 2952 | const MediaContentDescription* data_desc = content.media_description(); |
| 2953 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2954 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2955 | } |
| 2956 | } |
| 2957 | } |
| 2958 | return RTCError::OK(); |
| 2959 | } |
| 2960 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2961 | // This method will extract any send encodings that were sent by the remote |
| 2962 | // connection. This is currently only relevant for Simulcast scenario (where |
| 2963 | // the number of layers may be communicated by the server). |
| 2964 | static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription( |
| 2965 | const MediaContentDescription& desc) { |
| 2966 | if (!desc.HasSimulcast()) { |
| 2967 | return {}; |
| 2968 | } |
| 2969 | std::vector<RtpEncodingParameters> result; |
| 2970 | const SimulcastDescription& simulcast = desc.simulcast_description(); |
| 2971 | |
| 2972 | // This is a remote description, the parameters we are after should appear |
| 2973 | // as receive streams. |
| 2974 | for (const auto& alternatives : simulcast.receive_layers()) { |
| 2975 | RTC_DCHECK(!alternatives.empty()); |
| 2976 | // There is currently no way to specify or choose from alternatives. |
| 2977 | // We will always use the first alternative, which is the most preferred. |
| 2978 | const SimulcastLayer& layer = alternatives[0]; |
| 2979 | RtpEncodingParameters parameters; |
| 2980 | parameters.rid = layer.rid; |
| 2981 | parameters.active = !layer.is_paused; |
| 2982 | result.push_back(parameters); |
| 2983 | } |
| 2984 | |
| 2985 | return result; |
| 2986 | } |
| 2987 | |
| 2988 | static RTCError UpdateSimulcastLayerStatusInSender( |
| 2989 | const std::vector<SimulcastLayer>& layers, |
| 2990 | RtpSenderInterface* sender) { |
| 2991 | RTC_DCHECK(sender); |
| 2992 | RtpParameters parameters = sender->GetParameters(); |
| 2993 | |
| 2994 | // The simulcast envelope cannot be changed, only the status of the streams. |
| 2995 | // So we will iterate over the send encodings rather than the layers. |
| 2996 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 2997 | auto iter = std::find_if(layers.begin(), layers.end(), |
| 2998 | [&encoding](const SimulcastLayer& layer) { |
| 2999 | return layer.rid == encoding.rid; |
| 3000 | }); |
| 3001 | // A layer that cannot be found may have been removed by the remote party. |
| 3002 | encoding.active = iter != layers.end() && !iter->is_paused; |
| 3003 | } |
| 3004 | |
| 3005 | return sender->SetParameters(parameters); |
| 3006 | } |
| 3007 | |
| 3008 | static RTCError DisableSimulcastInSender(RtpSenderInterface* sender) { |
| 3009 | RTC_DCHECK(sender); |
| 3010 | RtpParameters parameters = sender->GetParameters(); |
| 3011 | if (parameters.encodings.empty()) { |
| 3012 | return RTCError::OK(); |
| 3013 | } |
| 3014 | |
| 3015 | bool first_layer_status = parameters.encodings[0].active; |
| 3016 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 3017 | // TODO(bugs.webrtc.org/10251): Active does not really disable the layer. |
| 3018 | // The user might enable it at a later point in time even though it was |
| 3019 | // rejected. |
| 3020 | encoding.active = false; |
| 3021 | } |
| 3022 | parameters.encodings[0].active = first_layer_status; |
| 3023 | |
| 3024 | return sender->SetParameters(parameters); |
| 3025 | } |
| 3026 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3027 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 3028 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3029 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3030 | size_t mline_index, |
| 3031 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3032 | const ContentInfo* old_local_content, |
| 3033 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3034 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3035 | // If this is an offer then the m= section might be recycled. If the m= |
| 3036 | // section is being recycled (defined as: rejected in the current local or |
| 3037 | // remote description and not rejected in new description), dissociate the |
| 3038 | // currently associated RtpTransceiver by setting its mid property to null, |
| 3039 | // and discard the mapping between the transceiver and its m= section index. |
| 3040 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 3041 | old_remote_content)) { |
| 3042 | // We want to dissociate the transceiver that has the rejected mid. |
| 3043 | const std::string& old_mid = |
| 3044 | (old_local_content && old_local_content->rejected) |
| 3045 | ? old_local_content->name |
| 3046 | : old_remote_content->name; |
| 3047 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3048 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3049 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 3050 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 3051 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 3052 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3053 | } |
| 3054 | } |
| 3055 | const MediaContentDescription* media_desc = content.media_description(); |
| 3056 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3057 | if (source == cricket::CS_LOCAL) { |
| 3058 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 3059 | // mapping between transceivers and m= section indices established when |
| 3060 | // creating the offer. |
| 3061 | if (!transceiver) { |
| 3062 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 3063 | } |
| 3064 | if (!transceiver) { |
| 3065 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3066 | "Unknown transceiver"); |
| 3067 | } |
| 3068 | } else { |
| 3069 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 3070 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 3071 | // of the same type... |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame^] | 3072 | // When simulcast is requested, a transceiver cannot be associated because |
| 3073 | // AddTrack cannot be called to initialize it. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3074 | if (!transceiver && |
Amit Hilbuch | aa58415 | 2019-02-06 17:09:52 -0800 | [diff] [blame^] | 3075 | RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 3076 | !media_desc->HasSimulcast()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3077 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 3078 | } |
| 3079 | // If no RtpTransceiver was found in the previous step, create one with a |
| 3080 | // recvonly direction. |
| 3081 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 3082 | RTC_LOG(LS_INFO) << "Adding " |
| 3083 | << cricket::MediaTypeToString(media_desc->type()) |
| 3084 | << " transceiver for MID=" << content.name |
| 3085 | << " at i=" << mline_index |
| 3086 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3087 | std::string sender_id = rtc::CreateRandomUuid(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3088 | std::vector<RtpEncodingParameters> send_encodings = |
| 3089 | GetSendEncodingsFromRemoteDescription(*media_desc); |
| 3090 | auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {}, |
| 3091 | send_encodings); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3092 | std::string receiver_id; |
| 3093 | if (!media_desc->streams().empty()) { |
| 3094 | receiver_id = media_desc->streams()[0].id; |
| 3095 | } else { |
| 3096 | receiver_id = rtc::CreateRandomUuid(); |
| 3097 | } |
| 3098 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3099 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3100 | transceiver->internal()->set_direction( |
| 3101 | RtpTransceiverDirection::kRecvOnly); |
| 3102 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3103 | |
| 3104 | // Check if the offer indicated simulcast but the answer rejected it. |
| 3105 | // This can happen when simulcast is not supported on the remote party. |
| 3106 | // This check can be simplified to comparing the number of send encodings, |
| 3107 | // but that might break legacy implementation in which simulcast is not |
| 3108 | // signaled in the remote description. |
| 3109 | if (old_local_content && old_local_content->media_description() && |
| 3110 | old_local_content->media_description()->HasSimulcast() && |
| 3111 | !media_desc->HasSimulcast()) { |
| 3112 | RTCError error = |
| 3113 | DisableSimulcastInSender(transceiver->internal()->sender()); |
| 3114 | if (!error.ok()) { |
| 3115 | RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast."; |
| 3116 | return std::move(error); |
| 3117 | } |
| 3118 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3119 | } |
| 3120 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3121 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3122 | LOG_AND_RETURN_ERROR( |
| 3123 | RTCErrorType::INVALID_PARAMETER, |
| 3124 | "Transceiver type does not match media description type."); |
| 3125 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 3126 | if (media_desc->HasSimulcast()) { |
| 3127 | std::vector<SimulcastLayer> layers = |
| 3128 | source == cricket::CS_LOCAL |
| 3129 | ? media_desc->simulcast_description().send_layers().GetAllLayers() |
| 3130 | : media_desc->simulcast_description() |
| 3131 | .receive_layers() |
| 3132 | .GetAllLayers(); |
| 3133 | RTCError error = UpdateSimulcastLayerStatusInSender( |
| 3134 | layers, transceiver->internal()->sender()); |
| 3135 | if (!error.ok()) { |
| 3136 | RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers."; |
| 3137 | return std::move(error); |
| 3138 | } |
| 3139 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3140 | // Associate the found or created RtpTransceiver with the m= section by |
| 3141 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 3142 | // section, and establish a mapping between the transceiver and the index of |
| 3143 | // the m= section. |
| 3144 | transceiver->internal()->set_mid(content.name); |
| 3145 | transceiver->internal()->set_mline_index(mline_index); |
| 3146 | return std::move(transceiver); |
| 3147 | } |
| 3148 | |
| 3149 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3150 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 3151 | RTC_DCHECK(IsUnifiedPlan()); |
| 3152 | for (auto transceiver : transceivers_) { |
| 3153 | if (transceiver->mid() == mid) { |
| 3154 | return transceiver; |
| 3155 | } |
| 3156 | } |
| 3157 | return nullptr; |
| 3158 | } |
| 3159 | |
| 3160 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3161 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 3162 | RTC_DCHECK(IsUnifiedPlan()); |
| 3163 | for (auto transceiver : transceivers_) { |
| 3164 | if (transceiver->internal()->mline_index() == mline_index) { |
| 3165 | return transceiver; |
| 3166 | } |
| 3167 | } |
| 3168 | return nullptr; |
| 3169 | } |
| 3170 | |
| 3171 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3172 | PeerConnection::FindAvailableTransceiverToReceive( |
| 3173 | cricket::MediaType media_type) const { |
| 3174 | RTC_DCHECK(IsUnifiedPlan()); |
| 3175 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3176 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3177 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3178 | // associated with any m= section and are not stopped, find the first such |
| 3179 | // RtpTransceiver. |
| 3180 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3181 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3182 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3183 | !transceiver->stopped()) { |
| 3184 | return transceiver; |
| 3185 | } |
| 3186 | } |
| 3187 | return nullptr; |
| 3188 | } |
| 3189 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3190 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 3191 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3192 | transceiver, |
| 3193 | const SessionDescriptionInterface* sdesc) const { |
| 3194 | RTC_DCHECK(transceiver); |
| 3195 | RTC_DCHECK(sdesc); |
| 3196 | if (IsUnifiedPlan()) { |
| 3197 | if (!transceiver->internal()->mid()) { |
| 3198 | // This transceiver is not associated with a media section yet. |
| 3199 | return nullptr; |
| 3200 | } |
| 3201 | return sdesc->description()->GetContentByName( |
| 3202 | *transceiver->internal()->mid()); |
| 3203 | } else { |
| 3204 | // Plan B only allows at most one audio and one video section, so use the |
| 3205 | // first media section of that type. |
| 3206 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3207 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 3211 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 3212 | return configuration_; |
| 3213 | } |
| 3214 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3215 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 3216 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3217 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3218 | if (IsClosed()) { |
| 3219 | RTC_LOG(LS_ERROR) << "SetConfiguration: PeerConnection is closed."; |
| 3220 | return SafeSetError(RTCErrorType::INVALID_STATE, error); |
| 3221 | } |
| 3222 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 3223 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 3224 | // size is not allowed, and changing the set of ICE servers will not result |
| 3225 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3226 | if (local_description() && configuration.ice_candidate_pool_size != |
| 3227 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3228 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 3229 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3230 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3231 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3232 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3233 | if (local_description() && |
| 3234 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 3235 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 3236 | "SetLocalDescription."; |
| 3237 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3238 | } |
| 3239 | |
| 3240 | if (remote_description() && |
| 3241 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 3242 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 3243 | "SetRemoteDescription."; |
| 3244 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3245 | } |
| 3246 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3247 | if (local_description() && |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3248 | configuration.use_media_transport_for_data_channels != |
| 3249 | configuration_.use_media_transport_for_data_channels) { |
| 3250 | RTC_LOG(LS_ERROR) << "Can't change media_transport_for_data_channels " |
| 3251 | "after calling SetLocalDescription."; |
| 3252 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3253 | } |
| 3254 | |
| 3255 | if (remote_description() && |
| 3256 | configuration.use_media_transport_for_data_channels != |
| 3257 | configuration_.use_media_transport_for_data_channels) { |
| 3258 | RTC_LOG(LS_ERROR) << "Can't change media_transport_for_data_channels " |
| 3259 | "after calling SetRemoteDescription."; |
| 3260 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3261 | } |
| 3262 | |
| 3263 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 3264 | configuration.crypto_options != configuration_.crypto_options) { |
| 3265 | RTC_LOG(LS_ERROR) << "Can't change crypto_options after calling " |
| 3266 | "SetLocalDescription."; |
| 3267 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3268 | } |
| 3269 | |
Piotr (Peter) Slatala | 37227be | 2018-11-21 07:42:22 -0800 | [diff] [blame] | 3270 | if (configuration.use_media_transport_for_data_channels || |
| 3271 | configuration.use_media_transport) { |
| 3272 | RTC_CHECK(configuration.bundle_policy == kBundlePolicyMaxBundle) |
| 3273 | << "Media transport requires MaxBundle policy."; |
| 3274 | } |
| 3275 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3276 | // The simplest (and most future-compatible) way to tell if the config was |
| 3277 | // modified in an invalid way is to copy each property we do support |
| 3278 | // modifying, then use operator==. There are far more properties we don't |
| 3279 | // support modifying than those we do, and more could be added. |
| 3280 | RTCConfiguration modified_config = configuration_; |
| 3281 | modified_config.servers = configuration.servers; |
| 3282 | modified_config.type = configuration.type; |
| 3283 | modified_config.ice_candidate_pool_size = |
| 3284 | configuration.ice_candidate_pool_size; |
| 3285 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3286 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 3287 | modified_config.ice_check_interval_strong_connectivity = |
| 3288 | configuration.ice_check_interval_strong_connectivity; |
| 3289 | modified_config.ice_check_interval_weak_connectivity = |
| 3290 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 3291 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 3292 | modified_config.ice_unwritable_min_checks = |
| 3293 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 3294 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3295 | modified_config.stun_candidate_keepalive_interval = |
| 3296 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3297 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 3298 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3299 | modified_config.active_reset_srtp_params = |
| 3300 | configuration.active_reset_srtp_params; |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 10:04:45 -0700 | [diff] [blame] | 3301 | modified_config.use_media_transport = configuration.use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 3302 | modified_config.use_media_transport_for_data_channels = |
| 3303 | configuration.use_media_transport_for_data_channels; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3304 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3305 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3306 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3307 | } |
| 3308 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 3309 | // Validate the modified configuration. |
| 3310 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 3311 | if (!validate_error.ok()) { |
| 3312 | return SafeSetError(std::move(validate_error), error); |
| 3313 | } |
| 3314 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3315 | // Note that this isn't possible through chromium, since it's an unsigned |
| 3316 | // short in WebIDL. |
| 3317 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 03:34:17 -0700 | [diff] [blame] | 3318 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3319 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 3320 | } |
| 3321 | |
| 3322 | // Parse ICE servers before hopping to network thread. |
| 3323 | cricket::ServerAddresses stun_servers; |
| 3324 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 3325 | RTCErrorType parse_error = |
| 3326 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 3327 | if (parse_error != RTCErrorType::NONE) { |
| 3328 | return SafeSetError(parse_error, error); |
| 3329 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3330 | // Note if STUN or TURN servers were supplied. |
| 3331 | if (!stun_servers.empty()) { |
| 3332 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 3333 | } |
| 3334 | if (!turn_servers.empty()) { |
| 3335 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 3336 | } |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3337 | |
| 3338 | // In theory this shouldn't fail. |
| 3339 | if (!network_thread()->Invoke<bool>( |
| 3340 | RTC_FROM_HERE, |
| 3341 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 3342 | stun_servers, turn_servers, modified_config.type, |
| 3343 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 3344 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 3345 | modified_config.turn_customizer, |
| 3346 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3347 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3348 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 3349 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3350 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3351 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 3352 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 3353 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3354 | if (modified_config.servers != configuration_.servers || |
| 3355 | modified_config.type != configuration_.type || |
| 3356 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3357 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 3358 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3359 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 3360 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 3361 | transport_controller_->SetMediaTransportSettings( |
| 3362 | modified_config.use_media_transport, |
| 3363 | modified_config.use_media_transport_for_data_channels); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 3364 | |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 3365 | if (configuration_.active_reset_srtp_params != |
| 3366 | modified_config.active_reset_srtp_params) { |
| 3367 | transport_controller_->SetActiveResetSrtpParams( |
| 3368 | modified_config.active_reset_srtp_params); |
| 3369 | } |
| 3370 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 3371 | configuration_ = modified_config; |
| 3372 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3375 | bool PeerConnection::AddIceCandidate( |
| 3376 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3377 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3378 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3379 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3380 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3381 | return false; |
| 3382 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3383 | |
| 3384 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3385 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3386 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3387 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3388 | return false; |
| 3389 | } |
| 3390 | |
| 3391 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3392 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3393 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3394 | return false; |
| 3395 | } |
| 3396 | |
| 3397 | bool valid = false; |
| 3398 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 3399 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3400 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3401 | return false; |
| 3402 | } |
| 3403 | |
| 3404 | // Add this candidate to the remote session description. |
| 3405 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3406 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3407 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3408 | return false; |
| 3409 | } |
| 3410 | |
| 3411 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3412 | bool result = UseCandidate(ice_candidate); |
| 3413 | if (result) { |
| 3414 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 3415 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 3416 | } else { |
| 3417 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 3418 | } |
| 3419 | return result; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3420 | } else { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3421 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 23:24:36 +0200 | [diff] [blame] | 3422 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3423 | return true; |
| 3424 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3427 | bool PeerConnection::RemoveIceCandidates( |
| 3428 | const std::vector<cricket::Candidate>& candidates) { |
| 3429 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3430 | if (IsClosed()) { |
| 3431 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 3432 | return false; |
| 3433 | } |
| 3434 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3435 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3436 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 3437 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3438 | return false; |
| 3439 | } |
| 3440 | |
| 3441 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3442 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3443 | return false; |
| 3444 | } |
| 3445 | |
| 3446 | size_t number_removed = |
| 3447 | mutable_remote_description()->RemoveCandidates(candidates); |
| 3448 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3449 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3450 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3451 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3452 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3453 | } |
| 3454 | |
| 3455 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 3456 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 3457 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 09:54:10 -0700 | [diff] [blame] | 3458 | RTC_LOG(LS_ERROR) |
| 3459 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 3460 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 3461 | } |
| 3462 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3463 | } |
| 3464 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3465 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3466 | if (!worker_thread()->IsCurrent()) { |
| 3467 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3468 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3469 | } |
| 3470 | |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3471 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 3472 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 3473 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3474 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3475 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3476 | "min_bitrate_bps <= 0"); |
| 3477 | } |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3478 | if (has_start) { |
| 3479 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3480 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3481 | "start_bitrate_bps < min_bitrate_bps"); |
| 3482 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3483 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3484 | "curent_bitrate_bps < 0"); |
| 3485 | } |
| 3486 | } |
| 3487 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3488 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3489 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3490 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3491 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3492 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3493 | "max_bitrate_bps < min_bitrate_bps"); |
| 3494 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3495 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3496 | "max_bitrate_bps < 0"); |
| 3497 | } |
| 3498 | } |
| 3499 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3500 | RTC_DCHECK(call_.get()); |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 3501 | call_->GetTransportControllerSend()->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 3502 | |
| 3503 | return RTCError::OK(); |
| 3504 | } |
| 3505 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3506 | void PeerConnection::SetBitrateAllocationStrategy( |
| 3507 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 3508 | bitrate_allocation_strategy) { |
| 3509 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 3510 | if (!worker_thread->IsCurrent()) { |
| 3511 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 3512 | bitrate_allocation_strategy.release(); |
| 3513 | auto functor = [this, strategy_raw]() { |
| 3514 | call_->SetBitrateAllocationStrategy( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3515 | absl::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 3516 | }; |
| 3517 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 3518 | return; |
| 3519 | } |
| 3520 | RTC_DCHECK(call_.get()); |
| 3521 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 3522 | } |
| 3523 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3524 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3525 | if (!worker_thread()->IsCurrent()) { |
| 3526 | worker_thread()->Invoke<void>( |
| 3527 | RTC_FROM_HERE, |
| 3528 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3529 | return; |
| 3530 | } |
| 3531 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3532 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3533 | audio_state->SetPlayout(playout); |
| 3534 | } |
| 3535 | |
| 3536 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3537 | if (!worker_thread()->IsCurrent()) { |
| 3538 | worker_thread()->Invoke<void>( |
| 3539 | RTC_FROM_HERE, |
| 3540 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3541 | return; |
| 3542 | } |
| 3543 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 3544 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 3545 | audio_state->SetRecording(recording); |
| 3546 | } |
| 3547 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3548 | std::unique_ptr<rtc::SSLCertificate> |
| 3549 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 3550 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3551 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3552 | return nullptr; |
| 3553 | } |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 3554 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 3555 | } |
| 3556 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3557 | std::unique_ptr<rtc::SSLCertChain> |
| 3558 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3559 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3560 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3561 | return nullptr; |
| 3562 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3563 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3564 | audio_transceiver->internal()->channel()->transport_name()); |
| 3565 | } |
| 3566 | |
| 3567 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3568 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3569 | for (auto transceiver : transceivers_) { |
| 3570 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3571 | return transceiver; |
| 3572 | } |
| 3573 | } |
| 3574 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 3575 | } |
| 3576 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3577 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3578 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3579 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3580 | const size_t max_size = (max_size_bytes < 0) |
| 3581 | ? RtcEventLog::kUnlimitedOutput |
| 3582 | : rtc::saturated_cast<size_t>(max_size_bytes); |
Bjorn Terelius | 8b55602 | 2018-11-27 15:43:01 +0100 | [diff] [blame] | 3583 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
| 3584 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
| 3585 | output_period_ms = 5000; |
| 3586 | } |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3587 | return StartRtcEventLog( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 3588 | absl::make_unique<RtcEventLogOutputFile>(file, max_size), |
Bjorn Terelius | 8b55602 | 2018-11-27 15:43:01 +0100 | [diff] [blame] | 3589 | output_period_ms); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3590 | } |
| 3591 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3592 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3593 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3594 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3595 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3596 | bool operator()() { |
| 3597 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3598 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 3599 | PeerConnection* const pc; |
| 3600 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3601 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 3602 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 3603 | return worker_thread()->Invoke<bool>( |
| 3604 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3608 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 3609 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3610 | } |
| 3611 | |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 3612 | rtc::scoped_refptr<DtlsTransportInterface> |
| 3613 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
| 3614 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3615 | } |
| 3616 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 3617 | rtc::scoped_refptr<DtlsTransport> |
| 3618 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
| 3619 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3620 | } |
| 3621 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3622 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3623 | return pending_local_description_ ? pending_local_description_.get() |
| 3624 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3628 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3629 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3632 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3633 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3634 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3638 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3639 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3643 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3644 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3648 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3649 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3650 | } |
| 3651 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3652 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3653 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3654 | // Update stats here so that we have the most recent stats for tracks and |
| 3655 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3656 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3657 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3658 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3659 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3660 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 3661 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3662 | transceiver->Stop(); |
| 3663 | } |
Steve Anton | 25cfeb9 | 2018-04-26 11:44:00 -0700 | [diff] [blame] | 3664 | |
| 3665 | // Ensure that all asynchronous stats requests are completed before destroying |
| 3666 | // the transport controller below. |
| 3667 | if (stats_collector_) { |
| 3668 | stats_collector_->WaitForPendingRequest(); |
| 3669 | } |
| 3670 | |
| 3671 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 3672 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3673 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3674 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3675 | // The event log is used in the transport controller, which must be outlived |
| 3676 | // by the former. CreateOffer by the peer connection is implemented |
| 3677 | // asynchronously and if the peer connection is closed without resetting the |
| 3678 | // WebRTC session description factory, the session description factory would |
| 3679 | // call the transport controller. |
| 3680 | webrtc_session_desc_factory_.reset(); |
| 3681 | transport_controller_.reset(); |
| 3682 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3683 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3684 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3685 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3686 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3687 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3688 | call_.reset(); |
| 3689 | // The event log must outlive call (and any other object that uses it). |
| 3690 | event_log_.reset(); |
| 3691 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3692 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3693 | // The .h file says that observer can be discarded after close() returns. |
| 3694 | // Make sure this is true. |
| 3695 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 3698 | void PeerConnection::OnMessage(rtc::Message* msg) { |
| 3699 | switch (msg->message_id) { |
| 3700 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3701 | SetSessionDescriptionMsg* param = |
| 3702 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3703 | param->observer->OnSuccess(); |
| 3704 | delete param; |
| 3705 | break; |
| 3706 | } |
| 3707 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3708 | SetSessionDescriptionMsg* param = |
| 3709 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3710 | param->observer->OnFailure(std::move(param->error)); |
| 3711 | delete param; |
| 3712 | break; |
| 3713 | } |
| 3714 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3715 | CreateSessionDescriptionMsg* param = |
| 3716 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 3717 | param->observer->OnFailure(std::move(param->error)); |
| 3718 | delete param; |
| 3719 | break; |
| 3720 | } |
| 3721 | case MSG_GETSTATS: { |
| 3722 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 3723 | StatsReports reports; |
| 3724 | stats_->GetStats(param->track, &reports); |
| 3725 | param->observer->OnComplete(reports); |
| 3726 | delete param; |
| 3727 | break; |
| 3728 | } |
| 3729 | case MSG_FREE_DATACHANNELS: { |
| 3730 | sctp_data_channels_to_free_.clear(); |
| 3731 | break; |
| 3732 | } |
| 3733 | case MSG_REPORT_USAGE_PATTERN: { |
| 3734 | ReportUsagePattern(); |
| 3735 | break; |
| 3736 | } |
| 3737 | default: |
| 3738 | RTC_NOTREACHED() << "Not implemented"; |
| 3739 | break; |
| 3740 | } |
| 3741 | } |
| 3742 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3743 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3744 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3745 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3746 | GetAudioTransceiver()->internal()->channel()); |
| 3747 | if (voice_channel) { |
| 3748 | return voice_channel->media_channel(); |
| 3749 | } else { |
| 3750 | return nullptr; |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3755 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3756 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3757 | GetVideoTransceiver()->internal()->channel()); |
| 3758 | if (video_channel) { |
| 3759 | return video_channel->media_channel(); |
| 3760 | } else { |
| 3761 | return nullptr; |
| 3762 | } |
| 3763 | } |
| 3764 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3765 | void PeerConnection::CreateAudioReceiver( |
| 3766 | MediaStreamInterface* stream, |
| 3767 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3768 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3769 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3770 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3771 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3772 | auto* audio_receiver = new AudioRtpReceiver( |
| 3773 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3774 | audio_receiver->SetMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3775 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3776 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3777 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3778 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 3779 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3780 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3783 | void PeerConnection::CreateVideoReceiver( |
| 3784 | MediaStreamInterface* stream, |
| 3785 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3786 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3787 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 20:51:53 +0200 | [diff] [blame] | 3788 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3789 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3790 | auto* video_receiver = new VideoRtpReceiver( |
| 3791 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3792 | video_receiver->SetMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3793 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3794 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3795 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3796 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 3797 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3798 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3801 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3802 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3803 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3804 | const RtpSenderInfo& remote_sender_info) { |
| 3805 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3806 | if (!receiver) { |
| 3807 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3808 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3809 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3810 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3811 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3812 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3813 | } else { |
| 3814 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3815 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3816 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3819 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3820 | MediaStreamInterface* stream) { |
| 3821 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3822 | RTC_DCHECK(track); |
| 3823 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3824 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3825 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3826 | // We already have a sender for this track, so just change the stream_id |
| 3827 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3828 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3829 | return; |
| 3830 | } |
| 3831 | |
| 3832 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3833 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 3834 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3835 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3836 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3837 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3838 | // which will connect the sender to the underlying transport. This can |
| 3839 | // occur if a local session description that contains the ID of the sender |
| 3840 | // is set before AddStream is called. It can also occur if the local |
| 3841 | // session description is not changed and RemoveStream is called, and |
| 3842 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3843 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3844 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3845 | if (sender_info) { |
| 3846 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3851 | // indefinitely, when we have unified plan SDP. |
| 3852 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3853 | MediaStreamInterface* stream) { |
| 3854 | RTC_DCHECK(!IsClosed()); |
| 3855 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3856 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3857 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3858 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3859 | return; |
| 3860 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3861 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3865 | MediaStreamInterface* stream) { |
| 3866 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3867 | RTC_DCHECK(track); |
| 3868 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3869 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3870 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3871 | // We already have a sender for this track, so just change the stream_id |
| 3872 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 3873 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3874 | return; |
| 3875 | } |
| 3876 | |
| 3877 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 3878 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 3879 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 3880 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3881 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3882 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 3883 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3884 | if (sender_info) { |
| 3885 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3890 | MediaStreamInterface* stream) { |
| 3891 | RTC_DCHECK(!IsClosed()); |
| 3892 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3893 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3894 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3895 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3896 | return; |
| 3897 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3898 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3899 | } |
| 3900 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3901 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3902 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3903 | if (ice_connection_state_ == new_state) { |
| 3904 | return; |
| 3905 | } |
| 3906 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3907 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3908 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3909 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3910 | return; |
| 3911 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3912 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3913 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3914 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3915 | RTC_DCHECK(ice_connection_state_ != |
| 3916 | PeerConnectionInterface::kIceConnectionClosed); |
| 3917 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3918 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3919 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 3922 | void PeerConnection::SetStandardizedIceConnectionState( |
| 3923 | PeerConnectionInterface::IceConnectionState new_state) { |
| 3924 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3925 | if (standardized_ice_connection_state_ == new_state) |
| 3926 | return; |
| 3927 | if (IsClosed()) |
| 3928 | return; |
| 3929 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 11:25:14 +0100 | [diff] [blame] | 3930 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 3933 | void PeerConnection::SetConnectionState( |
| 3934 | PeerConnectionInterface::PeerConnectionState new_state) { |
| 3935 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3936 | if (connection_state_ == new_state) |
| 3937 | return; |
| 3938 | if (IsClosed()) |
| 3939 | return; |
| 3940 | connection_state_ = new_state; |
| 3941 | Observer()->OnConnectionChange(new_state); |
| 3942 | } |
| 3943 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3944 | void PeerConnection::OnIceGatheringChange( |
| 3945 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3946 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3947 | if (IsClosed()) { |
| 3948 | return; |
| 3949 | } |
| 3950 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3951 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3954 | void PeerConnection::OnIceCandidate( |
| 3955 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3956 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3957 | if (IsClosed()) { |
| 3958 | return; |
| 3959 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 3960 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 3961 | if (candidate->candidate().type() == LOCAL_PORT_TYPE && |
| 3962 | candidate->candidate().address().IsPrivateIP()) { |
| 3963 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 3964 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3965 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3968 | void PeerConnection::OnIceCandidatesRemoved( |
| 3969 | const std::vector<cricket::Candidate>& candidates) { |
| 3970 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3971 | if (IsClosed()) { |
| 3972 | return; |
| 3973 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3974 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3975 | } |
| 3976 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3977 | void PeerConnection::ChangeSignalingState( |
| 3978 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3979 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3980 | if (signaling_state_ == signaling_state) { |
| 3981 | return; |
| 3982 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3983 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3984 | << GetSignalingStateString(signaling_state_) |
| 3985 | << " New state: " |
| 3986 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3987 | signaling_state_ = signaling_state; |
| 3988 | if (signaling_state == kClosed) { |
| 3989 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3990 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 3991 | standardized_ice_connection_state_ = |
| 3992 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 3993 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 3994 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3995 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3996 | ice_gathering_state_ = kIceGatheringComplete; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 3997 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4000 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4003 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 4004 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4005 | if (IsClosed()) { |
| 4006 | return; |
| 4007 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4008 | AddAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4009 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4010 | } |
| 4011 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4012 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 4013 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4014 | if (IsClosed()) { |
| 4015 | return; |
| 4016 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4017 | RemoveAudioTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4018 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 4022 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4023 | if (IsClosed()) { |
| 4024 | return; |
| 4025 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4026 | AddVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4027 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 4031 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 4032 | if (IsClosed()) { |
| 4033 | return; |
| 4034 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 4035 | RemoveVideoTrack(track, stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4036 | Observer()->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 4037 | } |
| 4038 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4039 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 4040 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4041 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4042 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4043 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 4044 | } |
| 4045 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4046 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 4047 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4048 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4049 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4050 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4051 | msg->error = std::move(error); |
| 4052 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4053 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 4057 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 4058 | RTCError error) { |
| 4059 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4060 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 4061 | msg->error = std::move(error); |
| 4062 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4063 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4064 | } |
| 4065 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4066 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4067 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4068 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4069 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4070 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4071 | if (IsUnifiedPlan()) { |
| 4072 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 4073 | } else { |
| 4074 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 4075 | } |
| 4076 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4077 | // Intentionally unset the data channel type for RTP data channel with the |
| 4078 | // second condition. Otherwise the RTP data channels would be successfully |
| 4079 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 4080 | // when building with chromium. We want to leave RTP data channels broken, so |
| 4081 | // people won't try to use them. |
| 4082 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4083 | session_options->data_channel_type = data_channel_type(); |
| 4084 | } |
| 4085 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4086 | // Apply ICE restart flag and renomination flag. |
| 4087 | for (auto& options : session_options->media_description_options) { |
| 4088 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 4089 | options.transport_options.enable_ice_renomination = |
| 4090 | configuration_.enable_ice_renomination; |
| 4091 | } |
| 4092 | |
| 4093 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4094 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4095 | session_options->pooled_ice_credentials = |
| 4096 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4097 | RTC_FROM_HERE, |
| 4098 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4099 | port_allocator_.get())); |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 4100 | session_options->offer_extmap_allow_mixed = |
| 4101 | configuration_.offer_extmap_allow_mixed; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | void PeerConnection::GetOptionsForPlanBOffer( |
| 4105 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4106 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4107 | // Figure out transceiver directional preferences. |
| 4108 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4109 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4110 | |
| 4111 | // By default, generate sendrecv/recvonly m= sections. |
| 4112 | bool recv_audio = true; |
| 4113 | bool recv_video = true; |
| 4114 | |
| 4115 | // By default, only offer a new m= section if we have media to send with it. |
| 4116 | bool offer_new_audio_description = send_audio; |
| 4117 | bool offer_new_video_description = send_video; |
| 4118 | bool offer_new_data_description = HasDataChannels(); |
| 4119 | |
| 4120 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4121 | if (offer_answer_options.offer_to_receive_audio != |
| 4122 | RTCOfferAnswerOptions::kUndefined) { |
| 4123 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4124 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4125 | offer_new_audio_description || |
| 4126 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4127 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4128 | if (offer_answer_options.offer_to_receive_video != |
| 4129 | RTCOfferAnswerOptions::kUndefined) { |
| 4130 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4131 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4132 | offer_new_video_description || |
| 4133 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4134 | } |
| 4135 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4136 | absl::optional<size_t> audio_index; |
| 4137 | absl::optional<size_t> video_index; |
| 4138 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4139 | // If a current description exists, generate m= sections in the same order, |
| 4140 | // using the first audio/video/data section that appears and rejecting |
| 4141 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4142 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4143 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4144 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4145 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4146 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4147 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4148 | } |
| 4149 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4150 | // Add audio/video/data m= sections to the end if needed. |
| 4151 | if (!audio_index && offer_new_audio_description) { |
| 4152 | session_options->media_description_options.push_back( |
| 4153 | cricket::MediaDescriptionOptions( |
| 4154 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4155 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4156 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4157 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4158 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4159 | if (!video_index && offer_new_video_description) { |
| 4160 | session_options->media_description_options.push_back( |
| 4161 | cricket::MediaDescriptionOptions( |
| 4162 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4163 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4164 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4165 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 4166 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4167 | if (!data_index && offer_new_data_description) { |
| 4168 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4169 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4170 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4174 | !audio_index ? nullptr |
| 4175 | : &session_options->media_description_options[*audio_index]; |
| 4176 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4177 | !video_index ? nullptr |
| 4178 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4179 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4180 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4181 | audio_media_description_options, |
| 4182 | video_media_description_options, |
| 4183 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4184 | } |
| 4185 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4186 | static cricket::MediaDescriptionOptions |
| 4187 | GetMediaDescriptionOptionsForTransceiver( |
| 4188 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4189 | transceiver, |
| 4190 | const std::string& mid) { |
| 4191 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4192 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4193 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4194 | // This behavior is specified in JSEP. The gist is that: |
| 4195 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 4196 | // sendrecv. |
| 4197 | // 2. If the MSID is included, then it must be included in any subsequent |
| 4198 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4199 | if (transceiver->stopped() || |
| 4200 | (!RtpTransceiverDirectionHasSend(transceiver->direction()) && |
| 4201 | !transceiver->internal()->has_ever_been_used_to_send())) { |
| 4202 | return media_description_options; |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 4203 | } |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4204 | |
| 4205 | cricket::SenderOptions sender_options; |
| 4206 | sender_options.track_id = transceiver->sender()->id(); |
| 4207 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 4208 | |
| 4209 | // The following sets up RIDs and Simulcast. |
| 4210 | // RIDs are included if Simulcast is requested or if any RID was specified. |
| 4211 | RtpParameters send_parameters = transceiver->sender()->GetParameters(); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4212 | bool has_rids = std::any_of(send_parameters.encodings.begin(), |
| 4213 | send_parameters.encodings.end(), |
| 4214 | [](const RtpEncodingParameters& encoding) { |
| 4215 | return !encoding.rid.empty(); |
| 4216 | }); |
| 4217 | |
Amit Hilbuch | b7446ed | 2019-01-28 12:25:25 -0800 | [diff] [blame] | 4218 | std::vector<RidDescription> send_rids; |
| 4219 | SimulcastLayerList send_layers; |
| 4220 | for (const RtpEncodingParameters& encoding : send_parameters.encodings) { |
| 4221 | if (encoding.rid.empty()) { |
| 4222 | continue; |
| 4223 | } |
| 4224 | send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend)); |
| 4225 | send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active)); |
| 4226 | } |
| 4227 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4228 | if (has_rids) { |
| 4229 | sender_options.rids = send_rids; |
| 4230 | } |
| 4231 | |
| 4232 | sender_options.simulcast_layers = send_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 4233 | // When RIDs are configured, we must set num_sim_layers to 0 to. |
| 4234 | // Otherwise, num_sim_layers must be 1 because either there is no |
| 4235 | // simulcast, or simulcast is acheived by munging the SDP. |
| 4236 | sender_options.num_sim_layers = has_rids ? 0 : 1; |
| 4237 | media_description_options.sender_options.push_back(sender_options); |
| 4238 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4239 | return media_description_options; |
| 4240 | } |
| 4241 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4242 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 4243 | static const ContentInfo* GetContentByIndex( |
| 4244 | const SessionDescriptionInterface* sdesc, |
| 4245 | size_t i) { |
| 4246 | if (!sdesc) { |
| 4247 | return nullptr; |
| 4248 | } |
| 4249 | const ContentInfos& contents = sdesc->description()->contents(); |
| 4250 | return (i < contents.size() ? &contents[i] : nullptr); |
| 4251 | } |
| 4252 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4253 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 4254 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4255 | cricket::MediaSessionOptions* session_options) { |
| 4256 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 4257 | // Offers) and 5.2.2 (Subsequent Offers). |
| 4258 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 4259 | const ContentInfos& local_contents = |
| 4260 | (local_description() ? local_description()->description()->contents() |
| 4261 | : ContentInfos()); |
| 4262 | const ContentInfos& remote_contents = |
| 4263 | (remote_description() ? remote_description()->description()->contents() |
| 4264 | : ContentInfos()); |
| 4265 | // The mline indices that can be recycled. New transceivers should reuse these |
| 4266 | // slots first. |
| 4267 | std::queue<size_t> recycleable_mline_indices; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4268 | // First, go through each media section that exists in either the local or |
| 4269 | // remote description and generate a media section in this offer for the |
| 4270 | // associated transceiver. If a media section can be recycled, generate a |
| 4271 | // default, rejected media section here that can be later overwritten. |
| 4272 | for (size_t i = 0; |
| 4273 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 4274 | // Either |local_content| or |remote_content| is non-null. |
| 4275 | const ContentInfo* local_content = |
| 4276 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4277 | const ContentInfo* current_local_content = |
| 4278 | GetContentByIndex(current_local_description(), i); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4279 | const ContentInfo* remote_content = |
| 4280 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4281 | const ContentInfo* current_remote_content = |
| 4282 | GetContentByIndex(current_remote_description(), i); |
| 4283 | bool had_been_rejected = |
| 4284 | (current_local_content && current_local_content->rejected) || |
| 4285 | (current_remote_content && current_remote_content->rejected); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4286 | const std::string& mid = |
| 4287 | (local_content ? local_content->name : remote_content->name); |
| 4288 | cricket::MediaType media_type = |
| 4289 | (local_content ? local_content->media_description()->type() |
| 4290 | : remote_content->media_description()->type()); |
| 4291 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4292 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4293 | auto transceiver = GetAssociatedTransceiver(mid); |
| 4294 | RTC_CHECK(transceiver); |
| 4295 | // A media section is considered eligible for recycling if it is marked as |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4296 | // rejected in either the current local or current remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 4297 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4298 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4299 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 4300 | RtpTransceiverDirection::kInactive, |
| 4301 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4302 | recycleable_mline_indices.push(i); |
| 4303 | } else { |
| 4304 | session_options->media_description_options.push_back( |
| 4305 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 4306 | // CreateOffer shouldn't really cause any state changes in |
| 4307 | // PeerConnection, but we need a way to match new transceivers to new |
| 4308 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 4309 | // by recording the index of the media section generated for the |
| 4310 | // transceiver in the offer. |
| 4311 | transceiver->internal()->set_mline_index(i); |
| 4312 | } |
| 4313 | } else { |
| 4314 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4315 | RTC_CHECK(GetDataMid()); |
| 4316 | if (had_been_rejected || mid != *GetDataMid()) { |
| 4317 | session_options->media_description_options.push_back( |
| 4318 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 4319 | } else { |
| 4320 | session_options->media_description_options.push_back( |
| 4321 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 4322 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4323 | } |
| 4324 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4325 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4326 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 4327 | // and not associated). Reuse media sections marked as recyclable first, |
| 4328 | // otherwise append to the end of the offer. New media sections should be |
| 4329 | // added in the order they were added to the PeerConnection. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 4330 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4331 | if (transceiver->mid() || transceiver->stopped()) { |
| 4332 | continue; |
| 4333 | } |
| 4334 | size_t mline_index; |
| 4335 | if (!recycleable_mline_indices.empty()) { |
| 4336 | mline_index = recycleable_mline_indices.front(); |
| 4337 | recycleable_mline_indices.pop(); |
| 4338 | session_options->media_description_options[mline_index] = |
| 4339 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4340 | mid_generator_()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4341 | } else { |
| 4342 | mline_index = session_options->media_description_options.size(); |
| 4343 | session_options->media_description_options.push_back( |
| 4344 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4345 | mid_generator_())); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4346 | } |
| 4347 | // See comment above for why CreateOffer changes the transceiver's state. |
| 4348 | transceiver->internal()->set_mline_index(mline_index); |
| 4349 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4350 | // Lastly, add a m-section if we have local data channels and an m section |
| 4351 | // does not already exist. |
| 4352 | if (!GetDataMid() && HasDataChannels()) { |
| 4353 | session_options->media_description_options.push_back( |
Amit Hilbuch | ae3df54 | 2019-01-07 12:13:08 -0800 | [diff] [blame] | 4354 | GetMediaDescriptionOptionsForActiveData(mid_generator_())); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4355 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | void PeerConnection::GetOptionsForAnswer( |
| 4359 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4360 | cricket::MediaSessionOptions* session_options) { |
| 4361 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 4362 | |
| 4363 | if (IsUnifiedPlan()) { |
| 4364 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 4365 | } else { |
| 4366 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 4367 | } |
| 4368 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4369 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 4370 | // the RTP data channels would be successfully negotiated by default and the |
| 4371 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 4372 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 4373 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4374 | session_options->data_channel_type = data_channel_type(); |
| 4375 | } |
| 4376 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4377 | // Apply ICE renomination flag. |
| 4378 | for (auto& options : session_options->media_description_options) { |
| 4379 | options.transport_options.enable_ice_renomination = |
| 4380 | configuration_.enable_ice_renomination; |
| 4381 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 4382 | |
| 4383 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 4384 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 4385 | session_options->pooled_ice_credentials = |
| 4386 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4387 | RTC_FROM_HERE, |
| 4388 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4389 | port_allocator_.get())); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4390 | } |
| 4391 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4392 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 4393 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4394 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4395 | // Figure out transceiver directional preferences. |
| 4396 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4397 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4398 | |
| 4399 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 4400 | // restricted by the direction in the offer. |
| 4401 | bool recv_audio = true; |
| 4402 | bool recv_video = true; |
| 4403 | |
| 4404 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4405 | if (offer_answer_options.offer_to_receive_audio != |
| 4406 | RTCOfferAnswerOptions::kUndefined) { |
| 4407 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 4408 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4409 | if (offer_answer_options.offer_to_receive_video != |
| 4410 | RTCOfferAnswerOptions::kUndefined) { |
| 4411 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4412 | } |
| 4413 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4414 | absl::optional<size_t> audio_index; |
| 4415 | absl::optional<size_t> video_index; |
| 4416 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 4417 | |
| 4418 | // Generate m= sections that match those in the offer. |
| 4419 | // Note that mediasession.cc will handle intersection our preferred |
| 4420 | // direction with the offered direction. |
| 4421 | GenerateMediaDescriptionOptions( |
| 4422 | remote_description(), |
| 4423 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4424 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 4425 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4426 | |
| 4427 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4428 | !audio_index ? nullptr |
| 4429 | : &session_options->media_description_options[*audio_index]; |
| 4430 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4431 | !video_index ? nullptr |
| 4432 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4433 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 4434 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4435 | audio_media_description_options, |
| 4436 | video_media_description_options, |
| 4437 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4438 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 4439 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4440 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 4441 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4442 | cricket::MediaSessionOptions* session_options) { |
| 4443 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 4444 | // Answers) and 5.3.2 (Subsequent Answers). |
| 4445 | RTC_DCHECK(remote_description()); |
| 4446 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 4447 | for (const ContentInfo& content : |
| 4448 | remote_description()->description()->contents()) { |
| 4449 | cricket::MediaType media_type = content.media_description()->type(); |
| 4450 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4451 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4452 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 4453 | RTC_CHECK(transceiver); |
| 4454 | session_options->media_description_options.push_back( |
| 4455 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 4456 | } else { |
| 4457 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 4458 | // Reject all data sections if data channels are disabled. |
| 4459 | // Reject a data section if it has already been rejected. |
| 4460 | // Reject all data sections except for the first one. |
| 4461 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 4462 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4463 | session_options->media_description_options.push_back( |
| 4464 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 4465 | } else { |
| 4466 | session_options->media_description_options.push_back( |
| 4467 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 4468 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4469 | } |
| 4470 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4471 | } |
| 4472 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4473 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 4474 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 4475 | RtpTransceiverDirection audio_direction, |
| 4476 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4477 | absl::optional<size_t>* audio_index, |
| 4478 | absl::optional<size_t>* video_index, |
| 4479 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4480 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4481 | for (const cricket::ContentInfo& content : |
| 4482 | session_desc->description()->contents()) { |
| 4483 | if (IsAudioContent(&content)) { |
| 4484 | // If we already have an audio m= section, reject this extra one. |
| 4485 | if (*audio_index) { |
| 4486 | session_options->media_description_options.push_back( |
| 4487 | cricket::MediaDescriptionOptions( |
| 4488 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4489 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4490 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4491 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4492 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4493 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 4494 | content.name, audio_direction, |
| 4495 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4496 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4497 | } |
| 4498 | } else if (IsVideoContent(&content)) { |
| 4499 | // If we already have an video m= section, reject this extra one. |
| 4500 | if (*video_index) { |
| 4501 | session_options->media_description_options.push_back( |
| 4502 | cricket::MediaDescriptionOptions( |
| 4503 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4504 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4505 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4506 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4507 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 4508 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 4509 | content.name, video_direction, |
| 4510 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4511 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4512 | } |
| 4513 | } else { |
| 4514 | RTC_DCHECK(IsDataContent(&content)); |
| 4515 | // If we already have an data m= section, reject this extra one. |
| 4516 | if (*data_index) { |
| 4517 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4518 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4519 | } else { |
| 4520 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4521 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 4522 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4523 | } |
| 4524 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 4525 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4526 | } |
| 4527 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4528 | cricket::MediaDescriptionOptions |
| 4529 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 4530 | const std::string& mid) const { |
| 4531 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4532 | // expect sendrecv. |
| 4533 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4534 | RtpTransceiverDirection::kSendRecv, |
| 4535 | /*stopped=*/false); |
| 4536 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4537 | return options; |
| 4538 | } |
| 4539 | |
| 4540 | cricket::MediaDescriptionOptions |
| 4541 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4542 | const std::string& mid) const { |
| 4543 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4544 | RtpTransceiverDirection::kInactive, |
| 4545 | /*stopped=*/true); |
| 4546 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4547 | return options; |
| 4548 | } |
| 4549 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4550 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4551 | switch (data_channel_type_) { |
| 4552 | case cricket::DCT_RTP: |
| 4553 | if (!rtp_data_channel_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4554 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4555 | } |
| 4556 | return rtp_data_channel_->content_name(); |
| 4557 | case cricket::DCT_SCTP: |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 4558 | return sctp_mid_; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 4559 | case cricket::DCT_MEDIA_TRANSPORT: |
| 4560 | return media_transport_data_mid_; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4561 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 4562 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 4563 | } |
| 4564 | } |
| 4565 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4566 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 4567 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4568 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4569 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 4570 | } |
| 4571 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4572 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4573 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4574 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4575 | cricket::MediaType media_type, |
| 4576 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4577 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4578 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4579 | std::vector<RtpSenderInfo>* current_senders = |
| 4580 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4581 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4582 | // 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] | 4583 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4584 | for (auto sender_it = current_senders->begin(); |
| 4585 | sender_it != current_senders->end(); |
| 4586 | /* incremented manually */) { |
| 4587 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4588 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4589 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4590 | std::string params_stream_id; |
| 4591 | if (params) { |
| 4592 | params_stream_id = |
| 4593 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 4594 | : kDefaultStreamId); |
| 4595 | } |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4596 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4597 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4598 | // 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] | 4599 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4600 | sender_exists) { |
| 4601 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4602 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4603 | OnRemoteSenderRemoved(info, media_type); |
| 4604 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4605 | } |
| 4606 | } |
| 4607 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4608 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4609 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4610 | if (!params.has_ssrcs()) { |
| 4611 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 4612 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 4613 | // create a default. |
| 4614 | default_sender_needed = true; |
| 4615 | break; |
| 4616 | } |
| 4617 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4618 | // |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] | 4619 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 4620 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 4621 | // not supported in Plan B, we just take the first here and create the |
| 4622 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4623 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4624 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 4625 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4626 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4627 | uint32_t ssrc = params.first_ssrc(); |
| 4628 | |
| 4629 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4630 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4631 | if (!stream) { |
| 4632 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4633 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4634 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4635 | remote_streams_->AddStream(stream); |
| 4636 | new_streams->AddStream(stream); |
| 4637 | } |
| 4638 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4639 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4640 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4641 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4642 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4643 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4644 | } |
| 4645 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4646 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4647 | // Add default sender if necessary. |
| 4648 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4649 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4650 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4651 | if (!default_stream) { |
| 4652 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4653 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4654 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4655 | remote_streams_->AddStream(default_stream); |
| 4656 | new_streams->AddStream(default_stream); |
| 4657 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4658 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4659 | ? kDefaultAudioSenderId |
| 4660 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4661 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4662 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4663 | if (!default_sender_info) { |
| 4664 | current_senders->push_back( |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4665 | RtpSenderInfo(kDefaultStreamId, default_sender_id, 0)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4666 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 4667 | } |
| 4668 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4671 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 4672 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4673 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 4674 | << " receiver for track_id=" << sender_info.sender_id |
| 4675 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4676 | |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 4677 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4678 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4679 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4680 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4681 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4682 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 4683 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4684 | } |
| 4685 | } |
| 4686 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4687 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 4688 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-05 18:12:09 -0700 | [diff] [blame] | 4689 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 4690 | << " receiver for track_id=" << sender_info.sender_id |
| 4691 | << " and stream_id=" << sender_info.stream_id; |
| 4692 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4693 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4694 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4695 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4696 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4697 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 4698 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4699 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4700 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4701 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4702 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4703 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4704 | } |
| 4705 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4706 | // Stopping or destroying a VideoRtpReceiver will end the |
| 4707 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4708 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4709 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4710 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4711 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 4712 | // There's no guarantee the track is still available, e.g. the track may |
| 4713 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4714 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4715 | } |
| 4716 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 4717 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4718 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4719 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4720 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 4721 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4725 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4726 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4727 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4728 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4729 | streams_to_remove.push_back(stream); |
| 4730 | } |
| 4731 | } |
| 4732 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4733 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4734 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4735 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4736 | } |
| 4737 | } |
| 4738 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4739 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4740 | const std::vector<cricket::StreamParams>& streams, |
| 4741 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4742 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4743 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4744 | // 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] | 4745 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4746 | for (auto sender_it = current_senders->begin(); |
| 4747 | sender_it != current_senders->end(); |
| 4748 | /* incremented manually */) { |
| 4749 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4750 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4751 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4752 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4753 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4754 | OnLocalSenderRemoved(info, media_type); |
| 4755 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4756 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4757 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4758 | } |
| 4759 | } |
| 4760 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4761 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4762 | for (const cricket::StreamParams& params : streams) { |
| 4763 | // 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] | 4764 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4765 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4766 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4767 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4768 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 4769 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4770 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4771 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4772 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4773 | } |
| 4774 | } |
| 4775 | } |
| 4776 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4777 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4778 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4779 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4780 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4781 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4782 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4783 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4784 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4785 | return; |
| 4786 | } |
| 4787 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4788 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4789 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4790 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4791 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4792 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4793 | |
Seth Hampson | 5b4f075 | 2018-04-02 16:31:36 -0700 | [diff] [blame] | 4794 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4795 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4796 | } |
| 4797 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4798 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4799 | cricket::MediaType media_type) { |
| 4800 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4801 | if (!sender) { |
| 4802 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4803 | // SessionDescriptions has been renegotiated. |
| 4804 | return; |
| 4805 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4806 | |
| 4807 | // A sender has been removed from the SessionDescription but it's still |
| 4808 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4809 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4810 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4811 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4812 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4813 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4814 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4815 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4816 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4817 | } |
| 4818 | |
| 4819 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4820 | const cricket::StreamParamsVec& streams) { |
| 4821 | std::vector<std::string> existing_channels; |
| 4822 | |
| 4823 | // Find new and active data channels. |
| 4824 | for (const cricket::StreamParams& params : streams) { |
| 4825 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4826 | // we use the same naming of data channels as we do for |
| 4827 | // MediaStreams and Tracks. |
| 4828 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4829 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4830 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4831 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4832 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4833 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4834 | continue; |
| 4835 | } |
| 4836 | // Set the SSRC the data channel should use for sending. |
| 4837 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4838 | existing_channels.push_back(data_channel_it->first); |
| 4839 | } |
| 4840 | |
| 4841 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4842 | } |
| 4843 | |
| 4844 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4845 | const cricket::StreamParamsVec& streams) { |
| 4846 | std::vector<std::string> existing_channels; |
| 4847 | |
| 4848 | // Find new and active data channels. |
| 4849 | for (const cricket::StreamParams& params : streams) { |
| 4850 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4851 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4852 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4853 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 4854 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4855 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4856 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4857 | // This is a new data channel. |
| 4858 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4859 | } else { |
| 4860 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4861 | } |
| 4862 | existing_channels.push_back(label); |
| 4863 | } |
| 4864 | |
| 4865 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4866 | } |
| 4867 | |
| 4868 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4869 | const std::vector<std::string>& active_channels, |
| 4870 | bool is_local_update) { |
| 4871 | auto it = rtp_data_channels_.begin(); |
| 4872 | while (it != rtp_data_channels_.end()) { |
| 4873 | DataChannel* data_channel = it->second; |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 4874 | if (absl::c_linear_search(active_channels, data_channel->label())) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4875 | ++it; |
| 4876 | continue; |
| 4877 | } |
| 4878 | |
| 4879 | if (is_local_update) { |
| 4880 | data_channel->SetSendSsrc(0); |
| 4881 | } else { |
| 4882 | data_channel->RemotePeerRequestClose(); |
| 4883 | } |
| 4884 | |
| 4885 | if (data_channel->state() == DataChannel::kClosed) { |
| 4886 | rtp_data_channels_.erase(it); |
| 4887 | it = rtp_data_channels_.begin(); |
| 4888 | } else { |
| 4889 | ++it; |
| 4890 | } |
| 4891 | } |
| 4892 | } |
| 4893 | |
| 4894 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4895 | uint32_t remote_ssrc) { |
| 4896 | rtc::scoped_refptr<DataChannel> channel( |
| 4897 | InternalCreateDataChannel(label, nullptr)); |
| 4898 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4899 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4900 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4901 | return; |
| 4902 | } |
| 4903 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4904 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4905 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 4906 | Observer()->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4910 | const std::string& label, |
| 4911 | const InternalDataChannelInit* config) { |
| 4912 | if (IsClosed()) { |
| 4913 | return nullptr; |
| 4914 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4915 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4916 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4917 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4918 | return nullptr; |
| 4919 | } |
| 4920 | InternalDataChannelInit new_config = |
| 4921 | config ? (*config) : InternalDataChannelInit(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 4922 | if (DataChannel::IsSctpLike(data_channel_type_)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4923 | if (new_config.id < 0) { |
| 4924 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4925 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4926 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4927 | RTC_LOG(LS_ERROR) |
| 4928 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4929 | return nullptr; |
| 4930 | } |
| 4931 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4932 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4933 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4934 | return nullptr; |
| 4935 | } |
| 4936 | } |
| 4937 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4938 | rtc::scoped_refptr<DataChannel> channel( |
| 4939 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4940 | if (!channel) { |
| 4941 | sid_allocator_.ReleaseSid(new_config.id); |
| 4942 | return nullptr; |
| 4943 | } |
| 4944 | |
| 4945 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4946 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4947 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4948 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4949 | return nullptr; |
| 4950 | } |
| 4951 | rtp_data_channels_[channel->label()] = channel; |
| 4952 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 4953 | RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4954 | sctp_data_channels_.push_back(channel); |
| 4955 | channel->SignalClosed.connect(this, |
| 4956 | &PeerConnection::OnSctpDataChannelClosed); |
| 4957 | } |
| 4958 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4959 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4960 | return channel; |
| 4961 | } |
| 4962 | |
| 4963 | bool PeerConnection::HasDataChannels() const { |
| 4964 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4965 | } |
| 4966 | |
| 4967 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4968 | for (const auto& channel : sctp_data_channels_) { |
| 4969 | if (channel->id() < 0) { |
| 4970 | int sid; |
| 4971 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4972 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4973 | continue; |
| 4974 | } |
| 4975 | channel->SetSctpSid(sid); |
| 4976 | } |
| 4977 | } |
| 4978 | } |
| 4979 | |
| 4980 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4981 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4982 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4983 | ++it) { |
| 4984 | if (it->get() == channel) { |
| 4985 | if (channel->id() >= 0) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 4986 | // After the closing procedure is done, it's safe to use this ID for |
| 4987 | // another data channel. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4988 | sid_allocator_.ReleaseSid(channel->id()); |
| 4989 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4990 | // Since this method is triggered by a signal from the DataChannel, |
| 4991 | // we can't free it directly here; we need to free it asynchronously. |
| 4992 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4993 | sctp_data_channels_.erase(it); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 4994 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4995 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4996 | return; |
| 4997 | } |
| 4998 | } |
| 4999 | } |
| 5000 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5001 | void PeerConnection::OnDataChannelDestroyed() { |
| 5002 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 5003 | // DataChannel may callback to us and try to modify the list. |
| 5004 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 5005 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 5006 | for (const auto& kv : temp_rtp_dcs) { |
| 5007 | kv.second->OnTransportChannelDestroyed(); |
| 5008 | } |
| 5009 | |
| 5010 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 5011 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 5012 | for (const auto& channel : temp_sctp_dcs) { |
| 5013 | channel->OnTransportChannelDestroyed(); |
| 5014 | } |
| 5015 | } |
| 5016 | |
| 5017 | void PeerConnection::OnDataChannelOpenMessage( |
| 5018 | const std::string& label, |
| 5019 | const InternalDataChannelInit& config) { |
| 5020 | rtc::scoped_refptr<DataChannel> channel( |
| 5021 | InternalCreateDataChannel(label, &config)); |
| 5022 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5023 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5024 | return; |
| 5025 | } |
| 5026 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 5027 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5028 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 5029 | Observer()->OnDataChannel(std::move(proxy_channel)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 5030 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5031 | } |
| 5032 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5033 | bool PeerConnection::HandleOpenMessage_s( |
| 5034 | const cricket::ReceiveDataParams& params, |
| 5035 | const rtc::CopyOnWriteBuffer& buffer) { |
| 5036 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(buffer)) { |
| 5037 | // Received OPEN message; parse and signal that a new data channel should |
| 5038 | // be created. |
| 5039 | std::string label; |
| 5040 | InternalDataChannelInit config; |
| 5041 | config.id = params.ssrc; |
| 5042 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
| 5043 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for ssrc " |
| 5044 | << params.ssrc; |
| 5045 | return true; |
| 5046 | } |
| 5047 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5048 | OnDataChannelOpenMessage(label, config); |
| 5049 | return true; |
| 5050 | } |
| 5051 | return false; |
| 5052 | } |
| 5053 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5054 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5055 | PeerConnection::GetAudioTransceiver() const { |
| 5056 | // This method only works with Plan B SDP, where there is a single |
| 5057 | // audio/video transceiver. |
| 5058 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5059 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5060 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5061 | return transceiver; |
| 5062 | } |
| 5063 | } |
| 5064 | RTC_NOTREACHED(); |
| 5065 | return nullptr; |
| 5066 | } |
| 5067 | |
| 5068 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5069 | PeerConnection::GetVideoTransceiver() const { |
| 5070 | // This method only works with Plan B SDP, where there is a single |
| 5071 | // audio/video transceiver. |
| 5072 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5073 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5074 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5075 | return transceiver; |
| 5076 | } |
| 5077 | } |
| 5078 | RTC_NOTREACHED(); |
| 5079 | return nullptr; |
| 5080 | } |
| 5081 | |
| 5082 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 5083 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5084 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5085 | switch (type) { |
| 5086 | case cricket::MEDIA_TYPE_AUDIO: |
| 5087 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 5088 | case cricket::MEDIA_TYPE_VIDEO: |
| 5089 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 5090 | case cricket::MEDIA_TYPE_DATA: |
| 5091 | return false; |
| 5092 | } |
| 5093 | RTC_NOTREACHED(); |
| 5094 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 5095 | } |
| 5096 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5097 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5098 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5099 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5100 | for (auto sender : transceiver->internal()->senders()) { |
| 5101 | if (sender->track() == track) { |
| 5102 | return sender; |
| 5103 | } |
| 5104 | } |
| 5105 | } |
| 5106 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 5107 | } |
| 5108 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5109 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5110 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5111 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5112 | for (auto sender : transceiver->internal()->senders()) { |
| 5113 | if (sender->id() == sender_id) { |
| 5114 | return sender; |
| 5115 | } |
| 5116 | } |
| 5117 | } |
| 5118 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5119 | } |
| 5120 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5121 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 5122 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5123 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5124 | for (auto receiver : transceiver->internal()->receivers()) { |
| 5125 | if (receiver->id() == receiver_id) { |
| 5126 | return receiver; |
| 5127 | } |
| 5128 | } |
| 5129 | } |
| 5130 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 5131 | } |
| 5132 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5133 | std::vector<PeerConnection::RtpSenderInfo>* |
| 5134 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 5135 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5136 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 5137 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5138 | ? &remote_audio_sender_infos_ |
| 5139 | : &remote_video_sender_infos_; |
| 5140 | } |
| 5141 | |
| 5142 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5143 | cricket::MediaType media_type) { |
| 5144 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5145 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5146 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 5147 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5148 | } |
| 5149 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5150 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 5151 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5152 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5153 | const std::string sender_id) const { |
| 5154 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609ea | 2018-03-27 21:57:18 +0000 | [diff] [blame] | 5155 | if (sender_info.stream_id == stream_id && |
| 5156 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5157 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 5158 | } |
| 5159 | } |
| 5160 | return nullptr; |
| 5161 | } |
| 5162 | |
| 5163 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 5164 | for (const auto& channel : sctp_data_channels_) { |
| 5165 | if (channel->id() == sid) { |
| 5166 | return channel; |
| 5167 | } |
| 5168 | } |
| 5169 | return nullptr; |
| 5170 | } |
| 5171 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 5172 | bool PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5173 | const cricket::ServerAddresses& stun_servers, |
| 5174 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5175 | const RTCConfiguration& configuration) { |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 5176 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5177 | // To handle both internal and externally created port allocator, we will |
| 5178 | // enable BUNDLE here. |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5179 | port_allocator_flags_ = port_allocator_->flags(); |
| 5180 | port_allocator_flags_ |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 5181 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 5182 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5183 | // If the disable-IPv6 flag was specified, we'll not override it |
| 5184 | // by experiment. |
| 5185 | if (configuration.disable_ipv6) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5186 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 5187 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 5188 | .find("Disabled") == 0) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5189 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5190 | } |
| 5191 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5192 | if (configuration.disable_ipv6_on_wifi) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5193 | port_allocator_flags_ &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5194 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 5195 | } |
| 5196 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5197 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5198 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5199 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5200 | } |
| 5201 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5202 | if (configuration.candidate_network_policy == |
| 5203 | kCandidateNetworkPolicyLowCost) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5204 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5205 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 5206 | } |
| 5207 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5208 | if (configuration.disable_link_local_networks) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5209 | port_allocator_flags_ |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 5210 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 5211 | } |
| 5212 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5213 | port_allocator_->set_flags(port_allocator_flags_); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5214 | // No step delay is used while allocating ports. |
| 5215 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 5216 | port_allocator_->set_candidate_filter( |
| 5217 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 5218 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5219 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 5220 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5221 | for (auto& turn_server : turn_servers_copy) { |
| 5222 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 5223 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5224 | // Call this last since it may create pooled allocator sessions using the |
| 5225 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5226 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5227 | stun_servers, std::move(turn_servers_copy), |
| 5228 | configuration.ice_candidate_pool_size, configuration.prune_turn_ports, |
| 5229 | configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5230 | configuration.stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5231 | return true; |
| 5232 | } |
| 5233 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 5234 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5235 | const cricket::ServerAddresses& stun_servers, |
| 5236 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 5237 | IceTransportsType type, |
| 5238 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 5239 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5240 | webrtc::TurnCustomizer* turn_customizer, |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5241 | absl::optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5242 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 5243 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5244 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 5245 | // size is not allowed, and changing the set of ICE servers will not result |
| 5246 | // in new candidates being gathered. |
| 5247 | if (local_description()) { |
| 5248 | port_allocator_->FreezeCandidatePool(); |
| 5249 | } |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5250 | // Add the custom tls turn servers if they exist. |
| 5251 | auto turn_servers_copy = turn_servers; |
| 5252 | for (auto& turn_server : turn_servers_copy) { |
| 5253 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 5254 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5255 | // Call this last since it may create pooled allocator sessions using the |
| 5256 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 5257 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-13 17:06:26 -0700 | [diff] [blame] | 5258 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
| 5259 | prune_turn_ports, turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 5260 | } |
| 5261 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 5262 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 5263 | return factory_->channel_manager(); |
| 5264 | } |
| 5265 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 5266 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5267 | std::unique_ptr<RtcEventLogOutput> output, |
| 5268 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5269 | if (!event_log_) { |
| 5270 | return false; |
| 5271 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 5272 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5273 | } |
| 5274 | |
| 5275 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 5276 | if (event_log_) { |
| 5277 | event_log_->StopLogging(); |
| 5278 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 5279 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 5280 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5281 | cricket::ChannelInterface* PeerConnection::GetChannel( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5282 | const std::string& content_name) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5283 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5284 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5285 | if (channel && channel->content_name() == content_name) { |
| 5286 | return channel; |
| 5287 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5288 | } |
| 5289 | if (rtp_data_channel() && |
| 5290 | rtp_data_channel()->content_name() == content_name) { |
| 5291 | return rtp_data_channel(); |
| 5292 | } |
| 5293 | return nullptr; |
| 5294 | } |
| 5295 | |
| 5296 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5297 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5298 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5299 | RTC_LOG(LS_INFO) |
| 5300 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5301 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5302 | return false; |
| 5303 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5304 | if (!sctp_transport_ && !media_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5305 | 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] | 5306 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5307 | return false; |
| 5308 | } |
| 5309 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5310 | absl::optional<rtc::SSLRole> dtls_role; |
| 5311 | if (sctp_mid_) { |
| 5312 | dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
| 5313 | } else if (is_caller_) { |
| 5314 | dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
| 5315 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5316 | if (dtls_role) { |
| 5317 | *role = *dtls_role; |
| 5318 | return true; |
| 5319 | } |
| 5320 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5321 | } |
| 5322 | |
| 5323 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 5324 | rtc::SSLRole* role) { |
| 5325 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5326 | RTC_LOG(LS_INFO) |
| 5327 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5328 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5329 | return false; |
| 5330 | } |
| 5331 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5332 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 5333 | if (dtls_role) { |
| 5334 | *role = *dtls_role; |
| 5335 | return true; |
| 5336 | } |
| 5337 | return false; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5338 | } |
| 5339 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5340 | void PeerConnection::SetSessionError(SessionError error, |
| 5341 | const std::string& error_desc) { |
| 5342 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5343 | if (error != session_error_) { |
| 5344 | session_error_ = error; |
| 5345 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5346 | } |
| 5347 | } |
| 5348 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5349 | RTCError PeerConnection::UpdateSessionState( |
| 5350 | SdpType type, |
| 5351 | cricket::ContentSource source, |
| 5352 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5353 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5354 | |
| 5355 | // If there's already a pending error then no state transition should happen. |
| 5356 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5357 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5358 | |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5359 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5360 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5361 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | // Update the signaling state according to the specified state machine (see |
| 5365 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5366 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5367 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5368 | ? PeerConnectionInterface::kHaveLocalOffer |
| 5369 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5370 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5371 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5372 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 5373 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5374 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5375 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5376 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 5377 | } |
| 5378 | |
| 5379 | // Update internal objects according to the session description's media |
| 5380 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5381 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5382 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 5383 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 5384 | } |
| 5385 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5386 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5387 | } |
| 5388 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5389 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5390 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5391 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5392 | const SessionDescriptionInterface* sdesc = |
| 5393 | (source == cricket::CS_LOCAL ? local_description() |
| 5394 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5395 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5396 | |
| 5397 | // Push down the new SDP media section for each audio/video transceiver. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5398 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5399 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5400 | FindMediaSectionForTransceiver(transceiver, sdesc); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5401 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5402 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5403 | continue; |
| 5404 | } |
| 5405 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5406 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5407 | if (!content_desc) { |
| 5408 | continue; |
| 5409 | } |
| 5410 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5411 | bool success = (source == cricket::CS_LOCAL) |
| 5412 | ? channel->SetLocalContent(content_desc, type, &error) |
| 5413 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5414 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5415 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5416 | } |
| 5417 | } |
| 5418 | |
| 5419 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 5420 | if (rtp_data_channel_) { |
| 5421 | const ContentInfo* data_content = |
| 5422 | cricket::GetFirstDataContent(sdesc->description()); |
| 5423 | if (data_content && !data_content->rejected) { |
| 5424 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5425 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5426 | if (data_desc) { |
| 5427 | std::string error; |
| 5428 | bool success = |
| 5429 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5430 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 5431 | : rtp_data_channel_->SetRemoteContent(data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5432 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 21:48:57 +0100 | [diff] [blame] | 5433 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5434 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5435 | } |
| 5436 | } |
| 5437 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5438 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5439 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 5440 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 5441 | if (sctp_transport_ && local_description() && remote_description() && |
| 5442 | cricket::GetFirstDataContent(local_description()->description()) && |
| 5443 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5444 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5445 | RTC_FROM_HERE, |
| 5446 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5447 | if (!success) { |
| 5448 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5449 | "Failed to push down SCTP parameters."); |
| 5450 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5451 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5452 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5453 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 5457 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5458 | RTC_DCHECK(local_description()); |
| 5459 | RTC_DCHECK(remote_description()); |
| 5460 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 5461 | // When we support "max-message-size", that would also be pushed down here. |
| 5462 | return sctp_transport_->Start( |
| 5463 | GetSctpPort(local_description()->description()), |
| 5464 | GetSctpPort(remote_description()->description())); |
| 5465 | } |
| 5466 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5467 | RTCError PeerConnection::PushdownTransportDescription( |
| 5468 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5469 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5470 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5471 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5472 | if (source == cricket::CS_LOCAL) { |
| 5473 | const SessionDescriptionInterface* sdesc = local_description(); |
| 5474 | RTC_DCHECK(sdesc); |
| 5475 | return transport_controller_->SetLocalDescription(type, |
| 5476 | sdesc->description()); |
| 5477 | } else { |
| 5478 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 5479 | RTC_DCHECK(sdesc); |
| 5480 | return transport_controller_->SetRemoteDescription(type, |
| 5481 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5482 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | bool PeerConnection::GetTransportDescription( |
| 5486 | const SessionDescription* description, |
| 5487 | const std::string& content_name, |
| 5488 | cricket::TransportDescription* tdesc) { |
| 5489 | if (!description || !tdesc) { |
| 5490 | return false; |
| 5491 | } |
| 5492 | const TransportInfo* transport_info = |
| 5493 | description->GetTransportInfoByName(content_name); |
| 5494 | if (!transport_info) { |
| 5495 | return false; |
| 5496 | } |
| 5497 | *tdesc = transport_info->description; |
| 5498 | return true; |
| 5499 | } |
| 5500 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5501 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 5502 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 5503 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5504 | switch (config.continual_gathering_policy) { |
| 5505 | case PeerConnectionInterface::GATHER_ONCE: |
| 5506 | gathering_policy = cricket::GATHER_ONCE; |
| 5507 | break; |
| 5508 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 5509 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 5510 | break; |
| 5511 | default: |
| 5512 | RTC_NOTREACHED(); |
| 5513 | gathering_policy = cricket::GATHER_ONCE; |
| 5514 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5515 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5516 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5517 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 5518 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5519 | ice_config.prioritize_most_likely_candidate_pairs = |
| 5520 | config.prioritize_most_likely_ice_candidate_pairs; |
| 5521 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 5522 | RTCConfigurationToIceConfigOptionalInt( |
| 5523 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5524 | ice_config.continual_gathering_policy = gathering_policy; |
| 5525 | ice_config.presume_writable_when_fully_relayed = |
| 5526 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 14:55:14 -0800 | [diff] [blame] | 5527 | ice_config.ice_check_interval_strong_connectivity = |
| 5528 | config.ice_check_interval_strong_connectivity; |
| 5529 | ice_config.ice_check_interval_weak_connectivity = |
| 5530 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5531 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Jiawei Ou | cc88737 | 2018-11-29 23:08:51 -0800 | [diff] [blame] | 5532 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 5533 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-06 23:30:17 -0800 | [diff] [blame] | 5534 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 5535 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5536 | ice_config.regather_all_networks_interval_range = |
| 5537 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 5538 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5539 | return ice_config; |
| 5540 | } |
| 5541 | |
Steve Anton | a41959e | 2018-11-28 11:15:33 -0800 | [diff] [blame] | 5542 | static absl::string_view GetTrackIdBySsrc( |
| 5543 | const SessionDescriptionInterface* session_description, |
| 5544 | uint32_t ssrc) { |
| 5545 | if (!session_description) { |
| 5546 | return {}; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5547 | } |
Steve Anton | a41959e | 2018-11-28 11:15:33 -0800 | [diff] [blame] | 5548 | for (const cricket::ContentInfo& content : |
| 5549 | session_description->description()->contents()) { |
| 5550 | const cricket::MediaContentDescription& media = |
| 5551 | *content.media_description(); |
| 5552 | if (media.type() == cricket::MEDIA_TYPE_AUDIO || |
| 5553 | media.type() == cricket::MEDIA_TYPE_VIDEO) { |
| 5554 | const cricket::StreamParams* stream_params = |
| 5555 | cricket::GetStreamBySsrc(media.streams(), ssrc); |
| 5556 | if (stream_params) { |
| 5557 | return stream_params->id; |
| 5558 | } |
| 5559 | } |
| 5560 | } |
| 5561 | return {}; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5562 | } |
| 5563 | |
Steve Anton | a41959e | 2018-11-28 11:15:33 -0800 | [diff] [blame] | 5564 | absl::string_view PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc) { |
| 5565 | return GetTrackIdBySsrc(local_description(), ssrc); |
| 5566 | } |
| 5567 | |
| 5568 | absl::string_view PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc) { |
| 5569 | return GetTrackIdBySsrc(remote_description(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 5573 | const rtc::CopyOnWriteBuffer& payload, |
| 5574 | cricket::SendDataResult* result) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5575 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
| 5576 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_, " |
| 5577 | "sctp_transport_, and media_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5578 | return false; |
| 5579 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5580 | if (media_transport_) { |
| 5581 | SendDataParams send_params; |
| 5582 | send_params.type = ToWebrtcDataMessageType(params.type); |
| 5583 | send_params.ordered = params.ordered; |
| 5584 | if (params.max_rtx_count >= 0) { |
| 5585 | send_params.max_rtx_count = params.max_rtx_count; |
| 5586 | } else if (params.max_rtx_ms >= 0) { |
| 5587 | send_params.max_rtx_ms = params.max_rtx_ms; |
| 5588 | } |
| 5589 | return media_transport_->SendData(params.sid, send_params, payload).ok(); |
| 5590 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5591 | return rtp_data_channel_ |
| 5592 | ? rtp_data_channel_->SendData(params, payload, result) |
| 5593 | : network_thread()->Invoke<bool>( |
| 5594 | RTC_FROM_HERE, |
| 5595 | Bind(&cricket::SctpTransportInternal::SendData, |
| 5596 | sctp_transport_.get(), params, payload, result)); |
| 5597 | } |
| 5598 | |
| 5599 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5600 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5601 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5602 | // Don't log an error here, because DataChannels are expected to call |
| 5603 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 5604 | // whether or not the underlying transport is ready. |
| 5605 | return false; |
| 5606 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5607 | if (media_transport_) { |
| 5608 | SignalMediaTransportWritable_s.connect(webrtc_data_channel, |
| 5609 | &DataChannel::OnChannelReady); |
| 5610 | SignalMediaTransportReceivedData_s.connect(webrtc_data_channel, |
| 5611 | &DataChannel::OnDataReceived); |
| 5612 | SignalMediaTransportChannelClosing_s.connect( |
| 5613 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5614 | SignalMediaTransportChannelClosed_s.connect( |
| 5615 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
| 5616 | } else if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5617 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 5618 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 5619 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 5620 | &DataChannel::OnDataReceived); |
| 5621 | } else { |
| 5622 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 5623 | &DataChannel::OnChannelReady); |
| 5624 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 5625 | &DataChannel::OnDataReceived); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5626 | SignalSctpClosingProcedureStartedRemotely.connect( |
| 5627 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5628 | SignalSctpClosingProcedureComplete.connect( |
| 5629 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5630 | } |
| 5631 | return true; |
| 5632 | } |
| 5633 | |
| 5634 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5635 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5636 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5637 | RTC_LOG(LS_ERROR) |
| 5638 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 5639 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5640 | return; |
| 5641 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5642 | if (media_transport_) { |
| 5643 | SignalMediaTransportWritable_s.disconnect(webrtc_data_channel); |
| 5644 | SignalMediaTransportReceivedData_s.disconnect(webrtc_data_channel); |
| 5645 | SignalMediaTransportChannelClosing_s.disconnect(webrtc_data_channel); |
| 5646 | SignalMediaTransportChannelClosed_s.disconnect(webrtc_data_channel); |
| 5647 | } else if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5648 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 5649 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 5650 | } else { |
| 5651 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 5652 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 5653 | SignalSctpClosingProcedureStartedRemotely.disconnect(webrtc_data_channel); |
| 5654 | SignalSctpClosingProcedureComplete.disconnect(webrtc_data_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5655 | } |
| 5656 | } |
| 5657 | |
| 5658 | void PeerConnection::AddSctpDataStream(int sid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5659 | if (media_transport_) { |
| 5660 | // No-op. Media transport does not need to add streams. |
| 5661 | return; |
| 5662 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5663 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5664 | RTC_LOG(LS_ERROR) |
| 5665 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5666 | return; |
| 5667 | } |
| 5668 | network_thread()->Invoke<void>( |
| 5669 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 5670 | sctp_transport_.get(), sid)); |
| 5671 | } |
| 5672 | |
| 5673 | void PeerConnection::RemoveSctpDataStream(int sid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5674 | if (media_transport_) { |
| 5675 | media_transport_->CloseChannel(sid); |
| 5676 | return; |
| 5677 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5678 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5679 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5680 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5681 | return; |
| 5682 | } |
| 5683 | network_thread()->Invoke<void>( |
| 5684 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 5685 | sctp_transport_.get(), sid)); |
| 5686 | } |
| 5687 | |
| 5688 | bool PeerConnection::ReadyToSendData() const { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5689 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5690 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5691 | (media_transport_ && media_transport_ready_to_send_data_) || |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5692 | sctp_ready_to_send_data_; |
| 5693 | } |
| 5694 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 5695 | void PeerConnection::OnDataReceived(int channel_id, |
| 5696 | DataMessageType type, |
| 5697 | const rtc::CopyOnWriteBuffer& buffer) { |
| 5698 | cricket::ReceiveDataParams params; |
| 5699 | params.sid = channel_id; |
| 5700 | params.type = ToCricketDataMessageType(type); |
| 5701 | media_transport_invoker_->AsyncInvoke<void>( |
| 5702 | RTC_FROM_HERE, signaling_thread(), [this, params, buffer] { |
| 5703 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5704 | if (!HandleOpenMessage_s(params, buffer)) { |
| 5705 | SignalMediaTransportReceivedData_s(params, buffer); |
| 5706 | } |
| 5707 | }); |
| 5708 | } |
| 5709 | |
| 5710 | void PeerConnection::OnChannelClosing(int channel_id) { |
| 5711 | media_transport_invoker_->AsyncInvoke<void>( |
| 5712 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 5713 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5714 | SignalMediaTransportChannelClosing_s(channel_id); |
| 5715 | }); |
| 5716 | } |
| 5717 | |
| 5718 | void PeerConnection::OnChannelClosed(int channel_id) { |
| 5719 | media_transport_invoker_->AsyncInvoke<void>( |
| 5720 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 5721 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5722 | SignalMediaTransportChannelClosed_s(channel_id); |
| 5723 | }); |
| 5724 | } |
| 5725 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5726 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5727 | if (sctp_mid_ && transport_controller_) { |
| 5728 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 5729 | if (dtls_transport) { |
| 5730 | return dtls_transport->transport_name(); |
| 5731 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5732 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5733 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5734 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5735 | } |
| 5736 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5737 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 5738 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 5739 | network_thread()->Invoke<void>( |
| 5740 | RTC_FROM_HERE, |
| 5741 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 5742 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 5743 | return candidate_states_list; |
| 5744 | } |
| 5745 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5746 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 5747 | const { |
| 5748 | std::map<std::string, std::string> transport_names_by_mid; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5749 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5750 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5751 | if (channel) { |
| 5752 | transport_names_by_mid[channel->content_name()] = |
| 5753 | channel->transport_name(); |
| 5754 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5755 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5756 | if (rtp_data_channel_) { |
| 5757 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5758 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5759 | } |
| 5760 | if (sctp_transport_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 5761 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5762 | RTC_DCHECK(transport_name); |
| 5763 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5764 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5765 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5766 | } |
| 5767 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5768 | std::map<std::string, cricket::TransportStats> |
| 5769 | PeerConnection::GetTransportStatsByNames( |
| 5770 | const std::set<std::string>& transport_names) { |
| 5771 | if (!network_thread()->IsCurrent()) { |
| 5772 | return network_thread() |
| 5773 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5774 | RTC_FROM_HERE, |
| 5775 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5776 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 5777 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5778 | for (const std::string& transport_name : transport_names) { |
| 5779 | cricket::TransportStats transport_stats; |
| 5780 | bool success = |
| 5781 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5782 | if (success) { |
| 5783 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5784 | } else { |
| 5785 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5786 | << transport_name; |
| 5787 | } |
| 5788 | } |
| 5789 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5790 | } |
| 5791 | |
| 5792 | bool PeerConnection::GetLocalCertificate( |
| 5793 | const std::string& transport_name, |
| 5794 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 5795 | if (!certificate) { |
| 5796 | return false; |
| 5797 | } |
| 5798 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 5799 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5800 | } |
| 5801 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5802 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5803 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 5804 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 5808 | return data_channel_type_; |
| 5809 | } |
| 5810 | |
| 5811 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5812 | return pending_ice_restarts_.find(content_name) != |
| 5813 | pending_ice_restarts_.end(); |
| 5814 | } |
| 5815 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5816 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5817 | return transport_controller_->NeedsIceRestart(content_name); |
| 5818 | } |
| 5819 | |
| 5820 | void PeerConnection::OnCertificateReady( |
| 5821 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5822 | transport_controller_->SetLocalCertificate(certificate); |
| 5823 | } |
| 5824 | |
| 5825 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5826 | SetSessionError(SessionError::kTransport, |
| 5827 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5828 | } |
| 5829 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 5830 | void PeerConnection::OnTransportControllerConnectionState( |
| 5831 | cricket::IceConnectionState state) { |
| 5832 | switch (state) { |
| 5833 | case cricket::kIceConnectionConnecting: |
| 5834 | // If the current state is Connected or Completed, then there were |
| 5835 | // writable channels but now there are not, so the next state must |
| 5836 | // be Disconnected. |
| 5837 | // kIceConnectionConnecting is currently used as the default, |
| 5838 | // un-connected state by the TransportController, so its only use is |
| 5839 | // detecting disconnections. |
| 5840 | if (ice_connection_state_ == |
| 5841 | PeerConnectionInterface::kIceConnectionConnected || |
| 5842 | ice_connection_state_ == |
| 5843 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5844 | SetIceConnectionState( |
| 5845 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5846 | } |
| 5847 | break; |
| 5848 | case cricket::kIceConnectionFailed: |
| 5849 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5850 | break; |
| 5851 | case cricket::kIceConnectionConnected: |
| 5852 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 5853 | "all transports are writable."; |
| 5854 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5855 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 5856 | break; |
| 5857 | case cricket::kIceConnectionCompleted: |
| 5858 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 5859 | "all transports are complete."; |
| 5860 | if (ice_connection_state_ != |
| 5861 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5862 | // If jumping directly from "checking" to "connected", |
| 5863 | // signal "connected" first. |
| 5864 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5865 | } |
| 5866 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 5867 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 5868 | ReportTransportStats(); |
| 5869 | break; |
| 5870 | default: |
| 5871 | RTC_NOTREACHED(); |
| 5872 | } |
| 5873 | } |
| 5874 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5875 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5876 | const std::string& transport_name, |
| 5877 | const cricket::Candidates& candidates) { |
| 5878 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5879 | int sdp_mline_index; |
| 5880 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5881 | RTC_LOG(LS_ERROR) |
| 5882 | << "OnTransportControllerCandidatesGathered: content name " |
| 5883 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5884 | return; |
| 5885 | } |
| 5886 | |
| 5887 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5888 | citer != candidates.end(); ++citer) { |
| 5889 | // Use transport_name as the candidate media id. |
| 5890 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5891 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5892 | if (local_description()) { |
| 5893 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5894 | } |
| 5895 | OnIceCandidate(std::move(candidate)); |
| 5896 | } |
| 5897 | } |
| 5898 | |
| 5899 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5900 | const std::vector<cricket::Candidate>& candidates) { |
| 5901 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5902 | // Sanity check. |
| 5903 | for (const cricket::Candidate& candidate : candidates) { |
| 5904 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5905 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5906 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5907 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5908 | return; |
| 5909 | } |
| 5910 | } |
| 5911 | |
| 5912 | if (local_description()) { |
| 5913 | mutable_local_description()->RemoveCandidates(candidates); |
| 5914 | } |
| 5915 | OnIceCandidatesRemoved(candidates); |
| 5916 | } |
| 5917 | |
| 5918 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5919 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 5920 | RTC_HISTOGRAM_ENUMERATION( |
| 5921 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 5922 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5923 | } |
| 5924 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5925 | void PeerConnection::EnableSending() { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 5926 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 5927 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5928 | if (channel && !channel->enabled()) { |
| 5929 | channel->Enable(true); |
| 5930 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5931 | } |
| 5932 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5933 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5934 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5935 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5936 | } |
| 5937 | |
| 5938 | // Returns the media index for a local ice candidate given the content name. |
| 5939 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5940 | const std::string& content_name, |
| 5941 | int* sdp_mline_index) { |
| 5942 | if (!local_description() || !sdp_mline_index) { |
| 5943 | return false; |
| 5944 | } |
| 5945 | |
| 5946 | bool content_found = false; |
| 5947 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5948 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5949 | if (contents[index].name == content_name) { |
| 5950 | *sdp_mline_index = static_cast<int>(index); |
| 5951 | content_found = true; |
| 5952 | break; |
| 5953 | } |
| 5954 | } |
| 5955 | return content_found; |
| 5956 | } |
| 5957 | |
| 5958 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5959 | const SessionDescriptionInterface* remote_desc) { |
| 5960 | if (!remote_desc) { |
| 5961 | return true; |
| 5962 | } |
| 5963 | bool ret = true; |
| 5964 | |
| 5965 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5966 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5967 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5968 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5969 | bool valid = false; |
| 5970 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5971 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5972 | RTC_LOG(LS_INFO) |
| 5973 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5974 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5975 | } |
| 5976 | continue; |
| 5977 | } |
| 5978 | ret = UseCandidate(candidate); |
| 5979 | if (!ret) { |
| 5980 | break; |
| 5981 | } |
| 5982 | } |
| 5983 | } |
| 5984 | return ret; |
| 5985 | } |
| 5986 | |
| 5987 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5988 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5989 | size_t remote_content_size = |
| 5990 | remote_description()->description()->contents().size(); |
| 5991 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5992 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5993 | return false; |
| 5994 | } |
| 5995 | |
| 5996 | cricket::ContentInfo content = |
| 5997 | remote_description()->description()->contents()[mediacontent_index]; |
| 5998 | std::vector<cricket::Candidate> candidates; |
| 5999 | candidates.push_back(candidate->candidate()); |
| 6000 | // Invoking BaseSession method to handle remote candidates. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6001 | RTCError error = |
| 6002 | transport_controller_->AddRemoteCandidates(content.name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 +0000 | [diff] [blame] | 6003 | if (error.ok()) { |
| 6004 | // Candidates successfully submitted for checking. |
| 6005 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 6006 | ice_connection_state_ == |
| 6007 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 6008 | // If state is New, then the session has just gotten its first remote ICE |
| 6009 | // candidates, so go to Checking. |
| 6010 | // If state is Disconnected, the session is re-using old candidates or |
| 6011 | // receiving additional ones, so go to Checking. |
| 6012 | // If state is Connected, stay Connected. |
| 6013 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 6014 | // newly added transport, then the state actually _should_ move to |
| 6015 | // checking. Add a way to distinguish that case. |
| 6016 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 6017 | } |
| 6018 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 6019 | } else { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6020 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6021 | } |
| 6022 | return true; |
| 6023 | } |
| 6024 | |
| 6025 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6026 | // Destroy video channel first since it may have a pointer to the |
| 6027 | // voice channel. |
| 6028 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6029 | if (!video_info || video_info->rejected) { |
| 6030 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6031 | } |
| 6032 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6033 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 6034 | if (!audio_info || audio_info->rejected) { |
| 6035 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6036 | } |
| 6037 | |
| 6038 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 6039 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6040 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6041 | } |
| 6042 | } |
| 6043 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6044 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 6045 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6046 | const cricket::ContentGroup* bundle_group = nullptr; |
| 6047 | if (configuration_.bundle_policy == |
| 6048 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6049 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6050 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6051 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6052 | "max-bundle configured but session description " |
| 6053 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6054 | } |
| 6055 | } |
Mirko Bonadei | 9f3a44f | 2019-01-30 13:47:42 +0100 | [diff] [blame] | 6056 | return bundle_group; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6057 | } |
| 6058 | |
| 6059 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6060 | // Creating the media channels. Transports should already have been created |
| 6061 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6062 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6063 | if (voice && !voice->rejected && |
| 6064 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6065 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6066 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6067 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6068 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6069 | } |
| 6070 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 6071 | } |
| 6072 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6073 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6074 | if (video && !video->rejected && |
| 6075 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6076 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6077 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6078 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6079 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6080 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6081 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6082 | } |
| 6083 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 6084 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6085 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6086 | !rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6087 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6088 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6089 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6090 | } |
| 6091 | } |
| 6092 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6093 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6094 | } |
| 6095 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6096 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6097 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6098 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6099 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Bjorn Mellem | a9bbd86 | 2018-11-02 09:07:48 -0700 | [diff] [blame] | 6100 | MediaTransportInterface* media_transport = nullptr; |
| 6101 | if (configuration_.use_media_transport) { |
| 6102 | media_transport = GetMediaTransport(mid); |
| 6103 | } |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6104 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6105 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6106 | call_.get(), configuration_.media_config, rtp_transport, media_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 6107 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 6108 | &ssrc_generator_, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6109 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6110 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6111 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6112 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6113 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6114 | voice_channel->SignalSentPacket.connect(this, |
| 6115 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6116 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6117 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6118 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6119 | } |
| 6120 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6121 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6122 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6123 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6124 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 6125 | MediaTransportInterface* media_transport = nullptr; |
| 6126 | if (configuration_.use_media_transport) { |
| 6127 | media_transport = GetMediaTransport(mid); |
| 6128 | } |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 6129 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6130 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 6131 | call_.get(), configuration_.media_config, rtp_transport, media_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 6132 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 6133 | &ssrc_generator_, video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6134 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6135 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6136 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6137 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6138 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6139 | video_channel->SignalSentPacket.connect(this, |
| 6140 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6141 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 6142 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 6143 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6144 | } |
| 6145 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6146 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6147 | switch (data_channel_type_) { |
| 6148 | case cricket::DCT_MEDIA_TRANSPORT: |
| 6149 | if (network_thread()->Invoke<bool>( |
| 6150 | RTC_FROM_HERE, |
| 6151 | rtc::Bind(&PeerConnection::SetupMediaTransportForDataChannels_n, |
| 6152 | this, mid))) { |
| 6153 | for (const auto& channel : sctp_data_channels_) { |
| 6154 | channel->OnTransportChannelCreated(); |
| 6155 | } |
| 6156 | return true; |
| 6157 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6158 | return false; |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6159 | case cricket::DCT_SCTP: |
| 6160 | if (!sctp_factory_) { |
| 6161 | RTC_LOG(LS_ERROR) |
| 6162 | << "Trying to create SCTP transport, but didn't compile with " |
| 6163 | "SCTP support (HAVE_SCTP)"; |
| 6164 | return false; |
| 6165 | } |
| 6166 | if (!network_thread()->Invoke<bool>( |
| 6167 | RTC_FROM_HERE, |
| 6168 | rtc::Bind(&PeerConnection::CreateSctpTransport_n, this, mid))) { |
| 6169 | return false; |
| 6170 | } |
| 6171 | for (const auto& channel : sctp_data_channels_) { |
| 6172 | channel->OnTransportChannelCreated(); |
| 6173 | } |
| 6174 | return true; |
| 6175 | case cricket::DCT_RTP: |
| 6176 | default: |
| 6177 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 6178 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 6179 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 6180 | SrtpRequired(), GetCryptoOptions(), &ssrc_generator_); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6181 | if (!rtp_data_channel_) { |
| 6182 | return false; |
| 6183 | } |
| 6184 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 6185 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 6186 | rtp_data_channel_->SignalSentPacket.connect( |
| 6187 | this, &PeerConnection::OnSentPacket_w); |
| 6188 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
| 6189 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6190 | } |
| 6191 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6192 | return true; |
| 6193 | } |
| 6194 | |
| 6195 | Call::Stats PeerConnection::GetCallStats() { |
| 6196 | if (!worker_thread()->IsCurrent()) { |
| 6197 | return worker_thread()->Invoke<Call::Stats>( |
| 6198 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 6199 | } |
| 6200 | if (call_) { |
| 6201 | return call_->GetStats(); |
| 6202 | } else { |
| 6203 | return Call::Stats(); |
| 6204 | } |
| 6205 | } |
| 6206 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6207 | bool PeerConnection::CreateSctpTransport_n(const std::string& mid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6208 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6209 | RTC_DCHECK(sctp_factory_); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6210 | cricket::DtlsTransportInternal* dtls_transport = |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6211 | transport_controller_->GetDtlsTransport(mid); |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6212 | RTC_DCHECK(dtls_transport); |
| 6213 | sctp_transport_ = sctp_factory_->CreateSctpTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6214 | RTC_DCHECK(sctp_transport_); |
| 6215 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 6216 | sctp_transport_->SignalReadyToSendData.connect( |
| 6217 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 6218 | sctp_transport_->SignalDataReceived.connect( |
| 6219 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6220 | // TODO(deadbeef): All we do here is AsyncInvoke to fire the signal on |
| 6221 | // another thread. Would be nice if there was a helper class similar to |
| 6222 | // sigslot::repeater that did this for us, eliminating a bunch of boilerplate |
| 6223 | // code. |
| 6224 | sctp_transport_->SignalClosingProcedureStartedRemotely.connect( |
| 6225 | this, &PeerConnection::OnSctpClosingProcedureStartedRemotely_n); |
| 6226 | sctp_transport_->SignalClosingProcedureComplete.connect( |
| 6227 | this, &PeerConnection::OnSctpClosingProcedureComplete_n); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6228 | sctp_mid_ = mid; |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6229 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6230 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6231 | } |
| 6232 | |
| 6233 | void PeerConnection::DestroySctpTransport_n() { |
| 6234 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6235 | sctp_transport_.reset(nullptr); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6236 | sctp_mid_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6237 | sctp_invoker_.reset(nullptr); |
| 6238 | sctp_ready_to_send_data_ = false; |
| 6239 | } |
| 6240 | |
| 6241 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 6242 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6243 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6244 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 6245 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 6246 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 6247 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 6248 | // pending tasks will be cleared. |
| 6249 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 6250 | OnSctpTransportReadyToSendData_s(true); |
| 6251 | }); |
| 6252 | } |
| 6253 | |
| 6254 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 6255 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 6256 | sctp_ready_to_send_data_ = ready; |
| 6257 | SignalSctpReadyToSendData(ready); |
| 6258 | } |
| 6259 | |
| 6260 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 6261 | const cricket::ReceiveDataParams& params, |
| 6262 | const rtc::CopyOnWriteBuffer& payload) { |
| 6263 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6264 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6265 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 6266 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 6267 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 6268 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 6269 | // pending tasks will be cleared. |
| 6270 | sctp_invoker_->AsyncInvoke<void>( |
| 6271 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 6272 | OnSctpTransportDataReceived_s(params, payload); |
| 6273 | }); |
| 6274 | } |
| 6275 | |
| 6276 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 6277 | const cricket::ReceiveDataParams& params, |
| 6278 | const rtc::CopyOnWriteBuffer& payload) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6279 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6280 | if (!HandleOpenMessage_s(params, payload)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6281 | SignalSctpDataReceived(params, payload); |
| 6282 | } |
| 6283 | } |
| 6284 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6285 | void PeerConnection::OnSctpClosingProcedureStartedRemotely_n(int sid) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6286 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6287 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6288 | sctp_invoker_->AsyncInvoke<void>( |
| 6289 | RTC_FROM_HERE, signaling_thread(), |
| 6290 | rtc::Bind(&sigslot::signal1<int>::operator(), |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 6291 | &SignalSctpClosingProcedureStartedRemotely, sid)); |
| 6292 | } |
| 6293 | |
| 6294 | void PeerConnection::OnSctpClosingProcedureComplete_n(int sid) { |
| 6295 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 6296 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 6297 | sctp_invoker_->AsyncInvoke<void>( |
| 6298 | RTC_FROM_HERE, signaling_thread(), |
| 6299 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 6300 | &SignalSctpClosingProcedureComplete, sid)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6301 | } |
| 6302 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6303 | bool PeerConnection::SetupMediaTransportForDataChannels_n( |
| 6304 | const std::string& mid) { |
| 6305 | media_transport_ = transport_controller_->GetMediaTransport(mid); |
| 6306 | if (!media_transport_) { |
| 6307 | RTC_LOG(LS_ERROR) << "Media transport is not available for data channels"; |
| 6308 | return false; |
| 6309 | } |
| 6310 | |
| 6311 | media_transport_invoker_ = absl::make_unique<rtc::AsyncInvoker>(); |
| 6312 | media_transport_->SetDataSink(this); |
| 6313 | media_transport_data_mid_ = mid; |
| 6314 | transport_controller_->SignalMediaTransportStateChanged.connect( |
| 6315 | this, &PeerConnection::OnMediaTransportStateChanged_n); |
| 6316 | // Check the initial state right away, in case transport is already writable. |
| 6317 | OnMediaTransportStateChanged_n(); |
| 6318 | return true; |
| 6319 | } |
| 6320 | |
| 6321 | void PeerConnection::TeardownMediaTransportForDataChannels_n() { |
| 6322 | if (!media_transport_) { |
| 6323 | return; |
| 6324 | } |
| 6325 | transport_controller_->SignalMediaTransportStateChanged.disconnect(this); |
| 6326 | media_transport_data_mid_.reset(); |
| 6327 | media_transport_->SetDataSink(nullptr); |
| 6328 | media_transport_invoker_ = nullptr; |
| 6329 | media_transport_ = nullptr; |
| 6330 | } |
| 6331 | |
| 6332 | void PeerConnection::OnMediaTransportStateChanged_n() { |
| 6333 | if (!media_transport_data_mid_ || |
| 6334 | transport_controller_->GetMediaTransportState( |
| 6335 | *media_transport_data_mid_) != MediaTransportState::kWritable) { |
| 6336 | return; |
| 6337 | } |
| 6338 | media_transport_invoker_->AsyncInvoke<void>( |
| 6339 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 6340 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6341 | media_transport_ready_to_send_data_ = true; |
| 6342 | SignalMediaTransportWritable_s(media_transport_ready_to_send_data_); |
| 6343 | }); |
| 6344 | } |
| 6345 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6346 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 6347 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 6348 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 6349 | if (!bundle_enabled) |
| 6350 | return true; |
| 6351 | |
| 6352 | const cricket::ContentGroup* bundle_group = |
| 6353 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 6354 | RTC_DCHECK(bundle_group != NULL); |
| 6355 | |
| 6356 | const cricket::ContentInfos& contents = desc->contents(); |
| 6357 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 6358 | citer != contents.end(); ++citer) { |
| 6359 | const cricket::ContentInfo* content = (&*citer); |
| 6360 | RTC_DCHECK(content != NULL); |
| 6361 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 6362 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6363 | if (!HasRtcpMuxEnabled(content)) |
| 6364 | return false; |
| 6365 | } |
| 6366 | } |
| 6367 | // RTCP-MUX is enabled in all the contents. |
| 6368 | return true; |
| 6369 | } |
| 6370 | |
| 6371 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 6372 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6373 | } |
| 6374 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6375 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 6376 | std::set<std::string> mids; |
| 6377 | for (const cricket::ContentInfo& content : description.contents()) { |
Steve Anton | ceac015 | 2018-12-19 11:32:20 -0800 | [diff] [blame] | 6378 | if (content.name.empty()) { |
| 6379 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6380 | "A media section is missing a MID attribute."); |
| 6381 | } |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6382 | if (!mids.insert(content.name).second) { |
| 6383 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6384 | "Duplicate a=mid value '" + content.name + "'."); |
| 6385 | } |
| 6386 | } |
| 6387 | return RTCError::OK(); |
| 6388 | } |
| 6389 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6390 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6391 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6392 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6393 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6394 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6395 | } |
| 6396 | |
| 6397 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6398 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6399 | } |
| 6400 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6401 | SdpType type = sdesc->GetType(); |
| 6402 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 6403 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6404 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 6405 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6406 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6407 | } |
| 6408 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 6409 | RTCError error = ValidateMids(*sdesc->description()); |
| 6410 | if (!error.ok()) { |
| 6411 | return error; |
| 6412 | } |
| 6413 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6414 | // Verify crypto settings. |
| 6415 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6416 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 6417 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6418 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6419 | if (!crypto_error.ok()) { |
| 6420 | return crypto_error; |
| 6421 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6422 | } |
| 6423 | |
| 6424 | // Verify ice-ufrag and ice-pwd. |
| 6425 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6426 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6427 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6431 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6432 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6433 | } |
| 6434 | |
| 6435 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 6436 | // m-lines that do not rtcp-mux enabled. |
| 6437 | |
| 6438 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6439 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6440 | // With an answer we want to compare the new answer session description with |
| 6441 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6442 | const cricket::SessionDescription* offer_desc = |
| 6443 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 6444 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6445 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 6446 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 6447 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6448 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6449 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6450 | } |
| 6451 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6452 | // The re-offers should respect the order of m= sections in current |
| 6453 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6454 | // With a re-offer, either the current local or current remote descriptions |
| 6455 | // could be the most up to date, so we would like to check against both of |
| 6456 | // them if they exist. It could be the case that one of them has a 0 port |
| 6457 | // for a media section, but the other does not. This is important to check |
| 6458 | // against in the case that we are recycling an m= section. |
| 6459 | const cricket::SessionDescription* current_desc = nullptr; |
| 6460 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 6461 | if (local_description()) { |
| 6462 | current_desc = local_description()->description(); |
| 6463 | if (remote_description()) { |
| 6464 | secondary_current_desc = remote_description()->description(); |
| 6465 | } |
| 6466 | } else if (remote_description()) { |
| 6467 | current_desc = remote_description()->description(); |
| 6468 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6469 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 6470 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 6471 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6472 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6473 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6474 | } |
| 6475 | } |
| 6476 | |
Steve Anton | ba42e99 | 2018-04-09 14:10:01 -0700 | [diff] [blame] | 6477 | if (IsUnifiedPlan()) { |
| 6478 | // Ensure that each audio and video media section has at most one |
| 6479 | // "StreamParams". This will return an error if receiving a session |
| 6480 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 6481 | // same type. With Unified Plan, there can only be at most one track per |
| 6482 | // media section. |
| 6483 | for (const ContentInfo& content : sdesc->description()->contents()) { |
| 6484 | const MediaContentDescription& desc = *content.description; |
| 6485 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 6486 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 6487 | desc.streams().size() > 1u) { |
| 6488 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6489 | "Media section has more than one track specified " |
| 6490 | "with a=ssrc lines which is not supported with " |
| 6491 | "Unified Plan."); |
| 6492 | } |
| 6493 | } |
| 6494 | } |
| 6495 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 6496 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6497 | } |
| 6498 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6499 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6500 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6501 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6502 | return (state == PeerConnectionInterface::kStable) || |
| 6503 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6504 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6505 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6506 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 6507 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 6508 | } |
| 6509 | } |
| 6510 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6511 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6512 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6513 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6514 | return (state == PeerConnectionInterface::kStable) || |
| 6515 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 6516 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 6517 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6518 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 6519 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6520 | } |
| 6521 | } |
| 6522 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6523 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 6524 | switch (error) { |
| 6525 | case SessionError::kNone: |
| 6526 | return "ERROR_NONE"; |
| 6527 | case SessionError::kContent: |
| 6528 | return "ERROR_CONTENT"; |
| 6529 | case SessionError::kTransport: |
| 6530 | return "ERROR_TRANSPORT"; |
| 6531 | } |
| 6532 | RTC_NOTREACHED(); |
| 6533 | return ""; |
| 6534 | } |
| 6535 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6536 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 6537 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 6538 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 6539 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 6540 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6541 | } |
| 6542 | |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6543 | void PeerConnection::ReportSdpFormatReceived( |
| 6544 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6545 | int num_audio_mlines = 0; |
| 6546 | int num_video_mlines = 0; |
| 6547 | int num_audio_tracks = 0; |
| 6548 | int num_video_tracks = 0; |
| 6549 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 6550 | cricket::MediaType media_type = content.media_description()->type(); |
| 6551 | int num_tracks = std::max( |
| 6552 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 6553 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 6554 | num_audio_mlines += 1; |
| 6555 | num_audio_tracks += num_tracks; |
| 6556 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 6557 | num_video_mlines += 1; |
| 6558 | num_video_tracks += num_tracks; |
| 6559 | } |
| 6560 | } |
| 6561 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 6562 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 6563 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 6564 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 6565 | format = kSdpFormatReceivedComplexPlanB; |
| 6566 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 6567 | format = kSdpFormatReceivedSimple; |
| 6568 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6569 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 6570 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 10:55:04 -0800 | [diff] [blame] | 6571 | } |
| 6572 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6573 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 6574 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6575 | usage_event_accumulator_ |= static_cast<int>(event); |
| 6576 | } |
| 6577 | |
| 6578 | void PeerConnection::ReportUsagePattern() const { |
| 6579 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6580 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 6581 | usage_event_accumulator_, |
| 6582 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6583 | const int bad_bits = |
| 6584 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED) | |
| 6585 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 6586 | const int good_bits = |
| 6587 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED) | |
| 6588 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 6589 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 6590 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 6591 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6592 | // If called after close(), we can't report, because observer may have |
| 6593 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 6594 | if (observer_) { |
| 6595 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 6596 | } else { |
| 6597 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 6598 | << usage_event_accumulator_ |
| 6599 | << " observed after observer shutdown"; |
| 6600 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame] | 6601 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 14:00:34 +0200 | [diff] [blame] | 6602 | } |
| 6603 | |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6604 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 6605 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6606 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6607 | switch (answer.description()->msid_signaling()) { |
| 6608 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6609 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6610 | break; |
| 6611 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6612 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6613 | break; |
| 6614 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6615 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6616 | break; |
| 6617 | case cricket::kMsidSignalingMediaSection | |
| 6618 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6619 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6620 | break; |
| 6621 | default: |
| 6622 | RTC_NOTREACHED(); |
| 6623 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6624 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 6625 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 10:31:30 -0800 | [diff] [blame] | 6626 | } |
| 6627 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6628 | // We need to check the local/remote description for the Transport instead of |
| 6629 | // the session, because a new Transport added during renegotiation may have |
| 6630 | // them unset while the session has them set from the previous negotiation. |
| 6631 | // Not doing so may trigger the auto generation of transport description and |
| 6632 | // mess up DTLS identity information, ICE credential, etc. |
| 6633 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 6634 | const IceCandidateInterface* candidate, |
| 6635 | const SessionDescriptionInterface* remote_desc, |
| 6636 | bool* valid) { |
| 6637 | *valid = true; |
| 6638 | |
| 6639 | const SessionDescriptionInterface* current_remote_desc = |
| 6640 | remote_desc ? remote_desc : remote_description(); |
| 6641 | |
| 6642 | if (!current_remote_desc) { |
| 6643 | return false; |
| 6644 | } |
| 6645 | |
| 6646 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 6647 | size_t remote_content_size = |
| 6648 | current_remote_desc->description()->contents().size(); |
| 6649 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 6650 | RTC_LOG(LS_ERROR) |
| 6651 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 6652 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6653 | |
| 6654 | *valid = false; |
| 6655 | return false; |
| 6656 | } |
| 6657 | |
| 6658 | cricket::ContentInfo content = |
| 6659 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 6660 | |
| 6661 | const std::string transport_name = GetTransportName(content.name); |
| 6662 | if (transport_name.empty()) { |
| 6663 | return false; |
| 6664 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6665 | return true; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6666 | } |
| 6667 | |
| 6668 | bool PeerConnection::SrtpRequired() const { |
| 6669 | return dtls_enabled_ || |
| 6670 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 6671 | } |
| 6672 | |
| 6673 | void PeerConnection::OnTransportControllerGatheringState( |
| 6674 | cricket::IceGatheringState state) { |
| 6675 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 6676 | if (state == cricket::kIceGatheringGathering) { |
| 6677 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 6678 | } else if (state == cricket::kIceGatheringComplete) { |
| 6679 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 6680 | } |
| 6681 | } |
| 6682 | |
| 6683 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6684 | std::map<std::string, std::set<cricket::MediaType>> |
| 6685 | media_types_by_transport_name; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 6686 | for (const auto& transceiver : transceivers_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6687 | if (transceiver->internal()->channel()) { |
| 6688 | const std::string& transport_name = |
| 6689 | transceiver->internal()->channel()->transport_name(); |
| 6690 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 6691 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6692 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6693 | } |
| 6694 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6695 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 6696 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6697 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6698 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 6699 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6700 | if (transport_name) { |
| 6701 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6702 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6703 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6704 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6705 | for (const auto& entry : media_types_by_transport_name) { |
| 6706 | const std::string& transport_name = entry.first; |
| 6707 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6708 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6709 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6710 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6711 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6712 | } |
| 6713 | } |
| 6714 | } |
| 6715 | // Walk through the ConnectionInfos to gather best connection usage |
| 6716 | // for IPv4 and IPv6. |
| 6717 | void PeerConnection::ReportBestConnectionState( |
| 6718 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6719 | for (const cricket::TransportChannelStats& channel_stats : |
| 6720 | stats.channel_stats) { |
| 6721 | for (const cricket::ConnectionInfo& connection_info : |
| 6722 | channel_stats.connection_infos) { |
| 6723 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6724 | continue; |
| 6725 | } |
| 6726 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6727 | const cricket::Candidate& local = connection_info.local_candidate; |
| 6728 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6729 | |
| 6730 | // Increment the counter for IceCandidatePairType. |
| 6731 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 6732 | (local.type() == RELAY_PORT_TYPE && |
| 6733 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6734 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 6735 | GetIceCandidatePairCounter(local, remote), |
| 6736 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6737 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6738 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 6739 | GetIceCandidatePairCounter(local, remote), |
| 6740 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6741 | } else { |
| 6742 | RTC_CHECK(0); |
| 6743 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6744 | |
| 6745 | // Increment the counter for IP type. |
| 6746 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6747 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6748 | kBestConnections_IPv4, |
| 6749 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6750 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6751 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6752 | kBestConnections_IPv6, |
| 6753 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6754 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 6755 | RTC_CHECK(!local.address().hostname().empty() && |
| 6756 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6757 | } |
| 6758 | |
| 6759 | return; |
| 6760 | } |
| 6761 | } |
| 6762 | } |
| 6763 | |
| 6764 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6765 | const cricket::TransportStats& stats, |
| 6766 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6767 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 6768 | return; |
| 6769 | } |
| 6770 | |
| 6771 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 6772 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 6773 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 6774 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6775 | return; |
| 6776 | } |
| 6777 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6778 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6779 | for (cricket::MediaType media_type : media_types) { |
| 6780 | switch (media_type) { |
| 6781 | case cricket::MEDIA_TYPE_AUDIO: |
| 6782 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6783 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 6784 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6785 | break; |
| 6786 | case cricket::MEDIA_TYPE_VIDEO: |
| 6787 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6788 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 6789 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6790 | break; |
| 6791 | case cricket::MEDIA_TYPE_DATA: |
| 6792 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6793 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 6794 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6795 | break; |
| 6796 | default: |
| 6797 | RTC_NOTREACHED(); |
| 6798 | continue; |
| 6799 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6800 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6804 | for (cricket::MediaType media_type : media_types) { |
| 6805 | switch (media_type) { |
| 6806 | case cricket::MEDIA_TYPE_AUDIO: |
| 6807 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6808 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 6809 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6810 | break; |
| 6811 | case cricket::MEDIA_TYPE_VIDEO: |
| 6812 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6813 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 6814 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6815 | break; |
| 6816 | case cricket::MEDIA_TYPE_DATA: |
| 6817 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6818 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 6819 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 6820 | break; |
| 6821 | default: |
| 6822 | RTC_NOTREACHED(); |
| 6823 | continue; |
| 6824 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 6825 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6826 | } |
| 6827 | } |
| 6828 | |
| 6829 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 6830 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 6831 | RTC_DCHECK(call_); |
| 6832 | call_->OnSentPacket(sent_packet); |
| 6833 | } |
| 6834 | |
| 6835 | const std::string PeerConnection::GetTransportName( |
| 6836 | const std::string& content_name) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6837 | cricket::ChannelInterface* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6838 | if (channel) { |
| 6839 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6840 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6841 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6842 | RTC_DCHECK(sctp_mid_); |
| 6843 | if (content_name == *sctp_mid_) { |
| 6844 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6845 | } |
| 6846 | } |
| 6847 | // Return an empty string if failed to retrieve the transport name. |
| 6848 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6849 | } |
| 6850 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6851 | void PeerConnection::DestroyTransceiverChannel( |
| 6852 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 6853 | transceiver) { |
| 6854 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6855 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6856 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6857 | if (channel) { |
| 6858 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6859 | DestroyChannelInterface(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6860 | } |
| 6861 | } |
| 6862 | |
| 6863 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6864 | if (rtp_data_channel_) { |
| 6865 | OnDataChannelDestroyed(); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6866 | DestroyChannelInterface(rtp_data_channel_); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6867 | rtp_data_channel_ = nullptr; |
| 6868 | } |
| 6869 | |
| 6870 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 6871 | // grab a reference to this PeerConnection. If this is called from the |
| 6872 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 6873 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 6874 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 6875 | |
| 6876 | if (sctp_transport_) { |
| 6877 | OnDataChannelDestroyed(); |
| 6878 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 6879 | [this] { DestroySctpTransport_n(); }); |
| 6880 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 6881 | |
| 6882 | if (media_transport_) { |
| 6883 | OnDataChannelDestroyed(); |
| 6884 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 6885 | RTC_DCHECK_RUN_ON(network_thread()); |
| 6886 | TeardownMediaTransportForDataChannels_n(); |
| 6887 | }); |
| 6888 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6889 | } |
| 6890 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 6891 | void PeerConnection::DestroyChannelInterface( |
| 6892 | cricket::ChannelInterface* channel) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6893 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6894 | switch (channel->media_type()) { |
| 6895 | case cricket::MEDIA_TYPE_AUDIO: |
| 6896 | channel_manager()->DestroyVoiceChannel( |
| 6897 | static_cast<cricket::VoiceChannel*>(channel)); |
| 6898 | break; |
| 6899 | case cricket::MEDIA_TYPE_VIDEO: |
| 6900 | channel_manager()->DestroyVideoChannel( |
| 6901 | static_cast<cricket::VideoChannel*>(channel)); |
| 6902 | break; |
| 6903 | case cricket::MEDIA_TYPE_DATA: |
| 6904 | channel_manager()->DestroyRtpDataChannel( |
| 6905 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 6906 | break; |
| 6907 | default: |
| 6908 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 6909 | break; |
| 6910 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6911 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 6912 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6913 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6914 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6915 | RtpTransportInternal* rtp_transport, |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 6916 | cricket::DtlsTransportInternal* dtls_transport, |
| 6917 | MediaTransportInterface* media_transport) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6918 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6919 | auto base_channel = GetChannel(mid); |
| 6920 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6921 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 6922 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6923 | if (sctp_transport_ && mid == sctp_mid_) { |
Zhi Huang | 644fde4 | 2018-04-02 19:16:26 -0700 | [diff] [blame] | 6924 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6925 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 6926 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 6927 | if (configuration_.use_media_transport) { |
| 6928 | // Only pass media transport to call object if media transport is used |
| 6929 | // for media (and not data channel). |
| 6930 | call_->MediaTransportChange(media_transport); |
| 6931 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 6932 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 6933 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6934 | } |
| 6935 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6936 | PeerConnectionObserver* PeerConnection::Observer() const { |
| 6937 | // In earlier production code, the pointer was not cleared on close, |
| 6938 | // which might have led to undefined behavior if the observer was not |
| 6939 | // deallocated, or strange crashes if it was. |
| 6940 | // We use CHECK in order to catch such behavior if it exists. |
| 6941 | // TODO(hta): Remove or replace with DCHECK if nothing is found. |
| 6942 | RTC_CHECK(observer_); |
| 6943 | return observer_; |
| 6944 | } |
| 6945 | |
Benjamin Wright | 8c27cca | 2018-10-25 10:16:44 -0700 | [diff] [blame] | 6946 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 6947 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 6948 | // after it has been removed. |
| 6949 | return configuration_.crypto_options.has_value() |
| 6950 | ? *configuration_.crypto_options |
| 6951 | : factory_->options().crypto_options; |
| 6952 | } |
| 6953 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 6954 | void PeerConnection::ClearStatsCache() { |
| 6955 | if (stats_collector_) { |
| 6956 | stats_collector_->ClearCachedStatsReport(); |
| 6957 | } |
| 6958 | } |
| 6959 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6960 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 +0000 | [diff] [blame] | 6961 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 6962 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 10:50:16 +0200 | [diff] [blame] | 6963 | } |
| 6964 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6965 | } // namespace webrtc |