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 | |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 13 | #include <limits.h> |
| 14 | #include <stddef.h> |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 15 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 16 | #include <algorithm> |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 17 | #include <memory> |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 18 | #include <set> |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 19 | #include <string> |
Harald Alvestrand | 3eaee6b | 2020-10-19 06:35:55 | [diff] [blame] | 20 | #include <utility> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 21 | |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 22 | #include "absl/algorithm/container.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 20:02:22 | [diff] [blame] | 23 | #include "absl/strings/match.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 24 | #include "absl/strings/string_view.h" |
Fredrik Hernqvist | efbe753 | 2023-01-13 15:42:36 | [diff] [blame] | 25 | #include "absl/types/optional.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 26 | #include "api/jsep_ice_candidate.h" |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 27 | #include "api/media_types.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 28 | #include "api/rtp_parameters.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 29 | #include "api/rtp_transceiver_direction.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 30 | #include "api/uma_metrics.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 31 | #include "api/video/video_codec_constants.h" |
| 32 | #include "call/audio_state.h" |
| 33 | #include "call/packet_receiver.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 34 | #include "media/base/media_channel.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 35 | #include "media/base/media_config.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 36 | #include "media/base/media_engine.h" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 37 | #include "media/base/rid_description.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 38 | #include "media/base/stream_params.h" |
| 39 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 40 | #include "p2p/base/basic_async_resolver_factory.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 41 | #include "p2p/base/connection.h" |
| 42 | #include "p2p/base/connection_info.h" |
| 43 | #include "p2p/base/dtls_transport_internal.h" |
| 44 | #include "p2p/base/p2p_constants.h" |
| 45 | #include "p2p/base/p2p_transport_channel.h" |
| 46 | #include "p2p/base/transport_info.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 47 | #include "pc/ice_server_parsing.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 48 | #include "pc/rtp_receiver.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 49 | #include "pc/rtp_receiver_proxy.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 50 | #include "pc/rtp_sender.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 51 | #include "pc/rtp_sender_proxy.h" |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 52 | #include "pc/sctp_transport.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 53 | #include "pc/simulcast_description.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 54 | #include "pc/webrtc_session_description_factory.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 55 | #include "rtc_base/helpers.h" |
| 56 | #include "rtc_base/ip_address.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 57 | #include "rtc_base/logging.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 58 | #include "rtc_base/net_helper.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 59 | #include "rtc_base/network.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 60 | #include "rtc_base/network_constants.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 61 | #include "rtc_base/socket_address.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 62 | #include "rtc_base/string_encode.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 63 | #include "rtc_base/trace_event.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 64 | #include "rtc_base/unique_id_generator.h" |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 65 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 66 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 67 | using cricket::ContentInfo; |
| 68 | using cricket::ContentInfos; |
| 69 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 70 | using cricket::MediaProtocolType; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 71 | using cricket::RidDescription; |
| 72 | using cricket::RidDirection; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 73 | using cricket::SessionDescription; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 74 | using cricket::SimulcastDescription; |
| 75 | using cricket::SimulcastLayer; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 76 | using cricket::SimulcastLayerList; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 77 | using cricket::StreamParams; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 78 | using cricket::TransportInfo; |
| 79 | |
| 80 | using cricket::LOCAL_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 81 | using cricket::PRFLX_PORT_TYPE; |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 82 | using cricket::RELAY_PORT_TYPE; |
| 83 | using cricket::STUN_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 84 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 85 | namespace webrtc { |
| 86 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 87 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 88 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 89 | // UMA metric names. |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 90 | const char kSimulcastNumberOfEncodings[] = |
| 91 | "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings"; |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 92 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 93 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 94 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 95 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 96 | PeerConnectionInterface::IceTransportsType type) { |
| 97 | switch (type) { |
| 98 | case PeerConnectionInterface::kNone: |
| 99 | return cricket::CF_NONE; |
| 100 | case PeerConnectionInterface::kRelay: |
| 101 | return cricket::CF_RELAY; |
| 102 | case PeerConnectionInterface::kNoHost: |
| 103 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 104 | case PeerConnectionInterface::kAll: |
| 105 | return cricket::CF_ALL; |
| 106 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 107 | RTC_DCHECK_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 108 | } |
| 109 | return cricket::CF_NONE; |
| 110 | } |
| 111 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 112 | IceCandidatePairType GetIceCandidatePairCounter( |
| 113 | const cricket::Candidate& local, |
| 114 | const cricket::Candidate& remote) { |
| 115 | const auto& l = local.type(); |
| 116 | const auto& r = remote.type(); |
| 117 | const auto& host = LOCAL_PORT_TYPE; |
| 118 | const auto& srflx = STUN_PORT_TYPE; |
| 119 | const auto& relay = RELAY_PORT_TYPE; |
| 120 | const auto& prflx = PRFLX_PORT_TYPE; |
| 121 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 122 | bool local_hostname = |
| 123 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 124 | bool remote_hostname = !remote.address().hostname().empty() && |
| 125 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 126 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 127 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 128 | if (local_hostname) { |
| 129 | if (remote_hostname) { |
| 130 | return kIceCandidatePairHostNameHostName; |
| 131 | } else if (remote_private) { |
| 132 | return kIceCandidatePairHostNameHostPrivate; |
| 133 | } else { |
| 134 | return kIceCandidatePairHostNameHostPublic; |
| 135 | } |
| 136 | } else if (local_private) { |
| 137 | if (remote_hostname) { |
| 138 | return kIceCandidatePairHostPrivateHostName; |
| 139 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 140 | return kIceCandidatePairHostPrivateHostPrivate; |
| 141 | } else { |
| 142 | return kIceCandidatePairHostPrivateHostPublic; |
| 143 | } |
| 144 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 145 | if (remote_hostname) { |
| 146 | return kIceCandidatePairHostPublicHostName; |
| 147 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 148 | return kIceCandidatePairHostPublicHostPrivate; |
| 149 | } else { |
| 150 | return kIceCandidatePairHostPublicHostPublic; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | if (l == host && r == srflx) |
| 155 | return kIceCandidatePairHostSrflx; |
| 156 | if (l == host && r == relay) |
| 157 | return kIceCandidatePairHostRelay; |
| 158 | if (l == host && r == prflx) |
| 159 | return kIceCandidatePairHostPrflx; |
| 160 | if (l == srflx && r == host) |
| 161 | return kIceCandidatePairSrflxHost; |
| 162 | if (l == srflx && r == srflx) |
| 163 | return kIceCandidatePairSrflxSrflx; |
| 164 | if (l == srflx && r == relay) |
| 165 | return kIceCandidatePairSrflxRelay; |
| 166 | if (l == srflx && r == prflx) |
| 167 | return kIceCandidatePairSrflxPrflx; |
| 168 | if (l == relay && r == host) |
| 169 | return kIceCandidatePairRelayHost; |
| 170 | if (l == relay && r == srflx) |
| 171 | return kIceCandidatePairRelaySrflx; |
| 172 | if (l == relay && r == relay) |
| 173 | return kIceCandidatePairRelayRelay; |
| 174 | if (l == relay && r == prflx) |
| 175 | return kIceCandidatePairRelayPrflx; |
| 176 | if (l == prflx && r == host) |
| 177 | return kIceCandidatePairPrflxHost; |
| 178 | if (l == prflx && r == srflx) |
| 179 | return kIceCandidatePairPrflxSrflx; |
| 180 | if (l == prflx && r == relay) |
| 181 | return kIceCandidatePairPrflxRelay; |
| 182 | return kIceCandidatePairMax; |
| 183 | } |
| 184 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 185 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 186 | int rtc_configuration_parameter) { |
| 187 | if (rtc_configuration_parameter == |
| 188 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 189 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 190 | } |
| 191 | return rtc_configuration_parameter; |
| 192 | } |
| 193 | |
Jonas Oreland | e309651 | 2020-05-27 07:01:05 | [diff] [blame] | 194 | // Check if the changes of IceTransportsType motives an ice restart. |
| 195 | bool NeedIceRestart(bool surface_ice_candidates_on_ice_transport_type_changed, |
| 196 | PeerConnectionInterface::IceTransportsType current, |
| 197 | PeerConnectionInterface::IceTransportsType modified) { |
| 198 | if (current == modified) { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | if (!surface_ice_candidates_on_ice_transport_type_changed) { |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | auto current_filter = ConvertIceTransportTypeToCandidateFilter(current); |
| 207 | auto modified_filter = ConvertIceTransportTypeToCandidateFilter(modified); |
| 208 | |
| 209 | // If surface_ice_candidates_on_ice_transport_type_changed is true and we |
| 210 | // extend the filter, then no ice restart is needed. |
| 211 | return (current_filter & modified_filter) != current_filter; |
| 212 | } |
| 213 | |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 214 | cricket::IceConfig ParseIceConfig( |
| 215 | const PeerConnectionInterface::RTCConfiguration& config) { |
| 216 | cricket::ContinualGatheringPolicy gathering_policy; |
| 217 | switch (config.continual_gathering_policy) { |
| 218 | case PeerConnectionInterface::GATHER_ONCE: |
| 219 | gathering_policy = cricket::GATHER_ONCE; |
| 220 | break; |
| 221 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 222 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 223 | break; |
| 224 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 225 | RTC_DCHECK_NOTREACHED(); |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 226 | gathering_policy = cricket::GATHER_ONCE; |
| 227 | } |
| 228 | |
| 229 | cricket::IceConfig ice_config; |
| 230 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 231 | config.ice_connection_receiving_timeout); |
| 232 | ice_config.prioritize_most_likely_candidate_pairs = |
| 233 | config.prioritize_most_likely_ice_candidate_pairs; |
| 234 | ice_config.backup_connection_ping_interval = |
| 235 | RTCConfigurationToIceConfigOptionalInt( |
| 236 | config.ice_backup_candidate_pair_ping_interval); |
| 237 | ice_config.continual_gathering_policy = gathering_policy; |
| 238 | ice_config.presume_writable_when_fully_relayed = |
| 239 | config.presume_writable_when_fully_relayed; |
| 240 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 241 | config.surface_ice_candidates_on_ice_transport_type_changed; |
| 242 | ice_config.ice_check_interval_strong_connectivity = |
| 243 | config.ice_check_interval_strong_connectivity; |
| 244 | ice_config.ice_check_interval_weak_connectivity = |
| 245 | config.ice_check_interval_weak_connectivity; |
| 246 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
| 247 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 248 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
| 249 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
| 250 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
| 251 | ice_config.network_preference = config.network_preference; |
Derek Bailey | 6c127a1 | 2021-04-15 19:42:41 | [diff] [blame] | 252 | ice_config.stable_writable_connection_ping_interval = |
| 253 | config.stable_writable_connection_ping_interval_ms; |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 254 | return ice_config; |
| 255 | } |
| 256 | |
| 257 | // Ensures the configuration doesn't have any parameters with invalid values, |
| 258 | // or values that conflict with other parameters. |
| 259 | // |
| 260 | // Returns RTCError::OK() if there are no issues. |
| 261 | RTCError ValidateConfiguration( |
| 262 | const PeerConnectionInterface::RTCConfiguration& config) { |
| 263 | return cricket::P2PTransportChannel::ValidateIceConfig( |
| 264 | ParseIceConfig(config)); |
| 265 | } |
| 266 | |
| 267 | bool HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
| 268 | return content->media_description()->rtcp_mux(); |
| 269 | } |
| 270 | |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 271 | bool DtlsEnabled(const PeerConnectionInterface::RTCConfiguration& configuration, |
| 272 | const PeerConnectionFactoryInterface::Options& options, |
| 273 | const PeerConnectionDependencies& dependencies) { |
| 274 | if (options.disable_encryption) |
| 275 | return false; |
| 276 | |
| 277 | // Enable DTLS by default if we have an identity store or a certificate. |
| 278 | bool default_enabled = |
| 279 | (dependencies.cert_generator || !configuration.certificates.empty()); |
| 280 | |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 281 | #if defined(WEBRTC_FUCHSIA) |
Harald Alvestrand | 50b9552 | 2021-11-18 10:01:06 | [diff] [blame] | 282 | // The `configuration` can override the default value. |
| 283 | return configuration.enable_dtls_srtp.value_or(default_enabled); |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 284 | #else |
| 285 | return default_enabled; |
| 286 | #endif |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 287 | } |
| 288 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 289 | } // namespace |
| 290 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 291 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 292 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 293 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 294 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 295 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 | [diff] [blame] | 296 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 297 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 298 | BundlePolicy bundle_policy; |
| 299 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 300 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 301 | int ice_candidate_pool_size; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 302 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 303 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 304 | bool disable_link_local_networks; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 305 | absl::optional<int> screencast_min_bitrate; |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 306 | #if defined(WEBRTC_FUCHSIA) |
Harald Alvestrand | 50b9552 | 2021-11-18 10:01:06 | [diff] [blame] | 307 | absl::optional<bool> enable_dtls_srtp; |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 308 | #endif |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 309 | TcpCandidatePolicy tcp_candidate_policy; |
| 310 | CandidateNetworkPolicy candidate_network_policy; |
| 311 | int audio_jitter_buffer_max_packets; |
| 312 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 313 | int audio_jitter_buffer_min_delay_ms; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 314 | int ice_connection_receiving_timeout; |
| 315 | int ice_backup_candidate_pair_ping_interval; |
| 316 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 317 | bool prioritize_most_likely_ice_candidate_pairs; |
| 318 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 319 | bool prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 320 | PortPrunePolicy turn_port_prune_policy; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 321 | bool presume_writable_when_fully_relayed; |
| 322 | bool enable_ice_renomination; |
| 323 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 324 | bool surface_ice_candidates_on_ice_transport_type_changed; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 325 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 326 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 327 | absl::optional<int> ice_check_min_interval; |
| 328 | absl::optional<int> ice_unwritable_timeout; |
| 329 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 330 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 331 | absl::optional<int> stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 332 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 333 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 334 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 335 | bool active_reset_srtp_params; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 336 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 337 | bool offer_extmap_allow_mixed; |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 338 | std::string turn_logging_id; |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 339 | bool enable_implicit_rollback; |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 340 | absl::optional<bool> allow_codec_switching; |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 341 | absl::optional<int> report_usage_pattern_delay_ms; |
Derek Bailey | 6c127a1 | 2021-04-15 19:42:41 | [diff] [blame] | 342 | absl::optional<int> stable_writable_connection_ping_interval_ms; |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 343 | webrtc::VpnPreference vpn_preference; |
Jonas Oreland | 2ee0e64 | 2021-08-25 13:43:02 | [diff] [blame] | 344 | std::vector<rtc::NetworkMask> vpn_list; |
Niels Möller | 73d0774 | 2021-12-02 12:58:01 | [diff] [blame] | 345 | PortAllocatorConfig port_allocator_config; |
Henrik Boström | cf2856b | 2022-11-15 08:23:19 | [diff] [blame] | 346 | absl::optional<TimeDelta> pacer_burst_interval; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 347 | }; |
| 348 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 349 | "Did you add something to RTCConfiguration and forget to " |
| 350 | "update operator==?"); |
| 351 | return type == o.type && servers == o.servers && |
| 352 | bundle_policy == o.bundle_policy && |
| 353 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 354 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 355 | candidate_network_policy == o.candidate_network_policy && |
| 356 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 357 | audio_jitter_buffer_fast_accelerate == |
| 358 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 359 | audio_jitter_buffer_min_delay_ms == |
| 360 | o.audio_jitter_buffer_min_delay_ms && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 361 | ice_connection_receiving_timeout == |
| 362 | o.ice_connection_receiving_timeout && |
| 363 | ice_backup_candidate_pair_ping_interval == |
| 364 | o.ice_backup_candidate_pair_ping_interval && |
| 365 | continual_gathering_policy == o.continual_gathering_policy && |
| 366 | certificates == o.certificates && |
| 367 | prioritize_most_likely_ice_candidate_pairs == |
| 368 | o.prioritize_most_likely_ice_candidate_pairs && |
Henrik Boström | 24e0337 | 2022-10-27 11:49:10 | [diff] [blame] | 369 | media_config == o.media_config && |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 370 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 371 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 372 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 373 | screencast_min_bitrate == o.screencast_min_bitrate && |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 374 | #if defined(WEBRTC_FUCHSIA) |
Harald Alvestrand | 50b9552 | 2021-11-18 10:01:06 | [diff] [blame] | 375 | enable_dtls_srtp == o.enable_dtls_srtp && |
Harald Alvestrand | ca32793 | 2022-04-04 15:37:31 | [diff] [blame] | 376 | #endif |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 377 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 378 | prune_turn_ports == o.prune_turn_ports && |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 379 | turn_port_prune_policy == o.turn_port_prune_policy && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 380 | presume_writable_when_fully_relayed == |
| 381 | o.presume_writable_when_fully_relayed && |
| 382 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 19:50:14 | [diff] [blame] | 383 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 384 | surface_ice_candidates_on_ice_transport_type_changed == |
| 385 | o.surface_ice_candidates_on_ice_transport_type_changed && |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 386 | ice_check_interval_strong_connectivity == |
| 387 | o.ice_check_interval_strong_connectivity && |
| 388 | ice_check_interval_weak_connectivity == |
| 389 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 390 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 391 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 392 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 393 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 394 | stun_candidate_keepalive_interval == |
| 395 | o.stun_candidate_keepalive_interval && |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 396 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 397 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 398 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 16:43:21 | [diff] [blame] | 399 | active_reset_srtp_params == o.active_reset_srtp_params && |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 400 | crypto_options == o.crypto_options && |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 401 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed && |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 402 | turn_logging_id == o.turn_logging_id && |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 403 | enable_implicit_rollback == o.enable_implicit_rollback && |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 404 | allow_codec_switching == o.allow_codec_switching && |
Derek Bailey | 6c127a1 | 2021-04-15 19:42:41 | [diff] [blame] | 405 | report_usage_pattern_delay_ms == o.report_usage_pattern_delay_ms && |
| 406 | stable_writable_connection_ping_interval_ms == |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 407 | o.stable_writable_connection_ping_interval_ms && |
Niels Möller | 73d0774 | 2021-12-02 12:58:01 | [diff] [blame] | 408 | vpn_preference == o.vpn_preference && vpn_list == o.vpn_list && |
| 409 | port_allocator_config.min_port == o.port_allocator_config.min_port && |
| 410 | port_allocator_config.max_port == o.port_allocator_config.max_port && |
Henrik Boström | cf2856b | 2022-11-15 08:23:19 | [diff] [blame] | 411 | port_allocator_config.flags == o.port_allocator_config.flags && |
| 412 | pacer_burst_interval == o.pacer_burst_interval; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 416 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 417 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 19:44:26 | [diff] [blame] | 418 | } |
| 419 | |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 420 | RTCErrorOr<rtc::scoped_refptr<PeerConnection>> PeerConnection::Create( |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 421 | rtc::scoped_refptr<ConnectionContext> context, |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 422 | const PeerConnectionFactoryInterface::Options& options, |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 423 | std::unique_ptr<RtcEventLog> event_log, |
| 424 | std::unique_ptr<Call> call, |
| 425 | const PeerConnectionInterface::RTCConfiguration& configuration, |
| 426 | PeerConnectionDependencies dependencies) { |
Henrik Boström | 62995db | 2022-01-03 08:58:10 | [diff] [blame] | 427 | // TODO(https://crbug.com/webrtc/13528): Remove support for kPlanB. |
Henrik Boström | 6d2fe89 | 2022-01-21 08:51:07 | [diff] [blame] | 428 | if (configuration.sdp_semantics == SdpSemantics::kPlanB_DEPRECATED) { |
| 429 | RTC_LOG(LS_WARNING) |
| 430 | << "PeerConnection constructed with legacy SDP semantics!"; |
| 431 | } |
Henrik Boström | 62995db | 2022-01-03 08:58:10 | [diff] [blame] | 432 | |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 433 | RTCError config_error = cricket::P2PTransportChannel::ValidateIceConfig( |
| 434 | ParseIceConfig(configuration)); |
| 435 | if (!config_error.ok()) { |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 436 | RTC_LOG(LS_ERROR) << "Invalid ICE configuration: " |
| 437 | << config_error.message(); |
| 438 | return config_error; |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if (!dependencies.allocator) { |
| 442 | RTC_LOG(LS_ERROR) |
| 443 | << "PeerConnection initialized without a PortAllocator? " |
| 444 | "This shouldn't happen if using PeerConnectionFactory."; |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 445 | return RTCError( |
| 446 | RTCErrorType::INVALID_PARAMETER, |
| 447 | "Attempt to create a PeerConnection without a PortAllocatorFactory"); |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | if (!dependencies.observer) { |
| 451 | // TODO(deadbeef): Why do we do this? |
| 452 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
| 453 | "PeerConnectionObserver"; |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 454 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 455 | "Attempt to create a PeerConnection without an observer"); |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | bool is_unified_plan = |
| 459 | configuration.sdp_semantics == SdpSemantics::kUnifiedPlan; |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 460 | bool dtls_enabled = DtlsEnabled(configuration, options, dependencies); |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 461 | |
| 462 | // Interim code: If an AsyncResolverFactory is given, but not an |
| 463 | // AsyncDnsResolverFactory, wrap it in a WrappingAsyncDnsResolverFactory |
Harald Alvestrand | b8617d1 | 2023-08-23 09:01:53 | [diff] [blame] | 464 | // If neither is given, create a BasicAsyncDnsResolverFactory. |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 465 | // TODO(bugs.webrtc.org/12598): Remove code once all callers pass a |
| 466 | // AsyncDnsResolverFactory. |
Harald Alvestrand | 4d25a77 | 2023-08-25 11:07:28 | [diff] [blame] | 467 | #pragma clang diagnostic push |
| 468 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 469 | if (dependencies.async_dns_resolver_factory && |
| 470 | dependencies.async_resolver_factory) { |
| 471 | RTC_LOG(LS_ERROR) |
| 472 | << "Attempt to set both old and new type of DNS resolver factory"; |
| 473 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 474 | "Both old and new type of DNS resolver given"); |
| 475 | } |
Harald Alvestrand | 4d25a77 | 2023-08-25 11:07:28 | [diff] [blame] | 476 | if (!dependencies.async_dns_resolver_factory) { |
| 477 | if (dependencies.async_resolver_factory) { |
| 478 | dependencies.async_dns_resolver_factory = |
| 479 | std::make_unique<WrappingAsyncDnsResolverFactory>( |
| 480 | std::move(dependencies.async_resolver_factory)); |
| 481 | } else { |
| 482 | dependencies.async_dns_resolver_factory = |
| 483 | std::make_unique<BasicAsyncDnsResolverFactory>(); |
| 484 | } |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 485 | } |
Harald Alvestrand | 4d25a77 | 2023-08-25 11:07:28 | [diff] [blame] | 486 | #pragma clang diagnostic pop |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 487 | |
Harald Alvestrand | fd9a8f8 | 2020-10-26 14:17:02 | [diff] [blame] | 488 | // The PeerConnection constructor consumes some, but not all, dependencies. |
Tommi | 87f7090 | 2021-04-27 12:43:08 | [diff] [blame] | 489 | auto pc = rtc::make_ref_counted<PeerConnection>( |
| 490 | context, options, is_unified_plan, std::move(event_log), std::move(call), |
| 491 | dependencies, dtls_enabled); |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 492 | RTCError init_error = pc->Initialize(configuration, std::move(dependencies)); |
| 493 | if (!init_error.ok()) { |
| 494 | RTC_LOG(LS_ERROR) << "PeerConnection initialization failed"; |
| 495 | return init_error; |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 496 | } |
| 497 | return pc; |
| 498 | } |
| 499 | |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 500 | PeerConnection::PeerConnection( |
| 501 | rtc::scoped_refptr<ConnectionContext> context, |
| 502 | const PeerConnectionFactoryInterface::Options& options, |
| 503 | bool is_unified_plan, |
| 504 | std::unique_ptr<RtcEventLog> event_log, |
| 505 | std::unique_ptr<Call> call, |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 506 | PeerConnectionDependencies& dependencies, |
| 507 | bool dtls_enabled) |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 | [diff] [blame] | 508 | : context_(context), |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 509 | trials_(std::move(dependencies.trials), &context->field_trials()), |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 510 | options_(options), |
Harald Alvestrand | fd9a8f8 | 2020-10-26 14:17:02 | [diff] [blame] | 511 | observer_(dependencies.observer), |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 512 | is_unified_plan_(is_unified_plan), |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 513 | event_log_(std::move(event_log)), |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 514 | event_log_ptr_(event_log_.get()), |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 515 | async_dns_resolver_factory_( |
| 516 | std::move(dependencies.async_dns_resolver_factory)), |
Harald Alvestrand | fd9a8f8 | 2020-10-26 14:17:02 | [diff] [blame] | 517 | port_allocator_(std::move(dependencies.allocator)), |
| 518 | ice_transport_factory_(std::move(dependencies.ice_transport_factory)), |
| 519 | tls_cert_verifier_(std::move(dependencies.tls_cert_verifier)), |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 520 | call_(std::move(call)), |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 521 | call_ptr_(call_.get()), |
Tomas Gunnarsson | 97a387d | 2021-03-29 19:04:29 | [diff] [blame] | 522 | // RFC 3264: The numeric value of the session id and version in the |
| 523 | // o line MUST be representable with a "64 bit signed integer". |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 524 | // Due to this constraint session id `session_id_` is max limited to |
Tomas Gunnarsson | 97a387d | 2021-03-29 19:04:29 | [diff] [blame] | 525 | // LLONG_MAX. |
| 526 | session_id_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)), |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 527 | dtls_enabled_(dtls_enabled), |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 528 | data_channel_controller_(this), |
Lahiru Ginnaliya Gamathige | 70f9e24 | 2021-01-28 07:32:46 | [diff] [blame] | 529 | message_handler_(signaling_thread()), |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 530 | weak_factory_(this) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 531 | worker_thread()->BlockingCall([this] { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 532 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 533 | worker_thread_safety_ = PendingTaskSafetyFlag::Create(); |
| 534 | if (!call_) |
| 535 | worker_thread_safety_->SetNotAlive(); |
| 536 | }); |
| 537 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 538 | |
| 539 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 540 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 541 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 542 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 543 | if (sdp_handler_) { |
| 544 | sdp_handler_->PrepareForShutdown(); |
| 545 | } |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 546 | |
Tommi | 94774d4 | 2023-04-24 09:31:45 | [diff] [blame] | 547 | // In case `Close()` wasn't called, always make sure the controller cancels |
| 548 | // potentially pending operations. |
| 549 | data_channel_controller_.PrepareForShutdown(); |
| 550 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 551 | // Need to stop transceivers before destroying the stats collector because |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 552 | // AudioRtpSender has a reference to the LegacyStatsCollector it will update |
| 553 | // when stopping. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 554 | if (rtp_manager()) { |
| 555 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
| 556 | transceiver->StopInternal(); |
| 557 | } |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 558 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 559 | |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 560 | legacy_stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 13:06:57 | [diff] [blame] | 561 | if (stats_collector_) { |
| 562 | stats_collector_->WaitForPendingRequest(); |
| 563 | stats_collector_ = nullptr; |
| 564 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 565 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 566 | if (sdp_handler_) { |
| 567 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 568 | // the last stats request can still read from the channels. |
| 569 | sdp_handler_->DestroyAllChannels(); |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 570 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 571 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 572 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 573 | sdp_handler_->ResetSessionDescFactory(); |
| 574 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 575 | |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 576 | // port_allocator_ and transport_controller_ live on the network thread and |
| 577 | // should be destroyed there. |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 578 | transport_controller_copy_ = nullptr; |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 579 | network_thread()->BlockingCall([this] { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 580 | RTC_DCHECK_RUN_ON(network_thread()); |
Tommi | b00d63c | 2023-04-12 17:49:53 | [diff] [blame] | 581 | TeardownDataChannelTransport_n(RTCError::OK()); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 582 | transport_controller_.reset(); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 583 | port_allocator_.reset(); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 584 | if (network_thread_safety_) |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 585 | network_thread_safety_->SetNotAlive(); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 586 | }); |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 587 | |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 588 | // call_ and event_log_ must be destroyed on the worker thread. |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 589 | worker_thread()->BlockingCall([this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 590 | RTC_DCHECK_RUN_ON(worker_thread()); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 591 | worker_thread_safety_->SetNotAlive(); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 592 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 593 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 594 | event_log_.reset(); |
| 595 | }); |
Tommi | 5271965 | 2023-04-04 09:59:55 | [diff] [blame] | 596 | |
| 597 | data_channel_controller_.PrepareForShutdown(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 598 | } |
| 599 | |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 600 | RTCError PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 601 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 602 | PeerConnectionDependencies dependencies) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 603 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 604 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 605 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 606 | cricket::ServerAddresses stun_servers; |
| 607 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 608 | |
Philipp Hancke | 633dc2f | 2022-10-12 08:16:14 | [diff] [blame] | 609 | RTCError parse_error = ParseIceServersOrError(configuration.servers, |
| 610 | &stun_servers, &turn_servers); |
| 611 | if (!parse_error.ok()) { |
| 612 | return parse_error; |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 613 | } |
| 614 | |
Philipp Hancke | 41a8357 | 2022-11-28 13:47:45 | [diff] [blame] | 615 | // Restrict number of TURN servers. |
Philipp Hancke | 3e224c7 | 2023-03-20 07:22:49 | [diff] [blame] | 616 | if (turn_servers.size() > cricket::kMaxTurnServers) { |
Philipp Hancke | 41a8357 | 2022-11-28 13:47:45 | [diff] [blame] | 617 | RTC_LOG(LS_WARNING) << "Number of configured TURN servers is " |
| 618 | << turn_servers.size() |
| 619 | << " which exceeds the maximum allowed number of " |
| 620 | << cricket::kMaxTurnServers; |
| 621 | turn_servers.resize(cricket::kMaxTurnServers); |
| 622 | } |
| 623 | |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 624 | // Add the turn logging id to all turn servers |
| 625 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 626 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 627 | } |
| 628 | |
Harald Alvestrand | fd9a8f8 | 2020-10-26 14:17:02 | [diff] [blame] | 629 | // Note if STUN or TURN servers were supplied. |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 630 | if (!stun_servers.empty()) { |
| 631 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 632 | } |
| 633 | if (!turn_servers.empty()) { |
| 634 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 635 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 636 | |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 637 | // Network thread initialization. |
Jared Siskin | bceec84 | 2023-04-20 21:10:51 | [diff] [blame] | 638 | transport_controller_copy_ = network_thread()->BlockingCall([&] { |
| 639 | RTC_DCHECK_RUN_ON(network_thread()); |
| 640 | network_thread_safety_ = PendingTaskSafetyFlag::Create(); |
| 641 | InitializePortAllocatorResult pa_result = |
| 642 | InitializePortAllocator_n(stun_servers, turn_servers, configuration); |
| 643 | // Send information about IPv4/IPv6 status. |
| 644 | PeerConnectionAddressFamilyCounter address_family = |
| 645 | pa_result.enable_ipv6 ? kPeerConnection_IPv6 : kPeerConnection_IPv4; |
| 646 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 647 | kPeerConnectionAddressFamilyCounter_Max); |
| 648 | return InitializeTransportController_n(configuration, dependencies); |
| 649 | }); |
Lahiru Ginnaliya Gamathige | e99c68d | 2020-09-30 21:33:45 | [diff] [blame] | 650 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 651 | configuration_ = configuration; |
| 652 | |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 653 | legacy_stats_ = std::make_unique<LegacyStatsCollector>(this); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 654 | stats_collector_ = RTCStatsCollector::Create(this); |
| 655 | |
Harald Alvestrand | 66c4036 | 2022-01-28 17:41:30 | [diff] [blame] | 656 | sdp_handler_ = SdpOfferAnswerHandler::Create(this, configuration, |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 657 | dependencies, context_.get()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 658 | |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 659 | rtp_manager_ = std::make_unique<RtpTransmissionManager>( |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 660 | IsUnifiedPlan(), context_.get(), &usage_pattern_, observer_, |
| 661 | legacy_stats_.get(), [this]() { |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 662 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 663 | sdp_handler_->UpdateNegotiationNeeded(); |
| 664 | }); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 665 | |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 666 | // Add default audio/video transceivers for Plan B SDP. |
| 667 | if (!IsUnifiedPlan()) { |
| 668 | rtp_manager()->transceivers()->Add( |
| 669 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 670 | signaling_thread(), rtc::make_ref_counted<RtpTransceiver>( |
| 671 | cricket::MEDIA_TYPE_AUDIO, context()))); |
Artem Titov | c6c02ef | 2022-05-09 08:30:09 | [diff] [blame] | 672 | rtp_manager()->transceivers()->Add( |
| 673 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 674 | signaling_thread(), rtc::make_ref_counted<RtpTransceiver>( |
| 675 | cricket::MEDIA_TYPE_VIDEO, context()))); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 676 | } |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 677 | |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 678 | int delay_ms = configuration.report_usage_pattern_delay_ms |
| 679 | ? *configuration.report_usage_pattern_delay_ms |
| 680 | : REPORT_USAGE_PATTERN_DELAY_MS; |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 681 | message_handler_.RequestUsagePatternReport( |
| 682 | [this]() { |
| 683 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 684 | ReportUsagePattern(); |
| 685 | }, |
| 686 | delay_ms); |
Jonas Oreland | a3aa9bd | 2019-04-17 05:38:40 | [diff] [blame] | 687 | |
Harald Alvestrand | a3dd772 | 2020-11-27 08:05:42 | [diff] [blame] | 688 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 689 | } |
| 690 | |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 691 | JsepTransportController* PeerConnection::InitializeTransportController_n( |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 692 | const RTCConfiguration& configuration, |
| 693 | const PeerConnectionDependencies& dependencies) { |
| 694 | JsepTransportController::Config config; |
| 695 | config.redetermine_role_on_ice_restart = |
| 696 | configuration.redetermine_role_on_ice_restart; |
| 697 | config.ssl_max_version = options_.ssl_max_version; |
| 698 | config.disable_encryption = options_.disable_encryption; |
| 699 | config.bundle_policy = configuration.bundle_policy; |
| 700 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
| 701 | // TODO(bugs.webrtc.org/9891) - Remove options_.crypto_options then remove |
| 702 | // this stub. |
| 703 | config.crypto_options = configuration.crypto_options.has_value() |
| 704 | ? *configuration.crypto_options |
| 705 | : options_.crypto_options; |
| 706 | config.transport_observer = this; |
| 707 | config.rtcp_handler = InitializeRtcpCallback(); |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 708 | config.un_demuxable_packet_handler = InitializeUnDemuxablePacketHandler(); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 709 | config.event_log = event_log_ptr_; |
| 710 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 711 | config.enable_external_auth = true; |
| 712 | #endif |
| 713 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
| 714 | |
| 715 | // DTLS has to be enabled to use SCTP. |
Florent Castelli | 516e284 | 2021-04-19 13:29:50 | [diff] [blame] | 716 | if (dtls_enabled_) { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 717 | config.sctp_factory = context_->sctp_transport_factory(); |
| 718 | } |
| 719 | |
| 720 | config.ice_transport_factory = ice_transport_factory_.get(); |
| 721 | config.on_dtls_handshake_error_ = |
| 722 | [weak_ptr = weak_factory_.GetWeakPtr()](rtc::SSLHandshakeError s) { |
| 723 | if (weak_ptr) { |
| 724 | weak_ptr->OnTransportControllerDtlsHandshakeError(s); |
| 725 | } |
| 726 | }; |
| 727 | |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 728 | config.field_trials = trials_.get(); |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 729 | |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 730 | transport_controller_.reset(new JsepTransportController( |
| 731 | network_thread(), port_allocator_.get(), |
| 732 | async_dns_resolver_factory_.get(), std::move(config))); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 733 | |
| 734 | transport_controller_->SubscribeIceConnectionState( |
| 735 | [this](cricket::IceConnectionState s) { |
| 736 | RTC_DCHECK_RUN_ON(network_thread()); |
| 737 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 738 | SafeTask(signaling_thread_safety_.flag(), [this, s]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 739 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 740 | OnTransportControllerConnectionState(s); |
| 741 | })); |
| 742 | }); |
| 743 | transport_controller_->SubscribeConnectionState( |
| 744 | [this](PeerConnectionInterface::PeerConnectionState s) { |
| 745 | RTC_DCHECK_RUN_ON(network_thread()); |
| 746 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 747 | SafeTask(signaling_thread_safety_.flag(), [this, s]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 748 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 749 | SetConnectionState(s); |
| 750 | })); |
| 751 | }); |
| 752 | transport_controller_->SubscribeStandardizedIceConnectionState( |
| 753 | [this](PeerConnectionInterface::IceConnectionState s) { |
| 754 | RTC_DCHECK_RUN_ON(network_thread()); |
| 755 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 756 | SafeTask(signaling_thread_safety_.flag(), [this, s]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 757 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 758 | SetStandardizedIceConnectionState(s); |
| 759 | })); |
| 760 | }); |
| 761 | transport_controller_->SubscribeIceGatheringState( |
| 762 | [this](cricket::IceGatheringState s) { |
| 763 | RTC_DCHECK_RUN_ON(network_thread()); |
| 764 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 765 | SafeTask(signaling_thread_safety_.flag(), [this, s]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 766 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 767 | OnTransportControllerGatheringState(s); |
| 768 | })); |
| 769 | }); |
| 770 | transport_controller_->SubscribeIceCandidateGathered( |
| 771 | [this](const std::string& transport, |
| 772 | const std::vector<cricket::Candidate>& candidates) { |
| 773 | RTC_DCHECK_RUN_ON(network_thread()); |
| 774 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 775 | SafeTask(signaling_thread_safety_.flag(), |
| 776 | [this, t = transport, c = candidates]() { |
| 777 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 778 | OnTransportControllerCandidatesGathered(t, c); |
| 779 | })); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 780 | }); |
| 781 | transport_controller_->SubscribeIceCandidateError( |
| 782 | [this](const cricket::IceCandidateErrorEvent& event) { |
| 783 | RTC_DCHECK_RUN_ON(network_thread()); |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 784 | signaling_thread()->PostTask( |
| 785 | SafeTask(signaling_thread_safety_.flag(), [this, event = event]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 786 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 787 | OnTransportControllerCandidateError(event); |
| 788 | })); |
| 789 | }); |
| 790 | transport_controller_->SubscribeIceCandidatesRemoved( |
| 791 | [this](const std::vector<cricket::Candidate>& c) { |
| 792 | RTC_DCHECK_RUN_ON(network_thread()); |
| 793 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 794 | SafeTask(signaling_thread_safety_.flag(), [this, c = c]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 795 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 796 | OnTransportControllerCandidatesRemoved(c); |
| 797 | })); |
| 798 | }); |
| 799 | transport_controller_->SubscribeIceCandidatePairChanged( |
| 800 | [this](const cricket::CandidatePairChangeEvent& event) { |
| 801 | RTC_DCHECK_RUN_ON(network_thread()); |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 802 | signaling_thread()->PostTask( |
| 803 | SafeTask(signaling_thread_safety_.flag(), [this, event = event]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 804 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 805 | OnTransportControllerCandidateChanged(event); |
| 806 | })); |
| 807 | }); |
| 808 | |
| 809 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 810 | return transport_controller_.get(); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 811 | } |
| 812 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 813 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 814 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 815 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 816 | "Plan SdpSemantics. Please use GetSenders " |
| 817 | "instead."; |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 818 | return sdp_handler_->local_streams(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 819 | } |
| 820 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 821 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 822 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 823 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 824 | "Plan SdpSemantics. Please use GetReceivers " |
| 825 | "instead."; |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 826 | return sdp_handler_->remote_streams(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 827 | } |
| 828 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 | [diff] [blame] | 829 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 830 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 831 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 832 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 833 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 834 | if (!ConfiguredForMedia()) { |
| 835 | RTC_LOG(LS_ERROR) << "AddStream: Not configured for media"; |
| 836 | return false; |
| 837 | } |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 838 | return sdp_handler_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 842 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 843 | RTC_DCHECK(ConfiguredForMedia()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 844 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 845 | "Plan SdpSemantics. Please use RemoveTrack " |
| 846 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 847 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 848 | sdp_handler_->RemoveStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 849 | } |
| 850 | |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 851 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 852 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 853 | const std::vector<std::string>& stream_ids) { |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 854 | return AddTrack(std::move(track), stream_ids, nullptr); |
| 855 | } |
| 856 | |
| 857 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
| 858 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 859 | const std::vector<std::string>& stream_ids, |
| 860 | const std::vector<RtpEncodingParameters>& init_send_encodings) { |
| 861 | return AddTrack(std::move(track), stream_ids, &init_send_encodings); |
| 862 | } |
| 863 | |
| 864 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
| 865 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 866 | const std::vector<std::string>& stream_ids, |
| 867 | const std::vector<RtpEncodingParameters>* init_send_encodings) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 868 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 869 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 870 | if (!ConfiguredForMedia()) { |
| 871 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 872 | "Not configured for media"); |
| 873 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 874 | if (!track) { |
| 875 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 876 | } |
| 877 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 878 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 879 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 880 | "Track has invalid kind: " + track->kind()); |
| 881 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 882 | if (IsClosed()) { |
| 883 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 884 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 885 | } |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 886 | if (rtp_manager()->FindSenderForTrack(track.get())) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 887 | LOG_AND_RETURN_ERROR( |
| 888 | RTCErrorType::INVALID_PARAMETER, |
| 889 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 890 | } |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 891 | auto sender_or_error = |
| 892 | rtp_manager()->AddTrack(track, stream_ids, init_send_encodings); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 893 | if (sender_or_error.ok()) { |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 894 | sdp_handler_->UpdateNegotiationNeeded(); |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 895 | legacy_stats_->AddTrack(track.get()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 896 | } |
| 897 | return sender_or_error; |
| 898 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 899 | |
Harald Alvestrand | 09a0d01 | 2022-01-04 19:42:07 | [diff] [blame] | 900 | RTCError PeerConnection::RemoveTrackOrError( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 901 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 902 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 903 | if (!ConfiguredForMedia()) { |
| 904 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 905 | "Not configured for media"); |
| 906 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 907 | if (!sender) { |
| 908 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 909 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 910 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 911 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 912 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 913 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 914 | if (IsUnifiedPlan()) { |
| 915 | auto transceiver = FindTransceiverBySender(sender); |
| 916 | if (!transceiver || !sender->track()) { |
| 917 | return RTCError::OK(); |
| 918 | } |
| 919 | sender->SetTrack(nullptr); |
| 920 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 921 | transceiver->internal()->set_direction( |
| 922 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 923 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 924 | transceiver->internal()->set_direction( |
| 925 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 926 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 927 | } else { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 928 | bool removed; |
| 929 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 930 | removed = rtp_manager()->GetAudioTransceiver()->internal()->RemoveSender( |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 931 | sender.get()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 932 | } else { |
| 933 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 934 | removed = rtp_manager()->GetVideoTransceiver()->internal()->RemoveSender( |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 935 | sender.get()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 936 | } |
| 937 | if (!removed) { |
| 938 | LOG_AND_RETURN_ERROR( |
| 939 | RTCErrorType::INVALID_PARAMETER, |
| 940 | "Couldn't find sender " + sender->id() + " to remove."); |
| 941 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 942 | } |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 943 | sdp_handler_->UpdateNegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 944 | return RTCError::OK(); |
| 945 | } |
| 946 | |
| 947 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 948 | PeerConnection::FindTransceiverBySender( |
| 949 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 950 | return rtp_manager()->transceivers()->FindBySender(sender); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 951 | } |
| 952 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 953 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 954 | PeerConnection::AddTransceiver( |
| 955 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 956 | if (!ConfiguredForMedia()) { |
| 957 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 958 | "Not configured for media"); |
| 959 | } |
| 960 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 961 | return AddTransceiver(track, RtpTransceiverInit()); |
| 962 | } |
| 963 | |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 964 | RtpTransportInternal* PeerConnection::GetRtpTransport(const std::string& mid) { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 965 | // TODO(bugs.webrtc.org/9987): Avoid the thread jump. |
| 966 | // This might be done by caching the value on the signaling thread. |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 967 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 968 | return network_thread()->BlockingCall([this, &mid] { |
| 969 | RTC_DCHECK_RUN_ON(network_thread()); |
| 970 | auto rtp_transport = transport_controller_->GetRtpTransport(mid); |
| 971 | RTC_DCHECK(rtp_transport); |
| 972 | return rtp_transport; |
| 973 | }); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 974 | } |
| 975 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 976 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 977 | PeerConnection::AddTransceiver( |
| 978 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 979 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 980 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 981 | if (!ConfiguredForMedia()) { |
| 982 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 983 | "Not configured for media"); |
| 984 | } |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 985 | RTC_CHECK(IsUnifiedPlan()) |
| 986 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 987 | if (!track) { |
| 988 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 989 | } |
| 990 | cricket::MediaType media_type; |
| 991 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 992 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 993 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 994 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 995 | } else { |
| 996 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 997 | "Track kind is not audio or video"); |
| 998 | } |
| 999 | return AddTransceiver(media_type, track, init); |
| 1000 | } |
| 1001 | |
| 1002 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1003 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1004 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1005 | } |
| 1006 | |
| 1007 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1008 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1009 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1010 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1011 | if (!ConfiguredForMedia()) { |
| 1012 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1013 | "Not configured for media"); |
| 1014 | } |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1015 | RTC_CHECK(IsUnifiedPlan()) |
| 1016 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1017 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1018 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1019 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1020 | "media type is not audio or video"); |
| 1021 | } |
| 1022 | return AddTransceiver(media_type, nullptr, init); |
| 1023 | } |
| 1024 | |
| 1025 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1026 | PeerConnection::AddTransceiver( |
| 1027 | cricket::MediaType media_type, |
| 1028 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1029 | const RtpTransceiverInit& init, |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1030 | bool update_negotiation_needed) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1031 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1032 | if (!ConfiguredForMedia()) { |
| 1033 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1034 | "Not configured for media"); |
| 1035 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1036 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1037 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1038 | if (track) { |
| 1039 | RTC_DCHECK_EQ(media_type, |
| 1040 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1041 | ? cricket::MEDIA_TYPE_AUDIO |
| 1042 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1043 | } |
| 1044 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 1045 | RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings, |
| 1046 | init.send_encodings.size(), 0, 7, 8); |
| 1047 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1048 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1049 | [](const RtpEncodingParameters& encoding) { |
| 1050 | return !encoding.rid.empty(); |
| 1051 | }); |
| 1052 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-07 01:09:52 | [diff] [blame] | 1053 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1054 | RTCErrorType::INVALID_PARAMETER, |
| 1055 | "RIDs must be provided for either all or none of the send encodings."); |
Emircan Uysaler | 7832343 | 2019-02-08 20:41:39 | [diff] [blame] | 1056 | } |
| 1057 | |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 1058 | if (num_rids > 0 && absl::c_any_of(init.send_encodings, |
| 1059 | [](const RtpEncodingParameters& encoding) { |
| 1060 | return !IsLegalRsidName(encoding.rid); |
| 1061 | })) { |
| 1062 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1063 | "Invalid RID value provided."); |
| 1064 | } |
| 1065 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1066 | if (absl::c_any_of(init.send_encodings, |
| 1067 | [](const RtpEncodingParameters& encoding) { |
| 1068 | return encoding.ssrc.has_value(); |
| 1069 | })) { |
| 1070 | LOG_AND_RETURN_ERROR( |
| 1071 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1072 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | RtpParameters parameters; |
| 1076 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1077 | |
| 1078 | // Encodings are dropped from the tail if too many are provided. |
Florent Castelli | e1b685a | 2021-04-30 17:11:37 | [diff] [blame] | 1079 | size_t max_simulcast_streams = |
| 1080 | media_type == cricket::MEDIA_TYPE_VIDEO ? kMaxSimulcastStreams : 1u; |
| 1081 | if (parameters.encodings.size() > max_simulcast_streams) { |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1082 | parameters.encodings.erase( |
Florent Castelli | e1b685a | 2021-04-30 17:11:37 | [diff] [blame] | 1083 | parameters.encodings.begin() + max_simulcast_streams, |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1084 | parameters.encodings.end()); |
| 1085 | } |
| 1086 | |
| 1087 | // Single RID should be removed. |
| 1088 | if (parameters.encodings.size() == 1 && |
| 1089 | !parameters.encodings[0].rid.empty()) { |
| 1090 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1091 | parameters.encodings[0].rid.clear(); |
| 1092 | } |
| 1093 | |
| 1094 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1095 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1096 | rtc::UniqueStringGenerator rid_generator; |
| 1097 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
Harald Alvestrand | 5ad491e | 2023-02-10 11:28:04 | [diff] [blame] | 1098 | encoding.rid = rid_generator.GenerateString(); |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | |
Florent Castelli | bd1e5d5 | 2023-01-17 17:08:26 | [diff] [blame] | 1102 | // If no encoding parameters were provided, a default entry is created. |
| 1103 | if (parameters.encodings.empty()) { |
| 1104 | parameters.encodings.push_back({}); |
| 1105 | } |
| 1106 | |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1107 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1108 | LOG_AND_RETURN_ERROR( |
| 1109 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1110 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1111 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1112 | |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 1113 | std::vector<cricket::VideoCodec> codecs; |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 1114 | // Gather the current codec capabilities to allow checking scalabilityMode and |
| 1115 | // codec selection against supported values. |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 1116 | if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 1117 | codecs = context_->media_engine()->video().send_codecs(false); |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 1118 | } else { |
| 1119 | codecs = context_->media_engine()->voice().send_codecs(); |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 1120 | } |
| 1121 | |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 1122 | auto result = |
| 1123 | cricket::CheckRtpParametersValues(parameters, codecs, absl::nullopt); |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 1124 | if (!result.ok()) { |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 1125 | if (result.type() == RTCErrorType::INVALID_MODIFICATION) { |
| 1126 | result.set_type(RTCErrorType::UNSUPPORTED_OPERATION); |
| 1127 | } |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 1128 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1129 | } |
| 1130 | |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1131 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1132 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1133 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1134 | // that sender ID is already in use. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1135 | std::string sender_id = (track && !rtp_manager()->FindSenderById(track->id()) |
| 1136 | ? track->id() |
| 1137 | : rtc::CreateRandomUuid()); |
| 1138 | auto sender = rtp_manager()->CreateSender( |
| 1139 | media_type, sender_id, track, init.stream_ids, parameters.encodings); |
| 1140 | auto receiver = |
| 1141 | rtp_manager()->CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1142 | auto transceiver = rtp_manager()->CreateAndAddTransceiver(sender, receiver); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1143 | transceiver->internal()->set_direction(init.direction); |
| 1144 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1145 | if (update_negotiation_needed) { |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1146 | sdp_handler_->UpdateNegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1147 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1148 | |
| 1149 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1150 | } |
| 1151 | |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1152 | void PeerConnection::OnNegotiationNeeded() { |
| 1153 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1154 | RTC_DCHECK(!IsClosed()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1155 | sdp_handler_->UpdateNegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1156 | } |
| 1157 | |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1158 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-19 00:58:44 | [diff] [blame] | 1159 | const std::string& kind, |
| 1160 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1161 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1162 | if (!ConfiguredForMedia()) { |
| 1163 | RTC_LOG(LS_ERROR) << "Not configured for media"; |
| 1164 | return nullptr; |
| 1165 | } |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1166 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1167 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1168 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1169 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 1170 | if (IsClosed()) { |
| 1171 | return nullptr; |
| 1172 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1173 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1174 | // Internally we need to have one stream with Plan B semantics, so we |
| 1175 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1176 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1177 | if (stream_id.empty()) { |
| 1178 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1179 | RTC_LOG(LS_INFO) |
| 1180 | << "No stream_id specified for sender. Generated stream ID: " |
| 1181 | << stream_ids[0]; |
| 1182 | } else { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1183 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1184 | } |
| 1185 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1186 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1187 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1188 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1189 | auto audio_sender = |
| 1190 | AudioRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), |
| 1191 | legacy_stats_.get(), rtp_manager()); |
Harald Alvestrand | c0d44d9 | 2022-12-13 12:57:24 | [diff] [blame] | 1192 | audio_sender->SetMediaChannel(rtp_manager()->voice_media_send_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1193 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1194 | signaling_thread(), audio_sender); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1195 | rtp_manager()->GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1196 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1197 | auto video_sender = VideoRtpSender::Create( |
| 1198 | worker_thread(), rtc::CreateRandomUuid(), rtp_manager()); |
Harald Alvestrand | c0d44d9 | 2022-12-13 12:57:24 | [diff] [blame] | 1199 | video_sender->SetMediaChannel(rtp_manager()->video_media_send_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1200 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1201 | signaling_thread(), video_sender); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1202 | rtp_manager()->GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1203 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1204 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1205 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1206 | } |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 1207 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1208 | |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1209 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1210 | } |
| 1211 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1212 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1213 | const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1214 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1215 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1216 | if (ConfiguredForMedia()) { |
| 1217 | for (const auto& sender : rtp_manager()->GetSendersInternal()) { |
| 1218 | ret.push_back(sender); |
| 1219 | } |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1220 | } |
| 1221 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1225 | PeerConnection::GetReceivers() const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1226 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1227 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1228 | if (ConfiguredForMedia()) { |
| 1229 | for (const auto& receiver : rtp_manager()->GetReceiversInternal()) { |
| 1230 | ret.push_back(receiver); |
| 1231 | } |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1232 | } |
| 1233 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1234 | } |
| 1235 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1236 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1237 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1238 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1239 | RTC_CHECK(IsUnifiedPlan()) |
| 1240 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1241 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1242 | if (ConfiguredForMedia()) { |
| 1243 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
| 1244 | all_transceivers.push_back(transceiver); |
| 1245 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1246 | } |
| 1247 | return all_transceivers; |
| 1248 | } |
| 1249 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1250 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 | [diff] [blame] | 1251 | MediaStreamTrackInterface* track, |
| 1252 | StatsOutputLevel level) { |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1253 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats (legacy)"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1254 | RTC_DCHECK_RUN_ON(signaling_thread()); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 1255 | if (!observer) { |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1256 | RTC_LOG(LS_ERROR) << "Legacy GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1257 | return false; |
| 1258 | } |
| 1259 | |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1260 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 1261 | |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1262 | legacy_stats_->UpdateStats(level); |
Tommi | 1901551 | 2022-02-02 10:49:35 | [diff] [blame] | 1263 | |
| 1264 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(4); |
| 1265 | |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1266 | // The LegacyStatsCollector is used to tell if a track is valid because it may |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 1267 | // remember tracks that the PeerConnection previously removed. |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1268 | if (track && !legacy_stats_->IsValidTrack(track->id())) { |
| 1269 | RTC_LOG(LS_WARNING) << "Legacy GetStats is called with an invalid track: " |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1270 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 1271 | return false; |
| 1272 | } |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1273 | message_handler_.PostGetStats(observer, legacy_stats_.get(), track); |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1274 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1275 | return true; |
| 1276 | } |
| 1277 | |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1278 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1279 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1280 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1281 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1282 | RTC_DCHECK(callback); |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1283 | RTC_LOG_THREAD_BLOCK_COUNT(); |
Niels Möller | e7cc883 | 2022-01-04 14:20:03 | [diff] [blame] | 1284 | stats_collector_->GetStatsReport( |
| 1285 | rtc::scoped_refptr<RTCStatsCollectorCallback>(callback)); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1286 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1287 | } |
| 1288 | |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1289 | void PeerConnection::GetStats( |
| 1290 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1291 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1292 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1293 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1294 | RTC_DCHECK(callback); |
| 1295 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1296 | RTC_LOG_THREAD_BLOCK_COUNT(); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1297 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1298 | if (selector) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1299 | for (const auto& proxy_transceiver : |
| 1300 | rtp_manager()->transceivers()->List()) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1301 | for (const auto& proxy_sender : |
| 1302 | proxy_transceiver->internal()->senders()) { |
| 1303 | if (proxy_sender == selector) { |
| 1304 | internal_sender = proxy_sender->internal(); |
| 1305 | break; |
| 1306 | } |
| 1307 | } |
| 1308 | if (internal_sender) |
| 1309 | break; |
| 1310 | } |
| 1311 | } |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 1312 | // 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] | 1313 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1314 | // PeerConnection). This means that "all the stats objects representing the |
| 1315 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1316 | // produces an empty stats report. |
| 1317 | stats_collector_->GetStatsReport(internal_sender, callback); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1318 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | void PeerConnection::GetStats( |
| 1322 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1323 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1324 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1325 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1326 | RTC_DCHECK(callback); |
| 1327 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1328 | RTC_LOG_THREAD_BLOCK_COUNT(); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1329 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1330 | if (selector) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1331 | for (const auto& proxy_transceiver : |
| 1332 | rtp_manager()->transceivers()->List()) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1333 | for (const auto& proxy_receiver : |
| 1334 | proxy_transceiver->internal()->receivers()) { |
| 1335 | if (proxy_receiver == selector) { |
| 1336 | internal_receiver = proxy_receiver->internal(); |
| 1337 | break; |
| 1338 | } |
| 1339 | } |
| 1340 | if (internal_receiver) |
| 1341 | break; |
| 1342 | } |
| 1343 | } |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 1344 | // 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] | 1345 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1346 | // the PeerConnection). This means that "all the stats objects representing |
| 1347 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1348 | // selector produces an empty stats report. |
| 1349 | stats_collector_->GetStatsReport(internal_receiver, callback); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1350 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1351 | } |
| 1352 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1353 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1354 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1355 | return sdp_handler_->signaling_state(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1356 | } |
| 1357 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1358 | PeerConnectionInterface::IceConnectionState |
| 1359 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1360 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1361 | return ice_connection_state_; |
| 1362 | } |
| 1363 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1364 | PeerConnectionInterface::IceConnectionState |
| 1365 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1366 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1367 | return standardized_ice_connection_state_; |
| 1368 | } |
| 1369 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1370 | PeerConnectionInterface::PeerConnectionState |
| 1371 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1372 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1373 | return connection_state_; |
| 1374 | } |
| 1375 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1376 | PeerConnectionInterface::IceGatheringState |
| 1377 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1378 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1379 | return ice_gathering_state_; |
| 1380 | } |
| 1381 | |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 1382 | absl::optional<bool> PeerConnection::can_trickle_ice_candidates() { |
| 1383 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1384 | const SessionDescriptionInterface* description = current_remote_description(); |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 1385 | if (!description) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1386 | description = pending_remote_description(); |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 1387 | } |
| 1388 | if (!description) { |
| 1389 | return absl::nullopt; |
| 1390 | } |
| 1391 | // TODO(bugs.webrtc.org/7443): Change to retrieve from session-level option. |
| 1392 | if (description->description()->transport_infos().size() < 1) { |
| 1393 | return absl::nullopt; |
| 1394 | } |
| 1395 | return description->description()->transport_infos()[0].description.HasOption( |
| 1396 | "trickle"); |
| 1397 | } |
| 1398 | |
Harald Alvestrand | a9af50f | 2021-05-21 13:33:51 | [diff] [blame] | 1399 | RTCErrorOr<rtc::scoped_refptr<DataChannelInterface>> |
| 1400 | PeerConnection::CreateDataChannelOrError(const std::string& label, |
| 1401 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 09:17:47 | [diff] [blame] | 1402 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1403 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 18:14:50 | [diff] [blame] | 1404 | |
Tommi | 4f7ade5 | 2023-03-29 18:46:59 | [diff] [blame] | 1405 | if (IsClosed()) { |
| 1406 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1407 | "CreateDataChannelOrError: PeerConnection is closed."); |
| 1408 | } |
| 1409 | |
Harald Alvestrand | 5da3eb0 | 2023-03-15 20:39:42 | [diff] [blame] | 1410 | bool first_datachannel = !data_channel_controller_.HasUsedDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1411 | |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 1412 | InternalDataChannelInit internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 1413 | if (config) { |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 1414 | internal_config = InternalDataChannelInit(*config); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 1415 | } |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 1416 | |
| 1417 | internal_config.fallback_ssl_role = sdp_handler_->GuessSslRole(); |
Tommi | 4f7ade5 | 2023-03-29 18:46:59 | [diff] [blame] | 1418 | RTCErrorOr<rtc::scoped_refptr<DataChannelInterface>> ret = |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 1419 | data_channel_controller_.InternalCreateDataChannelWithProxy( |
Tommi | 4f7ade5 | 2023-03-29 18:46:59 | [diff] [blame] | 1420 | label, internal_config); |
| 1421 | if (!ret.ok()) { |
| 1422 | return ret.MoveError(); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1423 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1424 | |
Tommi | 4f7ade5 | 2023-03-29 18:46:59 | [diff] [blame] | 1425 | rtc::scoped_refptr<DataChannelInterface> channel = ret.MoveValue(); |
| 1426 | |
Philipp Hancke | 522380f | 2023-05-09 07:41:03 | [diff] [blame] | 1427 | // Check the onRenegotiationNeeded event (with plan-b backward compat) |
| 1428 | if (configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan || |
| 1429 | (configuration_.sdp_semantics == SdpSemantics::kPlanB_DEPRECATED && |
| 1430 | first_datachannel)) { |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1431 | sdp_handler_->UpdateNegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1432 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1433 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 1434 | return channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1435 | } |
| 1436 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 1437 | void PeerConnection::RestartIce() { |
| 1438 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1439 | sdp_handler_->RestartIce(); |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 1440 | } |
| 1441 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1442 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 1443 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1444 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1445 | sdp_handler_->CreateOffer(observer, options); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1446 | } |
| 1447 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1448 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1449 | const RTCOfferAnswerOptions& options) { |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1450 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1451 | sdp_handler_->CreateAnswer(observer, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1452 | } |
| 1453 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1454 | void PeerConnection::SetLocalDescription( |
| 1455 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 1456 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1457 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1458 | sdp_handler_->SetLocalDescription(observer, desc_ptr); |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | void PeerConnection::SetLocalDescription( |
| 1462 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1463 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 1464 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1465 | sdp_handler_->SetLocalDescription(std::move(desc), observer); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1466 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 1467 | |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 1468 | void PeerConnection::SetLocalDescription( |
| 1469 | SetSessionDescriptionObserver* observer) { |
| 1470 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1471 | sdp_handler_->SetLocalDescription(observer); |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | void PeerConnection::SetLocalDescription( |
| 1475 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 1476 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1477 | sdp_handler_->SetLocalDescription(observer); |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 1478 | } |
| 1479 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1480 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 | [diff] [blame] | 1481 | SetSessionDescriptionObserver* observer, |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1482 | SessionDescriptionInterface* desc_ptr) { |
| 1483 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1484 | sdp_handler_->SetRemoteDescription(observer, desc_ptr); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | void PeerConnection::SetRemoteDescription( |
| 1488 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1489 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1490 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1491 | sdp_handler_->SetRemoteDescription(std::move(desc), observer); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 1492 | } |
| 1493 | |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 1494 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1495 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 1496 | return configuration_; |
| 1497 | } |
| 1498 | |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1499 | RTCError PeerConnection::SetConfiguration( |
| 1500 | const RTCConfiguration& configuration) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1501 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1502 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 1503 | if (IsClosed()) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1504 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1505 | "SetConfiguration: PeerConnection is closed."); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 1506 | } |
| 1507 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 1508 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1509 | // size is not allowed, and changing the set of ICE servers will not result |
| 1510 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1511 | if (local_description() && configuration.ice_candidate_pool_size != |
| 1512 | configuration_.ice_candidate_pool_size) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1513 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 1514 | "Can't change candidate pool size after calling " |
| 1515 | "SetLocalDescription."); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 1516 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1517 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 17:04:45 | [diff] [blame] | 1518 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1519 | configuration.crypto_options != configuration_.crypto_options) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1520 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 1521 | "Can't change crypto_options after calling " |
| 1522 | "SetLocalDescription."); |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1523 | } |
| 1524 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1525 | // The simplest (and most future-compatible) way to tell if the config was |
| 1526 | // modified in an invalid way is to copy each property we do support |
| 1527 | // modifying, then use operator==. There are far more properties we don't |
| 1528 | // support modifying than those we do, and more could be added. |
| 1529 | RTCConfiguration modified_config = configuration_; |
| 1530 | modified_config.servers = configuration.servers; |
| 1531 | modified_config.type = configuration.type; |
| 1532 | modified_config.ice_candidate_pool_size = |
| 1533 | configuration.ice_candidate_pool_size; |
| 1534 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1535 | modified_config.turn_port_prune_policy = configuration.turn_port_prune_policy; |
Qingsi Wang | bca1485 | 2019-06-26 21:56:02 | [diff] [blame] | 1536 | modified_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 1537 | configuration.surface_ice_candidates_on_ice_transport_type_changed; |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 1538 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 1539 | modified_config.ice_check_interval_strong_connectivity = |
| 1540 | configuration.ice_check_interval_strong_connectivity; |
| 1541 | modified_config.ice_check_interval_weak_connectivity = |
| 1542 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 1543 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 1544 | modified_config.ice_unwritable_min_checks = |
| 1545 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 1546 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 1547 | modified_config.stun_candidate_keepalive_interval = |
| 1548 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 1549 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 1550 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 1551 | modified_config.active_reset_srtp_params = |
| 1552 | configuration.active_reset_srtp_params; |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 1553 | modified_config.turn_logging_id = configuration.turn_logging_id; |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 1554 | modified_config.allow_codec_switching = configuration.allow_codec_switching; |
Derek Bailey | 6c127a1 | 2021-04-15 19:42:41 | [diff] [blame] | 1555 | modified_config.stable_writable_connection_ping_interval_ms = |
| 1556 | configuration.stable_writable_connection_ping_interval_ms; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1557 | if (configuration != modified_config) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1558 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 1559 | "Modifying the configuration in an unsupported way."); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1560 | } |
| 1561 | |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1562 | // Validate the modified configuration. |
| 1563 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 1564 | if (!validate_error.ok()) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1565 | return validate_error; |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1566 | } |
| 1567 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1568 | // Note that this isn't possible through chromium, since it's an unsigned |
| 1569 | // short in WebIDL. |
| 1570 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 10:34:17 | [diff] [blame] | 1571 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1572 | return RTCError(RTCErrorType::INVALID_RANGE); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | // Parse ICE servers before hopping to network thread. |
| 1576 | cricket::ServerAddresses stun_servers; |
| 1577 | std::vector<cricket::RelayServerConfig> turn_servers; |
Philipp Hancke | 633dc2f | 2022-10-12 08:16:14 | [diff] [blame] | 1578 | RTCError parse_error = ParseIceServersOrError(configuration.servers, |
| 1579 | &stun_servers, &turn_servers); |
| 1580 | if (!parse_error.ok()) { |
| 1581 | return parse_error; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1582 | } |
Philipp Hancke | 41a8357 | 2022-11-28 13:47:45 | [diff] [blame] | 1583 | |
| 1584 | // Restrict number of TURN servers. |
Philipp Hancke | 3e224c7 | 2023-03-20 07:22:49 | [diff] [blame] | 1585 | if (turn_servers.size() > cricket::kMaxTurnServers) { |
Philipp Hancke | 41a8357 | 2022-11-28 13:47:45 | [diff] [blame] | 1586 | RTC_LOG(LS_WARNING) << "Number of configured TURN servers is " |
| 1587 | << turn_servers.size() |
| 1588 | << " which exceeds the maximum allowed number of " |
| 1589 | << cricket::kMaxTurnServers; |
| 1590 | turn_servers.resize(cricket::kMaxTurnServers); |
| 1591 | } |
| 1592 | |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 1593 | // Add the turn logging id to all turn servers |
| 1594 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 1595 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 1596 | } |
| 1597 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1598 | // Note if STUN or TURN servers were supplied. |
| 1599 | if (!stun_servers.empty()) { |
| 1600 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 1601 | } |
| 1602 | if (!turn_servers.empty()) { |
| 1603 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 1604 | } |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1605 | |
Niels Möller | 4bab23f | 2021-01-18 08:24:33 | [diff] [blame] | 1606 | const bool has_local_description = local_description() != nullptr; |
| 1607 | |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 1608 | const bool needs_ice_restart = |
| 1609 | modified_config.servers != configuration_.servers || |
| 1610 | NeedIceRestart( |
| 1611 | configuration_.surface_ice_candidates_on_ice_transport_type_changed, |
| 1612 | configuration_.type, modified_config.type) || |
| 1613 | modified_config.GetTurnPortPrunePolicy() != |
| 1614 | configuration_.GetTurnPortPrunePolicy(); |
| 1615 | cricket::IceConfig ice_config = ParseIceConfig(modified_config); |
| 1616 | |
| 1617 | // Apply part of the configuration on the network thread. In theory this |
| 1618 | // shouldn't fail. |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1619 | if (!network_thread()->BlockingCall( |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 1620 | [this, needs_ice_restart, &ice_config, &stun_servers, &turn_servers, |
| 1621 | &modified_config, has_local_description] { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1622 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 1623 | // As described in JSEP, calling setConfiguration with new ICE |
| 1624 | // servers or candidate policy must set a "needs-ice-restart" bit so |
| 1625 | // that the next offer triggers an ICE restart which will pick up |
| 1626 | // the changes. |
| 1627 | if (needs_ice_restart) |
| 1628 | transport_controller_->SetNeedsIceRestartFlag(); |
| 1629 | |
| 1630 | transport_controller_->SetIceConfig(ice_config); |
Niels Möller | 4bab23f | 2021-01-18 08:24:33 | [diff] [blame] | 1631 | return ReconfigurePortAllocator_n( |
| 1632 | stun_servers, turn_servers, modified_config.type, |
| 1633 | modified_config.ice_candidate_pool_size, |
| 1634 | modified_config.GetTurnPortPrunePolicy(), |
| 1635 | modified_config.turn_customizer, |
| 1636 | modified_config.stun_candidate_keepalive_interval, |
| 1637 | has_local_description); |
| 1638 | })) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1639 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 1640 | "Failed to apply configuration to PortAllocator."); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1641 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 15:18:11 | [diff] [blame] | 1642 | |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 1643 | if (configuration_.active_reset_srtp_params != |
| 1644 | modified_config.active_reset_srtp_params) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1645 | // TODO(tommi): merge BlockingCalls |
| 1646 | network_thread()->BlockingCall([this, &modified_config] { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1647 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1648 | transport_controller_->SetActiveResetSrtpParams( |
| 1649 | modified_config.active_reset_srtp_params); |
| 1650 | }); |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 1651 | } |
| 1652 | |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 1653 | if (modified_config.allow_codec_switching.has_value()) { |
Harald Alvestrand | c0d44d9 | 2022-12-13 12:57:24 | [diff] [blame] | 1654 | std::vector<cricket::VideoMediaSendChannelInterface*> channels; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1655 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1656 | if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO) |
Taylor Brandstetter | 6b381c7 | 2020-08-24 20:28:47 | [diff] [blame] | 1657 | continue; |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1658 | |
Harald Alvestrand | 2c761b2 | 2022-04-30 16:27:40 | [diff] [blame] | 1659 | auto* video_channel = transceiver->internal()->channel(); |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1660 | if (video_channel) |
Harald Alvestrand | c0d44d9 | 2022-12-13 12:57:24 | [diff] [blame] | 1661 | channels.push_back( |
| 1662 | static_cast<cricket::VideoMediaSendChannelInterface*>( |
| 1663 | video_channel->media_send_channel())); |
philipel | 01294f0 | 2019-11-14 12:03:25 | [diff] [blame] | 1664 | } |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1665 | |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1666 | worker_thread()->BlockingCall( |
Tomas Gunnarsson | d41c2a6 | 2020-09-21 13:56:42 | [diff] [blame] | 1667 | [channels = std::move(channels), |
| 1668 | allow_codec_switching = *modified_config.allow_codec_switching]() { |
| 1669 | for (auto* ch : channels) |
| 1670 | ch->SetVideoCodecSwitchingEnabled(allow_codec_switching); |
| 1671 | }); |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 1672 | } |
| 1673 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1674 | configuration_ = modified_config; |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1675 | return RTCError::OK(); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 1676 | } |
| 1677 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1678 | bool PeerConnection::AddIceCandidate( |
| 1679 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1680 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Philipp Hancke | a09b921 | 2022-06-22 05:41:22 | [diff] [blame] | 1681 | ClearStatsCache(); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1682 | return sdp_handler_->AddIceCandidate(ice_candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1683 | } |
| 1684 | |
Henrik Boström | ee6f4f6 | 2019-11-06 11:36:12 | [diff] [blame] | 1685 | void PeerConnection::AddIceCandidate( |
| 1686 | std::unique_ptr<IceCandidateInterface> candidate, |
| 1687 | std::function<void(RTCError)> callback) { |
| 1688 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Philipp Hancke | a09b921 | 2022-06-22 05:41:22 | [diff] [blame] | 1689 | sdp_handler_->AddIceCandidate(std::move(candidate), |
| 1690 | [this, callback](webrtc::RTCError result) { |
| 1691 | ClearStatsCache(); |
| 1692 | callback(result); |
| 1693 | }); |
Henrik Boström | ee6f4f6 | 2019-11-06 11:36:12 | [diff] [blame] | 1694 | } |
| 1695 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 1696 | bool PeerConnection::RemoveIceCandidates( |
| 1697 | const std::vector<cricket::Candidate>& candidates) { |
| 1698 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 1699 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1700 | return sdp_handler_->RemoveIceCandidates(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 1701 | } |
| 1702 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1703 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 1704 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1705 | return worker_thread()->BlockingCall([&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1706 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1707 | RTC_DCHECK_RUN_ON(worker_thread()); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1708 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1709 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 1710 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 1711 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1712 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 1713 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1714 | "min_bitrate_bps <= 0"); |
| 1715 | } |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1716 | if (has_start) { |
| 1717 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1718 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1719 | "start_bitrate_bps < min_bitrate_bps"); |
| 1720 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1721 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1722 | "curent_bitrate_bps < 0"); |
| 1723 | } |
| 1724 | } |
| 1725 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1726 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1727 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1728 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1729 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 1730 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1731 | "max_bitrate_bps < min_bitrate_bps"); |
| 1732 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 1733 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1734 | "max_bitrate_bps < 0"); |
| 1735 | } |
| 1736 | } |
| 1737 | |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1738 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 17:31:54 | [diff] [blame] | 1739 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1740 | |
| 1741 | return RTCError::OK(); |
| 1742 | } |
| 1743 | |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1744 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 1745 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1746 | worker_thread()->BlockingCall( |
| 1747 | [this, playout] { SetAudioPlayout(playout); }); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1748 | return; |
| 1749 | } |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 1750 | auto audio_state = context_->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1751 | audio_state->SetPlayout(playout); |
| 1752 | } |
| 1753 | |
| 1754 | void PeerConnection::SetAudioRecording(bool recording) { |
| 1755 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1756 | worker_thread()->BlockingCall( |
| 1757 | [this, recording] { SetAudioRecording(recording); }); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1758 | return; |
| 1759 | } |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 1760 | auto audio_state = context_->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1761 | audio_state->SetRecording(recording); |
| 1762 | } |
| 1763 | |
Henrik Boström | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 1764 | void PeerConnection::AddAdaptationResource( |
| 1765 | rtc::scoped_refptr<Resource> resource) { |
| 1766 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1767 | return worker_thread()->BlockingCall( |
| 1768 | [this, resource]() { return AddAdaptationResource(resource); }); |
Henrik Boström | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 1769 | } |
| 1770 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1771 | if (!call_) { |
| 1772 | // The PeerConnection has been closed. |
| 1773 | return; |
| 1774 | } |
| 1775 | call_->AddAdaptationResource(resource); |
| 1776 | } |
| 1777 | |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1778 | bool PeerConnection::ConfiguredForMedia() const { |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 1779 | return context_->media_engine(); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1780 | } |
| 1781 | |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 1782 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 1783 | int64_t output_period_ms) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1784 | return worker_thread()->BlockingCall( |
Danil Chapovalov | 116ffe7 | 2019-09-05 08:21:11 | [diff] [blame] | 1785 | [this, output = std::move(output), output_period_ms]() mutable { |
| 1786 | return StartRtcEventLog_w(std::move(output), output_period_ms); |
| 1787 | }); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 1788 | } |
| 1789 | |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 1790 | bool PeerConnection::StartRtcEventLog( |
| 1791 | std::unique_ptr<RtcEventLogOutput> output) { |
Lionel Koenig | 0606eaf | 2023-06-27 08:46:19 | [diff] [blame] | 1792 | int64_t output_period_ms = 5000; |
| 1793 | if (trials().IsDisabled("WebRTC-RtcEventLogNewFormat")) { |
| 1794 | output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
Niels Möller | 695cf6a | 2019-05-13 10:27:23 | [diff] [blame] | 1795 | } |
| 1796 | return StartRtcEventLog(std::move(output), output_period_ms); |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 1797 | } |
| 1798 | |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 1799 | void PeerConnection::StopRtcEventLog() { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1800 | worker_thread()->BlockingCall([this] { StopRtcEventLog_w(); }); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 1801 | } |
| 1802 | |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 1803 | rtc::scoped_refptr<DtlsTransportInterface> |
| 1804 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
Tomas Gunnarsson | 2aeab5e | 2021-02-23 20:36:14 | [diff] [blame] | 1805 | RTC_DCHECK_RUN_ON(network_thread()); |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 1806 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 1807 | } |
| 1808 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 1809 | rtc::scoped_refptr<DtlsTransport> |
| 1810 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 1811 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1812 | // TODO(bugs.webrtc.org/9987): Avoid the thread jump. |
| 1813 | // This might be done by caching the value on the signaling thread. |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1814 | return network_thread()->BlockingCall([this, mid]() { |
| 1815 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1816 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 1817 | }); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 1818 | } |
| 1819 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 1820 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 1821 | const { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1822 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1823 | if (!sctp_mid_n_) |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 1824 | return nullptr; |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1825 | |
| 1826 | return transport_controller_->GetSctpTransport(*sctp_mid_n_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 1827 | } |
| 1828 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1829 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1830 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1831 | return sdp_handler_->local_description(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1835 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1836 | return sdp_handler_->remote_description(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1837 | } |
| 1838 | |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1839 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 1840 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1841 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1842 | return sdp_handler_->current_local_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 1846 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1847 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1848 | return sdp_handler_->current_remote_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 1852 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1853 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1854 | return sdp_handler_->pending_local_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 1858 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1859 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1860 | return sdp_handler_->pending_remote_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1861 | } |
| 1862 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1863 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1864 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1865 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1866 | |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1867 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 1868 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1869 | if (IsClosed()) { |
| 1870 | return; |
| 1871 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1872 | // Update stats here so that we have the most recent stats for tracks and |
| 1873 | // streams before the channels are closed. |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1874 | legacy_stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1875 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1876 | ice_connection_state_ = PeerConnectionInterface::kIceConnectionClosed; |
| 1877 | Observer()->OnIceConnectionChange(ice_connection_state_); |
| 1878 | standardized_ice_connection_state_ = |
| 1879 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
| 1880 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 1881 | Observer()->OnConnectionChange(connection_state_); |
| 1882 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1883 | sdp_handler_->Close(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1884 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1885 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1886 | |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1887 | if (ConfiguredForMedia()) { |
| 1888 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
| 1889 | transceiver->internal()->SetPeerConnectionClosed(); |
| 1890 | if (!transceiver->stopped()) |
| 1891 | transceiver->StopInternal(); |
| 1892 | } |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1893 | } |
Steve Anton | 25cfeb9 | 2018-04-26 18:44:00 | [diff] [blame] | 1894 | // Ensure that all asynchronous stats requests are completed before destroying |
| 1895 | // the transport controller below. |
| 1896 | if (stats_collector_) { |
| 1897 | stats_collector_->WaitForPendingRequest(); |
| 1898 | } |
| 1899 | |
| 1900 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 1901 | // the last stats request can still read from the channels. |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1902 | sdp_handler_->DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1903 | |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 1904 | // The event log is used in the transport controller, which must be outlived |
| 1905 | // by the former. CreateOffer by the peer connection is implemented |
| 1906 | // asynchronously and if the peer connection is closed without resetting the |
| 1907 | // WebRTC session description factory, the session description factory would |
| 1908 | // call the transport controller. |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1909 | sdp_handler_->ResetSessionDescFactory(); |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1910 | if (ConfiguredForMedia()) { |
| 1911 | rtp_manager_->Close(); |
| 1912 | } |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 1913 | |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1914 | network_thread()->BlockingCall([this] { |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 1915 | // Data channels will already have been unset via the DestroyAllChannels() |
| 1916 | // call above, which triggers a call to TeardownDataChannelTransport_n(). |
| 1917 | // TODO(tommi): ^^ That's not exactly optimal since this is yet another |
| 1918 | // blocking hop to the network thread during Close(). Further still, the |
| 1919 | // voice/video/data channels will be cleared on the worker thread. |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1920 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1921 | transport_controller_.reset(); |
| 1922 | port_allocator_->DiscardCandidatePool(); |
| 1923 | if (network_thread_safety_) { |
| 1924 | network_thread_safety_->SetNotAlive(); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1925 | } |
| 1926 | }); |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 1927 | |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1928 | worker_thread()->BlockingCall([this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1929 | RTC_DCHECK_RUN_ON(worker_thread()); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 1930 | worker_thread_safety_->SetNotAlive(); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1931 | call_.reset(); |
| 1932 | // The event log must outlive call (and any other object that uses it). |
| 1933 | event_log_.reset(); |
| 1934 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1935 | ReportUsagePattern(); |
Tomas Gunnarsson | 2efb8a5 | 2021-04-01 14:26:57 | [diff] [blame] | 1936 | |
| 1937 | // Signal shutdown to the sdp handler. This invalidates weak pointers for |
| 1938 | // internal pending callbacks. |
| 1939 | sdp_handler_->PrepareForShutdown(); |
Tommi | 1f708ef | 2023-03-31 16:40:50 | [diff] [blame] | 1940 | data_channel_controller_.PrepareForShutdown(); |
| 1941 | |
| 1942 | // The .h file says that observer can be discarded after close() returns. |
| 1943 | // Make sure this is true. |
| 1944 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1945 | } |
| 1946 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1947 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1948 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1949 | if (ice_connection_state_ == new_state) { |
| 1950 | return; |
| 1951 | } |
| 1952 | |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 1953 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1954 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1955 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 1956 | return; |
| 1957 | } |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1958 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1959 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 1960 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1961 | RTC_DCHECK(ice_connection_state_ != |
| 1962 | PeerConnectionInterface::kIceConnectionClosed); |
| 1963 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1964 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 1965 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1966 | } |
| 1967 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1968 | void PeerConnection::SetStandardizedIceConnectionState( |
| 1969 | PeerConnectionInterface::IceConnectionState new_state) { |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 1970 | if (standardized_ice_connection_state_ == new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1971 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | if (IsClosed()) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1975 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState " |
| 1979 | << standardized_ice_connection_state_ << " => " << new_state; |
| 1980 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1981 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 10:25:14 | [diff] [blame] | 1982 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1983 | } |
| 1984 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1985 | void PeerConnection::SetConnectionState( |
| 1986 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1987 | if (connection_state_ == new_state) |
| 1988 | return; |
| 1989 | if (IsClosed()) |
| 1990 | return; |
| 1991 | connection_state_ = new_state; |
| 1992 | Observer()->OnConnectionChange(new_state); |
Philipp Hancke | bb8f32f | 2021-02-04 20:50:50 | [diff] [blame] | 1993 | |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 1994 | // The first connection state change to connected happens once per |
| 1995 | // connection which makes it a good point to report metrics. |
Philipp Hancke | cd0373f | 2021-02-24 10:04:30 | [diff] [blame] | 1996 | if (new_state == PeerConnectionState::kConnected && !was_ever_connected_) { |
| 1997 | was_ever_connected_ = true; |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 1998 | ReportFirstConnectUsageMetrics(); |
Philipp Hancke | bb8f32f | 2021-02-04 20:50:50 | [diff] [blame] | 1999 | } |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 2000 | } |
| 2001 | |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 2002 | void PeerConnection::ReportFirstConnectUsageMetrics() { |
| 2003 | // Record bundle-policy from configuration. Done here from |
| 2004 | // connectionStateChange to limit to actually established connections. |
| 2005 | BundlePolicyUsage policy = kBundlePolicyUsageMax; |
| 2006 | switch (configuration_.bundle_policy) { |
| 2007 | case kBundlePolicyBalanced: |
| 2008 | policy = kBundlePolicyUsageBalanced; |
| 2009 | break; |
| 2010 | case kBundlePolicyMaxBundle: |
| 2011 | policy = kBundlePolicyUsageMaxBundle; |
| 2012 | break; |
| 2013 | case kBundlePolicyMaxCompat: |
| 2014 | policy = kBundlePolicyUsageMaxCompat; |
| 2015 | break; |
| 2016 | } |
| 2017 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.BundlePolicy", policy, |
| 2018 | kBundlePolicyUsageMax); |
| 2019 | |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 2020 | // Record whether there was a local or remote provisional answer. |
| 2021 | ProvisionalAnswerUsage pranswer = kProvisionalAnswerNotUsed; |
| 2022 | if (local_description()->GetType() == SdpType::kPrAnswer) { |
| 2023 | pranswer = kProvisionalAnswerLocal; |
| 2024 | } else if (remote_description()->GetType() == SdpType::kPrAnswer) { |
| 2025 | pranswer = kProvisionalAnswerRemote; |
| 2026 | } |
| 2027 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.ProvisionalAnswer", pranswer, |
| 2028 | kProvisionalAnswerMax); |
| 2029 | |
Philipp Hancke | 6f22eb5 | 2022-07-14 09:38:44 | [diff] [blame] | 2030 | // Record the number of valid / invalid ice-ufrag. We do allow certain |
| 2031 | // non-spec ice-char for backward-compat reasons. At this point we know |
| 2032 | // that the ufrag/pwd consists of a valid ice-char or one of the four |
| 2033 | // not allowed characters since we have passed the IsIceChar check done |
| 2034 | // by the p2p transport description on setRemoteDescription calls. |
| 2035 | auto transport_infos = remote_description()->description()->transport_infos(); |
| 2036 | if (transport_infos.size() > 0) { |
| 2037 | auto ice_parameters = transport_infos[0].description.GetIceParameters(); |
| 2038 | auto is_invalid_char = [](char c) { |
| 2039 | return c == '-' || c == '=' || c == '#' || c == '_'; |
| 2040 | }; |
| 2041 | bool isUsingInvalidIceCharInUfrag = |
| 2042 | absl::c_any_of(ice_parameters.ufrag, is_invalid_char); |
| 2043 | bool isUsingInvalidIceCharInPwd = |
| 2044 | absl::c_any_of(ice_parameters.pwd, is_invalid_char); |
| 2045 | RTC_HISTOGRAM_BOOLEAN( |
| 2046 | "WebRTC.PeerConnection.ValidIceChars", |
| 2047 | !(isUsingInvalidIceCharInUfrag || isUsingInvalidIceCharInPwd)); |
| 2048 | } |
Philipp Hancke | 66efab2 | 2023-01-25 10:37:45 | [diff] [blame] | 2049 | |
| 2050 | // Record RtcpMuxPolicy setting. |
| 2051 | RtcpMuxPolicyUsage rtcp_mux_policy = kRtcpMuxPolicyUsageMax; |
| 2052 | switch (configuration_.rtcp_mux_policy) { |
| 2053 | case kRtcpMuxPolicyNegotiate: |
| 2054 | rtcp_mux_policy = kRtcpMuxPolicyUsageNegotiate; |
| 2055 | break; |
| 2056 | case kRtcpMuxPolicyRequire: |
| 2057 | rtcp_mux_policy = kRtcpMuxPolicyUsageRequire; |
| 2058 | break; |
| 2059 | } |
| 2060 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.RtcpMuxPolicy", |
| 2061 | rtcp_mux_policy, kRtcpMuxPolicyUsageMax); |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 2062 | } |
| 2063 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2064 | void PeerConnection::OnIceGatheringChange( |
| 2065 | PeerConnectionInterface::IceGatheringState new_state) { |
| 2066 | if (IsClosed()) { |
| 2067 | return; |
| 2068 | } |
| 2069 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2070 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2071 | } |
| 2072 | |
jbauch | 81bf7b0 | 2017-03-25 15:31:12 | [diff] [blame] | 2073 | void PeerConnection::OnIceCandidate( |
| 2074 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 2075 | if (IsClosed()) { |
| 2076 | return; |
| 2077 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2078 | ReportIceCandidateCollected(candidate->candidate()); |
Philipp Hancke | 1fe14f2 | 2022-06-17 09:34:31 | [diff] [blame] | 2079 | ClearStatsCache(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2080 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2081 | } |
| 2082 | |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 2083 | void PeerConnection::OnIceCandidateError(const std::string& address, |
| 2084 | int port, |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2085 | const std::string& url, |
| 2086 | int error_code, |
| 2087 | const std::string& error_text) { |
| 2088 | if (IsClosed()) { |
| 2089 | return; |
| 2090 | } |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 2091 | Observer()->OnIceCandidateError(address, port, url, error_code, error_text); |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2092 | } |
| 2093 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 2094 | void PeerConnection::OnIceCandidatesRemoved( |
| 2095 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 2096 | if (IsClosed()) { |
| 2097 | return; |
| 2098 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2099 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 2100 | } |
| 2101 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 2102 | void PeerConnection::OnSelectedCandidatePairChanged( |
| 2103 | const cricket::CandidatePairChangeEvent& event) { |
| 2104 | if (IsClosed()) { |
| 2105 | return; |
| 2106 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2107 | |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 2108 | if (event.selected_candidate_pair.local_candidate().type() == |
| 2109 | LOCAL_PORT_TYPE && |
| 2110 | event.selected_candidate_pair.remote_candidate().type() == |
| 2111 | LOCAL_PORT_TYPE) { |
| 2112 | NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED); |
| 2113 | } |
| 2114 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 2115 | Observer()->OnIceSelectedCandidatePairChanged(event); |
| 2116 | } |
| 2117 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 2118 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2119 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Florent Castelli | 516e284 | 2021-04-19 13:29:50 | [diff] [blame] | 2120 | return sctp_mid_s_; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 2121 | } |
| 2122 | |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2123 | void PeerConnection::SetSctpDataInfo(absl::string_view mid, |
| 2124 | absl::string_view transport_name) { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2125 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2126 | sctp_mid_s_ = std::string(mid); |
| 2127 | SetSctpTransportName(std::string(transport_name)); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2128 | } |
| 2129 | |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2130 | void PeerConnection::ResetSctpDataInfo() { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2131 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2132 | sctp_mid_s_.reset(); |
Henrik Boström | 46053e4 | 2023-01-20 12:18:53 | [diff] [blame] | 2133 | SetSctpTransportName(""); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2134 | } |
| 2135 | |
Tommi | d2afbaf | 2023-03-02 09:51:16 | [diff] [blame] | 2136 | void PeerConnection::OnSctpDataChannelStateChanged( |
Tommi | 5654898 | 2023-03-27 16:07:34 | [diff] [blame] | 2137 | int channel_id, |
Tommi | d2afbaf | 2023-03-02 09:51:16 | [diff] [blame] | 2138 | DataChannelInterface::DataState state) { |
| 2139 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2140 | if (stats_collector_) |
Tommi | 5654898 | 2023-03-27 16:07:34 | [diff] [blame] | 2141 | stats_collector_->OnSctpDataChannelStateChanged(channel_id, state); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2142 | } |
| 2143 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2144 | PeerConnection::InitializePortAllocatorResult |
| 2145 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 2146 | const cricket::ServerAddresses& stun_servers, |
| 2147 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2148 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2149 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2150 | |
Taylor Brandstetter | f8e6577 | 2016-06-28 00:20:15 | [diff] [blame] | 2151 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2152 | // To handle both internal and externally created port allocator, we will |
| 2153 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2154 | int port_allocator_flags = port_allocator_->flags(); |
| 2155 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 2156 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 2157 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Henrik Boström | a445e6a | 2022-12-01 09:25:12 | [diff] [blame] | 2158 | if (trials().IsDisabled("WebRTC-IPv6Default")) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2159 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2160 | } |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 2161 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2162 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2163 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 2164 | } |
| 2165 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2166 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2167 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2168 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2169 | } |
| 2170 | |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 2171 | if (configuration.candidate_network_policy == |
| 2172 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2173 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2174 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 2175 | } |
| 2176 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 2177 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2178 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 2179 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 2180 | } |
| 2181 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2182 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2183 | // No step delay is used while allocating ports. |
| 2184 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2185 | port_allocator_->SetCandidateFilter( |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2186 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 2187 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2188 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 2189 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2190 | for (auto& turn_server : turn_servers_copy) { |
| 2191 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 2192 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2193 | // Call this last since it may create pooled allocator sessions using the |
| 2194 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 2195 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2196 | stun_servers, std::move(turn_servers_copy), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2197 | configuration.ice_candidate_pool_size, |
| 2198 | configuration.GetTurnPortPrunePolicy(), configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 2199 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2200 | |
| 2201 | InitializePortAllocatorResult res; |
| 2202 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 2203 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2204 | } |
| 2205 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 2206 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 2207 | const cricket::ServerAddresses& stun_servers, |
| 2208 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 2209 | IceTransportsType type, |
| 2210 | int candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2211 | PortPrunePolicy turn_port_prune_policy, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 2212 | webrtc::TurnCustomizer* turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 2213 | absl::optional<int> stun_candidate_keepalive_interval, |
| 2214 | bool have_local_description) { |
Harald Alvestrand | f598e49 | 2020-11-04 05:54:10 | [diff] [blame] | 2215 | RTC_DCHECK_RUN_ON(network_thread()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2216 | port_allocator_->SetCandidateFilter( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 2217 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 2218 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 2219 | // size is not allowed, and changing the set of ICE servers will not result |
| 2220 | // in new candidates being gathered. |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 2221 | if (have_local_description) { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 2222 | port_allocator_->FreezeCandidatePool(); |
| 2223 | } |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2224 | // Add the custom tls turn servers if they exist. |
| 2225 | auto turn_servers_copy = turn_servers; |
| 2226 | for (auto& turn_server : turn_servers_copy) { |
| 2227 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 2228 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2229 | // Call this last since it may create pooled allocator sessions using the |
| 2230 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-13 02:49:32 | [diff] [blame] | 2231 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2232 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2233 | turn_port_prune_policy, turn_customizer, |
| 2234 | stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2235 | } |
| 2236 | |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 2237 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 2238 | std::unique_ptr<RtcEventLogOutput> output, |
| 2239 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 2240 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 2241 | if (!event_log_) { |
| 2242 | return false; |
| 2243 | } |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 2244 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 2248 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 2249 | if (event_log_) { |
| 2250 | event_log_->StopLogging(); |
| 2251 | } |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 2252 | } |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 2253 | |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 2254 | absl::optional<rtc::SSLRole> PeerConnection::GetSctpSslRole_n() { |
Tommi | c61eee2 | 2023-03-22 07:25:38 | [diff] [blame] | 2255 | RTC_DCHECK_RUN_ON(network_thread()); |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 2256 | return sctp_mid_n_ ? transport_controller_->GetDtlsRole(*sctp_mid_n_) |
| 2257 | : absl::nullopt; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 2261 | rtc::SSLRole* role) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 2262 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2263 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2264 | RTC_LOG(LS_INFO) |
| 2265 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 2266 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2267 | return false; |
| 2268 | } |
| 2269 | |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 2270 | auto dtls_role = network_thread()->BlockingCall([this, content_name]() { |
| 2271 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2272 | return transport_controller_->GetDtlsRole(content_name); |
| 2273 | }); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2274 | if (dtls_role) { |
| 2275 | *role = *dtls_role; |
| 2276 | return true; |
| 2277 | } |
| 2278 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2279 | } |
| 2280 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2281 | bool PeerConnection::GetTransportDescription( |
| 2282 | const SessionDescription* description, |
| 2283 | const std::string& content_name, |
| 2284 | cricket::TransportDescription* tdesc) { |
| 2285 | if (!description || !tdesc) { |
| 2286 | return false; |
| 2287 | } |
| 2288 | const TransportInfo* transport_info = |
| 2289 | description->GetTransportInfoByName(content_name); |
| 2290 | if (!transport_info) { |
| 2291 | return false; |
| 2292 | } |
| 2293 | *tdesc = transport_info->description; |
| 2294 | return true; |
| 2295 | } |
| 2296 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 2297 | std::vector<DataChannelStats> PeerConnection::GetDataChannelStats() const { |
Tommi | f9e13f8 | 2023-04-06 19:21:45 | [diff] [blame] | 2298 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 2299 | return data_channel_controller_.GetDataChannelStats(); |
| 2300 | } |
| 2301 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 2302 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 2303 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 2304 | if (sctp_mid_s_ && transport_controller_copy_) |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2305 | return sctp_transport_name_s_; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 2306 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2307 | } |
| 2308 | |
Henrik Boström | 46053e4 | 2023-01-20 12:18:53 | [diff] [blame] | 2309 | void PeerConnection::SetSctpTransportName(std::string sctp_transport_name) { |
| 2310 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2311 | sctp_transport_name_s_ = std::move(sctp_transport_name); |
| 2312 | ClearStatsCache(); |
| 2313 | } |
| 2314 | |
Tomas Gunnarsson | bfd9ba8 | 2021-04-18 09:55:57 | [diff] [blame] | 2315 | absl::optional<std::string> PeerConnection::sctp_mid() const { |
| 2316 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2317 | return sctp_mid_s_; |
| 2318 | } |
| 2319 | |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 2320 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2321 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2322 | if (!network_thread_safety_->alive()) |
| 2323 | return {}; |
Philipp Hancke | 6e57ca2 | 2022-06-09 13:58:18 | [diff] [blame] | 2324 | cricket::CandidateStatsList candidate_stats_list; |
| 2325 | port_allocator_->GetCandidateStatsFromPooledSessions(&candidate_stats_list); |
| 2326 | return candidate_stats_list; |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 2327 | } |
| 2328 | |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 2329 | std::map<std::string, cricket::TransportStats> |
| 2330 | PeerConnection::GetTransportStatsByNames( |
| 2331 | const std::set<std::string>& transport_names) { |
Markus Handell | 518669d | 2021-06-07 11:30:46 | [diff] [blame] | 2332 | TRACE_EVENT0("webrtc", "PeerConnection::GetTransportStatsByNames"); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 2333 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2334 | if (!network_thread_safety_->alive()) |
| 2335 | return {}; |
| 2336 | |
| 2337 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 2338 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 2339 | for (const std::string& transport_name : transport_names) { |
| 2340 | cricket::TransportStats transport_stats; |
| 2341 | bool success = |
| 2342 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 2343 | if (success) { |
| 2344 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 2345 | } else { |
| 2346 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 2347 | << transport_name; |
| 2348 | } |
| 2349 | } |
| 2350 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | bool PeerConnection::GetLocalCertificate( |
| 2354 | const std::string& transport_name, |
| 2355 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2356 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2357 | if (!network_thread_safety_->alive() || !certificate) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2358 | return false; |
| 2359 | } |
| 2360 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 2361 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2362 | } |
| 2363 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 2364 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2365 | const std::string& transport_name) { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2366 | RTC_DCHECK_RUN_ON(network_thread()); |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 2367 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2368 | } |
| 2369 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2370 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
Karl Wiberg | f73f7d6 | 2019-04-08 13:36:53 | [diff] [blame] | 2371 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2372 | return sdp_handler_->IceRestartPending(content_name); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2373 | } |
| 2374 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2375 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 2376 | return network_thread()->BlockingCall([this, &content_name] { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2377 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2378 | return transport_controller_->NeedsIceRestart(content_name); |
| 2379 | }); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2380 | } |
| 2381 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2382 | void PeerConnection::OnTransportControllerConnectionState( |
| 2383 | cricket::IceConnectionState state) { |
| 2384 | switch (state) { |
| 2385 | case cricket::kIceConnectionConnecting: |
| 2386 | // If the current state is Connected or Completed, then there were |
| 2387 | // writable channels but now there are not, so the next state must |
| 2388 | // be Disconnected. |
| 2389 | // kIceConnectionConnecting is currently used as the default, |
| 2390 | // un-connected state by the TransportController, so its only use is |
| 2391 | // detecting disconnections. |
| 2392 | if (ice_connection_state_ == |
| 2393 | PeerConnectionInterface::kIceConnectionConnected || |
| 2394 | ice_connection_state_ == |
| 2395 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 2396 | SetIceConnectionState( |
| 2397 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 2398 | } |
| 2399 | break; |
| 2400 | case cricket::kIceConnectionFailed: |
| 2401 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 2402 | break; |
| 2403 | case cricket::kIceConnectionConnected: |
| 2404 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 2405 | "all transports are writable."; |
Tommi | dba22d3 | 2023-06-01 14:08:52 | [diff] [blame] | 2406 | { |
| 2407 | std::vector<RtpTransceiverProxyRefPtr> transceivers; |
| 2408 | if (ConfiguredForMedia()) { |
| 2409 | transceivers = rtp_manager()->transceivers()->List(); |
| 2410 | } |
| 2411 | |
| 2412 | network_thread()->PostTask( |
| 2413 | SafeTask(network_thread_safety_, |
| 2414 | [this, transceivers = std::move(transceivers)] { |
| 2415 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2416 | ReportTransportStats(std::move(transceivers)); |
| 2417 | })); |
| 2418 | } |
| 2419 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2420 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 2421 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 2422 | break; |
| 2423 | case cricket::kIceConnectionCompleted: |
| 2424 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 2425 | "all transports are complete."; |
| 2426 | if (ice_connection_state_ != |
| 2427 | PeerConnectionInterface::kIceConnectionConnected) { |
| 2428 | // If jumping directly from "checking" to "connected", |
| 2429 | // signal "connected" first. |
| 2430 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 2431 | } |
| 2432 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2433 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2434 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2435 | break; |
| 2436 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2437 | RTC_DCHECK_NOTREACHED(); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2441 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 2442 | const std::string& transport_name, |
| 2443 | const cricket::Candidates& candidates) { |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 2444 | // TODO(bugs.webrtc.org/12427): Expect this to come in on the network thread |
| 2445 | // (not signaling as it currently does), handle appropriately. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2446 | int sdp_mline_index; |
| 2447 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2448 | RTC_LOG(LS_ERROR) |
| 2449 | << "OnTransportControllerCandidatesGathered: content name " |
| 2450 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2451 | return; |
| 2452 | } |
| 2453 | |
| 2454 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 2455 | citer != candidates.end(); ++citer) { |
| 2456 | // Use transport_name as the candidate media id. |
| 2457 | std::unique_ptr<JsepIceCandidate> candidate( |
| 2458 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2459 | sdp_handler_->AddLocalIceCandidate(candidate.get()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2460 | OnIceCandidate(std::move(candidate)); |
| 2461 | } |
| 2462 | } |
| 2463 | |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2464 | void PeerConnection::OnTransportControllerCandidateError( |
| 2465 | const cricket::IceCandidateErrorEvent& event) { |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 2466 | OnIceCandidateError(event.address, event.port, event.url, event.error_code, |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2467 | event.error_text); |
| 2468 | } |
| 2469 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2470 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 2471 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2472 | // Sanity check. |
| 2473 | for (const cricket::Candidate& candidate : candidates) { |
| 2474 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2475 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 2476 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2477 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2478 | return; |
| 2479 | } |
| 2480 | } |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2481 | sdp_handler_->RemoveLocalIceCandidates(candidates); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2482 | OnIceCandidatesRemoved(candidates); |
| 2483 | } |
| 2484 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 2485 | void PeerConnection::OnTransportControllerCandidateChanged( |
| 2486 | const cricket::CandidatePairChangeEvent& event) { |
| 2487 | OnSelectedCandidatePairChanged(event); |
| 2488 | } |
| 2489 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2490 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 2491 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2492 | RTC_HISTOGRAM_ENUMERATION( |
| 2493 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 2494 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2495 | } |
| 2496 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2497 | // Returns the media index for a local ice candidate given the content name. |
| 2498 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 2499 | const std::string& content_name, |
| 2500 | int* sdp_mline_index) { |
| 2501 | if (!local_description() || !sdp_mline_index) { |
| 2502 | return false; |
| 2503 | } |
| 2504 | |
| 2505 | bool content_found = false; |
| 2506 | const ContentInfos& contents = local_description()->description()->contents(); |
| 2507 | for (size_t index = 0; index < contents.size(); ++index) { |
| 2508 | if (contents[index].name == content_name) { |
| 2509 | *sdp_mline_index = static_cast<int>(index); |
| 2510 | content_found = true; |
| 2511 | break; |
| 2512 | } |
| 2513 | } |
| 2514 | return content_found; |
| 2515 | } |
| 2516 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2517 | Call::Stats PeerConnection::GetCallStats() { |
| 2518 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 2519 | return worker_thread()->BlockingCall([this] { return GetCallStats(); }); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2520 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2521 | RTC_DCHECK_RUN_ON(worker_thread()); |
Henrik Boström | e88c95e | 2020-07-08 09:18:50 | [diff] [blame] | 2522 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2523 | if (call_) { |
| 2524 | return call_->GetStats(); |
| 2525 | } else { |
| 2526 | return Call::Stats(); |
| 2527 | } |
| 2528 | } |
| 2529 | |
Fredrik Hernqvist | efbe753 | 2023-01-13 15:42:36 | [diff] [blame] | 2530 | absl::optional<AudioDeviceModule::Stats> PeerConnection::GetAudioDeviceStats() { |
| 2531 | if (context_->media_engine()) { |
| 2532 | return context_->media_engine()->voice().GetAudioDeviceStats(); |
| 2533 | } |
| 2534 | return absl::nullopt; |
| 2535 | } |
| 2536 | |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2537 | absl::optional<std::string> PeerConnection::SetupDataChannelTransport_n( |
| 2538 | absl::string_view mid) { |
| 2539 | sctp_mid_n_ = std::string(mid); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2540 | DataChannelTransportInterface* transport = |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2541 | transport_controller_->GetDataChannelTransport(*sctp_mid_n_); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2542 | if (!transport) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 2543 | RTC_LOG(LS_ERROR) |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 2544 | << "Data channel transport is not available for data channels, mid=" |
| 2545 | << mid; |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2546 | sctp_mid_n_ = absl::nullopt; |
| 2547 | return absl::nullopt; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2548 | } |
| 2549 | |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2550 | absl::optional<std::string> transport_name; |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2551 | cricket::DtlsTransportInternal* dtls_transport = |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2552 | transport_controller_->GetDtlsTransport(*sctp_mid_n_); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2553 | if (dtls_transport) { |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2554 | transport_name = dtls_transport->transport_name(); |
| 2555 | } else { |
| 2556 | // Make sure we still set a valid string. |
| 2557 | transport_name = std::string(""); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2558 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2559 | |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2560 | data_channel_controller_.SetupDataChannelTransport_n(transport); |
| 2561 | |
| 2562 | return transport_name; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2563 | } |
| 2564 | |
Tommi | b00d63c | 2023-04-12 17:49:53 | [diff] [blame] | 2565 | void PeerConnection::TeardownDataChannelTransport_n(RTCError error) { |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 2566 | if (sctp_mid_n_) { |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 2567 | // `sctp_mid_` may still be active through an SCTP transport. If not, unset |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 2568 | // it. |
| 2569 | RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid=" |
| 2570 | << *sctp_mid_n_; |
| 2571 | sctp_mid_n_.reset(); |
| 2572 | } |
| 2573 | |
Tommi | b00d63c | 2023-04-12 17:49:53 | [diff] [blame] | 2574 | data_channel_controller_.TeardownDataChannelTransport_n(error); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2575 | } |
| 2576 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2577 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
Henrik Boström | f8187e0 | 2021-04-26 19:04:26 | [diff] [blame] | 2578 | bool PeerConnection::ValidateBundleSettings( |
| 2579 | const SessionDescription* desc, |
| 2580 | const std::map<std::string, const cricket::ContentGroup*>& |
| 2581 | bundle_groups_by_mid) { |
| 2582 | if (bundle_groups_by_mid.empty()) |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2583 | return true; |
| 2584 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2585 | const cricket::ContentInfos& contents = desc->contents(); |
| 2586 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 2587 | citer != contents.end(); ++citer) { |
| 2588 | const cricket::ContentInfo* content = (&*citer); |
| 2589 | RTC_DCHECK(content != NULL); |
Henrik Boström | f8187e0 | 2021-04-26 19:04:26 | [diff] [blame] | 2590 | auto it = bundle_groups_by_mid.find(content->name); |
Philipp Hancke | 32dae4b | 2023-05-12 04:53:55 | [diff] [blame] | 2591 | if (it != bundle_groups_by_mid.end() && |
| 2592 | !(content->rejected || content->bundle_only) && |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 2593 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2594 | if (!HasRtcpMuxEnabled(content)) |
| 2595 | return false; |
| 2596 | } |
| 2597 | } |
| 2598 | // RTCP-MUX is enabled in all the contents. |
| 2599 | return true; |
| 2600 | } |
| 2601 | |
Philipp Hancke | 54b925c | 2021-01-28 08:56:39 | [diff] [blame] | 2602 | void PeerConnection::ReportSdpBundleUsage( |
| 2603 | const SessionDescriptionInterface& remote_description) { |
| 2604 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2605 | |
| 2606 | bool using_bundle = |
| 2607 | remote_description.description()->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 2608 | int num_audio_mlines = 0; |
| 2609 | int num_video_mlines = 0; |
| 2610 | int num_data_mlines = 0; |
| 2611 | for (const ContentInfo& content : |
| 2612 | remote_description.description()->contents()) { |
| 2613 | cricket::MediaType media_type = content.media_description()->type(); |
| 2614 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 2615 | num_audio_mlines += 1; |
| 2616 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 2617 | num_video_mlines += 1; |
| 2618 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
| 2619 | num_data_mlines += 1; |
| 2620 | } |
| 2621 | } |
| 2622 | bool simple = num_audio_mlines <= 1 && num_video_mlines <= 1; |
| 2623 | BundleUsage usage = kBundleUsageMax; |
| 2624 | if (num_audio_mlines == 0 && num_video_mlines == 0) { |
| 2625 | if (num_data_mlines > 0) { |
| 2626 | usage = using_bundle ? kBundleUsageBundleDatachannelOnly |
| 2627 | : kBundleUsageNoBundleDatachannelOnly; |
| 2628 | } else { |
| 2629 | usage = kBundleUsageEmpty; |
| 2630 | } |
Harald Alvestrand | fa67aef | 2021-12-08 14:30:55 | [diff] [blame] | 2631 | } else if (configuration_.sdp_semantics == SdpSemantics::kPlanB_DEPRECATED) { |
Philipp Hancke | 54b925c | 2021-01-28 08:56:39 | [diff] [blame] | 2632 | // In plan-b, simple/complex usage will not show up in the number of |
| 2633 | // m-lines or BUNDLE. |
| 2634 | usage = using_bundle ? kBundleUsageBundlePlanB : kBundleUsageNoBundlePlanB; |
| 2635 | } else { |
| 2636 | if (simple) { |
| 2637 | usage = |
| 2638 | using_bundle ? kBundleUsageBundleSimple : kBundleUsageNoBundleSimple; |
| 2639 | } else { |
| 2640 | usage = using_bundle ? kBundleUsageBundleComplex |
| 2641 | : kBundleUsageNoBundleComplex; |
| 2642 | } |
| 2643 | } |
| 2644 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.BundleUsage", usage, |
| 2645 | kBundleUsageMax); |
| 2646 | } |
| 2647 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2648 | void PeerConnection::ReportIceCandidateCollected( |
| 2649 | const cricket::Candidate& candidate) { |
| 2650 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
| 2651 | if (candidate.address().IsPrivateIP()) { |
| 2652 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 2653 | } |
| 2654 | if (candidate.address().IsUnresolvedIP()) { |
| 2655 | NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED); |
| 2656 | } |
| 2657 | if (candidate.address().family() == AF_INET6) { |
| 2658 | NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED); |
| 2659 | } |
| 2660 | } |
| 2661 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2662 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 2663 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 2664 | usage_pattern_.NoteUsageEvent(event); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2665 | } |
| 2666 | |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2667 | // Asynchronously adds remote candidates on the network thread. |
| 2668 | void PeerConnection::AddRemoteCandidate(const std::string& mid, |
| 2669 | const cricket::Candidate& candidate) { |
| 2670 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2671 | |
Byoungchan Lee | 5a92577 | 2022-10-18 17:43:18 | [diff] [blame] | 2672 | if (candidate.network_type() != rtc::ADAPTER_TYPE_UNKNOWN) { |
| 2673 | RTC_DLOG(LS_WARNING) << "Using candidate with adapter type set - this " |
| 2674 | "should only happen in test"; |
| 2675 | } |
| 2676 | |
| 2677 | // Clear fields that do not make sense as remote candidates. |
| 2678 | cricket::Candidate new_candidate(candidate); |
| 2679 | new_candidate.set_network_type(rtc::ADAPTER_TYPE_UNKNOWN); |
| 2680 | new_candidate.set_relay_protocol(""); |
| 2681 | new_candidate.set_underlying_type_for_vpn(rtc::ADAPTER_TYPE_UNKNOWN); |
| 2682 | |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 2683 | network_thread()->PostTask(SafeTask( |
Byoungchan Lee | 5a92577 | 2022-10-18 17:43:18 | [diff] [blame] | 2684 | network_thread_safety_, [this, mid = mid, candidate = new_candidate] { |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2685 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2686 | std::vector<cricket::Candidate> candidates = {candidate}; |
| 2687 | RTCError error = |
| 2688 | transport_controller_->AddRemoteCandidates(mid, candidates); |
| 2689 | if (error.ok()) { |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 2690 | signaling_thread()->PostTask(SafeTask( |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2691 | signaling_thread_safety_.flag(), |
| 2692 | [this, candidate = std::move(candidate)] { |
| 2693 | ReportRemoteIceCandidateAdded(candidate); |
| 2694 | // Candidates successfully submitted for checking. |
| 2695 | if (ice_connection_state() == |
| 2696 | PeerConnectionInterface::kIceConnectionNew || |
| 2697 | ice_connection_state() == |
| 2698 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 2699 | // If state is New, then the session has just gotten its first |
| 2700 | // remote ICE candidates, so go to Checking. If state is |
| 2701 | // Disconnected, the session is re-using old candidates or |
| 2702 | // receiving additional ones, so go to Checking. If state is |
| 2703 | // Connected, stay Connected. |
| 2704 | // TODO(bemasc): If state is Connected, and the new candidates |
| 2705 | // are for a newly added transport, then the state actually |
| 2706 | // _should_ move to checking. Add a way to distinguish that |
| 2707 | // case. |
| 2708 | SetIceConnectionState( |
| 2709 | PeerConnectionInterface::kIceConnectionChecking); |
| 2710 | } |
| 2711 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 2712 | })); |
| 2713 | } else { |
| 2714 | RTC_LOG(LS_WARNING) << error.message(); |
| 2715 | } |
| 2716 | })); |
| 2717 | } |
| 2718 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2719 | void PeerConnection::ReportUsagePattern() const { |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 2720 | usage_pattern_.ReportUsagePattern(observer_); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2721 | } |
| 2722 | |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2723 | void PeerConnection::ReportRemoteIceCandidateAdded( |
| 2724 | const cricket::Candidate& candidate) { |
| 2725 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2726 | |
| 2727 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 2728 | |
| 2729 | if (candidate.address().IsPrivateIP()) { |
| 2730 | NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
| 2731 | } |
| 2732 | if (candidate.address().IsUnresolvedIP()) { |
| 2733 | NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
| 2734 | } |
| 2735 | if (candidate.address().family() == AF_INET6) { |
| 2736 | NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED); |
| 2737 | } |
| 2738 | } |
| 2739 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2740 | bool PeerConnection::SrtpRequired() const { |
Tomas Gunnarsson | 3278a71 | 2021-03-30 15:23:42 | [diff] [blame] | 2741 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 | [diff] [blame] | 2742 | return (dtls_enabled_ || |
| 2743 | sdp_handler_->webrtc_session_desc_factory()->SdesPolicy() == |
| 2744 | cricket::SEC_REQUIRED); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | void PeerConnection::OnTransportControllerGatheringState( |
| 2748 | cricket::IceGatheringState state) { |
| 2749 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 2750 | if (state == cricket::kIceGatheringGathering) { |
| 2751 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 2752 | } else if (state == cricket::kIceGatheringComplete) { |
| 2753 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
Harald Alvestrand | bedb605 | 2020-08-20 12:50:10 | [diff] [blame] | 2754 | } else if (state == cricket::kIceGatheringNew) { |
| 2755 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringNew); |
| 2756 | } else { |
| 2757 | RTC_LOG(LS_ERROR) << "Unknown state received: " << state; |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2758 | RTC_DCHECK_NOTREACHED(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2759 | } |
| 2760 | } |
| 2761 | |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2762 | // Runs on network_thread(). |
Tommi | dba22d3 | 2023-06-01 14:08:52 | [diff] [blame] | 2763 | void PeerConnection::ReportTransportStats( |
| 2764 | std::vector<RtpTransceiverProxyRefPtr> transceivers) { |
Markus Handell | 518669d | 2021-06-07 11:30:46 | [diff] [blame] | 2765 | TRACE_EVENT0("webrtc", "PeerConnection::ReportTransportStats"); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2766 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2767 | std::map<std::string, std::set<cricket::MediaType>> |
| 2768 | media_types_by_transport_name; |
Tommi | dba22d3 | 2023-06-01 14:08:52 | [diff] [blame] | 2769 | for (const auto& transceiver : transceivers) { |
| 2770 | if (transceiver->internal()->channel()) { |
| 2771 | std::string transport_name( |
| 2772 | transceiver->internal()->channel()->transport_name()); |
| 2773 | media_types_by_transport_name[transport_name].insert( |
| 2774 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2775 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2776 | } |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2777 | |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2778 | if (sctp_mid_n_) { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2779 | cricket::DtlsTransportInternal* dtls_transport = |
| 2780 | transport_controller_->GetDtlsTransport(*sctp_mid_n_); |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2781 | if (dtls_transport) { |
| 2782 | media_types_by_transport_name[dtls_transport->transport_name()].insert( |
| 2783 | cricket::MEDIA_TYPE_DATA); |
| 2784 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2785 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2786 | |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2787 | for (const auto& entry : media_types_by_transport_name) { |
| 2788 | const std::string& transport_name = entry.first; |
| 2789 | const std::set<cricket::MediaType> media_types = entry.second; |
| 2790 | cricket::TransportStats stats; |
| 2791 | if (transport_controller_->GetStats(transport_name, &stats)) { |
| 2792 | ReportBestConnectionState(stats); |
| 2793 | ReportNegotiatedCiphers(dtls_enabled_, stats, media_types); |
| 2794 | } |
| 2795 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2796 | } |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2797 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2798 | // Walk through the ConnectionInfos to gather best connection usage |
| 2799 | // for IPv4 and IPv6. |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2800 | // static (no member state required) |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2801 | void PeerConnection::ReportBestConnectionState( |
| 2802 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2803 | for (const cricket::TransportChannelStats& channel_stats : |
| 2804 | stats.channel_stats) { |
| 2805 | for (const cricket::ConnectionInfo& connection_info : |
Jonas Oreland | 149dc72 | 2019-08-28 06:10:27 | [diff] [blame] | 2806 | channel_stats.ice_transport_stats.connection_infos) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2807 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2808 | continue; |
| 2809 | } |
| 2810 | |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2811 | const cricket::Candidate& local = connection_info.local_candidate; |
| 2812 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2813 | |
| 2814 | // Increment the counter for IceCandidatePairType. |
| 2815 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 2816 | (local.type() == RELAY_PORT_TYPE && |
| 2817 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2818 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 2819 | GetIceCandidatePairCounter(local, remote), |
| 2820 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2821 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2822 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 2823 | GetIceCandidatePairCounter(local, remote), |
| 2824 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2825 | } else { |
Karl Wiberg | c95b939 | 2020-11-07 23:49:37 | [diff] [blame] | 2826 | RTC_CHECK_NOTREACHED(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2827 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2828 | |
| 2829 | // Increment the counter for IP type. |
| 2830 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2831 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 2832 | kBestConnections_IPv4, |
| 2833 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2834 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2835 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 2836 | kBestConnections_IPv6, |
| 2837 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2838 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 2839 | RTC_CHECK(!local.address().hostname().empty() && |
| 2840 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | return; |
| 2844 | } |
| 2845 | } |
| 2846 | } |
| 2847 | |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2848 | // static |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2849 | void PeerConnection::ReportNegotiatedCiphers( |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2850 | bool dtls_enabled, |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2851 | const cricket::TransportStats& stats, |
| 2852 | const std::set<cricket::MediaType>& media_types) { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2853 | if (!dtls_enabled || stats.channel_stats.empty()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2854 | return; |
| 2855 | } |
| 2856 | |
| 2857 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 2858 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2859 | if (srtp_crypto_suite == rtc::kSrtpInvalidCryptoSuite && |
| 2860 | ssl_cipher_suite == rtc::kTlsNullWithNullNull) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2861 | return; |
| 2862 | } |
| 2863 | |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2864 | if (ssl_cipher_suite != rtc::kTlsNullWithNullNull) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2865 | for (cricket::MediaType media_type : media_types) { |
| 2866 | switch (media_type) { |
| 2867 | case cricket::MEDIA_TYPE_AUDIO: |
| 2868 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2869 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2870 | rtc::kSslCipherSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2871 | break; |
| 2872 | case cricket::MEDIA_TYPE_VIDEO: |
| 2873 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2874 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2875 | rtc::kSslCipherSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2876 | break; |
| 2877 | case cricket::MEDIA_TYPE_DATA: |
| 2878 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2879 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2880 | rtc::kSslCipherSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2881 | break; |
| 2882 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2883 | RTC_DCHECK_NOTREACHED(); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2884 | continue; |
| 2885 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2886 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2890 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2891 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2892 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 2893 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2894 | DataChannelTransportInterface* data_channel_transport) { |
Karl Wiberg | ac02589 | 2019-03-26 12:08:37 | [diff] [blame] | 2895 | RTC_DCHECK_RUN_ON(network_thread()); |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2896 | bool ret = true; |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 2897 | if (ConfiguredForMedia()) { |
| 2898 | for (const auto& transceiver : |
| 2899 | rtp_manager()->transceivers()->UnsafeList()) { |
| 2900 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2901 | if (channel && channel->mid() == mid) { |
| 2902 | ret = channel->SetRtpTransport(rtp_transport); |
| 2903 | } |
Harald Alvestrand | 19ebabc | 2022-04-28 13:31:17 | [diff] [blame] | 2904 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2905 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2906 | |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 2907 | if (mid == sctp_mid_n_) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2908 | data_channel_controller_.OnTransportChanged(data_channel_transport); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2909 | if (dtls_transport) { |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 2910 | signaling_thread()->PostTask(SafeTask( |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2911 | signaling_thread_safety_.flag(), |
Tomas Gunnarsson | 94f0194 | 2022-01-03 14:59:12 | [diff] [blame] | 2912 | [this, |
| 2913 | name = std::string(dtls_transport->internal()->transport_name())] { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2914 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 46053e4 | 2023-01-20 12:18:53 | [diff] [blame] | 2915 | SetSctpTransportName(std::move(name)); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2916 | })); |
| 2917 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 2918 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2919 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2920 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2921 | } |
| 2922 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2923 | PeerConnectionObserver* PeerConnection::Observer() const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 2924 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2925 | RTC_DCHECK(observer_); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2926 | return observer_; |
| 2927 | } |
| 2928 | |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2929 | void PeerConnection::StartSctpTransport(int local_port, |
| 2930 | int remote_port, |
| 2931 | int max_message_size) { |
| 2932 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2933 | if (!sctp_mid_s_) |
| 2934 | return; |
| 2935 | |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 2936 | network_thread()->PostTask(SafeTask( |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2937 | network_thread_safety_, |
| 2938 | [this, mid = *sctp_mid_s_, local_port, remote_port, max_message_size] { |
| 2939 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 2940 | transport_controller_n()->GetSctpTransport(mid); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2941 | if (sctp_transport) |
| 2942 | sctp_transport->Start(local_port, remote_port, max_message_size); |
| 2943 | })); |
| 2944 | } |
| 2945 | |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 2946 | CryptoOptions PeerConnection::GetCryptoOptions() { |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 2947 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 2948 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 2949 | // after it has been removed. |
| 2950 | return configuration_.crypto_options.has_value() |
| 2951 | ? *configuration_.crypto_options |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 2952 | : options_.crypto_options; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 2953 | } |
| 2954 | |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 2955 | void PeerConnection::ClearStatsCache() { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2956 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Philipp Hancke | 7baa63f | 2022-09-01 13:39:50 | [diff] [blame] | 2957 | if (legacy_stats_) { |
| 2958 | legacy_stats_->InvalidateCache(); |
| 2959 | } |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 2960 | if (stats_collector_) { |
| 2961 | stats_collector_->ClearCachedStatsReport(); |
| 2962 | } |
| 2963 | } |
| 2964 | |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 2965 | bool PeerConnection::ShouldFireNegotiationNeededEvent(uint32_t event_id) { |
| 2966 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2967 | return sdp_handler_->ShouldFireNegotiationNeededEvent(event_id); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 2968 | } |
| 2969 | |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2970 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Harald Alvestrand | 00c62ed | 2021-10-20 08:52:12 | [diff] [blame] | 2971 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2972 | message_handler_.RequestUsagePatternReport( |
| 2973 | [this]() { |
| 2974 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2975 | ReportUsagePattern(); |
| 2976 | }, |
| 2977 | /* delay_ms= */ 0); |
| 2978 | } |
| 2979 | |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 08:39:31 | [diff] [blame] | 2980 | std::function<void(const rtc::CopyOnWriteBuffer& packet, |
| 2981 | int64_t packet_time_us)> |
| 2982 | PeerConnection::InitializeRtcpCallback() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2983 | RTC_DCHECK_RUN_ON(network_thread()); |
Per K | cf439a0 | 2023-01-05 13:01:39 | [diff] [blame] | 2984 | return [this](const rtc::CopyOnWriteBuffer& packet, |
| 2985 | int64_t /*packet_time_us*/) { |
| 2986 | worker_thread()->PostTask(SafeTask(worker_thread_safety_, [this, packet]() { |
| 2987 | call_ptr_->Receiver()->DeliverRtcpPacket(packet); |
| 2988 | })); |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 08:39:31 | [diff] [blame] | 2989 | }; |
| 2990 | } |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2991 | |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 2992 | std::function<void(const RtpPacketReceived& parsed_packet)> |
| 2993 | PeerConnection::InitializeUnDemuxablePacketHandler() { |
| 2994 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2995 | return [this](const RtpPacketReceived& parsed_packet) { |
| 2996 | worker_thread()->PostTask( |
| 2997 | SafeTask(worker_thread_safety_, [this, parsed_packet]() { |
| 2998 | // Deliver the packet anyway to Call to allow Call to do BWE. |
| 2999 | // Even if there is no media receiver, the packet has still |
| 3000 | // been received on the network and has been correcly parsed. |
| 3001 | call_ptr_->Receiver()->DeliverRtpPacket( |
| 3002 | MediaType::ANY, parsed_packet, |
| 3003 | /*undemuxable_packet_handler=*/ |
| 3004 | [](const RtpPacketReceived& packet) { return false; }); |
| 3005 | })); |
| 3006 | }; |
| 3007 | } |
| 3008 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3009 | } // namespace webrtc |