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