henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [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 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/peer_connection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 13 | #include <algorithm> |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 14 | #include <limits> |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 15 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 16 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 11:04:15 | [diff] [blame] | 17 | #include <utility> |
| 18 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 19 | |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 20 | #include "absl/algorithm/container.h" |
Karl Wiberg | 918f50c | 2018-07-05 09:40:33 | [diff] [blame] | 21 | #include "absl/memory/memory.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 20:02:22 | [diff] [blame] | 22 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [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" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 27 | #include "api/rtc_error.h" |
Niels Möller | d8b9ed7 | 2019-05-08 11:53:51 | [diff] [blame^] | 28 | #include "api/rtc_event_log_output_file.h" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 29 | #include "api/rtp_parameters.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 30 | #include "api/uma_metrics.h" |
Jonas Oreland | a3aa9bd | 2019-04-17 05:38:40 | [diff] [blame] | 31 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 32 | #include "call/call.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 33 | #include "logging/rtc_event_log/ice_logger.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 34 | #include "logging/rtc_event_log/rtc_event_log.h" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 35 | #include "media/base/rid_description.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 36 | #include "media/sctp/sctp_transport.h" |
Ruslan Burakov | 501bfba | 2019-02-11 09:29:19 | [diff] [blame] | 37 | #include "pc/audio_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 38 | #include "pc/audio_track.h" |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 39 | #include "pc/channel.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 40 | #include "pc/channel_manager.h" |
| 41 | #include "pc/dtmf_sender.h" |
| 42 | #include "pc/media_stream.h" |
| 43 | #include "pc/media_stream_observer.h" |
| 44 | #include "pc/remote_audio_source.h" |
| 45 | #include "pc/rtp_media_utils.h" |
| 46 | #include "pc/rtp_receiver.h" |
| 47 | #include "pc/rtp_sender.h" |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 48 | #include "pc/sctp_transport.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 49 | #include "pc/sctp_utils.h" |
| 50 | #include "pc/sdp_utils.h" |
| 51 | #include "pc/stream_collection.h" |
Ruslan Burakov | 501bfba | 2019-02-11 09:29:19 | [diff] [blame] | 52 | #include "pc/video_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 53 | #include "pc/video_track.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 54 | #include "rtc_base/bind.h" |
| 55 | #include "rtc_base/checks.h" |
| 56 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 09:42:26 | [diff] [blame] | 57 | #include "rtc_base/numerics/safe_conversions.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 58 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 59 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 60 | #include "rtc_base/trace_event.h" |
| 61 | #include "system_wrappers/include/clock.h" |
| 62 | #include "system_wrappers/include/field_trial.h" |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 63 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 64 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 65 | using cricket::ContentInfo; |
| 66 | using cricket::ContentInfos; |
| 67 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 68 | using cricket::MediaProtocolType; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 69 | using cricket::RidDescription; |
| 70 | using cricket::RidDirection; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 71 | using cricket::SessionDescription; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 72 | using cricket::SimulcastDescription; |
| 73 | using cricket::SimulcastLayer; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 74 | using cricket::SimulcastLayerList; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 75 | using cricket::StreamParams; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 76 | using cricket::TransportInfo; |
| 77 | |
| 78 | using cricket::LOCAL_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 79 | using cricket::PRFLX_PORT_TYPE; |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 80 | using cricket::RELAY_PORT_TYPE; |
| 81 | using cricket::STUN_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 82 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 83 | namespace webrtc { |
| 84 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 85 | // Error messages |
| 86 | const char kBundleWithoutRtcpMux[] = |
| 87 | "rtcp-mux must be enabled when BUNDLE " |
| 88 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 89 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 90 | const char kInvalidSdp[] = "Invalid session description."; |
| 91 | const char kMlineMismatchInAnswer[] = |
| 92 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 93 | "answer."; |
| 94 | const char kMlineMismatchInSubsequentOffer[] = |
| 95 | "The order of m-lines in subsequent offer doesn't match order from " |
| 96 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 97 | const char kSdpWithoutDtlsFingerprint[] = |
| 98 | "Called with SDP without DTLS fingerprint."; |
| 99 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 100 | const char kSdpWithoutIceUfragPwd[] = |
| 101 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 102 | const char kSessionError[] = "Session error code: "; |
| 103 | const char kSessionErrorDesc[] = "Session error description: "; |
| 104 | const char kDtlsSrtpSetupFailureRtp[] = |
| 105 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 106 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 107 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 108 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 109 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 110 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 111 | // UMA metric names. |
| 112 | const char kSimulcastVersionApplyLocalDescription[] = |
| 113 | "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription"; |
| 114 | const char kSimulcastVersionApplyRemoteDescription[] = |
| 115 | "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription"; |
| 116 | const char kSimulcastNumberOfEncodings[] = |
| 117 | "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings"; |
| 118 | const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled"; |
| 119 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 120 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 121 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 122 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 123 | |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 124 | // The length of RTCP CNAMEs. |
| 125 | static const int kRtcpCnameLength = 16; |
| 126 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 127 | enum { |
| 128 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 129 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 130 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 131 | MSG_GETSTATS, |
| 132 | MSG_FREE_DATACHANNELS, |
| 133 | MSG_REPORT_USAGE_PATTERN, |
| 134 | }; |
| 135 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 136 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 137 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 138 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 139 | explicit SetSessionDescriptionMsg( |
| 140 | webrtc::SetSessionDescriptionObserver* observer) |
| 141 | : observer(observer) {} |
| 142 | |
| 143 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 144 | RTCError error; |
| 145 | }; |
| 146 | |
| 147 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 148 | explicit CreateSessionDescriptionMsg( |
| 149 | webrtc::CreateSessionDescriptionObserver* observer) |
| 150 | : observer(observer) {} |
| 151 | |
| 152 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 153 | RTCError error; |
| 154 | }; |
| 155 | |
| 156 | struct GetStatsMsg : public rtc::MessageData { |
| 157 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 158 | webrtc::MediaStreamTrackInterface* track) |
| 159 | : observer(observer), track(track) {} |
| 160 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 161 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 162 | }; |
| 163 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 164 | // Check if we can send |new_stream| on a PeerConnection. |
| 165 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 166 | webrtc::MediaStreamInterface* new_stream) { |
| 167 | if (!new_stream || !current_streams) { |
| 168 | return false; |
| 169 | } |
Seth Hampson | 13b8bad | 2018-03-13 23:05:28 | [diff] [blame] | 170 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 171 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 172 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | return true; |
| 176 | } |
| 177 | |
deadbeef | 5e97fb5 | 2015-10-15 19:49:08 | [diff] [blame] | 178 | // If the direction is "recvonly" or "inactive", treat the description |
| 179 | // as containing no streams. |
| 180 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 181 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 182 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 22:57:10 | [diff] [blame] | 183 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 19:49:08 | [diff] [blame] | 184 | ? desc->streams() |
| 185 | : std::vector<cricket::StreamParams>(); |
| 186 | } |
| 187 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 188 | bool IsValidOfferToReceiveMedia(int value) { |
| 189 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 190 | return (value >= Options::kUndefined) && |
| 191 | (value <= Options::kMaxOfferToReceiveMedia); |
| 192 | } |
| 193 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 194 | // Add options to |[audio/video]_media_description_options| from |senders|. |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 195 | void AddPlanBRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 196 | const std::vector<rtc::scoped_refptr< |
| 197 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 198 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 08:58:37 | [diff] [blame] | 199 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 200 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 13:50:32 | [diff] [blame] | 201 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 202 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 203 | if (audio_media_description_options) { |
| 204 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 22:45:38 | [diff] [blame] | 205 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 206 | } |
| 207 | } else { |
| 208 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 209 | if (video_media_description_options) { |
| 210 | video_media_description_options->AddVideoSender( |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 211 | sender->id(), sender->internal()->stream_ids(), {}, |
| 212 | SimulcastLayerList(), num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 213 | } |
| 214 | } |
zhihuang | a77e6bb | 2017-08-15 01:17:48 | [diff] [blame] | 215 | } |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 216 | } |
olka | 3c74766 | 2017-08-17 13:50:32 | [diff] [blame] | 217 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 218 | // Add options to |session_options| from |rtp_data_channels|. |
| 219 | void AddRtpDataChannelOptions( |
| 220 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 221 | rtp_data_channels, |
| 222 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 223 | if (!data_media_description_options) { |
| 224 | return; |
| 225 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 226 | // Check for data channels. |
| 227 | for (const auto& kv : rtp_data_channels) { |
| 228 | const DataChannel* channel = kv.second; |
| 229 | if (channel->state() == DataChannel::kConnecting || |
| 230 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 231 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 232 | // the data channel's label. |
| 233 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 234 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 239 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 240 | PeerConnectionInterface::IceTransportsType type) { |
| 241 | switch (type) { |
| 242 | case PeerConnectionInterface::kNone: |
| 243 | return cricket::CF_NONE; |
| 244 | case PeerConnectionInterface::kRelay: |
| 245 | return cricket::CF_RELAY; |
| 246 | case PeerConnectionInterface::kNoHost: |
| 247 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 248 | case PeerConnectionInterface::kAll: |
| 249 | return cricket::CF_ALL; |
| 250 | default: |
nisse | c80e741 | 2017-01-11 13:56:46 | [diff] [blame] | 251 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 252 | } |
| 253 | return cricket::CF_NONE; |
| 254 | } |
| 255 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 256 | // Helper to set an error and return from a method. |
| 257 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 258 | if (error) { |
| 259 | error->set_type(type); |
| 260 | } |
| 261 | return type == webrtc::RTCErrorType::NONE; |
| 262 | } |
| 263 | |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 264 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
Steve Anton | f820a5e | 2018-08-10 17:22:52 | [diff] [blame] | 265 | bool ok = error.ok(); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 266 | if (error_out) { |
| 267 | *error_out = std::move(error); |
| 268 | } |
Steve Anton | f820a5e | 2018-08-10 17:22:52 | [diff] [blame] | 269 | return ok; |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 270 | } |
| 271 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 272 | std::string GetSignalingStateString( |
| 273 | PeerConnectionInterface::SignalingState state) { |
| 274 | switch (state) { |
| 275 | case PeerConnectionInterface::kStable: |
| 276 | return "kStable"; |
| 277 | case PeerConnectionInterface::kHaveLocalOffer: |
| 278 | return "kHaveLocalOffer"; |
| 279 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 280 | return "kHavePrAnswer"; |
| 281 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 282 | return "kHaveRemoteOffer"; |
| 283 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 284 | return "kHaveRemotePrAnswer"; |
| 285 | case PeerConnectionInterface::kClosed: |
| 286 | return "kClosed"; |
| 287 | } |
| 288 | RTC_NOTREACHED(); |
| 289 | return ""; |
| 290 | } |
deadbeef | 0a6c4ca | 2015-10-06 18:38:28 | [diff] [blame] | 291 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 292 | IceCandidatePairType GetIceCandidatePairCounter( |
| 293 | const cricket::Candidate& local, |
| 294 | const cricket::Candidate& remote) { |
| 295 | const auto& l = local.type(); |
| 296 | const auto& r = remote.type(); |
| 297 | const auto& host = LOCAL_PORT_TYPE; |
| 298 | const auto& srflx = STUN_PORT_TYPE; |
| 299 | const auto& relay = RELAY_PORT_TYPE; |
| 300 | const auto& prflx = PRFLX_PORT_TYPE; |
| 301 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 302 | bool local_hostname = |
| 303 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 304 | bool remote_hostname = !remote.address().hostname().empty() && |
| 305 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 306 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 307 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 308 | if (local_hostname) { |
| 309 | if (remote_hostname) { |
| 310 | return kIceCandidatePairHostNameHostName; |
| 311 | } else if (remote_private) { |
| 312 | return kIceCandidatePairHostNameHostPrivate; |
| 313 | } else { |
| 314 | return kIceCandidatePairHostNameHostPublic; |
| 315 | } |
| 316 | } else if (local_private) { |
| 317 | if (remote_hostname) { |
| 318 | return kIceCandidatePairHostPrivateHostName; |
| 319 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 320 | return kIceCandidatePairHostPrivateHostPrivate; |
| 321 | } else { |
| 322 | return kIceCandidatePairHostPrivateHostPublic; |
| 323 | } |
| 324 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 325 | if (remote_hostname) { |
| 326 | return kIceCandidatePairHostPublicHostName; |
| 327 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 328 | return kIceCandidatePairHostPublicHostPrivate; |
| 329 | } else { |
| 330 | return kIceCandidatePairHostPublicHostPublic; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | if (l == host && r == srflx) |
| 335 | return kIceCandidatePairHostSrflx; |
| 336 | if (l == host && r == relay) |
| 337 | return kIceCandidatePairHostRelay; |
| 338 | if (l == host && r == prflx) |
| 339 | return kIceCandidatePairHostPrflx; |
| 340 | if (l == srflx && r == host) |
| 341 | return kIceCandidatePairSrflxHost; |
| 342 | if (l == srflx && r == srflx) |
| 343 | return kIceCandidatePairSrflxSrflx; |
| 344 | if (l == srflx && r == relay) |
| 345 | return kIceCandidatePairSrflxRelay; |
| 346 | if (l == srflx && r == prflx) |
| 347 | return kIceCandidatePairSrflxPrflx; |
| 348 | if (l == relay && r == host) |
| 349 | return kIceCandidatePairRelayHost; |
| 350 | if (l == relay && r == srflx) |
| 351 | return kIceCandidatePairRelaySrflx; |
| 352 | if (l == relay && r == relay) |
| 353 | return kIceCandidatePairRelayRelay; |
| 354 | if (l == relay && r == prflx) |
| 355 | return kIceCandidatePairRelayPrflx; |
| 356 | if (l == prflx && r == host) |
| 357 | return kIceCandidatePairPrflxHost; |
| 358 | if (l == prflx && r == srflx) |
| 359 | return kIceCandidatePairPrflxSrflx; |
| 360 | if (l == prflx && r == relay) |
| 361 | return kIceCandidatePairPrflxRelay; |
| 362 | return kIceCandidatePairMax; |
| 363 | } |
| 364 | |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 365 | // Logic to decide if an m= section can be recycled. This means that the new |
| 366 | // m= section is not rejected, but the old local or remote m= section is |
| 367 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 368 | // of the old remote and old local descriptions in no particular order. |
| 369 | // We need to check both the old local and remote because either |
| 370 | // could be the most current from the latest negotation. |
| 371 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 372 | const ContentInfo& content, |
| 373 | const ContentInfo* old_content_one, |
| 374 | const ContentInfo* old_content_two) { |
| 375 | return type == SdpType::kOffer && !content.rejected && |
| 376 | ((old_content_one && old_content_one->rejected) || |
| 377 | (old_content_two && old_content_two->rejected)); |
| 378 | } |
| 379 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 380 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 381 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 382 | // less than |current_desc|. In the case of checking an answer's |
| 383 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 384 | // local or remote. In the case of checking an offer's |new_desc| we |
| 385 | // check against the local and remote descriptions stored from the last |
| 386 | // negotiation, because either of these could be the most up to date for |
| 387 | // possible rejected m sections. These are the |current_desc| and |
| 388 | // |secondary_current_desc|. |
| 389 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 390 | const SessionDescription* secondary_current_desc, |
| 391 | const SessionDescription& new_desc, |
| 392 | const SdpType type) { |
| 393 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 394 | return false; |
| 395 | } |
| 396 | |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 397 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 398 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 399 | if (secondary_current_desc && |
| 400 | i < secondary_current_desc->contents().size()) { |
| 401 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 402 | } |
| 403 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 404 | ¤t_desc.contents()[i], |
| 405 | secondary_content_info)) { |
| 406 | // For new offer descriptions, if the media section can be recycled, it's |
| 407 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 408 | continue; |
| 409 | } |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 410 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 414 | new_desc.contents()[i].media_description(); |
| 415 | const MediaContentDescription* current_desc_mdesc = |
| 416 | current_desc.contents()[i].media_description(); |
| 417 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 418 | return false; |
| 419 | } |
| 420 | } |
| 421 | return true; |
| 422 | } |
| 423 | |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 424 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 425 | const SessionDescription& desc2) { |
| 426 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 427 | } |
| 428 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 429 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 430 | cricket::MediaType media_type) { |
Mirko Bonadei | ab49982 | 2018-09-11 08:43:20 | [diff] [blame] | 431 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 432 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 433 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 434 | static constexpr struct { |
| 435 | KeyExchangeProtocolType protocol_type; |
| 436 | cricket::MediaType media_type; |
| 437 | KeyExchangeProtocolMedia protocol_media; |
| 438 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 439 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 440 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 441 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 442 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 443 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 444 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 445 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 446 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 447 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 448 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 449 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 450 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 451 | }; |
| 452 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 453 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 454 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 13:15:26 | [diff] [blame] | 455 | |
Mirko Bonadei | ab49982 | 2018-09-11 08:43:20 | [diff] [blame] | 456 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 457 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 458 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 459 | i.protocol_media, |
| 460 | kEnumCounterKeyProtocolMediaTypeMax); |
| 461 | } |
Harald Alvestrand | f9d0f1d | 2018-03-02 13:15:26 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 465 | void NoteAddIceCandidateResult(int result) { |
| 466 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 467 | kAddIceCandidateMax); |
| 468 | } |
| 469 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 470 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 471 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 472 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 473 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 474 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 475 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 476 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 477 | const cricket::ContentGroup* bundle = |
| 478 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 479 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 480 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 481 | continue; |
| 482 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 09:56:14 | [diff] [blame] | 483 | // Note what media is used with each crypto protocol, for all sections. |
| 484 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 485 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 486 | content_info.media_description()->type()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 487 | const std::string& mid = content_info.name; |
| 488 | if (bundle && bundle->HasContentName(mid) && |
| 489 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 490 | // This isn't the first media section in the BUNDLE group, so it's not |
| 491 | // required to have crypto attributes, since only the crypto attributes |
| 492 | // from the first section actually get used. |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | // If the content isn't rejected or bundled into another m= section, crypto |
| 497 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 498 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 499 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 500 | if (!media || !tinfo) { |
| 501 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 502 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 503 | } |
| 504 | if (dtls_enabled) { |
| 505 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 506 | RTC_LOG(LS_WARNING) |
| 507 | << "Session description must have DTLS fingerprint if " |
| 508 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 509 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 510 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 511 | } |
| 512 | } else { |
| 513 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 514 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 515 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 516 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 520 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 524 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 525 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 526 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 527 | const cricket::ContentGroup* bundle = |
| 528 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 529 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 530 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 531 | continue; |
| 532 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 533 | const std::string& mid = content_info.name; |
| 534 | if (bundle && bundle->HasContentName(mid) && |
| 535 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 536 | // This isn't the first media section in the BUNDLE group, so it's not |
| 537 | // required to have ufrag/password, since only the ufrag/password from |
| 538 | // the first section actually get used. |
| 539 | continue; |
| 540 | } |
| 541 | |
| 542 | // If the content isn't rejected or bundled into another m= section, |
| 543 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 544 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 545 | if (!tinfo) { |
| 546 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 547 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 548 | return false; |
| 549 | } |
| 550 | if (tinfo->description.ice_ufrag.empty() || |
| 551 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 552 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 553 | return false; |
| 554 | } |
| 555 | } |
| 556 | return true; |
| 557 | } |
| 558 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 559 | // Get the SCTP port out of a SessionDescription. |
| 560 | // Return -1 if not found. |
| 561 | int GetSctpPort(const SessionDescription* session_description) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 | [diff] [blame] | 562 | const cricket::DataContentDescription* data_desc = |
| 563 | GetFirstDataContentDescription(session_description); |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 564 | RTC_DCHECK(data_desc); |
| 565 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 566 | return -1; |
| 567 | } |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 | [diff] [blame] | 568 | std::string value; |
| 569 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 570 | cricket::kGoogleSctpDataCodecName); |
| 571 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
| 572 | if (!codec.Matches(match_pattern)) { |
| 573 | continue; |
| 574 | } |
| 575 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 576 | return rtc::FromString<int>(value); |
| 577 | } |
| 578 | } |
| 579 | return -1; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 580 | } |
| 581 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 582 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 583 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 584 | const SessionDescriptionInterface* new_desc, |
| 585 | const std::string& content_name) { |
| 586 | if (!old_desc) { |
| 587 | return false; |
| 588 | } |
| 589 | const SessionDescription* new_sd = new_desc->description(); |
| 590 | const SessionDescription* old_sd = old_desc->description(); |
| 591 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 592 | if (!cinfo || cinfo->rejected) { |
| 593 | return false; |
| 594 | } |
| 595 | // If the content isn't rejected, check if ufrag and password has changed. |
| 596 | const cricket::TransportDescription* new_transport_desc = |
| 597 | new_sd->GetTransportDescriptionByName(content_name); |
| 598 | const cricket::TransportDescription* old_transport_desc = |
| 599 | old_sd->GetTransportDescriptionByName(content_name); |
| 600 | if (!new_transport_desc || !old_transport_desc) { |
| 601 | // No transport description exists. This is not an ICE restart. |
| 602 | return false; |
| 603 | } |
| 604 | if (cricket::IceCredentialsChanged( |
| 605 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 606 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 607 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 608 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 609 | return true; |
| 610 | } |
| 611 | return false; |
| 612 | } |
| 613 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 614 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 615 | // from an RTCError. |
| 616 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 617 | SdpType type, |
| 618 | const RTCError& error) { |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 619 | rtc::StringBuilder oss; |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 620 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 621 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
Jonas Olsson | 84df1c7 | 2018-09-14 14:59:32 | [diff] [blame] | 622 | return oss.Release(); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 623 | } |
| 624 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 625 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 626 | std::string output = "streams=["; |
| 627 | const char* separator = ""; |
| 628 | for (const auto& stream_id : stream_ids) { |
| 629 | output.append(separator).append(stream_id); |
| 630 | separator = ", "; |
| 631 | } |
| 632 | output.append("]"); |
| 633 | return output; |
| 634 | } |
| 635 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 636 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 637 | int rtc_configuration_parameter) { |
| 638 | if (rtc_configuration_parameter == |
| 639 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 640 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 641 | } |
| 642 | return rtc_configuration_parameter; |
| 643 | } |
| 644 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 645 | cricket::DataMessageType ToCricketDataMessageType(DataMessageType type) { |
| 646 | switch (type) { |
| 647 | case DataMessageType::kText: |
| 648 | return cricket::DMT_TEXT; |
| 649 | case DataMessageType::kBinary: |
| 650 | return cricket::DMT_BINARY; |
| 651 | case DataMessageType::kControl: |
| 652 | return cricket::DMT_CONTROL; |
| 653 | default: |
| 654 | return cricket::DMT_NONE; |
| 655 | } |
| 656 | return cricket::DMT_NONE; |
| 657 | } |
| 658 | |
| 659 | DataMessageType ToWebrtcDataMessageType(cricket::DataMessageType type) { |
| 660 | switch (type) { |
| 661 | case cricket::DMT_TEXT: |
| 662 | return DataMessageType::kText; |
| 663 | case cricket::DMT_BINARY: |
| 664 | return DataMessageType::kBinary; |
| 665 | case cricket::DMT_CONTROL: |
| 666 | return DataMessageType::kControl; |
| 667 | case cricket::DMT_NONE: |
| 668 | default: |
| 669 | RTC_NOTREACHED(); |
| 670 | } |
| 671 | return DataMessageType::kControl; |
| 672 | } |
| 673 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 674 | void ReportSimulcastApiVersion(const char* name, |
| 675 | const SessionDescription& session) { |
| 676 | bool has_legacy = false; |
| 677 | bool has_spec_compliant = false; |
| 678 | for (const ContentInfo& content : session.contents()) { |
| 679 | if (!content.description) { |
| 680 | continue; |
| 681 | } |
| 682 | has_spec_compliant |= content.description->HasSimulcast(); |
| 683 | for (const StreamParams& sp : content.description->streams()) { |
| 684 | has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (has_legacy) { |
| 689 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy, |
| 690 | kSimulcastApiVersionMax); |
| 691 | } |
| 692 | if (has_spec_compliant) { |
| 693 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant, |
| 694 | kSimulcastApiVersionMax); |
| 695 | } |
| 696 | if (!has_legacy && !has_spec_compliant) { |
| 697 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone, |
| 698 | kSimulcastApiVersionMax); |
| 699 | } |
| 700 | } |
| 701 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 702 | const ContentInfo* FindTransceiverMSection( |
| 703 | RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver, |
| 704 | const SessionDescriptionInterface* session_description) { |
| 705 | return transceiver->mid() |
| 706 | ? session_description->description()->GetContentByName( |
| 707 | *transceiver->mid()) |
| 708 | : nullptr; |
| 709 | } |
| 710 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 711 | } // namespace |
| 712 | |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 713 | // Upon completion, posts a task to execute the callback of the |
| 714 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 715 | // point, the state of the peer connection might no longer reflect the effects |
| 716 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 717 | // modified during the post. |
| 718 | // TODO(hbos): Remove this class once we remove the version of |
| 719 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 720 | // SetSessionDescriptionObserver as an argument. |
| 721 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 722 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 723 | public: |
| 724 | SetRemoteDescriptionObserverAdapter( |
| 725 | rtc::scoped_refptr<PeerConnection> pc, |
| 726 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 727 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 728 | |
| 729 | // SetRemoteDescriptionObserverInterface implementation. |
| 730 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 731 | if (error.ok()) |
| 732 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 733 | else |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 734 | pc_->PostSetSessionDescriptionFailure(wrapper_, std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | private: |
| 738 | rtc::scoped_refptr<PeerConnection> pc_; |
| 739 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 740 | }; |
| 741 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 742 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 743 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 744 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 745 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 746 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 | [diff] [blame] | 747 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 748 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 749 | BundlePolicy bundle_policy; |
| 750 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 751 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 752 | int ice_candidate_pool_size; |
| 753 | bool disable_ipv6; |
| 754 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 755 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 756 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 757 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 758 | absl::optional<int> screencast_min_bitrate; |
| 759 | absl::optional<bool> combined_audio_video_bwe; |
| 760 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 761 | TcpCandidatePolicy tcp_candidate_policy; |
| 762 | CandidateNetworkPolicy candidate_network_policy; |
| 763 | int audio_jitter_buffer_max_packets; |
| 764 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 765 | int audio_jitter_buffer_min_delay_ms; |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 766 | bool audio_jitter_buffer_enable_rtx_handling; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 767 | int ice_connection_receiving_timeout; |
| 768 | int ice_backup_candidate_pair_ping_interval; |
| 769 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 770 | bool prioritize_most_likely_ice_candidate_pairs; |
| 771 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 772 | bool prune_turn_ports; |
| 773 | bool presume_writable_when_fully_relayed; |
| 774 | bool enable_ice_renomination; |
| 775 | bool redetermine_role_on_ice_restart; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 776 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 777 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 778 | absl::optional<int> ice_check_min_interval; |
| 779 | absl::optional<int> ice_unwritable_timeout; |
| 780 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 781 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 782 | absl::optional<int> stun_candidate_keepalive_interval; |
| 783 | absl::optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 784 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 785 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 786 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 787 | bool active_reset_srtp_params; |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 16:43:21 | [diff] [blame] | 788 | bool use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 789 | bool use_media_transport_for_data_channels; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 790 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 791 | bool offer_extmap_allow_mixed; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 792 | }; |
| 793 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 794 | "Did you add something to RTCConfiguration and forget to " |
| 795 | "update operator==?"); |
| 796 | return type == o.type && servers == o.servers && |
| 797 | bundle_policy == o.bundle_policy && |
| 798 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 799 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 800 | candidate_network_policy == o.candidate_network_policy && |
| 801 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 802 | audio_jitter_buffer_fast_accelerate == |
| 803 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 804 | audio_jitter_buffer_min_delay_ms == |
| 805 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 806 | audio_jitter_buffer_enable_rtx_handling == |
| 807 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 808 | ice_connection_receiving_timeout == |
| 809 | o.ice_connection_receiving_timeout && |
| 810 | ice_backup_candidate_pair_ping_interval == |
| 811 | o.ice_backup_candidate_pair_ping_interval && |
| 812 | continual_gathering_policy == o.continual_gathering_policy && |
| 813 | certificates == o.certificates && |
| 814 | prioritize_most_likely_ice_candidate_pairs == |
| 815 | o.prioritize_most_likely_ice_candidate_pairs && |
| 816 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 817 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 818 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 819 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 820 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 821 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 822 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 823 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 824 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 825 | prune_turn_ports == o.prune_turn_ports && |
| 826 | presume_writable_when_fully_relayed == |
| 827 | o.presume_writable_when_fully_relayed && |
| 828 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 19:50:14 | [diff] [blame] | 829 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 830 | ice_check_interval_strong_connectivity == |
| 831 | o.ice_check_interval_strong_connectivity && |
| 832 | ice_check_interval_weak_connectivity == |
| 833 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 834 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 835 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 836 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 837 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 838 | stun_candidate_keepalive_interval == |
| 839 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 840 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 841 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 842 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 843 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 16:43:21 | [diff] [blame] | 844 | active_reset_srtp_params == o.active_reset_srtp_params && |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 845 | use_media_transport == o.use_media_transport && |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 846 | use_media_transport_for_data_channels == |
| 847 | o.use_media_transport_for_data_channels && |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 848 | crypto_options == o.crypto_options && |
| 849 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 853 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 854 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 19:44:26 | [diff] [blame] | 855 | } |
| 856 | |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 857 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 858 | std::string GenerateRtcpCname() { |
| 859 | std::string cname; |
| 860 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 861 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 10:24:27 | [diff] [blame] | 862 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 863 | } |
| 864 | return cname; |
| 865 | } |
| 866 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 867 | bool ValidateOfferAnswerOptions( |
| 868 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 869 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 870 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 13:50:32 | [diff] [blame] | 871 | } |
| 872 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 873 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 874 | // m= sections (in other words, nothing that involves a map/array). |
| 875 | void ExtractSharedMediaSessionOptions( |
| 876 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 877 | cricket::MediaSessionOptions* session_options) { |
| 878 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 879 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 880 | } |
zhihuang | a77e6bb | 2017-08-15 01:17:48 | [diff] [blame] | 881 | |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 882 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 883 | std::unique_ptr<RtcEventLog> event_log, |
| 884 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 885 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 886 | event_log_(std::move(event_log)), |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 887 | event_log_ptr_(event_log_.get()), |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 888 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 889 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 890 | remote_streams_(StreamCollection::Create()), |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 891 | call_(std::move(call)), |
| 892 | call_ptr_(call_.get()) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 893 | |
| 894 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 895 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 896 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 897 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 898 | // Need to stop transceivers before destroying the stats collector because |
| 899 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 900 | // stopping. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 901 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 902 | transceiver->Stop(); |
| 903 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 904 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 905 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 13:06:57 | [diff] [blame] | 906 | if (stats_collector_) { |
| 907 | stats_collector_->WaitForPendingRequest(); |
| 908 | stats_collector_ = nullptr; |
| 909 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 910 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 911 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 912 | // the last stats request can still read from the channels. |
| 913 | DestroyAllChannels(); |
| 914 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 915 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 916 | |
| 917 | webrtc_session_desc_factory_.reset(); |
| 918 | sctp_invoker_.reset(); |
| 919 | sctp_factory_.reset(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 920 | media_transport_invoker_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 921 | transport_controller_.reset(); |
| 922 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 923 | // port_allocator_ lives on the network thread and should be destroyed there. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 924 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 925 | RTC_DCHECK_RUN_ON(network_thread()); |
| 926 | port_allocator_.reset(); |
| 927 | }); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 928 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 929 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 930 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 931 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 932 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 933 | event_log_.reset(); |
| 934 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 935 | } |
| 936 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 937 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 938 | // Destroy video channels first since they may have a pointer to a voice |
| 939 | // channel. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 940 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 941 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 942 | DestroyTransceiverChannel(transceiver); |
| 943 | } |
| 944 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 945 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 946 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 947 | DestroyTransceiverChannel(transceiver); |
| 948 | } |
| 949 | } |
| 950 | DestroyDataChannel(); |
| 951 | } |
| 952 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 953 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 954 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 955 | PeerConnectionDependencies dependencies) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 956 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 957 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 958 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 959 | |
| 960 | RTCError config_error = ValidateConfiguration(configuration); |
| 961 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 962 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 963 | return false; |
| 964 | } |
| 965 | |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 966 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 967 | RTC_LOG(LS_ERROR) |
| 968 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 969 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 970 | return false; |
| 971 | } |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 972 | |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 973 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 974 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 975 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 976 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 977 | return false; |
| 978 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 979 | |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 980 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 20:20:15 | [diff] [blame] | 981 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 982 | port_allocator_ = std::move(dependencies.allocator); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 983 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 984 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 985 | cricket::ServerAddresses stun_servers; |
| 986 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 987 | |
| 988 | RTCErrorType parse_error = |
| 989 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 990 | if (parse_error != RTCErrorType::NONE) { |
| 991 | return false; |
| 992 | } |
| 993 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 994 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 995 | // there. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 996 | const auto pa_result = |
| 997 | network_thread()->Invoke<InitializePortAllocatorResult>( |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 998 | RTC_FROM_HERE, |
| 999 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1000 | stun_servers, turn_servers, configuration)); |
| 1001 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 1002 | // If initialization was successful, note if STUN or TURN servers |
| 1003 | // were supplied. |
| 1004 | if (!stun_servers.empty()) { |
| 1005 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 1006 | } |
| 1007 | if (!turn_servers.empty()) { |
| 1008 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 1009 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1010 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 1011 | // Send information about IPv4/IPv6 status. |
| 1012 | PeerConnectionAddressFamilyCounter address_family; |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1013 | if (pa_result.enable_ipv6) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 1014 | address_family = kPeerConnection_IPv6; |
| 1015 | } else { |
| 1016 | address_family = kPeerConnection_IPv4; |
| 1017 | } |
| 1018 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 1019 | kPeerConnectionAddressFamilyCounter_Max); |
| 1020 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1021 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 1022 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1023 | // RFC 3264: The numeric value of the session id and version in the |
| 1024 | // o line MUST be representable with a "64 bit signed integer". |
| 1025 | // Due to this constraint session id |session_id_| is max limited to |
| 1026 | // LLONG_MAX. |
| 1027 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1028 | JsepTransportController::Config config; |
| 1029 | config.redetermine_role_on_ice_restart = |
| 1030 | configuration.redetermine_role_on_ice_restart; |
| 1031 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 1032 | config.disable_encryption = options.disable_encryption; |
| 1033 | config.bundle_policy = configuration.bundle_policy; |
| 1034 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1035 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 1036 | // stub. |
| 1037 | config.crypto_options = configuration.crypto_options.has_value() |
| 1038 | ? *configuration.crypto_options |
| 1039 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 1040 | config.transport_observer = this; |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1041 | config.event_log = event_log_ptr_; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1042 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 1043 | config.enable_external_auth = true; |
| 1044 | #endif |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 1045 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 1046 | |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 1047 | if (configuration.use_media_transport || |
| 1048 | configuration.use_media_transport_for_data_channels) { |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 1049 | if (!factory_->media_transport_factory()) { |
| 1050 | RTC_DCHECK(false) |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 1051 | << "PeerConnecton is initialized with use_media_transport = true or " |
| 1052 | << "use_media_transport_for_data_channels = true " |
| 1053 | << "but media transport factory is not set in PeerConnectionFactory"; |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 1054 | return false; |
| 1055 | } |
| 1056 | |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 1057 | if (configuration.use_media_transport || |
| 1058 | configuration.use_media_transport_for_data_channels) { |
| 1059 | // TODO(bugs.webrtc.org/9719): This check will eventually go away, when |
| 1060 | // RTP media transport is introduced. But until then, we require SDES to |
| 1061 | // be enabled. |
| 1062 | if (configuration.enable_dtls_srtp.has_value() && |
| 1063 | configuration.enable_dtls_srtp.value()) { |
| 1064 | RTC_LOG(LS_WARNING) |
| 1065 | << "When media transport is used, SDES must be enabled. Set " |
| 1066 | "configuration.enable_dtls_srtp to false. use_media_transport=" |
| 1067 | << configuration.use_media_transport |
| 1068 | << ", use_media_transport_for_data_channels=" |
| 1069 | << configuration.use_media_transport_for_data_channels; |
| 1070 | return false; |
| 1071 | } |
| 1072 | } |
| 1073 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 21:31:15 | [diff] [blame] | 1074 | config.use_media_transport_for_media = configuration.use_media_transport; |
| 1075 | config.use_media_transport_for_data_channels = |
| 1076 | configuration.use_media_transport_for_data_channels; |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 1077 | config.media_transport_factory = factory_->media_transport_factory(); |
| 1078 | } |
| 1079 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1080 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 20:20:15 | [diff] [blame] | 1081 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 1082 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1083 | transport_controller_->SignalIceConnectionState.connect( |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1084 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 1085 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 1086 | this, &PeerConnection::SetStandardizedIceConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1087 | transport_controller_->SignalConnectionState.connect( |
| 1088 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1089 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1090 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1091 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1092 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1093 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1094 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 1095 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 1096 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 1097 | |
| 1098 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 1099 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1100 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1101 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1102 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1103 | configuration_ = configuration; |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1104 | use_media_transport_ = configuration.use_media_transport; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1105 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1106 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 1107 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 1108 | if (!configuration.certificates.empty()) { |
| 1109 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 1110 | // just picking the first one. The decision should be made based on the DTLS |
| 1111 | // handshake. The DTLS negotiations need to know about all certificates. |
| 1112 | certificate = configuration.certificates[0]; |
| 1113 | } |
| 1114 | |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 1115 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1116 | |
| 1117 | if (options.disable_encryption) { |
| 1118 | dtls_enabled_ = false; |
| 1119 | } else { |
| 1120 | // Enable DTLS by default if we have an identity store or a certificate. |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1121 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1122 | // |configuration| can override the default |dtls_enabled_| value. |
| 1123 | if (configuration.enable_dtls_srtp) { |
| 1124 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1125 | } |
| 1126 | } |
| 1127 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 1128 | if (configuration.use_media_transport_for_data_channels) { |
| 1129 | if (configuration.enable_rtp_data_channel) { |
| 1130 | RTC_LOG(LS_ERROR) << "enable_rtp_data_channel and " |
| 1131 | "use_media_transport_for_data_channels are " |
| 1132 | "incompatible and cannot both be set to true"; |
| 1133 | return false; |
| 1134 | } |
| 1135 | data_channel_type_ = cricket::DCT_MEDIA_TRANSPORT; |
| 1136 | } else if (configuration.enable_rtp_data_channel) { |
| 1137 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is |
| 1138 | // set. It takes precendence over the disable_sctp_data_channels |
| 1139 | // PeerConnectionFactoryInterface::Options. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1140 | data_channel_type_ = cricket::DCT_RTP; |
| 1141 | } else { |
| 1142 | // DTLS has to be enabled to use SCTP. |
| 1143 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 1144 | data_channel_type_ = cricket::DCT_SCTP; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | video_options_.screencast_min_bitrate_kbps = |
| 1149 | configuration.screencast_min_bitrate; |
| 1150 | audio_options_.combined_audio_video_bwe = |
| 1151 | configuration.combined_audio_video_bwe; |
| 1152 | |
| 1153 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 1154 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1155 | |
| 1156 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 1157 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1158 | |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 1159 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 1160 | configuration.audio_jitter_buffer_min_delay_ms; |
| 1161 | |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 1162 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 1163 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 1164 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1165 | // Whether the certificate generator/certificate is null or not determines |
| 1166 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1167 | // the right instructions by clearing the variables if needed. |
| 1168 | if (!dtls_enabled_) { |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1169 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1170 | certificate = nullptr; |
| 1171 | } else if (certificate) { |
| 1172 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1173 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1177 | signaling_thread(), channel_manager(), this, session_id(), |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 1178 | std::move(dependencies.cert_generator), certificate, &ssrc_generator_)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1179 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1180 | this, &PeerConnection::OnCertificateReady); |
| 1181 | |
| 1182 | if (options.disable_encryption) { |
| 1183 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1184 | } |
| 1185 | |
| 1186 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1187 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
Steve Anton | 8f66ddb | 2018-12-11 00:08:05 | [diff] [blame] | 1188 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1189 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1190 | // Add default audio/video transceivers for Plan B SDP. |
| 1191 | if (!IsUnifiedPlan()) { |
| 1192 | transceivers_.push_back( |
| 1193 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1194 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1195 | transceivers_.push_back( |
| 1196 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1197 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1198 | } |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 1199 | int delay_ms = |
| 1200 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 1201 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1202 | MSG_REPORT_USAGE_PATTERN, nullptr); |
Jonas Oreland | a3aa9bd | 2019-04-17 05:38:40 | [diff] [blame] | 1203 | |
| 1204 | if (dependencies.video_bitrate_allocator_factory) { |
| 1205 | video_bitrate_allocator_factory_ = |
| 1206 | std::move(dependencies.video_bitrate_allocator_factory); |
| 1207 | } else { |
| 1208 | video_bitrate_allocator_factory_ = |
| 1209 | CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1210 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1211 | return true; |
| 1212 | } |
| 1213 | |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1214 | RTCError PeerConnection::ValidateConfiguration( |
| 1215 | const RTCConfiguration& config) const { |
| 1216 | if (config.ice_regather_interval_range && |
| 1217 | config.continual_gathering_policy == GATHER_ONCE) { |
| 1218 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1219 | "ice_regather_interval_range specified but continual " |
| 1220 | "gathering policy is GATHER_ONCE"); |
| 1221 | } |
Qingsi Wang | dea6889 | 2018-03-27 17:55:21 | [diff] [blame] | 1222 | auto result = |
| 1223 | cricket::P2PTransportChannel::ValidateIceConfig(ParseIceConfig(config)); |
| 1224 | return result; |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1225 | } |
| 1226 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1227 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1228 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1229 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1230 | "Plan SdpSemantics. Please use GetSenders " |
| 1231 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1232 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1233 | } |
| 1234 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1235 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1236 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1237 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1238 | "Plan SdpSemantics. Please use GetReceivers " |
| 1239 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1240 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1241 | } |
| 1242 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 | [diff] [blame] | 1243 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1244 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1245 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1246 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1247 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1248 | if (IsClosed()) { |
| 1249 | return false; |
| 1250 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1251 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1252 | return false; |
| 1253 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1254 | |
| 1255 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 1256 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1257 | observer->SignalAudioTrackAdded.connect(this, |
| 1258 | &PeerConnection::OnAudioTrackAdded); |
| 1259 | observer->SignalAudioTrackRemoved.connect( |
| 1260 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1261 | observer->SignalVideoTrackAdded.connect(this, |
| 1262 | &PeerConnection::OnVideoTrackAdded); |
| 1263 | observer->SignalVideoTrackRemoved.connect( |
| 1264 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 1265 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1266 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1267 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 1268 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1269 | } |
| 1270 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 1271 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1272 | } |
| 1273 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 1274 | stats_->AddStream(local_stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1275 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1276 | return true; |
| 1277 | } |
| 1278 | |
| 1279 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1280 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1281 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1282 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1283 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1284 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 1285 | if (!IsClosed()) { |
| 1286 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1287 | RemoveAudioTrack(track.get(), local_stream); |
| 1288 | } |
| 1289 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1290 | RemoveVideoTrack(track.get(), local_stream); |
| 1291 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1292 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1293 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 1294 | stream_observers_.erase( |
| 1295 | std::remove_if( |
| 1296 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 1297 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 23:05:28 | [diff] [blame] | 1298 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 1299 | }), |
| 1300 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1301 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1302 | if (IsClosed()) { |
| 1303 | return; |
| 1304 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1305 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1306 | } |
| 1307 | |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 1308 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1309 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1310 | const std::vector<std::string>& stream_ids) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1311 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 1312 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1313 | if (!track) { |
| 1314 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1315 | } |
| 1316 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1317 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1318 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1319 | "Track has invalid kind: " + track->kind()); |
| 1320 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1321 | if (IsClosed()) { |
| 1322 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1323 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1324 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1325 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1326 | LOG_AND_RETURN_ERROR( |
| 1327 | RTCErrorType::INVALID_PARAMETER, |
| 1328 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1329 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1330 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1331 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1332 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1333 | if (sender_or_error.ok()) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1334 | UpdateNegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 23:48:17 | [diff] [blame] | 1335 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1336 | } |
| 1337 | return sender_or_error; |
| 1338 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1339 | |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1340 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1341 | PeerConnection::AddTrackPlanB( |
| 1342 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1343 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1344 | if (stream_ids.size() > 1u) { |
| 1345 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1346 | "AddTrack with more than one stream is not " |
| 1347 | "supported with Plan B semantics."); |
| 1348 | } |
| 1349 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1350 | if (adjusted_stream_ids.empty()) { |
| 1351 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1352 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1353 | cricket::MediaType media_type = |
| 1354 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1355 | ? cricket::MEDIA_TYPE_AUDIO |
| 1356 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1357 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1358 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1359 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1360 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1361 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1362 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 1363 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1364 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1365 | if (sender_info) { |
| 1366 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1367 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1368 | } else { |
| 1369 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1370 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1371 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1372 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 1373 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1374 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1375 | if (sender_info) { |
| 1376 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1377 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1378 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1379 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1380 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1381 | |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1382 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1383 | PeerConnection::AddTrackUnifiedPlan( |
| 1384 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1385 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1386 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1387 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1388 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1389 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1390 | << " transceiver for AddTrack."; |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1391 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1392 | transceiver->internal()->set_direction( |
| 1393 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1394 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1395 | transceiver->internal()->set_direction( |
| 1396 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1397 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1398 | transceiver->sender()->SetTrack(track); |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1399 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1400 | } else { |
| 1401 | cricket::MediaType media_type = |
| 1402 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1403 | ? cricket::MEDIA_TYPE_AUDIO |
| 1404 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1405 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1406 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1407 | std::string sender_id = track->id(); |
| 1408 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1409 | // This can happen if this is the second time AddTrack has created a sender |
| 1410 | // for this track. |
| 1411 | if (FindSenderById(sender_id)) { |
| 1412 | sender_id = rtc::CreateRandomUuid(); |
| 1413 | } |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1414 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1415 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1416 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1417 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1418 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1419 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1420 | return transceiver->sender(); |
| 1421 | } |
| 1422 | |
| 1423 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1424 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1425 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1426 | RTC_DCHECK(track); |
| 1427 | for (auto transceiver : transceivers_) { |
| 1428 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 1429 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1430 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 19:54:42 | [diff] [blame] | 1431 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1432 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1433 | return transceiver; |
| 1434 | } |
| 1435 | } |
| 1436 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1440 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 17:27:33 | [diff] [blame] | 1441 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1442 | } |
| 1443 | |
Steve Anton | 24db573 | 2018-07-23 17:27:33 | [diff] [blame] | 1444 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1445 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1446 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1447 | if (!sender) { |
| 1448 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1449 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1450 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1451 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1452 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1453 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1454 | if (IsUnifiedPlan()) { |
| 1455 | auto transceiver = FindTransceiverBySender(sender); |
| 1456 | if (!transceiver || !sender->track()) { |
| 1457 | return RTCError::OK(); |
| 1458 | } |
| 1459 | sender->SetTrack(nullptr); |
| 1460 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1461 | transceiver->internal()->set_direction( |
| 1462 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1463 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1464 | transceiver->internal()->set_direction( |
| 1465 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1466 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1467 | } else { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1468 | bool removed; |
| 1469 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1470 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1471 | } else { |
| 1472 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1473 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1474 | } |
| 1475 | if (!removed) { |
| 1476 | LOG_AND_RETURN_ERROR( |
| 1477 | RTCErrorType::INVALID_PARAMETER, |
| 1478 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1479 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1480 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1481 | UpdateNegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1482 | return RTCError::OK(); |
| 1483 | } |
| 1484 | |
| 1485 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1486 | PeerConnection::FindTransceiverBySender( |
| 1487 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1488 | for (auto transceiver : transceivers_) { |
| 1489 | if (transceiver->sender() == sender) { |
| 1490 | return transceiver; |
| 1491 | } |
| 1492 | } |
| 1493 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1494 | } |
| 1495 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1496 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1497 | PeerConnection::AddTransceiver( |
| 1498 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1499 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1500 | } |
| 1501 | |
| 1502 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1503 | PeerConnection::AddTransceiver( |
| 1504 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1505 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1506 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1507 | RTC_CHECK(IsUnifiedPlan()) |
| 1508 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1509 | if (!track) { |
| 1510 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1511 | } |
| 1512 | cricket::MediaType media_type; |
| 1513 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1514 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1515 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1516 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1517 | } else { |
| 1518 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1519 | "Track kind is not audio or video"); |
| 1520 | } |
| 1521 | return AddTransceiver(media_type, track, init); |
| 1522 | } |
| 1523 | |
| 1524 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1525 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1526 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1527 | } |
| 1528 | |
| 1529 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1530 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1531 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1532 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1533 | RTC_CHECK(IsUnifiedPlan()) |
| 1534 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1535 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1536 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1537 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1538 | "media type is not audio or video"); |
| 1539 | } |
| 1540 | return AddTransceiver(media_type, nullptr, init); |
| 1541 | } |
| 1542 | |
| 1543 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1544 | PeerConnection::AddTransceiver( |
| 1545 | cricket::MediaType media_type, |
| 1546 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1547 | const RtpTransceiverInit& init, |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1548 | bool update_negotiation_needed) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1549 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1550 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1551 | if (track) { |
| 1552 | RTC_DCHECK_EQ(media_type, |
| 1553 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1554 | ? cricket::MEDIA_TYPE_AUDIO |
| 1555 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1556 | } |
| 1557 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 1558 | RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings, |
| 1559 | init.send_encodings.size(), 0, 7, 8); |
| 1560 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1561 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1562 | [](const RtpEncodingParameters& encoding) { |
| 1563 | return !encoding.rid.empty(); |
| 1564 | }); |
| 1565 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-07 01:09:52 | [diff] [blame] | 1566 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1567 | RTCErrorType::INVALID_PARAMETER, |
| 1568 | "RIDs must be provided for either all or none of the send encodings."); |
Emircan Uysaler | 7832343 | 2019-02-08 20:41:39 | [diff] [blame] | 1569 | } |
| 1570 | |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 1571 | if (num_rids > 0 && absl::c_any_of(init.send_encodings, |
| 1572 | [](const RtpEncodingParameters& encoding) { |
| 1573 | return !IsLegalRsidName(encoding.rid); |
| 1574 | })) { |
| 1575 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1576 | "Invalid RID value provided."); |
| 1577 | } |
| 1578 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1579 | if (absl::c_any_of(init.send_encodings, |
| 1580 | [](const RtpEncodingParameters& encoding) { |
| 1581 | return encoding.ssrc.has_value(); |
| 1582 | })) { |
| 1583 | LOG_AND_RETURN_ERROR( |
| 1584 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1585 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | RtpParameters parameters; |
| 1589 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1590 | |
| 1591 | // Encodings are dropped from the tail if too many are provided. |
| 1592 | if (parameters.encodings.size() > kMaxSimulcastStreams) { |
| 1593 | parameters.encodings.erase( |
| 1594 | parameters.encodings.begin() + kMaxSimulcastStreams, |
| 1595 | parameters.encodings.end()); |
| 1596 | } |
| 1597 | |
| 1598 | // Single RID should be removed. |
| 1599 | if (parameters.encodings.size() == 1 && |
| 1600 | !parameters.encodings[0].rid.empty()) { |
| 1601 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1602 | parameters.encodings[0].rid.clear(); |
| 1603 | } |
| 1604 | |
| 1605 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1606 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1607 | rtc::UniqueStringGenerator rid_generator; |
| 1608 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 1609 | encoding.rid = rid_generator(); |
| 1610 | } |
| 1611 | } |
| 1612 | |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1613 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1614 | LOG_AND_RETURN_ERROR( |
| 1615 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1616 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1617 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1618 | |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 1619 | auto result = cricket::CheckRtpParametersValues(parameters); |
| 1620 | if (!result.ok()) { |
| 1621 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1622 | } |
| 1623 | |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1624 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1625 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1626 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1627 | // that sender ID is already in use. |
| 1628 | std::string sender_id = |
| 1629 | (track && !FindSenderById(track->id()) ? track->id() |
| 1630 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1631 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1632 | parameters.encodings); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1633 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1634 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1635 | transceiver->internal()->set_direction(init.direction); |
| 1636 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1637 | if (update_negotiation_needed) { |
| 1638 | UpdateNegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1639 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1640 | |
| 1641 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1642 | } |
| 1643 | |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1644 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1645 | PeerConnection::CreateSender( |
| 1646 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1647 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1648 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1649 | const std::vector<std::string>& stream_ids, |
| 1650 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1651 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1652 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1653 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1654 | RTC_DCHECK(!track || |
| 1655 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1656 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1657 | signaling_thread(), |
Amit Hilbuch | ea7ef2a | 2019-02-19 23:20:21 | [diff] [blame] | 1658 | AudioRtpSender::Create(worker_thread(), id, stats_.get())); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1659 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1660 | } else { |
| 1661 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1662 | RTC_DCHECK(!track || |
| 1663 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1664 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Amit Hilbuch | ea7ef2a | 2019-02-19 23:20:21 | [diff] [blame] | 1665 | signaling_thread(), VideoRtpSender::Create(worker_thread(), id)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1666 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1667 | } |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1668 | bool set_track_succeeded = sender->SetTrack(track); |
| 1669 | RTC_DCHECK(set_track_succeeded); |
| 1670 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1671 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1672 | return sender; |
| 1673 | } |
| 1674 | |
| 1675 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1676 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1677 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1678 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1679 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1680 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1681 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 1682 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1683 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1684 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1685 | } else { |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1686 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1687 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 1688 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1689 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1690 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1691 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1692 | return receiver; |
| 1693 | } |
| 1694 | |
| 1695 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1696 | PeerConnection::CreateAndAddTransceiver( |
| 1697 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1698 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1699 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1700 | // Ensure that the new sender does not have an ID that is already in use by |
| 1701 | // another sender. |
| 1702 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1703 | // could be invalid, but should not cause a crash). |
| 1704 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1705 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 1706 | signaling_thread(), |
| 1707 | new RtpTransceiver(sender, receiver, channel_manager())); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1708 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1709 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1710 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1711 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1712 | } |
| 1713 | |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1714 | void PeerConnection::OnNegotiationNeeded() { |
| 1715 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1716 | RTC_DCHECK(!IsClosed()); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1717 | UpdateNegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1718 | } |
| 1719 | |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1720 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-19 00:58:44 | [diff] [blame] | 1721 | const std::string& kind, |
| 1722 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1723 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1724 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1725 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1726 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1727 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 1728 | if (IsClosed()) { |
| 1729 | return nullptr; |
| 1730 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1731 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1732 | // Internally we need to have one stream with Plan B semantics, so we |
| 1733 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1734 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1735 | if (stream_id.empty()) { |
| 1736 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1737 | RTC_LOG(LS_INFO) |
| 1738 | << "No stream_id specified for sender. Generated stream ID: " |
| 1739 | << stream_ids[0]; |
| 1740 | } else { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1741 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1742 | } |
| 1743 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1744 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1745 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1746 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 23:20:21 | [diff] [blame] | 1747 | auto audio_sender = AudioRtpSender::Create( |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1748 | worker_thread(), rtc::CreateRandomUuid(), stats_.get()); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1749 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1750 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1751 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1752 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1753 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 23:20:21 | [diff] [blame] | 1754 | auto video_sender = |
| 1755 | VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid()); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1756 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1757 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1758 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1759 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1760 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1761 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1762 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1763 | } |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1764 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1765 | |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1766 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1767 | } |
| 1768 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1769 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1770 | const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1771 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1772 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1773 | for (const auto& sender : GetSendersInternal()) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1774 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1775 | } |
| 1776 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1777 | } |
| 1778 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1779 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1780 | PeerConnection::GetSendersInternal() const { |
| 1781 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1782 | all_senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1783 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1784 | auto senders = transceiver->internal()->senders(); |
| 1785 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1786 | } |
| 1787 | return all_senders; |
| 1788 | } |
| 1789 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1790 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1791 | PeerConnection::GetReceivers() const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1792 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1793 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1794 | for (const auto& receiver : GetReceiversInternal()) { |
| 1795 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1796 | } |
| 1797 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1798 | } |
| 1799 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1800 | std::vector< |
| 1801 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1802 | PeerConnection::GetReceiversInternal() const { |
| 1803 | std::vector< |
| 1804 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1805 | all_receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1806 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1807 | auto receivers = transceiver->internal()->receivers(); |
| 1808 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1809 | receivers.end()); |
| 1810 | } |
| 1811 | return all_receivers; |
| 1812 | } |
| 1813 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1814 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1815 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1816 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1817 | RTC_CHECK(IsUnifiedPlan()) |
| 1818 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1819 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1820 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1821 | all_transceivers.push_back(transceiver); |
| 1822 | } |
| 1823 | return all_transceivers; |
| 1824 | } |
| 1825 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1826 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 | [diff] [blame] | 1827 | MediaStreamTrackInterface* track, |
| 1828 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1829 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1830 | RTC_DCHECK_RUN_ON(signaling_thread()); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 1831 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1832 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1833 | return false; |
| 1834 | } |
| 1835 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 1836 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 1837 | // The StatsCollector is used to tell if a track is valid because it may |
| 1838 | // remember tracks that the PeerConnection previously removed. |
| 1839 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1840 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1841 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 1842 | return false; |
| 1843 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 1844 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 1845 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1846 | return true; |
| 1847 | } |
| 1848 | |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1849 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1850 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1851 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1852 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1853 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1854 | stats_collector_->GetStatsReport(callback); |
| 1855 | } |
| 1856 | |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1857 | void PeerConnection::GetStats( |
| 1858 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1859 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1860 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1861 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1862 | RTC_DCHECK(callback); |
| 1863 | RTC_DCHECK(stats_collector_); |
| 1864 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1865 | if (selector) { |
| 1866 | for (const auto& proxy_transceiver : transceivers_) { |
| 1867 | for (const auto& proxy_sender : |
| 1868 | proxy_transceiver->internal()->senders()) { |
| 1869 | if (proxy_sender == selector) { |
| 1870 | internal_sender = proxy_sender->internal(); |
| 1871 | break; |
| 1872 | } |
| 1873 | } |
| 1874 | if (internal_sender) |
| 1875 | break; |
| 1876 | } |
| 1877 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 1878 | // If there is no |internal_sender| then |selector| is either null or does not |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1879 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1880 | // PeerConnection). This means that "all the stats objects representing the |
| 1881 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1882 | // produces an empty stats report. |
| 1883 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 1884 | } |
| 1885 | |
| 1886 | void PeerConnection::GetStats( |
| 1887 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1888 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1889 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1890 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1891 | RTC_DCHECK(callback); |
| 1892 | RTC_DCHECK(stats_collector_); |
| 1893 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1894 | if (selector) { |
| 1895 | for (const auto& proxy_transceiver : transceivers_) { |
| 1896 | for (const auto& proxy_receiver : |
| 1897 | proxy_transceiver->internal()->receivers()) { |
| 1898 | if (proxy_receiver == selector) { |
| 1899 | internal_receiver = proxy_receiver->internal(); |
| 1900 | break; |
| 1901 | } |
| 1902 | } |
| 1903 | if (internal_receiver) |
| 1904 | break; |
| 1905 | } |
| 1906 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 1907 | // If there is no |internal_receiver| then |selector| is either null or does |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1908 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1909 | // the PeerConnection). This means that "all the stats objects representing |
| 1910 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1911 | // selector produces an empty stats report. |
| 1912 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 1913 | } |
| 1914 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1915 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1916 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1917 | return signaling_state_; |
| 1918 | } |
| 1919 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1920 | PeerConnectionInterface::IceConnectionState |
| 1921 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1922 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1923 | return ice_connection_state_; |
| 1924 | } |
| 1925 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1926 | PeerConnectionInterface::IceConnectionState |
| 1927 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1928 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1929 | return standardized_ice_connection_state_; |
| 1930 | } |
| 1931 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1932 | PeerConnectionInterface::PeerConnectionState |
| 1933 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1934 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1935 | return connection_state_; |
| 1936 | } |
| 1937 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1938 | PeerConnectionInterface::IceGatheringState |
| 1939 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1940 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1941 | return ice_gathering_state_; |
| 1942 | } |
| 1943 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1944 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1945 | const std::string& label, |
| 1946 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 09:17:47 | [diff] [blame] | 1947 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1948 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 18:14:50 | [diff] [blame] | 1949 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1950 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1951 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 1952 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 1953 | if (config) { |
| 1954 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1955 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 1956 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1957 | InternalCreateDataChannel(label, internal_config.get())); |
| 1958 | if (!channel.get()) { |
| 1959 | return nullptr; |
| 1960 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1961 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1962 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1963 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1964 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1965 | UpdateNegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1966 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1967 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1968 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1969 | } |
| 1970 | |
| 1971 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 1972 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1973 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1974 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 1975 | |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 1976 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1977 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 1978 | return; |
| 1979 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1980 | |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 1981 | if (IsClosed()) { |
| 1982 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1983 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 1984 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 13:02:10 | [diff] [blame] | 1985 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 1986 | return; |
| 1987 | } |
| 1988 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 1989 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1990 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1991 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 1992 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 13:02:10 | [diff] [blame] | 1993 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1994 | return; |
| 1995 | } |
| 1996 | |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1997 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1998 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1999 | if (IsUnifiedPlan()) { |
| 2000 | RTCError error = HandleLegacyOfferOptions(options); |
| 2001 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2002 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2003 | return; |
| 2004 | } |
| 2005 | } |
| 2006 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 2007 | cricket::MediaSessionOptions session_options; |
| 2008 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 2009 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2010 | } |
| 2011 | |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2012 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 2013 | const RTCOfferAnswerOptions& options) { |
| 2014 | RTC_DCHECK(IsUnifiedPlan()); |
| 2015 | |
| 2016 | if (options.offer_to_receive_audio == 0) { |
| 2017 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2018 | cricket::MEDIA_TYPE_AUDIO); |
| 2019 | } else if (options.offer_to_receive_audio == 1) { |
| 2020 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 2021 | } else if (options.offer_to_receive_audio > 1) { |
| 2022 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2023 | "offer_to_receive_audio > 1 is not supported."); |
| 2024 | } |
| 2025 | |
| 2026 | if (options.offer_to_receive_video == 0) { |
| 2027 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2028 | cricket::MEDIA_TYPE_VIDEO); |
| 2029 | } else if (options.offer_to_receive_video == 1) { |
| 2030 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 2031 | } else if (options.offer_to_receive_video > 1) { |
| 2032 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2033 | "offer_to_receive_video > 1 is not supported."); |
| 2034 | } |
| 2035 | |
| 2036 | return RTCError::OK(); |
| 2037 | } |
| 2038 | |
| 2039 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2040 | cricket::MediaType media_type) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2041 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 2042 | RtpTransceiverDirection new_direction = |
| 2043 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 2044 | if (new_direction != transceiver->direction()) { |
| 2045 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 2046 | << " transceiver (MID=" |
| 2047 | << transceiver->mid().value_or("<not set>") << ") from " |
| 2048 | << RtpTransceiverDirectionToString( |
| 2049 | transceiver->direction()) |
| 2050 | << " to " |
| 2051 | << RtpTransceiverDirectionToString(new_direction) |
| 2052 | << " since CreateOffer specified offer_to_receive=0"; |
| 2053 | transceiver->internal()->set_direction(new_direction); |
| 2054 | } |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 2059 | cricket::MediaType media_type) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 2060 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2061 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 2062 | RTC_LOG(LS_INFO) |
| 2063 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 2064 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2065 | RtpTransceiverInit init; |
| 2066 | init.direction = RtpTransceiverDirection::kRecvOnly; |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2067 | AddTransceiver(media_type, nullptr, init, |
| 2068 | /*update_negotiation_needed=*/false); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2073 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 2074 | std::vector< |
| 2075 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2076 | receiving_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2077 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 2078 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2079 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 2080 | receiving_transceivers.push_back(transceiver); |
| 2081 | } |
| 2082 | } |
| 2083 | return receiving_transceivers; |
| 2084 | } |
| 2085 | |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2086 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 2087 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2088 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2089 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2090 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2091 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2092 | return; |
| 2093 | } |
| 2094 | |
Steve Anton | dffead8 | 2018-02-06 18:31:29 | [diff] [blame] | 2095 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 2096 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 2097 | std::string error = |
| 2098 | "PeerConnection cannot create an answer in a state other than " |
| 2099 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2100 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2101 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 13:02:10 | [diff] [blame] | 2102 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2103 | return; |
| 2104 | } |
| 2105 | |
Steve Anton | dffead8 | 2018-02-06 18:31:29 | [diff] [blame] | 2106 | // The remote description should be set if we're in the right state. |
| 2107 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2108 | |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2109 | if (IsUnifiedPlan()) { |
| 2110 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 2111 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 2112 | "supported with Unified Plan semantics. Use the " |
| 2113 | "RtpTransceiver API instead."; |
| 2114 | } |
| 2115 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 2116 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 2117 | "supported with Unified Plan semantics. Use the " |
| 2118 | "RtpTransceiver API instead."; |
| 2119 | } |
| 2120 | } |
| 2121 | |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2122 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 2123 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2124 | |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 2125 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | void PeerConnection::SetLocalDescription( |
| 2129 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2130 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 2131 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 2132 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2133 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2134 | // The SetLocalDescription contract is that we take ownership of the session |
| 2135 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 2136 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 2137 | // description as a unique_ptr argument to formalize this agreement. |
| 2138 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 2139 | |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2140 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2141 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2142 | return; |
| 2143 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2144 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2145 | if (!desc) { |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2146 | PostSetSessionDescriptionFailure( |
| 2147 | observer, |
| 2148 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2149 | return; |
| 2150 | } |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2151 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2152 | // If a session error has occurred the PeerConnection is in a possibly |
| 2153 | // inconsistent state so fail right away. |
| 2154 | if (session_error() != SessionError::kNone) { |
| 2155 | std::string error_message = GetSessionErrorMsg(); |
| 2156 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2157 | PostSetSessionDescriptionFailure( |
| 2158 | observer, |
| 2159 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2160 | return; |
| 2161 | } |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2162 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2163 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 2164 | if (!error.ok()) { |
| 2165 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2166 | cricket::CS_LOCAL, desc->GetType(), error); |
| 2167 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2168 | PostSetSessionDescriptionFailure( |
| 2169 | observer, |
| 2170 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2171 | return; |
| 2172 | } |
| 2173 | |
| 2174 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 2175 | // which may destroy it before returning. |
| 2176 | const SdpType type = desc->GetType(); |
| 2177 | |
| 2178 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2179 | // |desc| may be destroyed at this point. |
| 2180 | |
| 2181 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2182 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 2183 | // inconsistent state, so act conservatively here and set the session error |
| 2184 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2185 | SetSessionError(SessionError::kContent, error.message()); |
| 2186 | std::string error_message = |
| 2187 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 2188 | RTC_LOG(LS_ERROR) << error_message; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2189 | PostSetSessionDescriptionFailure( |
| 2190 | observer, |
| 2191 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2192 | return; |
| 2193 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2194 | RTC_DCHECK(local_description()); |
| 2195 | |
| 2196 | PostSetSessionDescriptionSuccess(observer); |
| 2197 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 2198 | // MaybeStartGathering needs to be called after posting |
| 2199 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 2200 | // before signaling that SetLocalDescription completed. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2201 | transport_controller_->MaybeStartGathering(); |
| 2202 | |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 2203 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2204 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2205 | // MaybeStartGathering... |
| 2206 | network_thread()->Invoke<void>( |
| 2207 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2208 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 2209 | // Make UMA notes about what was agreed to. |
| 2210 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2211 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2212 | |
| 2213 | if (IsUnifiedPlan()) { |
| 2214 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2215 | UpdateNegotiationNeeded(); |
| 2216 | if (signaling_state() == kStable && was_negotiation_needed && |
| 2217 | is_negotiation_needed_) { |
| 2218 | Observer()->OnRenegotiationNeeded(); |
| 2219 | } |
| 2220 | } |
| 2221 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2222 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | RTCError PeerConnection::ApplyLocalDescription( |
| 2226 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2227 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2228 | RTC_DCHECK(desc); |
| 2229 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2230 | // Update stats here so that we have the most recent stats for tracks and |
| 2231 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 2232 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2233 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2234 | // Take a reference to the old local description since it's used below to |
| 2235 | // compare against the new local description. When setting the new local |
| 2236 | // description, grab ownership of the replaced session description in case it |
| 2237 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2238 | // of the method. |
| 2239 | const SessionDescriptionInterface* old_local_description = |
| 2240 | local_description(); |
| 2241 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2242 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 2243 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2244 | replaced_local_description = pending_local_description_ |
| 2245 | ? std::move(pending_local_description_) |
| 2246 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2247 | current_local_description_ = std::move(desc); |
| 2248 | pending_local_description_ = nullptr; |
| 2249 | current_remote_description_ = std::move(pending_remote_description_); |
| 2250 | } else { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2251 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2252 | pending_local_description_ = std::move(desc); |
| 2253 | } |
| 2254 | // The session description to apply now must be accessed by |
| 2255 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2256 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2257 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 2258 | // Report statistics about any use of simulcast. |
| 2259 | ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription, |
| 2260 | *local_description()->description()); |
| 2261 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2262 | if (!is_caller_) { |
| 2263 | if (remote_description()) { |
| 2264 | // Remote description was applied first, so this PC is the callee. |
| 2265 | is_caller_ = false; |
| 2266 | } else { |
| 2267 | // Local description is applied first, so this PC is the caller. |
| 2268 | is_caller_ = true; |
| 2269 | } |
| 2270 | } |
| 2271 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2272 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2273 | if (!error.ok()) { |
| 2274 | return error; |
| 2275 | } |
| 2276 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2277 | if (IsUnifiedPlan()) { |
| 2278 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 2279 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2280 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2281 | if (!error.ok()) { |
| 2282 | return error; |
| 2283 | } |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2284 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2285 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2286 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 2287 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 2288 | // Note that code paths that don't set MID won't be able to use |
| 2289 | // information about DTLS transports. |
| 2290 | if (transceiver->mid()) { |
| 2291 | auto dtls_transport = |
| 2292 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2293 | transceiver->internal()->sender_internal()->set_transport( |
| 2294 | dtls_transport); |
| 2295 | transceiver->internal()->receiver_internal()->set_transport( |
| 2296 | dtls_transport); |
| 2297 | } |
| 2298 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2299 | const ContentInfo* content = |
| 2300 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2301 | if (!content) { |
| 2302 | continue; |
| 2303 | } |
| 2304 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2305 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2306 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2307 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2308 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2309 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2310 | // "recvonly", process the removal of a remote track for the media |
| 2311 | // description, given transceiver, removeList, and muteTracks. |
| 2312 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2313 | (transceiver->internal()->fired_direction() && |
| 2314 | RtpTransceiverDirectionHasRecv( |
| 2315 | *transceiver->internal()->fired_direction()))) { |
| 2316 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2317 | &removed_streams); |
| 2318 | } |
| 2319 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2320 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2321 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2322 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2323 | } |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2324 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2325 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2326 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2327 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2328 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2329 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2330 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2331 | } |
| 2332 | } else { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2333 | // Media channels will be created only when offer is set. These may use new |
| 2334 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2335 | if (type == SdpType::kOffer) { |
| 2336 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2337 | // description is applied. Restore back to old description. |
| 2338 | RTCError error = CreateChannels(*local_description()->description()); |
| 2339 | if (!error.ok()) { |
| 2340 | return error; |
| 2341 | } |
| 2342 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2343 | // Remove unused channels if MediaContentDescription is rejected. |
| 2344 | RemoveUnusedChannels(local_description()->description()); |
| 2345 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2346 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2347 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2348 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2349 | if (!error.ok()) { |
| 2350 | return error; |
| 2351 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2352 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2353 | if (remote_description()) { |
| 2354 | // Now that we have a local description, we can push down remote candidates. |
| 2355 | UseCandidatesInSessionDescription(remote_description()); |
| 2356 | } |
| 2357 | |
| 2358 | pending_ice_restarts_.clear(); |
| 2359 | if (session_error() != SessionError::kNone) { |
| 2360 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2361 | } |
| 2362 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2363 | // If setting the description decided our SSL role, allocate any necessary |
| 2364 | // SCTP sids. |
| 2365 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2366 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2367 | AllocateSctpSids(role); |
| 2368 | } |
| 2369 | |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2370 | if (IsUnifiedPlan()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2371 | for (const auto& transceiver : transceivers_) { |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2372 | const ContentInfo* content = |
| 2373 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2374 | if (!content) { |
| 2375 | continue; |
| 2376 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 2377 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2378 | if (content->rejected || !channel || channel->local_streams().empty()) { |
Steve Anton | 60b6c1d | 2018-06-13 18:32:27 | [diff] [blame] | 2379 | // 0 is a special value meaning "this sender has no associated send |
| 2380 | // stream". Need to call this so the sender won't attempt to configure |
| 2381 | // a no longer existing stream and run into DCHECKs in the lower |
| 2382 | // layers. |
| 2383 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 2384 | } else { |
| 2385 | // Get the StreamParams from the channel which could generate SSRCs. |
| 2386 | const std::vector<StreamParams>& streams = channel->local_streams(); |
| 2387 | transceiver->internal()->sender_internal()->set_stream_ids( |
| 2388 | streams[0].stream_ids()); |
| 2389 | transceiver->internal()->sender_internal()->SetSsrc( |
| 2390 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2391 | } |
| 2392 | } |
| 2393 | } else { |
| 2394 | // Plan B semantics. |
| 2395 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2396 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2397 | // local session description. |
| 2398 | const cricket::ContentInfo* audio_content = |
| 2399 | GetFirstAudioContent(local_description()->description()); |
| 2400 | if (audio_content) { |
| 2401 | if (audio_content->rejected) { |
| 2402 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2403 | } else { |
| 2404 | const cricket::AudioContentDescription* audio_desc = |
| 2405 | audio_content->media_description()->as_audio(); |
| 2406 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2407 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 2408 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2409 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2410 | const cricket::ContentInfo* video_content = |
| 2411 | GetFirstVideoContent(local_description()->description()); |
| 2412 | if (video_content) { |
| 2413 | if (video_content->rejected) { |
| 2414 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2415 | } else { |
| 2416 | const cricket::VideoContentDescription* video_desc = |
| 2417 | video_content->media_description()->as_video(); |
| 2418 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2419 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 2420 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2424 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2425 | if (data_content) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 | [diff] [blame] | 2426 | const cricket::DataContentDescription* data_desc = |
| 2427 | data_content->media_description()->as_data(); |
| 2428 | if (absl::StartsWith(data_desc->protocol(), |
| 2429 | cricket::kMediaProtocolRtpPrefix)) { |
| 2430 | UpdateLocalRtpDataChannels(data_desc->streams()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2431 | } |
| 2432 | } |
| 2433 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2434 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2435 | } |
| 2436 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2437 | // The SDP parser used to populate these values by default for the 'content |
| 2438 | // name' if an a=mid line was absent. |
| 2439 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 2440 | switch (media_type) { |
| 2441 | case cricket::MEDIA_TYPE_AUDIO: |
| 2442 | return cricket::CN_AUDIO; |
| 2443 | case cricket::MEDIA_TYPE_VIDEO: |
| 2444 | return cricket::CN_VIDEO; |
| 2445 | case cricket::MEDIA_TYPE_DATA: |
| 2446 | return cricket::CN_DATA; |
| 2447 | } |
| 2448 | RTC_NOTREACHED(); |
| 2449 | return ""; |
| 2450 | } |
| 2451 | |
| 2452 | void PeerConnection::FillInMissingRemoteMids( |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2453 | cricket::SessionDescription* new_remote_description) { |
| 2454 | RTC_DCHECK(new_remote_description); |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2455 | const cricket::ContentInfos& local_contents = |
| 2456 | (local_description() ? local_description()->description()->contents() |
| 2457 | : cricket::ContentInfos()); |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2458 | const cricket::ContentInfos& remote_contents = |
| 2459 | (remote_description() ? remote_description()->description()->contents() |
| 2460 | : cricket::ContentInfos()); |
| 2461 | for (size_t i = 0; i < new_remote_description->contents().size(); ++i) { |
| 2462 | cricket::ContentInfo& content = new_remote_description->contents()[i]; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2463 | if (!content.name.empty()) { |
| 2464 | continue; |
| 2465 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2466 | std::string new_mid; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2467 | absl::string_view source_explanation; |
| 2468 | if (IsUnifiedPlan()) { |
| 2469 | if (i < local_contents.size()) { |
| 2470 | new_mid = local_contents[i].name; |
| 2471 | source_explanation = "from the matching local media section"; |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2472 | } else if (i < remote_contents.size()) { |
| 2473 | new_mid = remote_contents[i].name; |
| 2474 | source_explanation = "from the matching previous remote media section"; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2475 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2476 | new_mid = mid_generator_(); |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2477 | source_explanation = "generated just now"; |
| 2478 | } |
| 2479 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2480 | new_mid = std::string( |
| 2481 | GetDefaultMidForPlanB(content.media_description()->type())); |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2482 | source_explanation = "to match pre-existing behavior"; |
| 2483 | } |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2484 | RTC_DCHECK(!new_mid.empty()); |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2485 | content.name = new_mid; |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2486 | new_remote_description->transport_infos()[i].content_name = new_mid; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2487 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 2488 | << " is missing an a=mid line. Filling in the value '" |
| 2489 | << new_mid << "' " << source_explanation << "."; |
| 2490 | } |
| 2491 | } |
| 2492 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2493 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 | [diff] [blame] | 2494 | SetSessionDescriptionObserver* observer, |
| 2495 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 2496 | SetRemoteDescription( |
| 2497 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 2498 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 2499 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 2500 | } |
| 2501 | |
| 2502 | void PeerConnection::SetRemoteDescription( |
| 2503 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2504 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 2505 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 2506 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2507 | |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2508 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2509 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2510 | return; |
| 2511 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2512 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2513 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 2514 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2515 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2516 | return; |
| 2517 | } |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2518 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2519 | // If a session error has occurred the PeerConnection is in a possibly |
| 2520 | // inconsistent state so fail right away. |
| 2521 | if (session_error() != SessionError::kNone) { |
| 2522 | std::string error_message = GetSessionErrorMsg(); |
| 2523 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2524 | observer->OnSetRemoteDescriptionComplete( |
| 2525 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2526 | return; |
| 2527 | } |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 2528 | |
Steve Anton | ba42e99 | 2018-04-09 21:10:01 | [diff] [blame] | 2529 | if (desc->GetType() == SdpType::kOffer) { |
| 2530 | // Report to UMA the format of the received offer. |
| 2531 | ReportSdpFormatReceived(*desc); |
| 2532 | } |
| 2533 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2534 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 2535 | // points. |
| 2536 | FillInMissingRemoteMids(desc->description()); |
| 2537 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2538 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 2539 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2540 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2541 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2542 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 2543 | observer->OnSetRemoteDescriptionComplete( |
| 2544 | RTCError(error.type(), std::move(error_message))); |
| 2545 | return; |
| 2546 | } |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 2547 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2548 | // Grab the description type before moving ownership to |
| 2549 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2550 | const SdpType type = desc->GetType(); |
| 2551 | |
| 2552 | error = ApplyRemoteDescription(std::move(desc)); |
| 2553 | // |desc| may be destroyed at this point. |
| 2554 | |
| 2555 | if (!error.ok()) { |
| 2556 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2557 | // inconsistent state, so act conservatively here and set the session error |
| 2558 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2559 | SetSessionError(SessionError::kContent, error.message()); |
| 2560 | std::string error_message = |
| 2561 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2562 | RTC_LOG(LS_ERROR) << error_message; |
| 2563 | observer->OnSetRemoteDescriptionComplete( |
| 2564 | RTCError(error.type(), std::move(error_message))); |
| 2565 | return; |
| 2566 | } |
| 2567 | RTC_DCHECK(remote_description()); |
| 2568 | |
| 2569 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2570 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2571 | // MaybeStartGathering... |
| 2572 | network_thread()->Invoke<void>( |
| 2573 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2574 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 22:48:00 | [diff] [blame] | 2575 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 2576 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2577 | } |
| 2578 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2579 | if (IsUnifiedPlan()) { |
| 2580 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2581 | UpdateNegotiationNeeded(); |
| 2582 | if (signaling_state() == kStable && was_negotiation_needed && |
| 2583 | is_negotiation_needed_) { |
| 2584 | Observer()->OnRenegotiationNeeded(); |
| 2585 | } |
| 2586 | } |
| 2587 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2588 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2589 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2593 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2594 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2595 | RTC_DCHECK(desc); |
| 2596 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2597 | // Update stats here so that we have the most recent stats for tracks and |
| 2598 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 2599 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2600 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2601 | // Take a reference to the old remote description since it's used below to |
| 2602 | // compare against the new remote description. When setting the new remote |
| 2603 | // description, grab ownership of the replaced session description in case it |
| 2604 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2605 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2606 | const SessionDescriptionInterface* old_remote_description = |
| 2607 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2608 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 2609 | SdpType type = desc->GetType(); |
| 2610 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2611 | replaced_remote_description = pending_remote_description_ |
| 2612 | ? std::move(pending_remote_description_) |
| 2613 | : std::move(current_remote_description_); |
| 2614 | current_remote_description_ = std::move(desc); |
| 2615 | pending_remote_description_ = nullptr; |
| 2616 | current_local_description_ = std::move(pending_local_description_); |
| 2617 | } else { |
| 2618 | replaced_remote_description = std::move(pending_remote_description_); |
| 2619 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2620 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2621 | // The session description to apply now must be accessed by |
| 2622 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2623 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2624 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 2625 | // Report statistics about any use of simulcast. |
| 2626 | ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription, |
| 2627 | *remote_description()->description()); |
| 2628 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2629 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 2630 | if (!error.ok()) { |
| 2631 | return error; |
| 2632 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2633 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2634 | if (IsUnifiedPlan()) { |
| 2635 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 2636 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2637 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2638 | if (!error.ok()) { |
| 2639 | return error; |
| 2640 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2641 | } else { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2642 | // Media channels will be created only when offer is set. These may use new |
| 2643 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2644 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2645 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2646 | // description is applied. Restore back to old description. |
| 2647 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2648 | if (!error.ok()) { |
| 2649 | return error; |
| 2650 | } |
| 2651 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2652 | // Remove unused channels if MediaContentDescription is rejected. |
| 2653 | RemoveUnusedChannels(remote_description()->description()); |
| 2654 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2655 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2656 | // NOTE: Candidates allocation will be initiated only when |
| 2657 | // SetLocalDescription is called. |
| 2658 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 2659 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2660 | if (!error.ok()) { |
| 2661 | return error; |
| 2662 | } |
| 2663 | |
| 2664 | if (local_description() && |
| 2665 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2666 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2667 | } |
| 2668 | |
| 2669 | if (old_remote_description) { |
| 2670 | for (const cricket::ContentInfo& content : |
| 2671 | old_remote_description->description()->contents()) { |
| 2672 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2673 | // password that indicates the remote peer requests an ICE restart. |
| 2674 | // TODO(deadbeef): When we start storing both the current and pending |
| 2675 | // remote description, this should reset pending_ice_restarts and compare |
| 2676 | // against the current description. |
| 2677 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2678 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 2679 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2680 | pending_ice_restarts_.insert(content.name); |
| 2681 | } |
| 2682 | } else { |
| 2683 | // We retain all received candidates only if ICE is not restarted. |
| 2684 | // When ICE is restarted, all previous candidates belong to an old |
| 2685 | // generation and should not be kept. |
| 2686 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2687 | // description should only contain candidates from the last set remote |
| 2688 | // description plus any candidates added since then. We should remove |
| 2689 | // this once we're sure it won't break anything. |
| 2690 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2691 | old_remote_description, content.name, mutable_remote_description()); |
| 2692 | } |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | if (session_error() != SessionError::kNone) { |
| 2697 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2698 | } |
| 2699 | |
| 2700 | // Set the the ICE connection state to connecting since the connection may |
| 2701 | // become writable with peer reflexive candidates before any remote candidate |
| 2702 | // is signaled. |
| 2703 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2704 | // is to have a new signal the indicates a change in checking state from the |
| 2705 | // transport and expose a new checking() member from transport that can be |
| 2706 | // read to determine the current checking state. The existing SignalConnecting |
| 2707 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 2708 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 21:32:17 | [diff] [blame] | 2709 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2710 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2711 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2712 | } |
| 2713 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2714 | // If setting the description decided our SSL role, allocate any necessary |
| 2715 | // SCTP sids. |
| 2716 | rtc::SSLRole role; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2717 | if (DataChannel::IsSctpLike(data_channel_type_) && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2718 | AllocateSctpSids(role); |
| 2719 | } |
| 2720 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2721 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-17 00:14:42 | [diff] [blame] | 2722 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 2723 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2724 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 22:28:13 | [diff] [blame] | 2725 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 2726 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2727 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2728 | const ContentInfo* content = |
| 2729 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2730 | if (!content) { |
| 2731 | continue; |
| 2732 | } |
| 2733 | const MediaContentDescription* media_desc = content->media_description(); |
| 2734 | RtpTransceiverDirection local_direction = |
| 2735 | RtpTransceiverDirectionReversed(media_desc->direction()); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 2736 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 2737 | // RTCSessionDescription: Set the associated remote streams given |
| 2738 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 2739 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 2740 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2741 | std::vector<std::string> stream_ids; |
| 2742 | if (!media_desc->streams().empty()) { |
| 2743 | // The remote description has signaled the stream IDs. |
| 2744 | stream_ids = media_desc->streams()[0].stream_ids(); |
Steve Anton | ef65ef1 | 2018-01-11 01:15:20 | [diff] [blame] | 2745 | } |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 2746 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 2747 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 2748 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 2749 | stream_ids, &added_streams, |
| 2750 | &removed_streams); |
| 2751 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 2752 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 2753 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 2754 | // process the addition of a remote track for the media description. |
| 2755 | if (!transceiver->fired_direction() || |
| 2756 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 2757 | RTC_LOG(LS_INFO) |
| 2758 | << "Processing the addition of a remote track for MID=" |
| 2759 | << content->name << "."; |
| 2760 | now_receiving_transceivers.push_back(transceiver); |
Henrik Boström | 5b14778 | 2018-12-04 10:25:05 | [diff] [blame] | 2761 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2762 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 2763 | // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2764 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 2765 | // removal of a remote track for the media description, given transceiver, |
| 2766 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2767 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2768 | (transceiver->fired_direction() && |
| 2769 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 2770 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2771 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2772 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 2773 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2774 | transceiver->internal()->set_fired_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 2775 | // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2776 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2777 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 2778 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2779 | // direction. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2780 | transceiver->internal()->set_current_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 2781 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 2782 | if (transceiver->mid()) { |
| 2783 | auto dtls_transport = |
| 2784 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2785 | transceiver->internal()->sender_internal()->set_transport( |
| 2786 | dtls_transport); |
| 2787 | transceiver->internal()->receiver_internal()->set_transport( |
| 2788 | dtls_transport); |
| 2789 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2790 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 2791 | // 2.2.8.1.12: If the media description is rejected, and transceiver is |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2792 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2793 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 2794 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 2795 | << " since the media section was rejected."; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2796 | transceiver->Stop(); |
| 2797 | } |
Seth Hampson | 2f0d702 | 2018-02-20 19:54:42 | [diff] [blame] | 2798 | if (!content->rejected && |
| 2799 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2800 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2801 | uint32_t ssrc = 0; |
Seth Hampson | 5897a6e | 2018-04-03 18:16:33 | [diff] [blame] | 2802 | if (!media_desc->streams().empty() && |
| 2803 | media_desc->streams()[0].has_ssrcs()) { |
Seth Hampson | 2f0d702 | 2018-02-20 19:54:42 | [diff] [blame] | 2804 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2805 | } |
| 2806 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2807 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2808 | } |
Steve Anton | c49bcd9 | 2018-02-14 22:28:13 | [diff] [blame] | 2809 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2810 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2811 | for (const auto& transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 20:59:16 | [diff] [blame] | 2812 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2813 | observer->OnTrack(transceiver); |
| 2814 | observer->OnAddTrack(transceiver->receiver(), |
| 2815 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-11 01:15:20 | [diff] [blame] | 2816 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2817 | for (const auto& stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2818 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 22:28:13 | [diff] [blame] | 2819 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2820 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2821 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 2822 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2823 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2824 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 2825 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2826 | } |
| 2827 | |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2828 | const cricket::ContentInfo* audio_content = |
| 2829 | GetFirstAudioContent(remote_description()->description()); |
| 2830 | const cricket::ContentInfo* video_content = |
| 2831 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 2832 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2833 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 2834 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2835 | GetFirstVideoContentDescription(remote_description()->description()); |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 | [diff] [blame] | 2836 | const cricket::DataContentDescription* data_desc = |
| 2837 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 2838 | |
| 2839 | // Check if the descriptions include streams, just in case the peer supports |
| 2840 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2841 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 2842 | (audio_desc && !audio_desc->streams().empty()) || |
| 2843 | (video_desc && !video_desc->streams().empty())) { |
| 2844 | remote_peer_supports_msid_ = true; |
| 2845 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2846 | |
| 2847 | // We wait to signal new streams until we finish processing the description, |
| 2848 | // since only at that point will new streams have all their tracks. |
| 2849 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2850 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2851 | if (!IsUnifiedPlan()) { |
| 2852 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2853 | // rejected media section, there is some cleanup logic that expects the |
| 2854 | // voice/ video channel to still be set. But in this method the voice/video |
| 2855 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2856 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2857 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2858 | // this. |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2859 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2860 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2861 | // and MediaStreams. |
| 2862 | if (audio_content) { |
| 2863 | if (audio_content->rejected) { |
| 2864 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2865 | } else { |
| 2866 | bool default_audio_track_needed = |
| 2867 | !remote_peer_supports_msid_ && |
| 2868 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2869 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2870 | default_audio_track_needed, audio_desc->type(), |
| 2871 | new_streams); |
| 2872 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 2873 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2874 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2875 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2876 | // and MediaStreams. |
| 2877 | if (video_content) { |
| 2878 | if (video_content->rejected) { |
| 2879 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2880 | } else { |
| 2881 | bool default_video_track_needed = |
| 2882 | !remote_peer_supports_msid_ && |
| 2883 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2884 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2885 | default_video_track_needed, video_desc->type(), |
| 2886 | new_streams); |
| 2887 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 2888 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2889 | |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 | [diff] [blame] | 2890 | // Update the DataChannels with the information from the remote peer. |
| 2891 | if (data_desc) { |
| 2892 | if (absl::StartsWith(data_desc->protocol(), |
| 2893 | cricket::kMediaProtocolRtpPrefix)) { |
| 2894 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2895 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2896 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2897 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2898 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2899 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2900 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2901 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2902 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2903 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2904 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2905 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2906 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2907 | UpdateEndedRemoteMediaStreams(); |
| 2908 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2909 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2910 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 23:42:33 | [diff] [blame] | 2911 | } |
| 2912 | |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 2913 | void PeerConnection::SetAssociatedRemoteStreams( |
| 2914 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 2915 | const std::vector<std::string>& stream_ids, |
| 2916 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 2917 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2918 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 2919 | for (const std::string& stream_id : stream_ids) { |
| 2920 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2921 | remote_streams_->find(stream_id); |
| 2922 | if (!stream) { |
| 2923 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2924 | MediaStream::Create(stream_id)); |
| 2925 | remote_streams_->AddStream(stream); |
| 2926 | added_streams->push_back(stream); |
| 2927 | } |
| 2928 | media_streams.push_back(stream); |
| 2929 | } |
| 2930 | // Special case: "a=msid" missing, use random stream ID. |
| 2931 | if (media_streams.empty() && |
| 2932 | !(remote_description()->description()->msid_signaling() & |
| 2933 | cricket::kMsidSignalingMediaSection)) { |
| 2934 | if (!missing_msid_default_stream_) { |
| 2935 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 2936 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 2937 | added_streams->push_back(missing_msid_default_stream_); |
| 2938 | } |
| 2939 | media_streams.push_back(missing_msid_default_stream_); |
| 2940 | } |
| 2941 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 2942 | receiver->streams(); |
| 2943 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 2944 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 2945 | // update the stream-track relationships in a later step. We do this earlier, |
| 2946 | // changing the order of things, but the end-result is the same. |
| 2947 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 2948 | // instead. https://crbug.com/webrtc/9480 |
| 2949 | receiver->SetStreams(media_streams); |
| 2950 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 2951 | } |
| 2952 | |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2953 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 2954 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2955 | transceiver, |
| 2956 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 2957 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2958 | RTC_DCHECK(transceiver->mid()); |
| 2959 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 2960 | << *transceiver->mid(); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 2961 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2962 | transceiver->internal()->receiver_internal()->streams(); |
| 2963 | // This will remove the remote track from the streams. |
| 2964 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 2965 | remove_list->push_back(transceiver); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 2966 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 2967 | } |
| 2968 | |
| 2969 | void PeerConnection::RemoveRemoteStreamsIfEmpty( |
| 2970 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 2971 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2972 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 2973 | // streams, see if the stream was removed by checking if this was the last |
| 2974 | // receiver with that stream ID. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2975 | for (const auto& remote_stream : remote_streams) { |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 2976 | if (remote_stream->GetAudioTracks().empty() && |
| 2977 | remote_stream->GetVideoTracks().empty()) { |
| 2978 | remote_streams_->RemoveStream(remote_stream); |
| 2979 | removed_streams->push_back(remote_stream); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2980 | } |
| 2981 | } |
| 2982 | } |
| 2983 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2984 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2985 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 2986 | const SessionDescriptionInterface& new_session, |
| 2987 | const SessionDescriptionInterface* old_local_description, |
| 2988 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2989 | RTC_DCHECK(IsUnifiedPlan()); |
| 2990 | |
Steve Anton | 7464fca | 2018-01-19 19:10:37 | [diff] [blame] | 2991 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2992 | if (new_session.GetType() == SdpType::kOffer) { |
| 2993 | auto bundle_group_or_error = |
| 2994 | GetEarlyBundleGroup(*new_session.description()); |
| 2995 | if (!bundle_group_or_error.ok()) { |
| 2996 | return bundle_group_or_error.MoveError(); |
| 2997 | } |
| 2998 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2999 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3000 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3001 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3002 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 3003 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3004 | cricket::MediaType media_type = new_content.media_description()->type(); |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 3005 | mid_generator_.AddKnownId(new_content.name); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3006 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3007 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3008 | const cricket::ContentInfo* old_local_content = nullptr; |
| 3009 | if (old_local_description && |
| 3010 | i < old_local_description->description()->contents().size()) { |
| 3011 | old_local_content = |
| 3012 | &old_local_description->description()->contents()[i]; |
| 3013 | } |
| 3014 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 3015 | if (old_remote_description && |
| 3016 | i < old_remote_description->description()->contents().size()) { |
| 3017 | old_remote_content = |
| 3018 | &old_remote_description->description()->contents()[i]; |
| 3019 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3020 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3021 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 3022 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3023 | if (!transceiver_or_error.ok()) { |
| 3024 | return transceiver_or_error.MoveError(); |
| 3025 | } |
| 3026 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3027 | RTCError error = |
| 3028 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 3029 | if (!error.ok()) { |
| 3030 | return error; |
| 3031 | } |
| 3032 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3033 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 3034 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3035 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 3036 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3037 | continue; |
| 3038 | } |
| 3039 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 3040 | if (!error.ok()) { |
| 3041 | return error; |
| 3042 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3043 | } else { |
| 3044 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3045 | "Unknown section type."); |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | return RTCError::OK(); |
| 3050 | } |
| 3051 | |
| 3052 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 3053 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3054 | transceiver, |
| 3055 | const cricket::ContentInfo& content, |
| 3056 | const cricket::ContentGroup* bundle_group) { |
| 3057 | RTC_DCHECK(IsUnifiedPlan()); |
| 3058 | RTC_DCHECK(transceiver); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 3059 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3060 | if (content.rejected) { |
| 3061 | if (channel) { |
| 3062 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 3063 | DestroyChannelInterface(channel); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3064 | } |
| 3065 | } else { |
| 3066 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3067 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3068 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3069 | } else { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3070 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3071 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3072 | } |
| 3073 | if (!channel) { |
| 3074 | LOG_AND_RETURN_ERROR( |
| 3075 | RTCErrorType::INTERNAL_ERROR, |
| 3076 | "Failed to create channel for mid=" + content.name); |
| 3077 | } |
| 3078 | transceiver->internal()->SetChannel(channel); |
| 3079 | } |
| 3080 | } |
| 3081 | return RTCError::OK(); |
| 3082 | } |
| 3083 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3084 | RTCError PeerConnection::UpdateDataChannel( |
| 3085 | cricket::ContentSource source, |
| 3086 | const cricket::ContentInfo& content, |
| 3087 | const cricket::ContentGroup* bundle_group) { |
| 3088 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 23:23:40 | [diff] [blame] | 3089 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 3090 | // will take care of rejecting it. |
| 3091 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3092 | } |
| 3093 | if (content.rejected) { |
| 3094 | DestroyDataChannel(); |
| 3095 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 3096 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3097 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3098 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3099 | "Failed to create data channel."); |
| 3100 | } |
| 3101 | } |
| 3102 | if (source == cricket::CS_REMOTE) { |
| 3103 | const MediaContentDescription* data_desc = content.media_description(); |
| 3104 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 3105 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 3106 | } |
| 3107 | } |
| 3108 | } |
| 3109 | return RTCError::OK(); |
| 3110 | } |
| 3111 | |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3112 | // This method will extract any send encodings that were sent by the remote |
| 3113 | // connection. This is currently only relevant for Simulcast scenario (where |
| 3114 | // the number of layers may be communicated by the server). |
| 3115 | static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription( |
| 3116 | const MediaContentDescription& desc) { |
| 3117 | if (!desc.HasSimulcast()) { |
| 3118 | return {}; |
| 3119 | } |
| 3120 | std::vector<RtpEncodingParameters> result; |
| 3121 | const SimulcastDescription& simulcast = desc.simulcast_description(); |
| 3122 | |
| 3123 | // This is a remote description, the parameters we are after should appear |
| 3124 | // as receive streams. |
| 3125 | for (const auto& alternatives : simulcast.receive_layers()) { |
| 3126 | RTC_DCHECK(!alternatives.empty()); |
| 3127 | // There is currently no way to specify or choose from alternatives. |
| 3128 | // We will always use the first alternative, which is the most preferred. |
| 3129 | const SimulcastLayer& layer = alternatives[0]; |
| 3130 | RtpEncodingParameters parameters; |
| 3131 | parameters.rid = layer.rid; |
| 3132 | parameters.active = !layer.is_paused; |
| 3133 | result.push_back(parameters); |
| 3134 | } |
| 3135 | |
| 3136 | return result; |
| 3137 | } |
| 3138 | |
| 3139 | static RTCError UpdateSimulcastLayerStatusInSender( |
| 3140 | const std::vector<SimulcastLayer>& layers, |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3141 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3142 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 3143 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3144 | std::vector<std::string> disabled_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3145 | |
| 3146 | // The simulcast envelope cannot be changed, only the status of the streams. |
| 3147 | // So we will iterate over the send encodings rather than the layers. |
| 3148 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 3149 | auto iter = std::find_if(layers.begin(), layers.end(), |
| 3150 | [&encoding](const SimulcastLayer& layer) { |
| 3151 | return layer.rid == encoding.rid; |
| 3152 | }); |
| 3153 | // A layer that cannot be found may have been removed by the remote party. |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3154 | if (iter == layers.end()) { |
| 3155 | disabled_layers.push_back(encoding.rid); |
| 3156 | continue; |
| 3157 | } |
| 3158 | |
| 3159 | encoding.active = !iter->is_paused; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3160 | } |
| 3161 | |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 3162 | RTCError result = sender->SetParametersInternal(parameters); |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3163 | if (result.ok()) { |
| 3164 | result = sender->DisableEncodingLayers(disabled_layers); |
| 3165 | } |
| 3166 | |
| 3167 | return result; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3168 | } |
| 3169 | |
Amit Hilbuch | aabd036 | 2019-03-01 21:14:46 | [diff] [blame] | 3170 | static bool SimulcastIsRejected( |
| 3171 | const ContentInfo* local_content, |
| 3172 | const MediaContentDescription& answer_media_desc) { |
| 3173 | bool simulcast_offered = local_content && |
| 3174 | local_content->media_description() && |
| 3175 | local_content->media_description()->HasSimulcast(); |
| 3176 | bool simulcast_answered = answer_media_desc.HasSimulcast(); |
| 3177 | bool rids_supported = RtpExtension::FindHeaderExtensionByUri( |
| 3178 | answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri); |
| 3179 | return simulcast_offered && (!simulcast_answered || !rids_supported); |
| 3180 | } |
| 3181 | |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3182 | static RTCError DisableSimulcastInSender( |
| 3183 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3184 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 3185 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3186 | if (parameters.encodings.size() <= 1) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3187 | return RTCError::OK(); |
| 3188 | } |
| 3189 | |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3190 | std::vector<std::string> disabled_layers; |
| 3191 | std::transform( |
| 3192 | parameters.encodings.begin() + 1, parameters.encodings.end(), |
| 3193 | std::back_inserter(disabled_layers), |
| 3194 | [](const RtpEncodingParameters& encoding) { return encoding.rid; }); |
| 3195 | return sender->DisableEncodingLayers(disabled_layers); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3196 | } |
| 3197 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3198 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 3199 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3200 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3201 | size_t mline_index, |
| 3202 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3203 | const ContentInfo* old_local_content, |
| 3204 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3205 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3206 | // If this is an offer then the m= section might be recycled. If the m= |
| 3207 | // section is being recycled (defined as: rejected in the current local or |
| 3208 | // remote description and not rejected in new description), dissociate the |
| 3209 | // currently associated RtpTransceiver by setting its mid property to null, |
| 3210 | // and discard the mapping between the transceiver and its m= section index. |
| 3211 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 3212 | old_remote_content)) { |
| 3213 | // We want to dissociate the transceiver that has the rejected mid. |
| 3214 | const std::string& old_mid = |
| 3215 | (old_local_content && old_local_content->rejected) |
| 3216 | ? old_local_content->name |
| 3217 | : old_remote_content->name; |
| 3218 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3219 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3220 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 3221 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 3222 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 3223 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | const MediaContentDescription* media_desc = content.media_description(); |
| 3227 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3228 | if (source == cricket::CS_LOCAL) { |
| 3229 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 3230 | // mapping between transceivers and m= section indices established when |
| 3231 | // creating the offer. |
| 3232 | if (!transceiver) { |
| 3233 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 3234 | } |
| 3235 | if (!transceiver) { |
| 3236 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3237 | "Unknown transceiver"); |
| 3238 | } |
| 3239 | } else { |
| 3240 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 3241 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 3242 | // of the same type... |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 3243 | // When simulcast is requested, a transceiver cannot be associated because |
| 3244 | // AddTrack cannot be called to initialize it. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3245 | if (!transceiver && |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 3246 | RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 3247 | !media_desc->HasSimulcast()) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3248 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 3249 | } |
| 3250 | // If no RtpTransceiver was found in the previous step, create one with a |
| 3251 | // recvonly direction. |
| 3252 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3253 | RTC_LOG(LS_INFO) << "Adding " |
| 3254 | << cricket::MediaTypeToString(media_desc->type()) |
| 3255 | << " transceiver for MID=" << content.name |
| 3256 | << " at i=" << mline_index |
| 3257 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 3258 | std::string sender_id = rtc::CreateRandomUuid(); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3259 | std::vector<RtpEncodingParameters> send_encodings = |
| 3260 | GetSendEncodingsFromRemoteDescription(*media_desc); |
| 3261 | auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {}, |
| 3262 | send_encodings); |
Steve Anton | 5f94aa2 | 2018-02-01 18:58:30 | [diff] [blame] | 3263 | std::string receiver_id; |
| 3264 | if (!media_desc->streams().empty()) { |
| 3265 | receiver_id = media_desc->streams()[0].id; |
| 3266 | } else { |
| 3267 | receiver_id = rtc::CreateRandomUuid(); |
| 3268 | } |
| 3269 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 3270 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3271 | transceiver->internal()->set_direction( |
| 3272 | RtpTransceiverDirection::kRecvOnly); |
| 3273 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3274 | |
| 3275 | // Check if the offer indicated simulcast but the answer rejected it. |
| 3276 | // This can happen when simulcast is not supported on the remote party. |
Amit Hilbuch | aabd036 | 2019-03-01 21:14:46 | [diff] [blame] | 3277 | if (SimulcastIsRejected(old_local_content, *media_desc)) { |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 3278 | RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3279 | RTCError error = |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3280 | DisableSimulcastInSender(transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3281 | if (!error.ok()) { |
| 3282 | RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast."; |
| 3283 | return std::move(error); |
| 3284 | } |
| 3285 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3286 | } |
| 3287 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3288 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3289 | LOG_AND_RETURN_ERROR( |
| 3290 | RTCErrorType::INVALID_PARAMETER, |
| 3291 | "Transceiver type does not match media description type."); |
| 3292 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3293 | if (media_desc->HasSimulcast()) { |
| 3294 | std::vector<SimulcastLayer> layers = |
| 3295 | source == cricket::CS_LOCAL |
| 3296 | ? media_desc->simulcast_description().send_layers().GetAllLayers() |
| 3297 | : media_desc->simulcast_description() |
| 3298 | .receive_layers() |
| 3299 | .GetAllLayers(); |
| 3300 | RTCError error = UpdateSimulcastLayerStatusInSender( |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3301 | layers, transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3302 | if (!error.ok()) { |
| 3303 | RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers."; |
| 3304 | return std::move(error); |
| 3305 | } |
| 3306 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3307 | // Associate the found or created RtpTransceiver with the m= section by |
| 3308 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 3309 | // section, and establish a mapping between the transceiver and the index of |
| 3310 | // the m= section. |
| 3311 | transceiver->internal()->set_mid(content.name); |
| 3312 | transceiver->internal()->set_mline_index(mline_index); |
| 3313 | return std::move(transceiver); |
| 3314 | } |
| 3315 | |
| 3316 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3317 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 3318 | RTC_DCHECK(IsUnifiedPlan()); |
| 3319 | for (auto transceiver : transceivers_) { |
| 3320 | if (transceiver->mid() == mid) { |
| 3321 | return transceiver; |
| 3322 | } |
| 3323 | } |
| 3324 | return nullptr; |
| 3325 | } |
| 3326 | |
| 3327 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3328 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 3329 | RTC_DCHECK(IsUnifiedPlan()); |
| 3330 | for (auto transceiver : transceivers_) { |
| 3331 | if (transceiver->internal()->mline_index() == mline_index) { |
| 3332 | return transceiver; |
| 3333 | } |
| 3334 | } |
| 3335 | return nullptr; |
| 3336 | } |
| 3337 | |
| 3338 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3339 | PeerConnection::FindAvailableTransceiverToReceive( |
| 3340 | cricket::MediaType media_type) const { |
| 3341 | RTC_DCHECK(IsUnifiedPlan()); |
| 3342 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3343 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3344 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3345 | // associated with any m= section and are not stopped, find the first such |
| 3346 | // RtpTransceiver. |
| 3347 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3348 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3349 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3350 | !transceiver->stopped()) { |
| 3351 | return transceiver; |
| 3352 | } |
| 3353 | } |
| 3354 | return nullptr; |
| 3355 | } |
| 3356 | |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 3357 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 3358 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3359 | transceiver, |
| 3360 | const SessionDescriptionInterface* sdesc) const { |
| 3361 | RTC_DCHECK(transceiver); |
| 3362 | RTC_DCHECK(sdesc); |
| 3363 | if (IsUnifiedPlan()) { |
| 3364 | if (!transceiver->internal()->mid()) { |
| 3365 | // This transceiver is not associated with a media section yet. |
| 3366 | return nullptr; |
| 3367 | } |
| 3368 | return sdesc->description()->GetContentByName( |
| 3369 | *transceiver->internal()->mid()); |
| 3370 | } else { |
| 3371 | // Plan B only allows at most one audio and one video section, so use the |
| 3372 | // first media section of that type. |
| 3373 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3374 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 3378 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 3379 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 3380 | return configuration_; |
| 3381 | } |
| 3382 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3383 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 3384 | RTCError* error) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 3385 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3386 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 3387 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3388 | if (IsClosed()) { |
| 3389 | RTC_LOG(LS_ERROR) << "SetConfiguration: PeerConnection is closed."; |
| 3390 | return SafeSetError(RTCErrorType::INVALID_STATE, error); |
| 3391 | } |
| 3392 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 3393 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 3394 | // size is not allowed, and changing the set of ICE servers will not result |
| 3395 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3396 | if (local_description() && configuration.ice_candidate_pool_size != |
| 3397 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 3398 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 3399 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3400 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 3401 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 3402 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 17:04:45 | [diff] [blame] | 3403 | if (local_description() && |
| 3404 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 3405 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 3406 | "SetLocalDescription."; |
| 3407 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3408 | } |
| 3409 | |
| 3410 | if (remote_description() && |
| 3411 | configuration.use_media_transport != configuration_.use_media_transport) { |
| 3412 | RTC_LOG(LS_ERROR) << "Can't change media_transport after calling " |
| 3413 | "SetRemoteDescription."; |
| 3414 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3415 | } |
| 3416 | |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 3417 | if (local_description() && |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 3418 | configuration.use_media_transport_for_data_channels != |
| 3419 | configuration_.use_media_transport_for_data_channels) { |
| 3420 | RTC_LOG(LS_ERROR) << "Can't change media_transport_for_data_channels " |
| 3421 | "after calling SetLocalDescription."; |
| 3422 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3423 | } |
| 3424 | |
| 3425 | if (remote_description() && |
| 3426 | configuration.use_media_transport_for_data_channels != |
| 3427 | configuration_.use_media_transport_for_data_channels) { |
| 3428 | RTC_LOG(LS_ERROR) << "Can't change media_transport_for_data_channels " |
| 3429 | "after calling SetRemoteDescription."; |
| 3430 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3431 | } |
| 3432 | |
| 3433 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 3434 | configuration.crypto_options != configuration_.crypto_options) { |
| 3435 | RTC_LOG(LS_ERROR) << "Can't change crypto_options after calling " |
| 3436 | "SetLocalDescription."; |
| 3437 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3438 | } |
| 3439 | |
Piotr (Peter) Slatala | 37227be | 2018-11-21 15:42:22 | [diff] [blame] | 3440 | if (configuration.use_media_transport_for_data_channels || |
| 3441 | configuration.use_media_transport) { |
| 3442 | RTC_CHECK(configuration.bundle_policy == kBundlePolicyMaxBundle) |
| 3443 | << "Media transport requires MaxBundle policy."; |
| 3444 | } |
| 3445 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3446 | // The simplest (and most future-compatible) way to tell if the config was |
| 3447 | // modified in an invalid way is to copy each property we do support |
| 3448 | // modifying, then use operator==. There are far more properties we don't |
| 3449 | // support modifying than those we do, and more could be added. |
| 3450 | RTCConfiguration modified_config = configuration_; |
| 3451 | modified_config.servers = configuration.servers; |
| 3452 | modified_config.type = configuration.type; |
| 3453 | modified_config.ice_candidate_pool_size = |
| 3454 | configuration.ice_candidate_pool_size; |
| 3455 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 3456 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 3457 | modified_config.ice_check_interval_strong_connectivity = |
| 3458 | configuration.ice_check_interval_strong_connectivity; |
| 3459 | modified_config.ice_check_interval_weak_connectivity = |
| 3460 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 3461 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 3462 | modified_config.ice_unwritable_min_checks = |
| 3463 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 3464 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 3465 | modified_config.stun_candidate_keepalive_interval = |
| 3466 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 3467 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 3468 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 3469 | modified_config.active_reset_srtp_params = |
| 3470 | configuration.active_reset_srtp_params; |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 17:04:45 | [diff] [blame] | 3471 | modified_config.use_media_transport = configuration.use_media_transport; |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 3472 | modified_config.use_media_transport_for_data_channels = |
| 3473 | configuration.use_media_transport_for_data_channels; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3474 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 3475 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3476 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 3477 | } |
| 3478 | |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 3479 | // Validate the modified configuration. |
| 3480 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 3481 | if (!validate_error.ok()) { |
| 3482 | return SafeSetError(std::move(validate_error), error); |
| 3483 | } |
| 3484 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3485 | // Note that this isn't possible through chromium, since it's an unsigned |
| 3486 | // short in WebIDL. |
| 3487 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 10:34:17 | [diff] [blame] | 3488 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3489 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 3490 | } |
| 3491 | |
| 3492 | // Parse ICE servers before hopping to network thread. |
| 3493 | cricket::ServerAddresses stun_servers; |
| 3494 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 3495 | RTCErrorType parse_error = |
| 3496 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 3497 | if (parse_error != RTCErrorType::NONE) { |
| 3498 | return SafeSetError(parse_error, error); |
| 3499 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 3500 | // Note if STUN or TURN servers were supplied. |
| 3501 | if (!stun_servers.empty()) { |
| 3502 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 3503 | } |
| 3504 | if (!turn_servers.empty()) { |
| 3505 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 3506 | } |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3507 | |
| 3508 | // In theory this shouldn't fail. |
| 3509 | if (!network_thread()->Invoke<bool>( |
| 3510 | RTC_FROM_HERE, |
| 3511 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 3512 | stun_servers, turn_servers, modified_config.type, |
| 3513 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 3514 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 3515 | modified_config.turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 3516 | modified_config.stun_candidate_keepalive_interval, |
| 3517 | static_cast<bool>(local_description())))) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 3518 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3519 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 3520 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 15:18:11 | [diff] [blame] | 3521 | |
deadbeef | d1a38b5 | 2016-12-10 21:15:33 | [diff] [blame] | 3522 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 3523 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 3524 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3525 | if (modified_config.servers != configuration_.servers || |
| 3526 | modified_config.type != configuration_.type || |
| 3527 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3528 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 21:15:33 | [diff] [blame] | 3529 | } |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 3530 | |
Qingsi Wang | 9c98f0c | 2018-02-15 23:10:59 | [diff] [blame] | 3531 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 21:31:15 | [diff] [blame] | 3532 | transport_controller_->SetMediaTransportSettings( |
| 3533 | modified_config.use_media_transport, |
| 3534 | modified_config.use_media_transport_for_data_channels); |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 3535 | |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 3536 | if (configuration_.active_reset_srtp_params != |
| 3537 | modified_config.active_reset_srtp_params) { |
| 3538 | transport_controller_->SetActiveResetSrtpParams( |
| 3539 | modified_config.active_reset_srtp_params); |
| 3540 | } |
| 3541 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3542 | configuration_ = modified_config; |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 3543 | use_media_transport_ = configuration.use_media_transport; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3544 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 3545 | } |
| 3546 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3547 | bool PeerConnection::AddIceCandidate( |
| 3548 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 3549 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 3550 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 3551 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3552 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 3553 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 3554 | return false; |
| 3555 | } |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3556 | |
| 3557 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3558 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 3559 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 3560 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3561 | return false; |
| 3562 | } |
| 3563 | |
| 3564 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3565 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 3566 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3567 | return false; |
| 3568 | } |
| 3569 | |
| 3570 | bool valid = false; |
| 3571 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 3572 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 3573 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3574 | return false; |
| 3575 | } |
| 3576 | |
| 3577 | // Add this candidate to the remote session description. |
| 3578 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3579 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 3580 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3581 | return false; |
| 3582 | } |
| 3583 | |
| 3584 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 3585 | bool result = UseCandidate(ice_candidate); |
| 3586 | if (result) { |
| 3587 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 3588 | if (ice_candidate->candidate().address().IsUnresolvedIP()) { |
| 3589 | NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
| 3590 | } |
| 3591 | if (ice_candidate->candidate().address().IsPrivateIP()) { |
| 3592 | NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
| 3593 | } |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 3594 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 3595 | } else { |
| 3596 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 3597 | } |
| 3598 | return result; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3599 | } else { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3600 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 3601 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3602 | return true; |
| 3603 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3604 | } |
| 3605 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 3606 | bool PeerConnection::RemoveIceCandidates( |
| 3607 | const std::vector<cricket::Candidate>& candidates) { |
| 3608 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 3609 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3610 | if (IsClosed()) { |
| 3611 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 3612 | return false; |
| 3613 | } |
| 3614 | |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3615 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3616 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 3617 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3618 | return false; |
| 3619 | } |
| 3620 | |
| 3621 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3622 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3623 | return false; |
| 3624 | } |
| 3625 | |
| 3626 | size_t number_removed = |
| 3627 | mutable_remote_description()->RemoveCandidates(candidates); |
| 3628 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 3629 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3630 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 3631 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 3632 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3636 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 3637 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3638 | RTC_LOG(LS_ERROR) |
| 3639 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 3640 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 3641 | } |
| 3642 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 3643 | } |
| 3644 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 3645 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 3646 | if (!worker_thread()->IsCurrent()) { |
| 3647 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 3648 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 3649 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 3650 | RTC_DCHECK_RUN_ON(worker_thread()); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 3651 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 3652 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 3653 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 3654 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 3655 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 3656 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3657 | "min_bitrate_bps <= 0"); |
| 3658 | } |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 3659 | if (has_start) { |
| 3660 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 3661 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 3662 | "start_bitrate_bps < min_bitrate_bps"); |
| 3663 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 3664 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3665 | "curent_bitrate_bps < 0"); |
| 3666 | } |
| 3667 | } |
| 3668 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 3669 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 3670 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 3671 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 3672 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 3673 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3674 | "max_bitrate_bps < min_bitrate_bps"); |
| 3675 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 3676 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3677 | "max_bitrate_bps < 0"); |
| 3678 | } |
| 3679 | } |
| 3680 | |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 3681 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 17:31:54 | [diff] [blame] | 3682 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 3683 | |
| 3684 | return RTCError::OK(); |
| 3685 | } |
| 3686 | |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 3687 | void PeerConnection::SetBitrateAllocationStrategy( |
| 3688 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 3689 | bitrate_allocation_strategy) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 3690 | if (!worker_thread()->IsCurrent()) { |
Karl Wiberg | 12ba3ad | 2019-03-26 10:18:39 | [diff] [blame] | 3691 | // TODO(kwiberg): Use a lambda instead when C++14 makes it possible to |
| 3692 | // move-capture values in lambdas. |
| 3693 | struct Task { |
| 3694 | PeerConnection* const pc; |
| 3695 | std::unique_ptr<rtc::BitrateAllocationStrategy> strategy; |
| 3696 | void operator()() { |
| 3697 | RTC_DCHECK_RUN_ON(pc->worker_thread()); |
| 3698 | pc->call_->SetBitrateAllocationStrategy(std::move(strategy)); |
| 3699 | } |
| 3700 | }; |
| 3701 | worker_thread()->Invoke<void>( |
| 3702 | RTC_FROM_HERE, Task{this, std::move(bitrate_allocation_strategy)}); |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 3703 | return; |
| 3704 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 3705 | RTC_DCHECK_RUN_ON(worker_thread()); |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 3706 | RTC_DCHECK(call_.get()); |
| 3707 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 3708 | } |
| 3709 | |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 3710 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 3711 | if (!worker_thread()->IsCurrent()) { |
| 3712 | worker_thread()->Invoke<void>( |
| 3713 | RTC_FROM_HERE, |
| 3714 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 3715 | return; |
| 3716 | } |
| 3717 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 3718 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 3719 | audio_state->SetPlayout(playout); |
| 3720 | } |
| 3721 | |
| 3722 | void PeerConnection::SetAudioRecording(bool recording) { |
| 3723 | if (!worker_thread()->IsCurrent()) { |
| 3724 | worker_thread()->Invoke<void>( |
| 3725 | RTC_FROM_HERE, |
| 3726 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 3727 | return; |
| 3728 | } |
| 3729 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 3730 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 3731 | audio_state->SetRecording(recording); |
| 3732 | } |
| 3733 | |
Steve Anton | 8c0f7a7 | 2017-10-03 17:03:10 | [diff] [blame] | 3734 | std::unique_ptr<rtc::SSLCertificate> |
| 3735 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 3736 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 3737 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 17:03:10 | [diff] [blame] | 3738 | return nullptr; |
| 3739 | } |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 3740 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 17:03:10 | [diff] [blame] | 3741 | } |
| 3742 | |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 3743 | std::unique_ptr<rtc::SSLCertChain> |
| 3744 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 3745 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | afb0bb7 | 2018-02-20 19:35:37 | [diff] [blame] | 3746 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 3747 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 3748 | return nullptr; |
| 3749 | } |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 3750 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 19:35:37 | [diff] [blame] | 3751 | audio_transceiver->internal()->channel()->transport_name()); |
| 3752 | } |
| 3753 | |
| 3754 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3755 | PeerConnection::GetFirstAudioTransceiver() const { |
| 3756 | for (auto transceiver : transceivers_) { |
| 3757 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3758 | return transceiver; |
| 3759 | } |
| 3760 | } |
| 3761 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 3762 | } |
| 3763 | |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 3764 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 3765 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 3766 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 3767 | const size_t max_size = (max_size_bytes < 0) |
| 3768 | ? RtcEventLog::kUnlimitedOutput |
| 3769 | : rtc::saturated_cast<size_t>(max_size_bytes); |
Bjorn Terelius | 8b55602 | 2018-11-27 14:43:01 | [diff] [blame] | 3770 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
| 3771 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
| 3772 | output_period_ms = 5000; |
| 3773 | } |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 3774 | return StartRtcEventLog( |
Karl Wiberg | 918f50c | 2018-07-05 09:40:33 | [diff] [blame] | 3775 | absl::make_unique<RtcEventLogOutputFile>(file, max_size), |
Bjorn Terelius | 8b55602 | 2018-11-27 14:43:01 | [diff] [blame] | 3776 | output_period_ms); |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 3777 | } |
| 3778 | |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 3779 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 3780 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 21:01:06 | [diff] [blame] | 3781 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 3782 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 3783 | bool operator()() { |
| 3784 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 3785 | } |
Karl Wiberg | d6b4819 | 2017-10-16 21:01:06 | [diff] [blame] | 3786 | PeerConnection* const pc; |
| 3787 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 3788 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 3789 | }; |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 3790 | return worker_thread()->Invoke<bool>( |
| 3791 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 3795 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 3796 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 3797 | } |
| 3798 | |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 3799 | rtc::scoped_refptr<DtlsTransportInterface> |
| 3800 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 3801 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 3802 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3803 | } |
| 3804 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3805 | rtc::scoped_refptr<DtlsTransport> |
| 3806 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 3807 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3808 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 3809 | } |
| 3810 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 3811 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 3812 | const { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 3813 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 3814 | return sctp_transport_; |
| 3815 | } |
| 3816 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3817 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 3818 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3819 | return pending_local_description_ ? pending_local_description_.get() |
| 3820 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 3824 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3825 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3826 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3827 | } |
| 3828 | |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 3829 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3830 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 3831 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3832 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3836 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 3837 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3838 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3842 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 3843 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3844 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3848 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 3849 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3850 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 3851 | } |
| 3852 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3853 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 3854 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 3855 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3856 | // Update stats here so that we have the most recent stats for tracks and |
| 3857 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 3858 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3859 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3860 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 3861 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 3862 | |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3863 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 3864 | transceiver->Stop(); |
| 3865 | } |
Steve Anton | 25cfeb9 | 2018-04-26 18:44:00 | [diff] [blame] | 3866 | |
| 3867 | // Ensure that all asynchronous stats requests are completed before destroying |
| 3868 | // the transport controller below. |
| 3869 | if (stats_collector_) { |
| 3870 | stats_collector_->WaitForPendingRequest(); |
| 3871 | } |
| 3872 | |
| 3873 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 3874 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 3875 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3876 | |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 3877 | // The event log is used in the transport controller, which must be outlived |
| 3878 | // by the former. CreateOffer by the peer connection is implemented |
| 3879 | // asynchronously and if the peer connection is closed without resetting the |
| 3880 | // WebRTC session description factory, the session description factory would |
| 3881 | // call the transport controller. |
| 3882 | webrtc_session_desc_factory_.reset(); |
| 3883 | transport_controller_.reset(); |
| 3884 | |
deadbeef | 42a4263 | 2017-03-10 23:18:00 | [diff] [blame] | 3885 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 3886 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3887 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 3888 | |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 3889 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 3890 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 3891 | call_.reset(); |
| 3892 | // The event log must outlive call (and any other object that uses it). |
| 3893 | event_log_.reset(); |
| 3894 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 3895 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3896 | // The .h file says that observer can be discarded after close() returns. |
| 3897 | // Make sure this is true. |
| 3898 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3899 | } |
| 3900 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 3901 | void PeerConnection::OnMessage(rtc::Message* msg) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 3902 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 3903 | switch (msg->message_id) { |
| 3904 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3905 | SetSessionDescriptionMsg* param = |
| 3906 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3907 | param->observer->OnSuccess(); |
| 3908 | delete param; |
| 3909 | break; |
| 3910 | } |
| 3911 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3912 | SetSessionDescriptionMsg* param = |
| 3913 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3914 | param->observer->OnFailure(std::move(param->error)); |
| 3915 | delete param; |
| 3916 | break; |
| 3917 | } |
| 3918 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3919 | CreateSessionDescriptionMsg* param = |
| 3920 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 3921 | param->observer->OnFailure(std::move(param->error)); |
| 3922 | delete param; |
| 3923 | break; |
| 3924 | } |
| 3925 | case MSG_GETSTATS: { |
| 3926 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 3927 | StatsReports reports; |
| 3928 | stats_->GetStats(param->track, &reports); |
| 3929 | param->observer->OnComplete(reports); |
| 3930 | delete param; |
| 3931 | break; |
| 3932 | } |
| 3933 | case MSG_FREE_DATACHANNELS: { |
| 3934 | sctp_data_channels_to_free_.clear(); |
| 3935 | break; |
| 3936 | } |
| 3937 | case MSG_REPORT_USAGE_PATTERN: { |
| 3938 | ReportUsagePattern(); |
| 3939 | break; |
| 3940 | } |
| 3941 | default: |
| 3942 | RTC_NOTREACHED() << "Not implemented"; |
| 3943 | break; |
| 3944 | } |
| 3945 | } |
| 3946 | |
Steve Anton | afb0bb7 | 2018-02-20 19:35:37 | [diff] [blame] | 3947 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3948 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3949 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3950 | GetAudioTransceiver()->internal()->channel()); |
| 3951 | if (voice_channel) { |
| 3952 | return voice_channel->media_channel(); |
| 3953 | } else { |
| 3954 | return nullptr; |
| 3955 | } |
| 3956 | } |
| 3957 | |
| 3958 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3959 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3960 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3961 | GetVideoTransceiver()->internal()->channel()); |
| 3962 | if (video_channel) { |
| 3963 | return video_channel->media_channel(); |
| 3964 | } else { |
| 3965 | return nullptr; |
| 3966 | } |
| 3967 | } |
| 3968 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 3969 | void PeerConnection::CreateAudioReceiver( |
| 3970 | MediaStreamInterface* stream, |
| 3971 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 12:41:51 | [diff] [blame] | 3972 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3973 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 3974 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3975 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 3976 | auto* audio_receiver = new AudioRtpReceiver( |
| 3977 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 3978 | audio_receiver->SetMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 3979 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3980 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3981 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 3982 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 20:38:12 | [diff] [blame] | 3983 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 3984 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3985 | } |
| 3986 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 3987 | void PeerConnection::CreateVideoReceiver( |
| 3988 | MediaStreamInterface* stream, |
| 3989 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 12:41:51 | [diff] [blame] | 3990 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3991 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 3992 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 3993 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 3994 | auto* video_receiver = new VideoRtpReceiver( |
| 3995 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 3996 | video_receiver->SetMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 3997 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3998 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3999 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4000 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 20:38:12 | [diff] [blame] | 4001 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4002 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4003 | } |
| 4004 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 4005 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 4006 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4007 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4008 | const RtpSenderInfo& remote_sender_info) { |
| 4009 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 4010 | if (!receiver) { |
| 4011 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 4012 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4013 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 4014 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4015 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4016 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 4017 | } else { |
| 4018 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 4019 | } |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4020 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4021 | } |
| 4022 | |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4023 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 4024 | MediaStreamInterface* stream) { |
| 4025 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4026 | RTC_DCHECK(track); |
| 4027 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4028 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4029 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4030 | // We already have a sender for this track, so just change the stream_id |
| 4031 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 4032 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4033 | return; |
| 4034 | } |
| 4035 | |
| 4036 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4037 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 4038 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4039 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4040 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4041 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 4042 | // which will connect the sender to the underlying transport. This can |
| 4043 | // occur if a local session description that contains the ID of the sender |
| 4044 | // is set before AddStream is called. It can also occur if the local |
| 4045 | // session description is not changed and RemoveStream is called, and |
| 4046 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4047 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 4048 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4049 | if (sender_info) { |
| 4050 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4051 | } |
| 4052 | } |
| 4053 | |
| 4054 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 4055 | // indefinitely, when we have unified plan SDP. |
| 4056 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 4057 | MediaStreamInterface* stream) { |
| 4058 | RTC_DCHECK(!IsClosed()); |
| 4059 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4060 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4061 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4062 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4063 | return; |
| 4064 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4065 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 4069 | MediaStreamInterface* stream) { |
| 4070 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4071 | RTC_DCHECK(track); |
| 4072 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4073 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4074 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4075 | // We already have a sender for this track, so just change the stream_id |
| 4076 | // so that it's correct in the next call to CreateOffer. |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 4077 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4078 | return; |
| 4079 | } |
| 4080 | |
| 4081 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4082 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 4083 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4084 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4085 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 4086 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 4087 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4088 | if (sender_info) { |
| 4089 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4090 | } |
| 4091 | } |
| 4092 | |
| 4093 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 4094 | MediaStreamInterface* stream) { |
| 4095 | RTC_DCHECK(!IsClosed()); |
| 4096 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4097 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4098 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4099 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4100 | return; |
| 4101 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4102 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4103 | } |
| 4104 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4105 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4106 | if (ice_connection_state_ == new_state) { |
| 4107 | return; |
| 4108 | } |
| 4109 | |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 4110 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4111 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4112 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 4113 | return; |
| 4114 | } |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4115 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4116 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 4117 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4118 | RTC_DCHECK(ice_connection_state_ != |
| 4119 | PeerConnectionInterface::kIceConnectionClosed); |
| 4120 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4121 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4122 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4123 | } |
| 4124 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4125 | void PeerConnection::SetStandardizedIceConnectionState( |
| 4126 | PeerConnectionInterface::IceConnectionState new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4127 | if (standardized_ice_connection_state_ == new_state) |
| 4128 | return; |
| 4129 | if (IsClosed()) |
| 4130 | return; |
| 4131 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 10:25:14 | [diff] [blame] | 4132 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4133 | } |
| 4134 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 4135 | void PeerConnection::SetConnectionState( |
| 4136 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 4137 | if (connection_state_ == new_state) |
| 4138 | return; |
| 4139 | if (IsClosed()) |
| 4140 | return; |
| 4141 | connection_state_ = new_state; |
| 4142 | Observer()->OnConnectionChange(new_state); |
| 4143 | } |
| 4144 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4145 | void PeerConnection::OnIceGatheringChange( |
| 4146 | PeerConnectionInterface::IceGatheringState new_state) { |
| 4147 | if (IsClosed()) { |
| 4148 | return; |
| 4149 | } |
| 4150 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4151 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4152 | } |
| 4153 | |
jbauch | 81bf7b0 | 2017-03-25 15:31:12 | [diff] [blame] | 4154 | void PeerConnection::OnIceCandidate( |
| 4155 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4156 | if (IsClosed()) { |
| 4157 | return; |
| 4158 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4159 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 4160 | if (candidate->candidate().type() == LOCAL_PORT_TYPE && |
| 4161 | candidate->candidate().address().IsPrivateIP()) { |
| 4162 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 4163 | } |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 4164 | if (candidate->candidate().type() == LOCAL_PORT_TYPE && |
| 4165 | candidate->candidate().address().IsUnresolvedIP()) { |
| 4166 | NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED); |
| 4167 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4168 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4169 | } |
| 4170 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4171 | void PeerConnection::OnIceCandidatesRemoved( |
| 4172 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4173 | if (IsClosed()) { |
| 4174 | return; |
| 4175 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4176 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4177 | } |
| 4178 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4179 | void PeerConnection::ChangeSignalingState( |
| 4180 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4181 | if (signaling_state_ == signaling_state) { |
| 4182 | return; |
| 4183 | } |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4184 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 4185 | << GetSignalingStateString(signaling_state_) |
| 4186 | << " New state: " |
| 4187 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4188 | signaling_state_ = signaling_state; |
| 4189 | if (signaling_state == kClosed) { |
| 4190 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4191 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4192 | standardized_ice_connection_state_ = |
| 4193 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 4194 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 4195 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4196 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 4197 | ice_gathering_state_ = kIceGatheringComplete; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4198 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4199 | } |
| 4200 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4201 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4202 | } |
| 4203 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4204 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 4205 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4206 | if (IsClosed()) { |
| 4207 | return; |
| 4208 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4209 | AddAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4210 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4211 | } |
| 4212 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4213 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 4214 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4215 | if (IsClosed()) { |
| 4216 | return; |
| 4217 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4218 | RemoveAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4219 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 4223 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4224 | if (IsClosed()) { |
| 4225 | return; |
| 4226 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4227 | AddVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4228 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 4232 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4233 | if (IsClosed()) { |
| 4234 | return; |
| 4235 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4236 | RemoveVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4237 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4238 | } |
| 4239 | |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 4240 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 4241 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4242 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4243 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4244 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 4245 | } |
| 4246 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4247 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 4248 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4249 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 4250 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4251 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4252 | msg->error = std::move(error); |
| 4253 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4254 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 4258 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 4259 | RTCError error) { |
| 4260 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4261 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 4262 | msg->error = std::move(error); |
| 4263 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4264 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4265 | } |
| 4266 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4267 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4268 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4269 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4270 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4271 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4272 | if (IsUnifiedPlan()) { |
| 4273 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 4274 | } else { |
| 4275 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 4276 | } |
| 4277 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4278 | // Intentionally unset the data channel type for RTP data channel with the |
| 4279 | // second condition. Otherwise the RTP data channels would be successfully |
| 4280 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 4281 | // when building with chromium. We want to leave RTP data channels broken, so |
| 4282 | // people won't try to use them. |
| 4283 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4284 | session_options->data_channel_type = data_channel_type(); |
| 4285 | } |
| 4286 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4287 | // Apply ICE restart flag and renomination flag. |
| 4288 | for (auto& options : session_options->media_description_options) { |
| 4289 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 4290 | options.transport_options.enable_ice_renomination = |
| 4291 | configuration_.enable_ice_renomination; |
| 4292 | } |
| 4293 | |
| 4294 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 4295 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 4296 | session_options->pooled_ice_credentials = |
| 4297 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4298 | RTC_FROM_HERE, |
| 4299 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4300 | port_allocator_.get())); |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 4301 | session_options->offer_extmap_allow_mixed = |
| 4302 | configuration_.offer_extmap_allow_mixed; |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 4303 | |
| 4304 | if (configuration_.enable_dtls_srtp && |
| 4305 | !configuration_.enable_dtls_srtp.value()) { |
| 4306 | session_options->media_transport_settings = |
| 4307 | transport_controller_->GenerateOrGetLastMediaTransportOffer(); |
| 4308 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | void PeerConnection::GetOptionsForPlanBOffer( |
| 4312 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4313 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4314 | // Figure out transceiver directional preferences. |
| 4315 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4316 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4317 | |
| 4318 | // By default, generate sendrecv/recvonly m= sections. |
| 4319 | bool recv_audio = true; |
| 4320 | bool recv_video = true; |
| 4321 | |
| 4322 | // By default, only offer a new m= section if we have media to send with it. |
| 4323 | bool offer_new_audio_description = send_audio; |
| 4324 | bool offer_new_video_description = send_video; |
| 4325 | bool offer_new_data_description = HasDataChannels(); |
| 4326 | |
| 4327 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4328 | if (offer_answer_options.offer_to_receive_audio != |
| 4329 | RTCOfferAnswerOptions::kUndefined) { |
| 4330 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4331 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4332 | offer_new_audio_description || |
| 4333 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4334 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4335 | if (offer_answer_options.offer_to_receive_video != |
| 4336 | RTCOfferAnswerOptions::kUndefined) { |
| 4337 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4338 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4339 | offer_new_video_description || |
| 4340 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4341 | } |
| 4342 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 4343 | absl::optional<size_t> audio_index; |
| 4344 | absl::optional<size_t> video_index; |
| 4345 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4346 | // If a current description exists, generate m= sections in the same order, |
| 4347 | // using the first audio/video/data section that appears and rejecting |
| 4348 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4349 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4350 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4351 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 4352 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4353 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4354 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4355 | } |
| 4356 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4357 | // Add audio/video/data m= sections to the end if needed. |
| 4358 | if (!audio_index && offer_new_audio_description) { |
| 4359 | session_options->media_description_options.push_back( |
| 4360 | cricket::MediaDescriptionOptions( |
| 4361 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 4362 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4363 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 4364 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 20:14:45 | [diff] [blame] | 4365 | } |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4366 | if (!video_index && offer_new_video_description) { |
| 4367 | session_options->media_description_options.push_back( |
| 4368 | cricket::MediaDescriptionOptions( |
| 4369 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 4370 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4371 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 4372 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 20:14:45 | [diff] [blame] | 4373 | } |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4374 | if (!data_index && offer_new_data_description) { |
| 4375 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4376 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 4377 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4381 | !audio_index ? nullptr |
| 4382 | : &session_options->media_description_options[*audio_index]; |
| 4383 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4384 | !video_index ? nullptr |
| 4385 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4386 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 4387 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4388 | audio_media_description_options, |
| 4389 | video_media_description_options, |
| 4390 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4391 | } |
| 4392 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4393 | static cricket::MediaDescriptionOptions |
| 4394 | GetMediaDescriptionOptionsForTransceiver( |
| 4395 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4396 | transceiver, |
| 4397 | const std::string& mid) { |
| 4398 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 4399 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4400 | transceiver->stopped()); |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 4401 | media_description_options.codec_preferences = |
| 4402 | transceiver->codec_preferences(); |
Steve Anton | 5f94aa2 | 2018-02-01 18:58:30 | [diff] [blame] | 4403 | // This behavior is specified in JSEP. The gist is that: |
| 4404 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 4405 | // sendrecv. |
| 4406 | // 2. If the MSID is included, then it must be included in any subsequent |
| 4407 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 4408 | if (transceiver->stopped() || |
| 4409 | (!RtpTransceiverDirectionHasSend(transceiver->direction()) && |
| 4410 | !transceiver->internal()->has_ever_been_used_to_send())) { |
| 4411 | return media_description_options; |
Steve Anton | 5f94aa2 | 2018-02-01 18:58:30 | [diff] [blame] | 4412 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 4413 | |
| 4414 | cricket::SenderOptions sender_options; |
| 4415 | sender_options.track_id = transceiver->sender()->id(); |
| 4416 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 4417 | |
| 4418 | // The following sets up RIDs and Simulcast. |
| 4419 | // RIDs are included if Simulcast is requested or if any RID was specified. |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 4420 | RtpParameters send_parameters = |
| 4421 | transceiver->internal()->sender_internal()->GetParametersInternal(); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 4422 | bool has_rids = std::any_of(send_parameters.encodings.begin(), |
| 4423 | send_parameters.encodings.end(), |
| 4424 | [](const RtpEncodingParameters& encoding) { |
| 4425 | return !encoding.rid.empty(); |
| 4426 | }); |
| 4427 | |
Amit Hilbuch | b7446ed | 2019-01-28 20:25:25 | [diff] [blame] | 4428 | std::vector<RidDescription> send_rids; |
| 4429 | SimulcastLayerList send_layers; |
| 4430 | for (const RtpEncodingParameters& encoding : send_parameters.encodings) { |
| 4431 | if (encoding.rid.empty()) { |
| 4432 | continue; |
| 4433 | } |
| 4434 | send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend)); |
| 4435 | send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active)); |
| 4436 | } |
| 4437 | |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 4438 | if (has_rids) { |
| 4439 | sender_options.rids = send_rids; |
| 4440 | } |
| 4441 | |
| 4442 | sender_options.simulcast_layers = send_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 4443 | // When RIDs are configured, we must set num_sim_layers to 0 to. |
| 4444 | // Otherwise, num_sim_layers must be 1 because either there is no |
| 4445 | // simulcast, or simulcast is acheived by munging the SDP. |
| 4446 | sender_options.num_sim_layers = has_rids ? 0 : 1; |
| 4447 | media_description_options.sender_options.push_back(sender_options); |
| 4448 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4449 | return media_description_options; |
| 4450 | } |
| 4451 | |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4452 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 4453 | static const ContentInfo* GetContentByIndex( |
| 4454 | const SessionDescriptionInterface* sdesc, |
| 4455 | size_t i) { |
| 4456 | if (!sdesc) { |
| 4457 | return nullptr; |
| 4458 | } |
| 4459 | const ContentInfos& contents = sdesc->description()->contents(); |
| 4460 | return (i < contents.size() ? &contents[i] : nullptr); |
| 4461 | } |
| 4462 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4463 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 4464 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4465 | cricket::MediaSessionOptions* session_options) { |
| 4466 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 4467 | // Offers) and 5.2.2 (Subsequent Offers). |
| 4468 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 4469 | const ContentInfos& local_contents = |
| 4470 | (local_description() ? local_description()->description()->contents() |
| 4471 | : ContentInfos()); |
| 4472 | const ContentInfos& remote_contents = |
| 4473 | (remote_description() ? remote_description()->description()->contents() |
| 4474 | : ContentInfos()); |
| 4475 | // The mline indices that can be recycled. New transceivers should reuse these |
| 4476 | // slots first. |
| 4477 | std::queue<size_t> recycleable_mline_indices; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4478 | // First, go through each media section that exists in either the local or |
| 4479 | // remote description and generate a media section in this offer for the |
| 4480 | // associated transceiver. If a media section can be recycled, generate a |
| 4481 | // default, rejected media section here that can be later overwritten. |
| 4482 | for (size_t i = 0; |
| 4483 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 4484 | // Either |local_content| or |remote_content| is non-null. |
| 4485 | const ContentInfo* local_content = |
| 4486 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4487 | const ContentInfo* current_local_content = |
| 4488 | GetContentByIndex(current_local_description(), i); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4489 | const ContentInfo* remote_content = |
| 4490 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4491 | const ContentInfo* current_remote_content = |
| 4492 | GetContentByIndex(current_remote_description(), i); |
| 4493 | bool had_been_rejected = |
| 4494 | (current_local_content && current_local_content->rejected) || |
| 4495 | (current_remote_content && current_remote_content->rejected); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4496 | const std::string& mid = |
| 4497 | (local_content ? local_content->name : remote_content->name); |
| 4498 | cricket::MediaType media_type = |
| 4499 | (local_content ? local_content->media_description()->type() |
| 4500 | : remote_content->media_description()->type()); |
| 4501 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4502 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4503 | auto transceiver = GetAssociatedTransceiver(mid); |
| 4504 | RTC_CHECK(transceiver); |
| 4505 | // A media section is considered eligible for recycling if it is marked as |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4506 | // rejected in either the current local or current remote description. |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 4507 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4508 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 4509 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 4510 | RtpTransceiverDirection::kInactive, |
| 4511 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4512 | recycleable_mline_indices.push(i); |
| 4513 | } else { |
| 4514 | session_options->media_description_options.push_back( |
| 4515 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 4516 | // CreateOffer shouldn't really cause any state changes in |
| 4517 | // PeerConnection, but we need a way to match new transceivers to new |
| 4518 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 4519 | // by recording the index of the media section generated for the |
| 4520 | // transceiver in the offer. |
| 4521 | transceiver->internal()->set_mline_index(i); |
| 4522 | } |
| 4523 | } else { |
| 4524 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4525 | RTC_CHECK(GetDataMid()); |
| 4526 | if (had_been_rejected || mid != *GetDataMid()) { |
| 4527 | session_options->media_description_options.push_back( |
| 4528 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 4529 | } else { |
| 4530 | session_options->media_description_options.push_back( |
| 4531 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 4532 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4533 | } |
| 4534 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 4535 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4536 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 4537 | // and not associated). Reuse media sections marked as recyclable first, |
| 4538 | // otherwise append to the end of the offer. New media sections should be |
| 4539 | // added in the order they were added to the PeerConnection. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 4540 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4541 | if (transceiver->mid() || transceiver->stopped()) { |
| 4542 | continue; |
| 4543 | } |
| 4544 | size_t mline_index; |
| 4545 | if (!recycleable_mline_indices.empty()) { |
| 4546 | mline_index = recycleable_mline_indices.front(); |
| 4547 | recycleable_mline_indices.pop(); |
| 4548 | session_options->media_description_options[mline_index] = |
| 4549 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 4550 | mid_generator_()); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4551 | } else { |
| 4552 | mline_index = session_options->media_description_options.size(); |
| 4553 | session_options->media_description_options.push_back( |
| 4554 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 4555 | mid_generator_())); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4556 | } |
| 4557 | // See comment above for why CreateOffer changes the transceiver's state. |
| 4558 | transceiver->internal()->set_mline_index(mline_index); |
| 4559 | } |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4560 | // Lastly, add a m-section if we have local data channels and an m section |
| 4561 | // does not already exist. |
| 4562 | if (!GetDataMid() && HasDataChannels()) { |
| 4563 | session_options->media_description_options.push_back( |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 4564 | GetMediaDescriptionOptionsForActiveData(mid_generator_())); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4565 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | void PeerConnection::GetOptionsForAnswer( |
| 4569 | const RTCOfferAnswerOptions& offer_answer_options, |
| 4570 | cricket::MediaSessionOptions* session_options) { |
| 4571 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 4572 | |
| 4573 | if (IsUnifiedPlan()) { |
| 4574 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 4575 | } else { |
| 4576 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 4577 | } |
| 4578 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4579 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 4580 | // the RTP data channels would be successfully negotiated by default and the |
| 4581 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 4582 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 4583 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 4584 | session_options->data_channel_type = data_channel_type(); |
| 4585 | } |
| 4586 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4587 | // Apply ICE renomination flag. |
| 4588 | for (auto& options : session_options->media_description_options) { |
| 4589 | options.transport_options.enable_ice_renomination = |
| 4590 | configuration_.enable_ice_renomination; |
| 4591 | } |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 4592 | |
| 4593 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 4594 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 4595 | session_options->pooled_ice_credentials = |
| 4596 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4597 | RTC_FROM_HERE, |
| 4598 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4599 | port_allocator_.get())); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4600 | } |
| 4601 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4602 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 4603 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 15:18:11 | [diff] [blame] | 4604 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4605 | // Figure out transceiver directional preferences. |
| 4606 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 4607 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 4608 | |
| 4609 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 4610 | // restricted by the direction in the offer. |
| 4611 | bool recv_audio = true; |
| 4612 | bool recv_video = true; |
| 4613 | |
| 4614 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4615 | if (offer_answer_options.offer_to_receive_audio != |
| 4616 | RTCOfferAnswerOptions::kUndefined) { |
| 4617 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 4618 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4619 | if (offer_answer_options.offer_to_receive_video != |
| 4620 | RTCOfferAnswerOptions::kUndefined) { |
| 4621 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4622 | } |
| 4623 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 4624 | absl::optional<size_t> audio_index; |
| 4625 | absl::optional<size_t> video_index; |
| 4626 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 18:31:29 | [diff] [blame] | 4627 | |
| 4628 | // Generate m= sections that match those in the offer. |
| 4629 | // Note that mediasession.cc will handle intersection our preferred |
| 4630 | // direction with the offered direction. |
| 4631 | GenerateMediaDescriptionOptions( |
| 4632 | remote_description(), |
| 4633 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4634 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 4635 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4636 | |
| 4637 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4638 | !audio_index ? nullptr |
| 4639 | : &session_options->media_description_options[*audio_index]; |
| 4640 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4641 | !video_index ? nullptr |
| 4642 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4643 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 4644 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4645 | audio_media_description_options, |
| 4646 | video_media_description_options, |
| 4647 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4648 | } |
zhihuang | af38847 | 2016-11-02 23:49:48 | [diff] [blame] | 4649 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4650 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 4651 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4652 | cricket::MediaSessionOptions* session_options) { |
| 4653 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 4654 | // Answers) and 5.3.2 (Subsequent Answers). |
| 4655 | RTC_DCHECK(remote_description()); |
| 4656 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 4657 | for (const ContentInfo& content : |
| 4658 | remote_description()->description()->contents()) { |
| 4659 | cricket::MediaType media_type = content.media_description()->type(); |
| 4660 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4661 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 4662 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 4663 | RTC_CHECK(transceiver); |
| 4664 | session_options->media_description_options.push_back( |
| 4665 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 4666 | } else { |
| 4667 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 23:23:40 | [diff] [blame] | 4668 | // Reject all data sections if data channels are disabled. |
| 4669 | // Reject a data section if it has already been rejected. |
| 4670 | // Reject all data sections except for the first one. |
| 4671 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 4672 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4673 | session_options->media_description_options.push_back( |
| 4674 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 4675 | } else { |
| 4676 | session_options->media_description_options.push_back( |
| 4677 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 4678 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4679 | } |
| 4680 | } |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 4681 | } |
| 4682 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4683 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 4684 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 4685 | RtpTransceiverDirection audio_direction, |
| 4686 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 4687 | absl::optional<size_t>* audio_index, |
| 4688 | absl::optional<size_t>* video_index, |
| 4689 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 4690 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4691 | for (const cricket::ContentInfo& content : |
| 4692 | session_desc->description()->contents()) { |
| 4693 | if (IsAudioContent(&content)) { |
| 4694 | // If we already have an audio m= section, reject this extra one. |
| 4695 | if (*audio_index) { |
| 4696 | session_options->media_description_options.push_back( |
| 4697 | cricket::MediaDescriptionOptions( |
| 4698 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4699 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4700 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4701 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4702 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4703 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 4704 | content.name, audio_direction, |
| 4705 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 4706 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4707 | } |
| 4708 | } else if (IsVideoContent(&content)) { |
| 4709 | // If we already have an video m= section, reject this extra one. |
| 4710 | if (*video_index) { |
| 4711 | session_options->media_description_options.push_back( |
| 4712 | cricket::MediaDescriptionOptions( |
| 4713 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4714 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4715 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4716 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4717 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 4718 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 4719 | content.name, video_direction, |
| 4720 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 4721 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4722 | } |
| 4723 | } else { |
| 4724 | RTC_DCHECK(IsDataContent(&content)); |
| 4725 | // If we already have an data m= section, reject this extra one. |
| 4726 | if (*data_index) { |
| 4727 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4728 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4729 | } else { |
| 4730 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4731 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 4732 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4733 | } |
| 4734 | } |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 4735 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4736 | } |
| 4737 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4738 | cricket::MediaDescriptionOptions |
| 4739 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 4740 | const std::string& mid) const { |
| 4741 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4742 | // expect sendrecv. |
| 4743 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4744 | RtpTransceiverDirection::kSendRecv, |
| 4745 | /*stopped=*/false); |
| 4746 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4747 | return options; |
| 4748 | } |
| 4749 | |
| 4750 | cricket::MediaDescriptionOptions |
| 4751 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 4752 | const std::string& mid) const { |
| 4753 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4754 | RtpTransceiverDirection::kInactive, |
| 4755 | /*stopped=*/true); |
| 4756 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 4757 | return options; |
| 4758 | } |
| 4759 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 4760 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4761 | switch (data_channel_type_) { |
| 4762 | case cricket::DCT_RTP: |
| 4763 | if (!rtp_data_channel_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 4764 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4765 | } |
| 4766 | return rtp_data_channel_->content_name(); |
| 4767 | case cricket::DCT_SCTP: |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 4768 | return sctp_mid_; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 4769 | case cricket::DCT_MEDIA_TRANSPORT: |
| 4770 | return media_transport_data_mid_; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4771 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 4772 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4773 | } |
| 4774 | } |
| 4775 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4776 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 4777 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4778 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 4779 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 4780 | } |
| 4781 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4782 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4783 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4784 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4785 | cricket::MediaType media_type, |
| 4786 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 4787 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4788 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4789 | std::vector<RtpSenderInfo>* current_senders = |
| 4790 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4791 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4792 | // Find removed senders. I.e., senders where the sender id or ssrc don't match |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 4793 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4794 | for (auto sender_it = current_senders->begin(); |
| 4795 | sender_it != current_senders->end(); |
| 4796 | /* incremented manually */) { |
| 4797 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4798 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4799 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 4800 | std::string params_stream_id; |
| 4801 | if (params) { |
| 4802 | params_stream_id = |
| 4803 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 4804 | : kDefaultStreamId); |
| 4805 | } |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 4806 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 4807 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 4808 | // If this is a default track, and we still need it, don't remove it. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4809 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4810 | sender_exists) { |
| 4811 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 4812 | } else { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4813 | OnRemoteSenderRemoved(info, media_type); |
| 4814 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4815 | } |
| 4816 | } |
| 4817 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4818 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4819 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 18:16:33 | [diff] [blame] | 4820 | if (!params.has_ssrcs()) { |
| 4821 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 4822 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 4823 | // create a default. |
| 4824 | default_sender_needed = true; |
| 4825 | break; |
| 4826 | } |
| 4827 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4828 | // |params.id| is the sender id and the stream id uses the first of |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 4829 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 18:16:33 | [diff] [blame] | 4830 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 4831 | // not supported in Plan B, we just take the first here and create the |
| 4832 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4833 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 4834 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 4835 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4836 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4837 | uint32_t ssrc = params.first_ssrc(); |
| 4838 | |
| 4839 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4840 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4841 | if (!stream) { |
| 4842 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 4843 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4844 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4845 | remote_streams_->AddStream(stream); |
| 4846 | new_streams->AddStream(stream); |
| 4847 | } |
| 4848 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4849 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 4850 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4851 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4852 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4853 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4854 | } |
| 4855 | } |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 4856 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4857 | // Add default sender if necessary. |
| 4858 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 4859 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4860 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 4861 | if (!default_stream) { |
| 4862 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 4863 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4864 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 4865 | remote_streams_->AddStream(default_stream); |
| 4866 | new_streams->AddStream(default_stream); |
| 4867 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4868 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4869 | ? kDefaultAudioSenderId |
| 4870 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4871 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 4872 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4873 | if (!default_sender_info) { |
| 4874 | current_senders->push_back( |
Ruslan Burakov | 7ea4605 | 2019-02-16 01:07:05 | [diff] [blame] | 4875 | RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0)); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4876 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 4877 | } |
| 4878 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4879 | } |
| 4880 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4881 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 4882 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 4883 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 4884 | << " receiver for track_id=" << sender_info.sender_id |
| 4885 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4886 | |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 4887 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4888 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4889 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4890 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4891 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4892 | } else { |
nisse | eb4ca4e | 2017-01-12 10:24:27 | [diff] [blame] | 4893 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4894 | } |
| 4895 | } |
| 4896 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4897 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 4898 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 4899 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 4900 | << " receiver for track_id=" << sender_info.sender_id |
| 4901 | << " and stream_id=" << sender_info.stream_id; |
| 4902 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4903 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4904 | |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4905 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4906 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 4907 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 4908 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4909 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4910 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4911 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4912 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4913 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4914 | } |
| 4915 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 4916 | // Stopping or destroying a VideoRtpReceiver will end the |
| 4917 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4918 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4919 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4920 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4921 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 4922 | // There's no guarantee the track is still available, e.g. the track may |
| 4923 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4924 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4925 | } |
| 4926 | } else { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 4927 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4928 | } |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4929 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4930 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4931 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 4935 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 4936 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4937 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 4938 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4939 | streams_to_remove.push_back(stream); |
| 4940 | } |
| 4941 | } |
| 4942 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 20:02:21 | [diff] [blame] | 4943 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4944 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4945 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4946 | } |
| 4947 | } |
| 4948 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4949 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4950 | const std::vector<cricket::StreamParams>& streams, |
| 4951 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4952 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4953 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4954 | // Find removed tracks. I.e., tracks where the track id, stream id or ssrc |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4955 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4956 | for (auto sender_it = current_senders->begin(); |
| 4957 | sender_it != current_senders->end(); |
| 4958 | /* incremented manually */) { |
| 4959 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4960 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4961 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4962 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4963 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4964 | OnLocalSenderRemoved(info, media_type); |
| 4965 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4966 | } else { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4967 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4968 | } |
| 4969 | } |
| 4970 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4971 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4972 | for (const cricket::StreamParams& params : streams) { |
| 4973 | // The sync_label is the MediaStream label and the |stream.id| is the |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4974 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4975 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4976 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4977 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4978 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 4979 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4980 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 4981 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4982 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4983 | } |
| 4984 | } |
| 4985 | } |
| 4986 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4987 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4988 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 4989 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4990 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 4991 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4992 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4993 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4994 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4995 | return; |
| 4996 | } |
| 4997 | |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 4998 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4999 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5000 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5001 | return; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5002 | } |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5003 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 5004 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5005 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5006 | } |
| 5007 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5008 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 5009 | cricket::MediaType media_type) { |
| 5010 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5011 | if (!sender) { |
| 5012 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5013 | // SessionDescriptions has been renegotiated. |
| 5014 | return; |
| 5015 | } |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5016 | |
| 5017 | // A sender has been removed from the SessionDescription but it's still |
| 5018 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 5019 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 5020 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5021 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5022 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5023 | return; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5024 | } |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5025 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5026 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 5030 | const cricket::StreamParamsVec& streams) { |
| 5031 | std::vector<std::string> existing_channels; |
| 5032 | |
| 5033 | // Find new and active data channels. |
| 5034 | for (const cricket::StreamParams& params : streams) { |
| 5035 | // |it->sync_label| is actually the data channel label. The reason is that |
| 5036 | // we use the same naming of data channels as we do for |
| 5037 | // MediaStreams and Tracks. |
| 5038 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 5039 | // track label is the same as |streamid|. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5040 | const std::string& channel_label = params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5041 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 5042 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5043 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5044 | continue; |
| 5045 | } |
| 5046 | // Set the SSRC the data channel should use for sending. |
| 5047 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 5048 | existing_channels.push_back(data_channel_it->first); |
| 5049 | } |
| 5050 | |
| 5051 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 5052 | } |
| 5053 | |
| 5054 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 5055 | const cricket::StreamParamsVec& streams) { |
| 5056 | std::vector<std::string> existing_channels; |
| 5057 | |
| 5058 | // Find new and active data channels. |
| 5059 | for (const cricket::StreamParams& params : streams) { |
| 5060 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 5061 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5062 | std::string label = params.first_stream_id().empty() |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5063 | ? rtc::ToString(params.first_ssrc()) |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5064 | : params.first_stream_id(); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5065 | auto data_channel_it = rtp_data_channels_.find(label); |
| 5066 | if (data_channel_it == rtp_data_channels_.end()) { |
| 5067 | // This is a new data channel. |
| 5068 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 5069 | } else { |
| 5070 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 5071 | } |
| 5072 | existing_channels.push_back(label); |
| 5073 | } |
| 5074 | |
| 5075 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 5076 | } |
| 5077 | |
| 5078 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 5079 | const std::vector<std::string>& active_channels, |
| 5080 | bool is_local_update) { |
| 5081 | auto it = rtp_data_channels_.begin(); |
| 5082 | while (it != rtp_data_channels_.end()) { |
| 5083 | DataChannel* data_channel = it->second; |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 5084 | if (absl::c_linear_search(active_channels, data_channel->label())) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5085 | ++it; |
| 5086 | continue; |
| 5087 | } |
| 5088 | |
| 5089 | if (is_local_update) { |
| 5090 | data_channel->SetSendSsrc(0); |
| 5091 | } else { |
| 5092 | data_channel->RemotePeerRequestClose(); |
| 5093 | } |
| 5094 | |
| 5095 | if (data_channel->state() == DataChannel::kClosed) { |
| 5096 | rtp_data_channels_.erase(it); |
| 5097 | it = rtp_data_channels_.begin(); |
| 5098 | } else { |
| 5099 | ++it; |
| 5100 | } |
| 5101 | } |
| 5102 | } |
| 5103 | |
| 5104 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 5105 | uint32_t remote_ssrc) { |
| 5106 | rtc::scoped_refptr<DataChannel> channel( |
| 5107 | InternalCreateDataChannel(label, nullptr)); |
| 5108 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5109 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5110 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5111 | return; |
| 5112 | } |
| 5113 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 5114 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5115 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 5116 | Observer()->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 5120 | const std::string& label, |
| 5121 | const InternalDataChannelInit* config) { |
| 5122 | if (IsClosed()) { |
| 5123 | return nullptr; |
| 5124 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5125 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5126 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5127 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 5128 | return nullptr; |
| 5129 | } |
| 5130 | InternalDataChannelInit new_config = |
| 5131 | config ? (*config) : InternalDataChannelInit(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5132 | if (DataChannel::IsSctpLike(data_channel_type_)) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5133 | if (new_config.id < 0) { |
| 5134 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5135 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5136 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5137 | RTC_LOG(LS_ERROR) |
| 5138 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5139 | return nullptr; |
| 5140 | } |
| 5141 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5142 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5143 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5144 | return nullptr; |
| 5145 | } |
| 5146 | } |
| 5147 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5148 | rtc::scoped_refptr<DataChannel> channel( |
| 5149 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5150 | if (!channel) { |
| 5151 | sid_allocator_.ReleaseSid(new_config.id); |
| 5152 | return nullptr; |
| 5153 | } |
| 5154 | |
| 5155 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 5156 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5157 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 5158 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5159 | return nullptr; |
| 5160 | } |
| 5161 | rtp_data_channels_[channel->label()] = channel; |
| 5162 | } else { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5163 | RTC_DCHECK(DataChannel::IsSctpLike(data_channel_type_)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5164 | sctp_data_channels_.push_back(channel); |
| 5165 | channel->SignalClosed.connect(this, |
| 5166 | &PeerConnection::OnSctpDataChannelClosed); |
| 5167 | } |
| 5168 | |
Steve Anton | 2d8609c | 2018-01-24 00:38:46 | [diff] [blame] | 5169 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5170 | return channel; |
| 5171 | } |
| 5172 | |
| 5173 | bool PeerConnection::HasDataChannels() const { |
| 5174 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 5175 | } |
| 5176 | |
| 5177 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
Harald Alvestrand | 1f928d3 | 2019-03-28 10:29:38 | [diff] [blame] | 5178 | std::vector<rtc::scoped_refptr<DataChannel>> channels_to_close; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5179 | for (const auto& channel : sctp_data_channels_) { |
| 5180 | if (channel->id() < 0) { |
| 5181 | int sid; |
| 5182 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Harald Alvestrand | 1f928d3 | 2019-03-28 10:29:38 | [diff] [blame] | 5183 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid, closing channel."; |
| 5184 | channels_to_close.push_back(channel); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5185 | continue; |
| 5186 | } |
| 5187 | channel->SetSctpSid(sid); |
| 5188 | } |
| 5189 | } |
Harald Alvestrand | 1f928d3 | 2019-03-28 10:29:38 | [diff] [blame] | 5190 | // Since closing modifies the list of channels, we have to do the actual |
| 5191 | // closing outside the loop. |
| 5192 | for (const auto& channel : channels_to_close) { |
| 5193 | channel->CloseAbruptly(); |
| 5194 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-15 02:15:29 | [diff] [blame] | 5198 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5199 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 5200 | ++it) { |
| 5201 | if (it->get() == channel) { |
| 5202 | if (channel->id() >= 0) { |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 5203 | // After the closing procedure is done, it's safe to use this ID for |
| 5204 | // another data channel. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5205 | sid_allocator_.ReleaseSid(channel->id()); |
| 5206 | } |
deadbeef | bd29246 | 2015-12-15 02:15:29 | [diff] [blame] | 5207 | // Since this method is triggered by a signal from the DataChannel, |
| 5208 | // we can't free it directly here; we need to free it asynchronously. |
| 5209 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5210 | sctp_data_channels_.erase(it); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 5211 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 5212 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5213 | return; |
| 5214 | } |
| 5215 | } |
| 5216 | } |
| 5217 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5218 | void PeerConnection::OnDataChannelDestroyed() { |
| 5219 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 5220 | // DataChannel may callback to us and try to modify the list. |
| 5221 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 5222 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 5223 | for (const auto& kv : temp_rtp_dcs) { |
| 5224 | kv.second->OnTransportChannelDestroyed(); |
| 5225 | } |
| 5226 | |
| 5227 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 5228 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 5229 | for (const auto& channel : temp_sctp_dcs) { |
| 5230 | channel->OnTransportChannelDestroyed(); |
| 5231 | } |
| 5232 | } |
| 5233 | |
| 5234 | void PeerConnection::OnDataChannelOpenMessage( |
| 5235 | const std::string& label, |
| 5236 | const InternalDataChannelInit& config) { |
| 5237 | rtc::scoped_refptr<DataChannel> channel( |
| 5238 | InternalCreateDataChannel(label, &config)); |
| 5239 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5240 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5241 | return; |
| 5242 | } |
| 5243 | |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 5244 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 5245 | DataChannelProxy::Create(signaling_thread(), channel); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 5246 | Observer()->OnDataChannel(std::move(proxy_channel)); |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 5247 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5248 | } |
| 5249 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5250 | bool PeerConnection::HandleOpenMessage_s( |
| 5251 | const cricket::ReceiveDataParams& params, |
| 5252 | const rtc::CopyOnWriteBuffer& buffer) { |
| 5253 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(buffer)) { |
| 5254 | // Received OPEN message; parse and signal that a new data channel should |
| 5255 | // be created. |
| 5256 | std::string label; |
| 5257 | InternalDataChannelInit config; |
| 5258 | config.id = params.ssrc; |
| 5259 | if (!ParseDataChannelOpenMessage(buffer, &label, &config)) { |
| 5260 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for ssrc " |
| 5261 | << params.ssrc; |
| 5262 | return true; |
| 5263 | } |
| 5264 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5265 | OnDataChannelOpenMessage(label, config); |
| 5266 | return true; |
| 5267 | } |
| 5268 | return false; |
| 5269 | } |
| 5270 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5271 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5272 | PeerConnection::GetAudioTransceiver() const { |
| 5273 | // This method only works with Plan B SDP, where there is a single |
| 5274 | // audio/video transceiver. |
| 5275 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5276 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 5277 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5278 | return transceiver; |
| 5279 | } |
| 5280 | } |
| 5281 | RTC_NOTREACHED(); |
| 5282 | return nullptr; |
| 5283 | } |
| 5284 | |
| 5285 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5286 | PeerConnection::GetVideoTransceiver() const { |
| 5287 | // This method only works with Plan B SDP, where there is a single |
| 5288 | // audio/video transceiver. |
| 5289 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5290 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 5291 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5292 | return transceiver; |
| 5293 | } |
| 5294 | } |
| 5295 | RTC_NOTREACHED(); |
| 5296 | return nullptr; |
| 5297 | } |
| 5298 | |
| 5299 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 5300 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5301 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5302 | switch (type) { |
| 5303 | case cricket::MEDIA_TYPE_AUDIO: |
| 5304 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 5305 | case cricket::MEDIA_TYPE_VIDEO: |
| 5306 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 5307 | case cricket::MEDIA_TYPE_DATA: |
| 5308 | return false; |
| 5309 | } |
| 5310 | RTC_NOTREACHED(); |
| 5311 | return false; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5312 | } |
| 5313 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5314 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5315 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5316 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5317 | for (auto sender : transceiver->internal()->senders()) { |
| 5318 | if (sender->track() == track) { |
| 5319 | return sender; |
| 5320 | } |
| 5321 | } |
| 5322 | } |
| 5323 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5324 | } |
| 5325 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5326 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5327 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5328 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5329 | for (auto sender : transceiver->internal()->senders()) { |
| 5330 | if (sender->id() == sender_id) { |
| 5331 | return sender; |
| 5332 | } |
| 5333 | } |
| 5334 | } |
| 5335 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 5336 | } |
| 5337 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5338 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 5339 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5340 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5341 | for (auto receiver : transceiver->internal()->receivers()) { |
| 5342 | if (receiver->id() == receiver_id) { |
| 5343 | return receiver; |
| 5344 | } |
| 5345 | } |
| 5346 | } |
| 5347 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 5348 | } |
| 5349 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5350 | std::vector<PeerConnection::RtpSenderInfo>* |
| 5351 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 5352 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5353 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 5354 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5355 | ? &remote_audio_sender_infos_ |
| 5356 | : &remote_video_sender_infos_; |
| 5357 | } |
| 5358 | |
| 5359 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5360 | cricket::MediaType media_type) { |
| 5361 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5362 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5363 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 5364 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5365 | } |
| 5366 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5367 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 5368 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5369 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5370 | const std::string sender_id) const { |
| 5371 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5372 | if (sender_info.stream_id == stream_id && |
| 5373 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5374 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5375 | } |
| 5376 | } |
| 5377 | return nullptr; |
| 5378 | } |
| 5379 | |
| 5380 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 5381 | for (const auto& channel : sctp_data_channels_) { |
| 5382 | if (channel->id() == sid) { |
| 5383 | return channel; |
| 5384 | } |
| 5385 | } |
| 5386 | return nullptr; |
| 5387 | } |
| 5388 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5389 | PeerConnection::InitializePortAllocatorResult |
| 5390 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 5391 | const cricket::ServerAddresses& stun_servers, |
| 5392 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5393 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5394 | RTC_DCHECK_RUN_ON(network_thread()); |
| 5395 | |
Taylor Brandstetter | f8e6577 | 2016-06-28 00:20:15 | [diff] [blame] | 5396 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5397 | // To handle both internal and externally created port allocator, we will |
| 5398 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5399 | int port_allocator_flags = port_allocator_->flags(); |
| 5400 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 5401 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 5402 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5403 | // If the disable-IPv6 flag was specified, we'll not override it |
| 5404 | // by experiment. |
| 5405 | if (configuration.disable_ipv6) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5406 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 16:50:47 | [diff] [blame] | 5407 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 5408 | .find("Disabled") == 0) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5409 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5410 | } |
| 5411 | |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 5412 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5413 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5414 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 5415 | } |
| 5416 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5417 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5418 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5419 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5420 | } |
| 5421 | |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 5422 | if (configuration.candidate_network_policy == |
| 5423 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5424 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5425 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 5426 | } |
| 5427 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 5428 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5429 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 5430 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 5431 | } |
| 5432 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5433 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5434 | // No step delay is used while allocating ports. |
| 5435 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 5436 | port_allocator_->SetCandidateFilter( |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5437 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 5438 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5439 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 5440 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 5441 | for (auto& turn_server : turn_servers_copy) { |
| 5442 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 5443 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5444 | // Call this last since it may create pooled allocator sessions using the |
| 5445 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 5446 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 5447 | stun_servers, std::move(turn_servers_copy), |
| 5448 | configuration.ice_candidate_pool_size, configuration.prune_turn_ports, |
| 5449 | configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 5450 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5451 | |
| 5452 | InitializePortAllocatorResult res; |
| 5453 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 5454 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5455 | } |
| 5456 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 5457 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 5458 | const cricket::ServerAddresses& stun_servers, |
| 5459 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 5460 | IceTransportsType type, |
| 5461 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 5462 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 5463 | webrtc::TurnCustomizer* turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5464 | absl::optional<int> stun_candidate_keepalive_interval, |
| 5465 | bool have_local_description) { |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 5466 | port_allocator_->SetCandidateFilter( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 5467 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 5468 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 5469 | // size is not allowed, and changing the set of ICE servers will not result |
| 5470 | // in new candidates being gathered. |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5471 | if (have_local_description) { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 5472 | port_allocator_->FreezeCandidatePool(); |
| 5473 | } |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 5474 | // Add the custom tls turn servers if they exist. |
| 5475 | auto turn_servers_copy = turn_servers; |
| 5476 | for (auto& turn_server : turn_servers_copy) { |
| 5477 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 5478 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5479 | // Call this last since it may create pooled allocator sessions using the |
| 5480 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-13 02:49:32 | [diff] [blame] | 5481 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 5482 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
| 5483 | prune_turn_ports, turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5484 | } |
| 5485 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 5486 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 5487 | return factory_->channel_manager(); |
| 5488 | } |
| 5489 | |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 5490 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 5491 | std::unique_ptr<RtcEventLogOutput> output, |
| 5492 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 5493 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 5494 | if (!event_log_) { |
| 5495 | return false; |
| 5496 | } |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 5497 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 5498 | } |
| 5499 | |
| 5500 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 5501 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 5502 | if (event_log_) { |
| 5503 | event_log_->StopLogging(); |
| 5504 | } |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 5505 | } |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 5506 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 5507 | cricket::ChannelInterface* PeerConnection::GetChannel( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5508 | const std::string& content_name) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5509 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 5510 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5511 | if (channel && channel->content_name() == content_name) { |
| 5512 | return channel; |
| 5513 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5514 | } |
| 5515 | if (rtp_data_channel() && |
| 5516 | rtp_data_channel()->content_name() == content_name) { |
| 5517 | return rtp_data_channel(); |
| 5518 | } |
| 5519 | return nullptr; |
| 5520 | } |
| 5521 | |
| 5522 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5523 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5524 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5525 | RTC_LOG(LS_INFO) |
| 5526 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5527 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5528 | return false; |
| 5529 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5530 | if (!sctp_transport_ && !media_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5531 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5532 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5533 | return false; |
| 5534 | } |
| 5535 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5536 | absl::optional<rtc::SSLRole> dtls_role; |
| 5537 | if (sctp_mid_) { |
| 5538 | dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_); |
| 5539 | } else if (is_caller_) { |
| 5540 | dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
| 5541 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5542 | if (dtls_role) { |
| 5543 | *role = *dtls_role; |
| 5544 | return true; |
| 5545 | } |
| 5546 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5547 | } |
| 5548 | |
| 5549 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 5550 | rtc::SSLRole* role) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 5551 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5552 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5553 | RTC_LOG(LS_INFO) |
| 5554 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5555 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5556 | return false; |
| 5557 | } |
| 5558 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5559 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 5560 | if (dtls_role) { |
| 5561 | *role = *dtls_role; |
| 5562 | return true; |
| 5563 | } |
| 5564 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5565 | } |
| 5566 | |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 5567 | void PeerConnection::SetSessionError(SessionError error, |
| 5568 | const std::string& error_desc) { |
| 5569 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5570 | if (error != session_error_) { |
| 5571 | session_error_ = error; |
| 5572 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5573 | } |
| 5574 | } |
| 5575 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5576 | RTCError PeerConnection::UpdateSessionState( |
| 5577 | SdpType type, |
| 5578 | cricket::ContentSource source, |
| 5579 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5580 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5581 | |
| 5582 | // If there's already a pending error then no state transition should happen. |
| 5583 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 5584 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5585 | |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5586 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5587 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5588 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | // Update the signaling state according to the specified state machine (see |
| 5592 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5593 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5594 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5595 | ? PeerConnectionInterface::kHaveLocalOffer |
| 5596 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5597 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5598 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5599 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 5600 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5601 | } else { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5602 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5603 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 5604 | } |
| 5605 | |
| 5606 | // Update internal objects according to the session description's media |
| 5607 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5608 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5609 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 5610 | return error; |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5611 | } |
| 5612 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5613 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5614 | } |
| 5615 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5616 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5617 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5618 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5619 | const SessionDescriptionInterface* sdesc = |
| 5620 | (source == cricket::CS_LOCAL ? local_description() |
| 5621 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5622 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5623 | |
| 5624 | // Push down the new SDP media section for each audio/video transceiver. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5625 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5626 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5627 | FindMediaSectionForTransceiver(transceiver, sdesc); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 5628 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5629 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5630 | continue; |
| 5631 | } |
| 5632 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 5633 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5634 | if (!content_desc) { |
| 5635 | continue; |
| 5636 | } |
| 5637 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 5638 | bool success = (source == cricket::CS_LOCAL) |
| 5639 | ? channel->SetLocalContent(content_desc, type, &error) |
| 5640 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5641 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 20:48:57 | [diff] [blame] | 5642 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5643 | } |
| 5644 | } |
| 5645 | |
| 5646 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 5647 | if (rtp_data_channel_) { |
| 5648 | const ContentInfo* data_content = |
| 5649 | cricket::GetFirstDataContent(sdesc->description()); |
| 5650 | if (data_content && !data_content->rejected) { |
| 5651 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 5652 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5653 | if (data_desc) { |
| 5654 | std::string error; |
| 5655 | bool success = |
| 5656 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5657 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 5658 | : rtp_data_channel_->SetRemoteContent(data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5659 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 20:48:57 | [diff] [blame] | 5660 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5661 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5662 | } |
| 5663 | } |
| 5664 | } |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5665 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5666 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 5667 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 5668 | if (sctp_transport_ && local_description() && remote_description() && |
| 5669 | cricket::GetFirstDataContent(local_description()->description()) && |
| 5670 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5671 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5672 | RTC_FROM_HERE, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5673 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source, |
| 5674 | GetSctpPort(local_description()->description()), |
| 5675 | GetSctpPort(remote_description()->description()))); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5676 | if (!success) { |
| 5677 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5678 | "Failed to push down SCTP parameters."); |
| 5679 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5680 | } |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5681 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5682 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5683 | } |
| 5684 | |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5685 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source, |
| 5686 | int local_sctp_port, |
| 5687 | int remote_sctp_port) { |
| 5688 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5689 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 5690 | // When we support "max-message-size", that would also be pushed down here. |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5691 | return cricket_sctp_transport()->Start(local_sctp_port, remote_sctp_port); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5692 | } |
| 5693 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5694 | RTCError PeerConnection::PushdownTransportDescription( |
| 5695 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5696 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5697 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5698 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5699 | if (source == cricket::CS_LOCAL) { |
| 5700 | const SessionDescriptionInterface* sdesc = local_description(); |
| 5701 | RTC_DCHECK(sdesc); |
| 5702 | return transport_controller_->SetLocalDescription(type, |
| 5703 | sdesc->description()); |
| 5704 | } else { |
| 5705 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 5706 | RTC_DCHECK(sdesc); |
| 5707 | return transport_controller_->SetRemoteDescription(type, |
| 5708 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5709 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5710 | } |
| 5711 | |
| 5712 | bool PeerConnection::GetTransportDescription( |
| 5713 | const SessionDescription* description, |
| 5714 | const std::string& content_name, |
| 5715 | cricket::TransportDescription* tdesc) { |
| 5716 | if (!description || !tdesc) { |
| 5717 | return false; |
| 5718 | } |
| 5719 | const TransportInfo* transport_info = |
| 5720 | description->GetTransportInfoByName(content_name); |
| 5721 | if (!transport_info) { |
| 5722 | return false; |
| 5723 | } |
| 5724 | *tdesc = transport_info->description; |
| 5725 | return true; |
| 5726 | } |
| 5727 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5728 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 5729 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 5730 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5731 | switch (config.continual_gathering_policy) { |
| 5732 | case PeerConnectionInterface::GATHER_ONCE: |
| 5733 | gathering_policy = cricket::GATHER_ONCE; |
| 5734 | break; |
| 5735 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 5736 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 5737 | break; |
| 5738 | default: |
| 5739 | RTC_NOTREACHED(); |
| 5740 | gathering_policy = cricket::GATHER_ONCE; |
| 5741 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 5742 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5743 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 5744 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 5745 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5746 | ice_config.prioritize_most_likely_candidate_pairs = |
| 5747 | config.prioritize_most_likely_ice_candidate_pairs; |
| 5748 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 5749 | RTCConfigurationToIceConfigOptionalInt( |
| 5750 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5751 | ice_config.continual_gathering_policy = gathering_policy; |
| 5752 | ice_config.presume_writable_when_fully_relayed = |
| 5753 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 5754 | ice_config.ice_check_interval_strong_connectivity = |
| 5755 | config.ice_check_interval_strong_connectivity; |
| 5756 | ice_config.ice_check_interval_weak_connectivity = |
| 5757 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5758 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Jiawei Ou | cc88737 | 2018-11-30 07:08:51 | [diff] [blame] | 5759 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 5760 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 5761 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 5762 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5763 | ice_config.regather_all_networks_interval_range = |
| 5764 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 5765 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5766 | return ice_config; |
| 5767 | } |
| 5768 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5769 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 5770 | const rtc::CopyOnWriteBuffer& payload, |
| 5771 | cricket::SendDataResult* result) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 5772 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5773 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
| 5774 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_, " |
| 5775 | "sctp_transport_, and media_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5776 | return false; |
| 5777 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5778 | if (media_transport_) { |
| 5779 | SendDataParams send_params; |
| 5780 | send_params.type = ToWebrtcDataMessageType(params.type); |
| 5781 | send_params.ordered = params.ordered; |
| 5782 | if (params.max_rtx_count >= 0) { |
| 5783 | send_params.max_rtx_count = params.max_rtx_count; |
| 5784 | } else if (params.max_rtx_ms >= 0) { |
| 5785 | send_params.max_rtx_ms = params.max_rtx_ms; |
| 5786 | } |
| 5787 | return media_transport_->SendData(params.sid, send_params, payload).ok(); |
| 5788 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5789 | return rtp_data_channel_ |
| 5790 | ? rtp_data_channel_->SendData(params, payload, result) |
| 5791 | : network_thread()->Invoke<bool>( |
| 5792 | RTC_FROM_HERE, |
| 5793 | Bind(&cricket::SctpTransportInternal::SendData, |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 5794 | cricket_sctp_transport(), params, payload, result)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5795 | } |
| 5796 | |
| 5797 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5798 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5799 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5800 | // Don't log an error here, because DataChannels are expected to call |
| 5801 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 5802 | // whether or not the underlying transport is ready. |
| 5803 | return false; |
| 5804 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5805 | if (media_transport_) { |
| 5806 | SignalMediaTransportWritable_s.connect(webrtc_data_channel, |
| 5807 | &DataChannel::OnChannelReady); |
| 5808 | SignalMediaTransportReceivedData_s.connect(webrtc_data_channel, |
| 5809 | &DataChannel::OnDataReceived); |
| 5810 | SignalMediaTransportChannelClosing_s.connect( |
| 5811 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5812 | SignalMediaTransportChannelClosed_s.connect( |
| 5813 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
| 5814 | } else if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5815 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 5816 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 5817 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 5818 | &DataChannel::OnDataReceived); |
| 5819 | } else { |
| 5820 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 5821 | &DataChannel::OnChannelReady); |
| 5822 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 5823 | &DataChannel::OnDataReceived); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 5824 | SignalSctpClosingProcedureStartedRemotely.connect( |
| 5825 | webrtc_data_channel, &DataChannel::OnClosingProcedureStartedRemotely); |
| 5826 | SignalSctpClosingProcedureComplete.connect( |
| 5827 | webrtc_data_channel, &DataChannel::OnClosingProcedureComplete); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5828 | } |
| 5829 | return true; |
| 5830 | } |
| 5831 | |
| 5832 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5833 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5834 | if (!rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5835 | RTC_LOG(LS_ERROR) |
| 5836 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 5837 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5838 | return; |
| 5839 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5840 | if (media_transport_) { |
| 5841 | SignalMediaTransportWritable_s.disconnect(webrtc_data_channel); |
| 5842 | SignalMediaTransportReceivedData_s.disconnect(webrtc_data_channel); |
| 5843 | SignalMediaTransportChannelClosing_s.disconnect(webrtc_data_channel); |
| 5844 | SignalMediaTransportChannelClosed_s.disconnect(webrtc_data_channel); |
| 5845 | } else if (rtp_data_channel_) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5846 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 5847 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 5848 | } else { |
| 5849 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 5850 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 5851 | SignalSctpClosingProcedureStartedRemotely.disconnect(webrtc_data_channel); |
| 5852 | SignalSctpClosingProcedureComplete.disconnect(webrtc_data_channel); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5853 | } |
| 5854 | } |
| 5855 | |
| 5856 | void PeerConnection::AddSctpDataStream(int sid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5857 | if (media_transport_) { |
Bjorn Mellem | f58e43e | 2019-02-22 18:31:48 | [diff] [blame] | 5858 | media_transport_->OpenChannel(sid); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5859 | return; |
| 5860 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5861 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5862 | RTC_LOG(LS_ERROR) |
| 5863 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5864 | return; |
| 5865 | } |
| 5866 | network_thread()->Invoke<void>( |
| 5867 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 5868 | cricket_sctp_transport(), sid)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | void PeerConnection::RemoveSctpDataStream(int sid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5872 | if (media_transport_) { |
| 5873 | media_transport_->CloseChannel(sid); |
| 5874 | return; |
| 5875 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5876 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5877 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5878 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5879 | return; |
| 5880 | } |
| 5881 | network_thread()->Invoke<void>( |
| 5882 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 5883 | cricket_sctp_transport(), sid)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | bool PeerConnection::ReadyToSendData() const { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5887 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5888 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5889 | (media_transport_ && media_transport_ready_to_send_data_) || |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5890 | sctp_ready_to_send_data_; |
| 5891 | } |
| 5892 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5893 | void PeerConnection::OnDataReceived(int channel_id, |
| 5894 | DataMessageType type, |
| 5895 | const rtc::CopyOnWriteBuffer& buffer) { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5896 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5897 | cricket::ReceiveDataParams params; |
| 5898 | params.sid = channel_id; |
| 5899 | params.type = ToCricketDataMessageType(type); |
| 5900 | media_transport_invoker_->AsyncInvoke<void>( |
| 5901 | RTC_FROM_HERE, signaling_thread(), [this, params, buffer] { |
| 5902 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5903 | if (!HandleOpenMessage_s(params, buffer)) { |
| 5904 | SignalMediaTransportReceivedData_s(params, buffer); |
| 5905 | } |
| 5906 | }); |
| 5907 | } |
| 5908 | |
| 5909 | void PeerConnection::OnChannelClosing(int channel_id) { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5910 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5911 | media_transport_invoker_->AsyncInvoke<void>( |
| 5912 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 5913 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5914 | SignalMediaTransportChannelClosing_s(channel_id); |
| 5915 | }); |
| 5916 | } |
| 5917 | |
| 5918 | void PeerConnection::OnChannelClosed(int channel_id) { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5919 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5920 | media_transport_invoker_->AsyncInvoke<void>( |
| 5921 | RTC_FROM_HERE, signaling_thread(), [this, channel_id] { |
| 5922 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5923 | SignalMediaTransportChannelClosed_s(channel_id); |
| 5924 | }); |
| 5925 | } |
| 5926 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5927 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 5928 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5929 | if (sctp_mid_ && transport_controller_) { |
| 5930 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_); |
| 5931 | if (dtls_transport) { |
| 5932 | return dtls_transport->transport_name(); |
| 5933 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5934 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5935 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5936 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5937 | } |
| 5938 | |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 5939 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 5940 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 5941 | network_thread()->Invoke<void>( |
| 5942 | RTC_FROM_HERE, |
| 5943 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 5944 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 5945 | return candidate_states_list; |
| 5946 | } |
| 5947 | |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 5948 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 5949 | const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 5950 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 5951 | std::map<std::string, std::string> transport_names_by_mid; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5952 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 5953 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 5954 | if (channel) { |
| 5955 | transport_names_by_mid[channel->content_name()] = |
| 5956 | channel->transport_name(); |
| 5957 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5958 | } |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 5959 | if (rtp_data_channel_) { |
| 5960 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 5961 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5962 | } |
| 5963 | if (sctp_transport_) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5964 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5965 | RTC_DCHECK(transport_name); |
| 5966 | transport_names_by_mid[*sctp_mid_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5967 | } |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 5968 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5969 | } |
| 5970 | |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 5971 | std::map<std::string, cricket::TransportStats> |
| 5972 | PeerConnection::GetTransportStatsByNames( |
| 5973 | const std::set<std::string>& transport_names) { |
| 5974 | if (!network_thread()->IsCurrent()) { |
| 5975 | return network_thread() |
| 5976 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 5977 | RTC_FROM_HERE, |
| 5978 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5979 | } |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 5980 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 5981 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 5982 | for (const std::string& transport_name : transport_names) { |
| 5983 | cricket::TransportStats transport_stats; |
| 5984 | bool success = |
| 5985 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 5986 | if (success) { |
| 5987 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 5988 | } else { |
| 5989 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 5990 | << transport_name; |
| 5991 | } |
| 5992 | } |
| 5993 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5994 | } |
| 5995 | |
| 5996 | bool PeerConnection::GetLocalCertificate( |
| 5997 | const std::string& transport_name, |
| 5998 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5999 | if (!certificate) { |
| 6000 | return false; |
| 6001 | } |
| 6002 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 6003 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6004 | } |
| 6005 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 6006 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6007 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 6008 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6009 | } |
| 6010 | |
| 6011 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 6012 | return data_channel_type_; |
| 6013 | } |
| 6014 | |
| 6015 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
Karl Wiberg | f73f7d6 | 2019-04-08 13:36:53 | [diff] [blame] | 6016 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6017 | return pending_ice_restarts_.find(content_name) != |
| 6018 | pending_ice_restarts_.end(); |
| 6019 | } |
| 6020 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6021 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 6022 | return transport_controller_->NeedsIceRestart(content_name); |
| 6023 | } |
| 6024 | |
| 6025 | void PeerConnection::OnCertificateReady( |
| 6026 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 6027 | transport_controller_->SetLocalCertificate(certificate); |
| 6028 | } |
| 6029 | |
| 6030 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6031 | SetSessionError(SessionError::kTransport, |
| 6032 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6033 | } |
| 6034 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 6035 | void PeerConnection::OnTransportControllerConnectionState( |
| 6036 | cricket::IceConnectionState state) { |
| 6037 | switch (state) { |
| 6038 | case cricket::kIceConnectionConnecting: |
| 6039 | // If the current state is Connected or Completed, then there were |
| 6040 | // writable channels but now there are not, so the next state must |
| 6041 | // be Disconnected. |
| 6042 | // kIceConnectionConnecting is currently used as the default, |
| 6043 | // un-connected state by the TransportController, so its only use is |
| 6044 | // detecting disconnections. |
| 6045 | if (ice_connection_state_ == |
| 6046 | PeerConnectionInterface::kIceConnectionConnected || |
| 6047 | ice_connection_state_ == |
| 6048 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 6049 | SetIceConnectionState( |
| 6050 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 6051 | } |
| 6052 | break; |
| 6053 | case cricket::kIceConnectionFailed: |
| 6054 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 6055 | break; |
| 6056 | case cricket::kIceConnectionConnected: |
| 6057 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 6058 | "all transports are writable."; |
| 6059 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6060 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6061 | break; |
| 6062 | case cricket::kIceConnectionCompleted: |
| 6063 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 6064 | "all transports are complete."; |
| 6065 | if (ice_connection_state_ != |
| 6066 | PeerConnectionInterface::kIceConnectionConnected) { |
| 6067 | // If jumping directly from "checking" to "connected", |
| 6068 | // signal "connected" first. |
| 6069 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6070 | } |
| 6071 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 6072 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6073 | ReportTransportStats(); |
| 6074 | break; |
| 6075 | default: |
| 6076 | RTC_NOTREACHED(); |
| 6077 | } |
| 6078 | } |
| 6079 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6080 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 6081 | const std::string& transport_name, |
| 6082 | const cricket::Candidates& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6083 | int sdp_mline_index; |
| 6084 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6085 | RTC_LOG(LS_ERROR) |
| 6086 | << "OnTransportControllerCandidatesGathered: content name " |
| 6087 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6088 | return; |
| 6089 | } |
| 6090 | |
| 6091 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 6092 | citer != candidates.end(); ++citer) { |
| 6093 | // Use transport_name as the candidate media id. |
| 6094 | std::unique_ptr<JsepIceCandidate> candidate( |
| 6095 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 6096 | if (local_description()) { |
| 6097 | mutable_local_description()->AddCandidate(candidate.get()); |
| 6098 | } |
| 6099 | OnIceCandidate(std::move(candidate)); |
| 6100 | } |
| 6101 | } |
| 6102 | |
| 6103 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 6104 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6105 | // Sanity check. |
| 6106 | for (const cricket::Candidate& candidate : candidates) { |
| 6107 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6108 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 6109 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6110 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6111 | return; |
| 6112 | } |
| 6113 | } |
| 6114 | |
| 6115 | if (local_description()) { |
| 6116 | mutable_local_description()->RemoveCandidates(candidates); |
| 6117 | } |
| 6118 | OnIceCandidatesRemoved(candidates); |
| 6119 | } |
| 6120 | |
| 6121 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 6122 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6123 | RTC_HISTOGRAM_ENUMERATION( |
| 6124 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 6125 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6126 | } |
| 6127 | |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6128 | void PeerConnection::EnableSending() { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6129 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 6130 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6131 | if (channel && !channel->enabled()) { |
| 6132 | channel->Enable(true); |
| 6133 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6134 | } |
| 6135 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6136 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6137 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6138 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6139 | } |
| 6140 | |
| 6141 | // Returns the media index for a local ice candidate given the content name. |
| 6142 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 6143 | const std::string& content_name, |
| 6144 | int* sdp_mline_index) { |
| 6145 | if (!local_description() || !sdp_mline_index) { |
| 6146 | return false; |
| 6147 | } |
| 6148 | |
| 6149 | bool content_found = false; |
| 6150 | const ContentInfos& contents = local_description()->description()->contents(); |
| 6151 | for (size_t index = 0; index < contents.size(); ++index) { |
| 6152 | if (contents[index].name == content_name) { |
| 6153 | *sdp_mline_index = static_cast<int>(index); |
| 6154 | content_found = true; |
| 6155 | break; |
| 6156 | } |
| 6157 | } |
| 6158 | return content_found; |
| 6159 | } |
| 6160 | |
| 6161 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 6162 | const SessionDescriptionInterface* remote_desc) { |
| 6163 | if (!remote_desc) { |
| 6164 | return true; |
| 6165 | } |
| 6166 | bool ret = true; |
| 6167 | |
| 6168 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 6169 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 6170 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 6171 | const IceCandidateInterface* candidate = candidates->at(n); |
| 6172 | bool valid = false; |
| 6173 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 6174 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6175 | RTC_LOG(LS_INFO) |
| 6176 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 6177 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6178 | } |
| 6179 | continue; |
| 6180 | } |
| 6181 | ret = UseCandidate(candidate); |
| 6182 | if (!ret) { |
| 6183 | break; |
| 6184 | } |
| 6185 | } |
| 6186 | } |
| 6187 | return ret; |
| 6188 | } |
| 6189 | |
| 6190 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 6191 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 6192 | size_t remote_content_size = |
| 6193 | remote_description()->description()->contents().size(); |
| 6194 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6195 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6196 | return false; |
| 6197 | } |
| 6198 | |
| 6199 | cricket::ContentInfo content = |
| 6200 | remote_description()->description()->contents()[mediacontent_index]; |
| 6201 | std::vector<cricket::Candidate> candidates; |
| 6202 | candidates.push_back(candidate->candidate()); |
| 6203 | // Invoking BaseSession method to handle remote candidates. |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6204 | RTCError error = |
| 6205 | transport_controller_->AddRemoteCandidates(content.name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 | [diff] [blame] | 6206 | if (error.ok()) { |
| 6207 | // Candidates successfully submitted for checking. |
| 6208 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 6209 | ice_connection_state_ == |
| 6210 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 6211 | // If state is New, then the session has just gotten its first remote ICE |
| 6212 | // candidates, so go to Checking. |
| 6213 | // If state is Disconnected, the session is re-using old candidates or |
| 6214 | // receiving additional ones, so go to Checking. |
| 6215 | // If state is Connected, stay Connected. |
| 6216 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 6217 | // newly added transport, then the state actually _should_ move to |
| 6218 | // checking. Add a way to distinguish that case. |
| 6219 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 6220 | } |
| 6221 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 08:07:07 | [diff] [blame] | 6222 | } else { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6223 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6224 | } |
| 6225 | return true; |
| 6226 | } |
| 6227 | |
| 6228 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6229 | // Destroy video channel first since it may have a pointer to the |
| 6230 | // voice channel. |
| 6231 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 6232 | if (!video_info || video_info->rejected) { |
| 6233 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6234 | } |
| 6235 | |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 6236 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 6237 | if (!audio_info || audio_info->rejected) { |
| 6238 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6239 | } |
| 6240 | |
| 6241 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 6242 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 6243 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6244 | } |
| 6245 | } |
| 6246 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6247 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 6248 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6249 | const cricket::ContentGroup* bundle_group = nullptr; |
| 6250 | if (configuration_.bundle_policy == |
| 6251 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6252 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6253 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6254 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6255 | "max-bundle configured but session description " |
| 6256 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6257 | } |
| 6258 | } |
Mirko Bonadei | 9f3a44f | 2019-01-30 12:47:42 | [diff] [blame] | 6259 | return bundle_group; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6260 | } |
| 6261 | |
| 6262 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6263 | // Creating the media channels. Transports should already have been created |
| 6264 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6265 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6266 | if (voice && !voice->rejected && |
| 6267 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6268 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6269 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6270 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6271 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6272 | } |
| 6273 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 6274 | } |
| 6275 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6276 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6277 | if (video && !video->rejected && |
| 6278 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6279 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6280 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6281 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6282 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6283 | } |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6284 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6285 | } |
| 6286 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6287 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6288 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6289 | !rtp_data_channel_ && !sctp_transport_ && !media_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6290 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6291 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6292 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6293 | } |
| 6294 | } |
| 6295 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6296 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6297 | } |
| 6298 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6299 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6300 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6301 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 6302 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Bjorn Mellem | a9bbd86 | 2018-11-02 16:07:48 | [diff] [blame] | 6303 | MediaTransportInterface* media_transport = nullptr; |
| 6304 | if (configuration_.use_media_transport) { |
| 6305 | media_transport = GetMediaTransport(mid); |
| 6306 | } |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 6307 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6308 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 6309 | call_ptr_, configuration_.media_config, rtp_transport, media_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 6310 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 6311 | &ssrc_generator_, audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6312 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6313 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6314 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6315 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6316 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6317 | voice_channel->SignalSentPacket.connect(this, |
| 6318 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6319 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6320 | |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6321 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6322 | } |
| 6323 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6324 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6325 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6326 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 6327 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Niels Möller | 4687915 | 2019-01-07 14:54:47 | [diff] [blame] | 6328 | MediaTransportInterface* media_transport = nullptr; |
| 6329 | if (configuration_.use_media_transport) { |
| 6330 | media_transport = GetMediaTransport(mid); |
| 6331 | } |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 6332 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6333 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 6334 | call_ptr_, configuration_.media_config, rtp_transport, media_transport, |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 6335 | signaling_thread(), mid, SrtpRequired(), GetCryptoOptions(), |
Jonas Oreland | a3aa9bd | 2019-04-17 05:38:40 | [diff] [blame] | 6336 | &ssrc_generator_, video_options_, video_bitrate_allocator_factory_.get()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6337 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6338 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6339 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6340 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6341 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6342 | video_channel->SignalSentPacket.connect(this, |
| 6343 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6344 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6345 | |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6346 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6347 | } |
| 6348 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6349 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6350 | switch (data_channel_type_) { |
| 6351 | case cricket::DCT_MEDIA_TRANSPORT: |
| 6352 | if (network_thread()->Invoke<bool>( |
| 6353 | RTC_FROM_HERE, |
| 6354 | rtc::Bind(&PeerConnection::SetupMediaTransportForDataChannels_n, |
| 6355 | this, mid))) { |
| 6356 | for (const auto& channel : sctp_data_channels_) { |
| 6357 | channel->OnTransportChannelCreated(); |
| 6358 | } |
| 6359 | return true; |
| 6360 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6361 | return false; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6362 | case cricket::DCT_SCTP: |
| 6363 | if (!sctp_factory_) { |
| 6364 | RTC_LOG(LS_ERROR) |
| 6365 | << "Trying to create SCTP transport, but didn't compile with " |
| 6366 | "SCTP support (HAVE_SCTP)"; |
| 6367 | return false; |
| 6368 | } |
| 6369 | if (!network_thread()->Invoke<bool>( |
| 6370 | RTC_FROM_HERE, |
| 6371 | rtc::Bind(&PeerConnection::CreateSctpTransport_n, this, mid))) { |
| 6372 | return false; |
| 6373 | } |
| 6374 | for (const auto& channel : sctp_data_channels_) { |
| 6375 | channel->OnTransportChannelCreated(); |
| 6376 | } |
| 6377 | return true; |
| 6378 | case cricket::DCT_RTP: |
| 6379 | default: |
| 6380 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 6381 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 6382 | configuration_.media_config, rtp_transport, signaling_thread(), mid, |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 6383 | SrtpRequired(), GetCryptoOptions(), &ssrc_generator_); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6384 | if (!rtp_data_channel_) { |
| 6385 | return false; |
| 6386 | } |
| 6387 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 6388 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 6389 | rtp_data_channel_->SignalSentPacket.connect( |
| 6390 | this, &PeerConnection::OnSentPacket_w); |
| 6391 | rtp_data_channel_->SetRtpTransport(rtp_transport); |
| 6392 | return true; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6393 | } |
| 6394 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6395 | return true; |
| 6396 | } |
| 6397 | |
| 6398 | Call::Stats PeerConnection::GetCallStats() { |
| 6399 | if (!worker_thread()->IsCurrent()) { |
| 6400 | return worker_thread()->Invoke<Call::Stats>( |
| 6401 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 6402 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 6403 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6404 | if (call_) { |
| 6405 | return call_->GetStats(); |
| 6406 | } else { |
| 6407 | return Call::Stats(); |
| 6408 | } |
| 6409 | } |
| 6410 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6411 | bool PeerConnection::CreateSctpTransport_n(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 6412 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6413 | RTC_DCHECK(sctp_factory_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 6414 | rtc::scoped_refptr<DtlsTransport> webrtc_dtls_transport = |
| 6415 | transport_controller_->LookupDtlsTransportByMid(mid); |
Zhi Huang | 644fde4 | 2018-04-03 02:16:26 | [diff] [blame] | 6416 | cricket::DtlsTransportInternal* dtls_transport = |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 6417 | webrtc_dtls_transport->internal(); |
Zhi Huang | 644fde4 | 2018-04-03 02:16:26 | [diff] [blame] | 6418 | RTC_DCHECK(dtls_transport); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 6419 | std::unique_ptr<cricket::SctpTransportInternal> cricket_sctp_transport = |
| 6420 | sctp_factory_->CreateSctpTransport(dtls_transport); |
| 6421 | RTC_DCHECK(cricket_sctp_transport); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6422 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 6423 | cricket_sctp_transport->SignalReadyToSendData.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6424 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 6425 | cricket_sctp_transport->SignalDataReceived.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6426 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 6427 | // TODO(deadbeef): All we do here is AsyncInvoke to fire the signal on |
| 6428 | // another thread. Would be nice if there was a helper class similar to |
| 6429 | // sigslot::repeater that did this for us, eliminating a bunch of boilerplate |
| 6430 | // code. |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 6431 | cricket_sctp_transport->SignalClosingProcedureStartedRemotely.connect( |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 6432 | this, &PeerConnection::OnSctpClosingProcedureStartedRemotely_n); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 6433 | cricket_sctp_transport->SignalClosingProcedureComplete.connect( |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 6434 | this, &PeerConnection::OnSctpClosingProcedureComplete_n); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6435 | sctp_mid_ = mid; |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 6436 | sctp_transport_ = new rtc::RefCountedObject<SctpTransport>( |
| 6437 | std::move(cricket_sctp_transport)); |
| 6438 | sctp_transport_->SetDtlsTransport(std::move(webrtc_dtls_transport)); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6439 | return true; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6440 | } |
| 6441 | |
| 6442 | void PeerConnection::DestroySctpTransport_n() { |
Karl Wiberg | 7a651c6e | 2019-04-02 11:00:46 | [diff] [blame] | 6443 | RTC_DCHECK_RUN_ON(network_thread()); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 6444 | sctp_transport_->Clear(); |
| 6445 | sctp_transport_ = nullptr; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6446 | sctp_mid_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6447 | sctp_invoker_.reset(nullptr); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6448 | } |
| 6449 | |
| 6450 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
Karl Wiberg | 7a651c6e | 2019-04-02 11:00:46 | [diff] [blame] | 6451 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6452 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6453 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 6454 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 6455 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 6456 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 6457 | // pending tasks will be cleared. |
| 6458 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 6459 | OnSctpTransportReadyToSendData_s(true); |
| 6460 | }); |
| 6461 | } |
| 6462 | |
| 6463 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 6464 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6465 | sctp_ready_to_send_data_ = ready; |
| 6466 | SignalSctpReadyToSendData(ready); |
| 6467 | } |
| 6468 | |
| 6469 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 6470 | const cricket::ReceiveDataParams& params, |
| 6471 | const rtc::CopyOnWriteBuffer& payload) { |
Karl Wiberg | 7a651c6e | 2019-04-02 11:00:46 | [diff] [blame] | 6472 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6473 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6474 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 6475 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 6476 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 6477 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 6478 | // pending tasks will be cleared. |
| 6479 | sctp_invoker_->AsyncInvoke<void>( |
| 6480 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 6481 | OnSctpTransportDataReceived_s(params, payload); |
| 6482 | }); |
| 6483 | } |
| 6484 | |
| 6485 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 6486 | const cricket::ReceiveDataParams& params, |
| 6487 | const rtc::CopyOnWriteBuffer& payload) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6488 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6489 | if (!HandleOpenMessage_s(params, payload)) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6490 | SignalSctpDataReceived(params, payload); |
| 6491 | } |
| 6492 | } |
| 6493 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 6494 | void PeerConnection::OnSctpClosingProcedureStartedRemotely_n(int sid) { |
Karl Wiberg | 7a651c6e | 2019-04-02 11:00:46 | [diff] [blame] | 6495 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6496 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6497 | sctp_invoker_->AsyncInvoke<void>( |
| 6498 | RTC_FROM_HERE, signaling_thread(), |
| 6499 | rtc::Bind(&sigslot::signal1<int>::operator(), |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 6500 | &SignalSctpClosingProcedureStartedRemotely, sid)); |
| 6501 | } |
| 6502 | |
| 6503 | void PeerConnection::OnSctpClosingProcedureComplete_n(int sid) { |
Karl Wiberg | 7a651c6e | 2019-04-02 11:00:46 | [diff] [blame] | 6504 | RTC_DCHECK_RUN_ON(network_thread()); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 6505 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 6506 | sctp_invoker_->AsyncInvoke<void>( |
| 6507 | RTC_FROM_HERE, signaling_thread(), |
| 6508 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 6509 | &SignalSctpClosingProcedureComplete, sid)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6510 | } |
| 6511 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6512 | bool PeerConnection::SetupMediaTransportForDataChannels_n( |
| 6513 | const std::string& mid) { |
| 6514 | media_transport_ = transport_controller_->GetMediaTransport(mid); |
| 6515 | if (!media_transport_) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 6516 | RTC_LOG(LS_ERROR) |
| 6517 | << "Media transport is not available for data channels, mid=" << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6518 | return false; |
| 6519 | } |
| 6520 | |
| 6521 | media_transport_invoker_ = absl::make_unique<rtc::AsyncInvoker>(); |
| 6522 | media_transport_->SetDataSink(this); |
| 6523 | media_transport_data_mid_ = mid; |
| 6524 | transport_controller_->SignalMediaTransportStateChanged.connect( |
| 6525 | this, &PeerConnection::OnMediaTransportStateChanged_n); |
| 6526 | // Check the initial state right away, in case transport is already writable. |
| 6527 | OnMediaTransportStateChanged_n(); |
| 6528 | return true; |
| 6529 | } |
| 6530 | |
| 6531 | void PeerConnection::TeardownMediaTransportForDataChannels_n() { |
| 6532 | if (!media_transport_) { |
| 6533 | return; |
| 6534 | } |
| 6535 | transport_controller_->SignalMediaTransportStateChanged.disconnect(this); |
| 6536 | media_transport_data_mid_.reset(); |
| 6537 | media_transport_->SetDataSink(nullptr); |
| 6538 | media_transport_invoker_ = nullptr; |
| 6539 | media_transport_ = nullptr; |
| 6540 | } |
| 6541 | |
| 6542 | void PeerConnection::OnMediaTransportStateChanged_n() { |
| 6543 | if (!media_transport_data_mid_ || |
| 6544 | transport_controller_->GetMediaTransportState( |
| 6545 | *media_transport_data_mid_) != MediaTransportState::kWritable) { |
| 6546 | return; |
| 6547 | } |
| 6548 | media_transport_invoker_->AsyncInvoke<void>( |
| 6549 | RTC_FROM_HERE, signaling_thread(), [this] { |
| 6550 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6551 | media_transport_ready_to_send_data_ = true; |
| 6552 | SignalMediaTransportWritable_s(media_transport_ready_to_send_data_); |
| 6553 | }); |
| 6554 | } |
| 6555 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6556 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 6557 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 6558 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 6559 | if (!bundle_enabled) |
| 6560 | return true; |
| 6561 | |
| 6562 | const cricket::ContentGroup* bundle_group = |
| 6563 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 6564 | RTC_DCHECK(bundle_group != NULL); |
| 6565 | |
| 6566 | const cricket::ContentInfos& contents = desc->contents(); |
| 6567 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 6568 | citer != contents.end(); ++citer) { |
| 6569 | const cricket::ContentInfo* content = (&*citer); |
| 6570 | RTC_DCHECK(content != NULL); |
| 6571 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 6572 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6573 | if (!HasRtcpMuxEnabled(content)) |
| 6574 | return false; |
| 6575 | } |
| 6576 | } |
| 6577 | // RTCP-MUX is enabled in all the contents. |
| 6578 | return true; |
| 6579 | } |
| 6580 | |
| 6581 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 6582 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6583 | } |
| 6584 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 6585 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 6586 | std::set<std::string> mids; |
| 6587 | for (const cricket::ContentInfo& content : description.contents()) { |
Steve Anton | ceac015 | 2018-12-19 19:32:20 | [diff] [blame] | 6588 | if (content.name.empty()) { |
| 6589 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6590 | "A media section is missing a MID attribute."); |
| 6591 | } |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 6592 | if (!mids.insert(content.name).second) { |
| 6593 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6594 | "Duplicate a=mid value '" + content.name + "'."); |
| 6595 | } |
| 6596 | } |
| 6597 | return RTCError::OK(); |
| 6598 | } |
| 6599 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6600 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6601 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6602 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6603 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6604 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6605 | } |
| 6606 | |
| 6607 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6608 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6609 | } |
| 6610 | |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6611 | SdpType type = sdesc->GetType(); |
| 6612 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 6613 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6614 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 6615 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6616 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6617 | } |
| 6618 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 6619 | RTCError error = ValidateMids(*sdesc->description()); |
| 6620 | if (!error.ok()) { |
| 6621 | return error; |
| 6622 | } |
| 6623 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6624 | // Verify crypto settings. |
| 6625 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6626 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 6627 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6628 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6629 | if (!crypto_error.ok()) { |
| 6630 | return crypto_error; |
| 6631 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6632 | } |
| 6633 | |
| 6634 | // Verify ice-ufrag and ice-pwd. |
| 6635 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6636 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6637 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6638 | } |
| 6639 | |
| 6640 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6641 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6642 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6643 | } |
| 6644 | |
| 6645 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 6646 | // m-lines that do not rtcp-mux enabled. |
| 6647 | |
| 6648 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6649 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 6650 | // With an answer we want to compare the new answer session description with |
| 6651 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6652 | const cricket::SessionDescription* offer_desc = |
| 6653 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 6654 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 6655 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 6656 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 6657 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6658 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6659 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6660 | } |
| 6661 | } else { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6662 | // The re-offers should respect the order of m= sections in current |
| 6663 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 6664 | // With a re-offer, either the current local or current remote descriptions |
| 6665 | // could be the most up to date, so we would like to check against both of |
| 6666 | // them if they exist. It could be the case that one of them has a 0 port |
| 6667 | // for a media section, but the other does not. This is important to check |
| 6668 | // against in the case that we are recycling an m= section. |
| 6669 | const cricket::SessionDescription* current_desc = nullptr; |
| 6670 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 6671 | if (local_description()) { |
| 6672 | current_desc = local_description()->description(); |
| 6673 | if (remote_description()) { |
| 6674 | secondary_current_desc = remote_description()->description(); |
| 6675 | } |
| 6676 | } else if (remote_description()) { |
| 6677 | current_desc = remote_description()->description(); |
| 6678 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6679 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 6680 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 6681 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6682 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6683 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6684 | } |
| 6685 | } |
| 6686 | |
Steve Anton | ba42e99 | 2018-04-09 21:10:01 | [diff] [blame] | 6687 | if (IsUnifiedPlan()) { |
| 6688 | // Ensure that each audio and video media section has at most one |
| 6689 | // "StreamParams". This will return an error if receiving a session |
| 6690 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 6691 | // same type. With Unified Plan, there can only be at most one track per |
| 6692 | // media section. |
| 6693 | for (const ContentInfo& content : sdesc->description()->contents()) { |
| 6694 | const MediaContentDescription& desc = *content.description; |
| 6695 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 6696 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 6697 | desc.streams().size() > 1u) { |
| 6698 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6699 | "Media section has more than one track specified " |
| 6700 | "with a=ssrc lines which is not supported with " |
| 6701 | "Unified Plan."); |
| 6702 | } |
| 6703 | } |
| 6704 | } |
| 6705 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6706 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6707 | } |
| 6708 | |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6709 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6710 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6711 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6712 | return (state == PeerConnectionInterface::kStable) || |
| 6713 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-05 00:24:52 | [diff] [blame] | 6714 | } else { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6715 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6716 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 6717 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 6718 | } |
| 6719 | } |
| 6720 | |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6721 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6722 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6723 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6724 | return (state == PeerConnectionInterface::kStable) || |
| 6725 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-05 00:24:52 | [diff] [blame] | 6726 | } else { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6727 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6728 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 6729 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6730 | } |
| 6731 | } |
| 6732 | |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6733 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 6734 | switch (error) { |
| 6735 | case SessionError::kNone: |
| 6736 | return "ERROR_NONE"; |
| 6737 | case SessionError::kContent: |
| 6738 | return "ERROR_CONTENT"; |
| 6739 | case SessionError::kTransport: |
| 6740 | return "ERROR_TRANSPORT"; |
| 6741 | } |
| 6742 | RTC_NOTREACHED(); |
| 6743 | return ""; |
| 6744 | } |
| 6745 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6746 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 6747 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6748 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 6749 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 14:59:32 | [diff] [blame] | 6750 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6751 | } |
| 6752 | |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 6753 | void PeerConnection::ReportSdpFormatReceived( |
| 6754 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 6755 | int num_audio_mlines = 0; |
| 6756 | int num_video_mlines = 0; |
| 6757 | int num_audio_tracks = 0; |
| 6758 | int num_video_tracks = 0; |
| 6759 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 6760 | cricket::MediaType media_type = content.media_description()->type(); |
| 6761 | int num_tracks = std::max( |
| 6762 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 6763 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 6764 | num_audio_mlines += 1; |
| 6765 | num_audio_tracks += num_tracks; |
| 6766 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 6767 | num_video_mlines += 1; |
| 6768 | num_video_tracks += num_tracks; |
| 6769 | } |
| 6770 | } |
| 6771 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 6772 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 6773 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 6774 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 6775 | format = kSdpFormatReceivedComplexPlanB; |
| 6776 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 6777 | format = kSdpFormatReceivedSimple; |
| 6778 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6779 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 6780 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 6781 | } |
| 6782 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 6783 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 6784 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6785 | usage_event_accumulator_ |= static_cast<int>(event); |
| 6786 | } |
| 6787 | |
| 6788 | void PeerConnection::ReportUsagePattern() const { |
| 6789 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6790 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 6791 | usage_event_accumulator_, |
| 6792 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 6793 | const int bad_bits = |
| 6794 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_CALLED) | |
| 6795 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 6796 | const int good_bits = |
| 6797 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_CALLED) | |
| 6798 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 6799 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 6800 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 6801 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 6802 | // If called after close(), we can't report, because observer may have |
| 6803 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 6804 | if (observer_) { |
| 6805 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 6806 | } else { |
| 6807 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 6808 | << usage_event_accumulator_ |
| 6809 | << " observed after observer shutdown"; |
| 6810 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 6811 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 6812 | } |
| 6813 | |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6814 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 6815 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6816 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6817 | switch (answer.description()->msid_signaling()) { |
| 6818 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6819 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6820 | break; |
| 6821 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6822 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6823 | break; |
| 6824 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6825 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6826 | break; |
| 6827 | case cricket::kMsidSignalingMediaSection | |
| 6828 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6829 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6830 | break; |
| 6831 | default: |
| 6832 | RTC_NOTREACHED(); |
| 6833 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6834 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 6835 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6836 | } |
| 6837 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6838 | // We need to check the local/remote description for the Transport instead of |
| 6839 | // the session, because a new Transport added during renegotiation may have |
| 6840 | // them unset while the session has them set from the previous negotiation. |
| 6841 | // Not doing so may trigger the auto generation of transport description and |
| 6842 | // mess up DTLS identity information, ICE credential, etc. |
| 6843 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 6844 | const IceCandidateInterface* candidate, |
| 6845 | const SessionDescriptionInterface* remote_desc, |
| 6846 | bool* valid) { |
| 6847 | *valid = true; |
| 6848 | |
| 6849 | const SessionDescriptionInterface* current_remote_desc = |
| 6850 | remote_desc ? remote_desc : remote_description(); |
| 6851 | |
| 6852 | if (!current_remote_desc) { |
| 6853 | return false; |
| 6854 | } |
| 6855 | |
| 6856 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 6857 | size_t remote_content_size = |
| 6858 | current_remote_desc->description()->contents().size(); |
| 6859 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6860 | RTC_LOG(LS_ERROR) |
| 6861 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 6862 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6863 | |
| 6864 | *valid = false; |
| 6865 | return false; |
| 6866 | } |
| 6867 | |
| 6868 | cricket::ContentInfo content = |
| 6869 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 6870 | |
| 6871 | const std::string transport_name = GetTransportName(content.name); |
| 6872 | if (transport_name.empty()) { |
| 6873 | return false; |
| 6874 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6875 | return true; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6876 | } |
| 6877 | |
| 6878 | bool PeerConnection::SrtpRequired() const { |
| 6879 | return dtls_enabled_ || |
| 6880 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 6881 | } |
| 6882 | |
| 6883 | void PeerConnection::OnTransportControllerGatheringState( |
| 6884 | cricket::IceGatheringState state) { |
| 6885 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 6886 | if (state == cricket::kIceGatheringGathering) { |
| 6887 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 6888 | } else if (state == cricket::kIceGatheringComplete) { |
| 6889 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 6890 | } |
| 6891 | } |
| 6892 | |
| 6893 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6894 | std::map<std::string, std::set<cricket::MediaType>> |
| 6895 | media_types_by_transport_name; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6896 | for (const auto& transceiver : transceivers_) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6897 | if (transceiver->internal()->channel()) { |
| 6898 | const std::string& transport_name = |
| 6899 | transceiver->internal()->channel()->transport_name(); |
| 6900 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 6901 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6902 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6903 | } |
| 6904 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6905 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 6906 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6907 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6908 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 6909 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6910 | if (transport_name) { |
| 6911 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6912 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6913 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6914 | |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6915 | for (const auto& entry : media_types_by_transport_name) { |
| 6916 | const std::string& transport_name = entry.first; |
| 6917 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6918 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6919 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6920 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6921 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6922 | } |
| 6923 | } |
| 6924 | } |
| 6925 | // Walk through the ConnectionInfos to gather best connection usage |
| 6926 | // for IPv4 and IPv6. |
| 6927 | void PeerConnection::ReportBestConnectionState( |
| 6928 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6929 | for (const cricket::TransportChannelStats& channel_stats : |
| 6930 | stats.channel_stats) { |
| 6931 | for (const cricket::ConnectionInfo& connection_info : |
| 6932 | channel_stats.connection_infos) { |
| 6933 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6934 | continue; |
| 6935 | } |
| 6936 | |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6937 | const cricket::Candidate& local = connection_info.local_candidate; |
| 6938 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6939 | |
| 6940 | // Increment the counter for IceCandidatePairType. |
| 6941 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 6942 | (local.type() == RELAY_PORT_TYPE && |
| 6943 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6944 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 6945 | GetIceCandidatePairCounter(local, remote), |
| 6946 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6947 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6948 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 6949 | GetIceCandidatePairCounter(local, remote), |
| 6950 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6951 | } else { |
| 6952 | RTC_CHECK(0); |
| 6953 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6954 | |
| 6955 | // Increment the counter for IP type. |
| 6956 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6957 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6958 | kBestConnections_IPv4, |
| 6959 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6960 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6961 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 6962 | kBestConnections_IPv6, |
| 6963 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6964 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 6965 | RTC_CHECK(!local.address().hostname().empty() && |
| 6966 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6967 | } |
| 6968 | |
| 6969 | return; |
| 6970 | } |
| 6971 | } |
| 6972 | } |
| 6973 | |
| 6974 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 6975 | const cricket::TransportStats& stats, |
| 6976 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6977 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 6978 | return; |
| 6979 | } |
| 6980 | |
| 6981 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 6982 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 6983 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 6984 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 6985 | return; |
| 6986 | } |
| 6987 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6988 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 6989 | for (cricket::MediaType media_type : media_types) { |
| 6990 | switch (media_type) { |
| 6991 | case cricket::MEDIA_TYPE_AUDIO: |
| 6992 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6993 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 6994 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 6995 | break; |
| 6996 | case cricket::MEDIA_TYPE_VIDEO: |
| 6997 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 6998 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 6999 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7000 | break; |
| 7001 | case cricket::MEDIA_TYPE_DATA: |
| 7002 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7003 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 7004 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7005 | break; |
| 7006 | default: |
| 7007 | RTC_NOTREACHED(); |
| 7008 | continue; |
| 7009 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7010 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7011 | } |
| 7012 | |
| 7013 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7014 | for (cricket::MediaType media_type : media_types) { |
| 7015 | switch (media_type) { |
| 7016 | case cricket::MEDIA_TYPE_AUDIO: |
| 7017 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7018 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 7019 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7020 | break; |
| 7021 | case cricket::MEDIA_TYPE_VIDEO: |
| 7022 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7023 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 7024 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7025 | break; |
| 7026 | case cricket::MEDIA_TYPE_DATA: |
| 7027 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7028 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 7029 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7030 | break; |
| 7031 | default: |
| 7032 | RTC_NOTREACHED(); |
| 7033 | continue; |
| 7034 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7035 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7036 | } |
| 7037 | } |
| 7038 | |
| 7039 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 7040 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7041 | RTC_DCHECK(call_); |
| 7042 | call_->OnSentPacket(sent_packet); |
| 7043 | } |
| 7044 | |
| 7045 | const std::string PeerConnection::GetTransportName( |
| 7046 | const std::string& content_name) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7047 | cricket::ChannelInterface* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7048 | if (channel) { |
| 7049 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7050 | } |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7051 | if (sctp_transport_) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7052 | RTC_DCHECK(sctp_mid_); |
| 7053 | if (content_name == *sctp_mid_) { |
| 7054 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7055 | } |
| 7056 | } |
| 7057 | // Return an empty string if failed to retrieve the transport name. |
| 7058 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7059 | } |
| 7060 | |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7061 | void PeerConnection::DestroyTransceiverChannel( |
| 7062 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 7063 | transceiver) { |
| 7064 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7065 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7066 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7067 | if (channel) { |
| 7068 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7069 | DestroyChannelInterface(channel); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7070 | } |
| 7071 | } |
| 7072 | |
| 7073 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7074 | if (rtp_data_channel_) { |
| 7075 | OnDataChannelDestroyed(); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7076 | DestroyChannelInterface(rtp_data_channel_); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7077 | rtp_data_channel_ = nullptr; |
| 7078 | } |
| 7079 | |
| 7080 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 7081 | // grab a reference to this PeerConnection. If this is called from the |
| 7082 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 7083 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 7084 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 7085 | |
| 7086 | if (sctp_transport_) { |
| 7087 | OnDataChannelDestroyed(); |
| 7088 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 7089 | [this] { DestroySctpTransport_n(); }); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 7090 | sctp_ready_to_send_data_ = false; |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7091 | } |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7092 | |
| 7093 | if (media_transport_) { |
| 7094 | OnDataChannelDestroyed(); |
| 7095 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 7096 | RTC_DCHECK_RUN_ON(network_thread()); |
| 7097 | TeardownMediaTransportForDataChannels_n(); |
| 7098 | }); |
| 7099 | } |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7100 | } |
| 7101 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7102 | void PeerConnection::DestroyChannelInterface( |
| 7103 | cricket::ChannelInterface* channel) { |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7104 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7105 | switch (channel->media_type()) { |
| 7106 | case cricket::MEDIA_TYPE_AUDIO: |
| 7107 | channel_manager()->DestroyVoiceChannel( |
| 7108 | static_cast<cricket::VoiceChannel*>(channel)); |
| 7109 | break; |
| 7110 | case cricket::MEDIA_TYPE_VIDEO: |
| 7111 | channel_manager()->DestroyVideoChannel( |
| 7112 | static_cast<cricket::VideoChannel*>(channel)); |
| 7113 | break; |
| 7114 | case cricket::MEDIA_TYPE_DATA: |
| 7115 | channel_manager()->DestroyRtpDataChannel( |
| 7116 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 7117 | break; |
| 7118 | default: |
| 7119 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 7120 | break; |
| 7121 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7122 | } |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7123 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7124 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7125 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7126 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 7127 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 16:26:19 | [diff] [blame] | 7128 | MediaTransportInterface* media_transport) { |
Karl Wiberg | ac02589 | 2019-03-26 12:08:37 | [diff] [blame] | 7129 | RTC_DCHECK_RUN_ON(network_thread()); |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 7130 | RTC_DCHECK_RUNS_SERIALIZED(&use_media_transport_race_checker_); |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7131 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7132 | auto base_channel = GetChannel(mid); |
| 7133 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7134 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7135 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7136 | if (sctp_transport_ && mid == sctp_mid_) { |
Zhi Huang | 644fde4 | 2018-04-03 02:16:26 | [diff] [blame] | 7137 | sctp_transport_->SetDtlsTransport(dtls_transport); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7138 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 16:26:19 | [diff] [blame] | 7139 | |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 7140 | if (use_media_transport_) { |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 21:31:15 | [diff] [blame] | 7141 | // Only pass media transport to call object if media transport is used |
| 7142 | // for media (and not data channel). |
Karl Wiberg | ac02589 | 2019-03-26 12:08:37 | [diff] [blame] | 7143 | call_ptr_->MediaTransportChange(media_transport); |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 21:31:15 | [diff] [blame] | 7144 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 16:26:19 | [diff] [blame] | 7145 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7146 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7147 | } |
| 7148 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7149 | PeerConnectionObserver* PeerConnection::Observer() const { |
| 7150 | // In earlier production code, the pointer was not cleared on close, |
| 7151 | // which might have led to undefined behavior if the observer was not |
| 7152 | // deallocated, or strange crashes if it was. |
| 7153 | // We use CHECK in order to catch such behavior if it exists. |
| 7154 | // TODO(hta): Remove or replace with DCHECK if nothing is found. |
| 7155 | RTC_CHECK(observer_); |
| 7156 | return observer_; |
| 7157 | } |
| 7158 | |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 7159 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 7160 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 7161 | // after it has been removed. |
| 7162 | return configuration_.crypto_options.has_value() |
| 7163 | ? *configuration_.crypto_options |
| 7164 | : factory_->options().crypto_options; |
| 7165 | } |
| 7166 | |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 7167 | void PeerConnection::ClearStatsCache() { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 7168 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 7169 | if (stats_collector_) { |
| 7170 | stats_collector_->ClearCachedStatsReport(); |
| 7171 | } |
| 7172 | } |
| 7173 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7174 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 7175 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 7176 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7177 | } |
| 7178 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7179 | void PeerConnection::UpdateNegotiationNeeded() { |
| 7180 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7181 | if (!IsUnifiedPlan()) { |
| 7182 | Observer()->OnRenegotiationNeeded(); |
| 7183 | return; |
| 7184 | } |
| 7185 | |
| 7186 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7187 | if (IsClosed()) |
| 7188 | return; |
| 7189 | |
| 7190 | // If connection's signaling state is not "stable", abort these steps. |
| 7191 | if (signaling_state() != kStable) |
| 7192 | return; |
| 7193 | |
| 7194 | // NOTE |
| 7195 | // The negotiation-needed flag will be updated once the state transitions to |
| 7196 | // "stable", as part of the steps for setting an RTCSessionDescription. |
| 7197 | |
| 7198 | // If the result of checking if negotiation is needed is false, clear the |
| 7199 | // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot |
| 7200 | // to false, and abort these steps. |
| 7201 | bool is_negotiation_needed = CheckIfNegotiationIsNeeded(); |
| 7202 | if (!is_negotiation_needed) { |
| 7203 | is_negotiation_needed_ = false; |
| 7204 | return; |
| 7205 | } |
| 7206 | |
| 7207 | // If connection's [[NegotiationNeeded]] slot is already true, abort these |
| 7208 | // steps. |
| 7209 | if (is_negotiation_needed_) |
| 7210 | return; |
| 7211 | |
| 7212 | // Set connection's [[NegotiationNeeded]] slot to true. |
| 7213 | is_negotiation_needed_ = true; |
| 7214 | |
| 7215 | // Queue a task that runs the following steps: |
| 7216 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7217 | // If connection's [[NegotiationNeeded]] slot is false, abort these steps. |
| 7218 | // Fire an event named negotiationneeded at connection. |
| 7219 | Observer()->OnRenegotiationNeeded(); |
| 7220 | } |
| 7221 | |
| 7222 | bool PeerConnection::CheckIfNegotiationIsNeeded() { |
| 7223 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7224 | // 1. If any implementation-specific negotiation is required, as described at |
| 7225 | // the start of this section, return true. |
| 7226 | |
| 7227 | // 2. Let description be connection.[[CurrentLocalDescription]]. |
| 7228 | const SessionDescriptionInterface* description = current_local_description(); |
| 7229 | if (!description) |
| 7230 | return true; |
| 7231 | |
| 7232 | // 3. If connection has created any RTCDataChannels, and no m= section in |
| 7233 | // description has been negotiated yet for data, return true. |
| 7234 | if (!sctp_data_channels_.empty()) { |
| 7235 | if (!cricket::GetFirstDataContent(description->description()->contents())) |
| 7236 | return true; |
| 7237 | } |
| 7238 | |
| 7239 | // 4. For each transceiver in connection's set of transceivers, perform the |
| 7240 | // following checks: |
| 7241 | for (const auto& transceiver : transceivers_) { |
| 7242 | const ContentInfo* current_local_msection = |
| 7243 | FindTransceiverMSection(transceiver.get(), description); |
| 7244 | |
| 7245 | const ContentInfo* current_remote_msection = FindTransceiverMSection( |
| 7246 | transceiver.get(), current_remote_description()); |
| 7247 | |
| 7248 | // 4.3 If transceiver is stopped and is associated with an m= section, |
| 7249 | // but the associated m= section is not yet rejected in |
| 7250 | // connection.[[CurrentLocalDescription]] or |
| 7251 | // connection.[[CurrentRemoteDescription]], return true. |
| 7252 | if (transceiver->stopped()) { |
| 7253 | if (current_local_msection && !current_local_msection->rejected && |
| 7254 | ((current_remote_msection && !current_remote_msection->rejected) || |
| 7255 | !current_remote_msection)) { |
| 7256 | return true; |
| 7257 | } |
| 7258 | continue; |
| 7259 | } |
| 7260 | |
| 7261 | // 4.1 If transceiver isn't stopped and isn't yet associated with an m= |
| 7262 | // section in description, return true. |
| 7263 | if (!current_local_msection) |
| 7264 | return true; |
| 7265 | |
| 7266 | const MediaContentDescription* current_local_media_description = |
| 7267 | current_local_msection->media_description(); |
| 7268 | // 4.2 If transceiver isn't stopped and is associated with an m= section |
| 7269 | // in description then perform the following checks: |
| 7270 | |
| 7271 | // 4.2.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the |
| 7272 | // associated m= section in description either doesn't contain a single |
| 7273 | // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this |
| 7274 | // m= section, or the MSID values themselves, differ from what is in |
| 7275 | // transceiver.sender.[[AssociatedMediaStreamIds]], return true. |
| 7276 | if (RtpTransceiverDirectionHasSend(transceiver->direction())) { |
| 7277 | if (current_local_media_description->streams().size() == 0) |
| 7278 | return true; |
| 7279 | |
| 7280 | std::vector<std::string> msection_msids; |
| 7281 | for (const auto& stream : current_local_media_description->streams()) { |
| 7282 | for (const std::string& msid : stream.stream_ids()) |
| 7283 | msection_msids.push_back(msid); |
| 7284 | } |
| 7285 | |
| 7286 | std::vector<std::string> transceiver_msids = |
| 7287 | transceiver->sender()->stream_ids(); |
| 7288 | if (msection_msids.size() != transceiver_msids.size()) |
| 7289 | return true; |
| 7290 | |
| 7291 | absl::c_sort(transceiver_msids); |
| 7292 | absl::c_sort(msection_msids); |
| 7293 | if (transceiver_msids != msection_msids) |
| 7294 | return true; |
| 7295 | } |
| 7296 | |
| 7297 | // 4.2.2 If description is of type "offer", and the direction of the |
| 7298 | // associated m= section in neither connection.[[CurrentLocalDescription]] |
| 7299 | // nor connection.[[CurrentRemoteDescription]] matches |
| 7300 | // transceiver.[[Direction]], return true. |
| 7301 | if (description->GetType() == SdpType::kOffer) { |
| 7302 | if (!current_remote_description()) |
| 7303 | return true; |
| 7304 | |
| 7305 | if (!current_remote_msection) |
| 7306 | return true; |
| 7307 | |
| 7308 | RtpTransceiverDirection current_local_direction = |
| 7309 | current_local_media_description->direction(); |
| 7310 | RtpTransceiverDirection current_remote_direction = |
| 7311 | current_remote_msection->media_description()->direction(); |
| 7312 | if (transceiver->direction() != current_local_direction && |
| 7313 | transceiver->direction() != |
| 7314 | RtpTransceiverDirectionReversed(current_remote_direction)) { |
| 7315 | return true; |
| 7316 | } |
| 7317 | } |
| 7318 | |
| 7319 | // 4.2.3 If description is of type "answer", and the direction of the |
| 7320 | // associated m= section in the description does not match |
| 7321 | // transceiver.[[Direction]] intersected with the offered direction (as |
| 7322 | // described in [JSEP] (section 5.3.1.)), return true. |
| 7323 | if (description->GetType() == SdpType::kAnswer) { |
| 7324 | if (!remote_description()) |
| 7325 | return true; |
| 7326 | |
| 7327 | const ContentInfo* offered_remote_msection = |
| 7328 | FindTransceiverMSection(transceiver.get(), remote_description()); |
| 7329 | |
| 7330 | RtpTransceiverDirection offered_direction = |
| 7331 | offered_remote_msection |
| 7332 | ? offered_remote_msection->media_description()->direction() |
| 7333 | : RtpTransceiverDirection::kInactive; |
| 7334 | |
| 7335 | if (current_local_media_description->direction() != |
| 7336 | (RtpTransceiverDirectionIntersection( |
| 7337 | transceiver->direction(), |
| 7338 | RtpTransceiverDirectionReversed(offered_direction)))) { |
| 7339 | return true; |
| 7340 | } |
| 7341 | } |
| 7342 | } |
| 7343 | |
| 7344 | // If all the preceding checks were performed and true was not returned, |
| 7345 | // nothing remains to be negotiated; return false. |
| 7346 | return false; |
| 7347 | } |
| 7348 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 7349 | } // namespace webrtc |