henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/peer_connection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 12 | |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 13 | #include <limits.h> |
| 14 | #include <stddef.h> |
Harald Alvestrand | 0ccfbd2 | 2021-04-08 07:25:04 | [diff] [blame] | 15 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 16 | #include <algorithm> |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 17 | #include <memory> |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 18 | #include <set> |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 19 | #include <string> |
Harald Alvestrand | 3eaee6b | 2020-10-19 06:35:55 | [diff] [blame] | 20 | #include <utility> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 21 | |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 22 | #include "absl/algorithm/container.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 20:02:22 | [diff] [blame] | 23 | #include "absl/strings/match.h" |
Harald Alvestrand | 0d4af12 | 2022-01-31 09:36:05 | [diff] [blame] | 24 | #include "absl/strings/string_view.h" |
Fredrik Hernqvist | efbe753 | 2023-01-13 15:42:36 | [diff] [blame] | 25 | #include "absl/types/optional.h" |
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" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 56 | #include "rtc_base/helpers.h" |
| 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 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 178 | absl::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) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 182 | return absl::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, |
| 264 | absl::optional<int> previous_ice_candidate_pool_size) { |
| 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; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 410 | absl::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; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 427 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 428 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 429 | absl::optional<int> ice_check_min_interval; |
| 430 | absl::optional<int> ice_unwritable_timeout; |
| 431 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 432 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 433 | absl::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; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 436 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 437 | bool active_reset_srtp_params; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 438 | absl::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; |
Harald Alvestrand | 6216693 | 2020-10-26 08:30:41 | [diff] [blame] | 442 | absl::optional<int> report_usage_pattern_delay_ms; |
Derek Bailey | 6c127a1 | 2021-04-15 19:42:41 | [diff] [blame] | 443 | absl::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; |
Henrik Boström | cf2856b | 2022-11-15 08:23:19 | [diff] [blame] | 447 | absl::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>( |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 708 | IsUnifiedPlan(), context_.get(), &usage_pattern_, observer_, |
| 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 | |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 776 | transport_controller_.reset(new JsepTransportController( |
Victor Boivie | cd54fd8 | 2024-02-28 11:16:15 | [diff] [blame] | 777 | env_, network_thread(), port_allocator_.get(), |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 778 | async_dns_resolver_factory_.get(), std::move(config))); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 779 | |
| 780 | transport_controller_->SubscribeIceConnectionState( |
| 781 | [this](cricket::IceConnectionState s) { |
| 782 | RTC_DCHECK_RUN_ON(network_thread()); |
| 783 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 784 | SafeTask(signaling_thread_safety_.flag(), [this, s]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 785 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 786 | OnTransportControllerConnectionState(s); |
| 787 | })); |
| 788 | }); |
| 789 | transport_controller_->SubscribeConnectionState( |
| 790 | [this](PeerConnectionInterface::PeerConnectionState s) { |
| 791 | RTC_DCHECK_RUN_ON(network_thread()); |
| 792 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 793 | SafeTask(signaling_thread_safety_.flag(), [this, s]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 794 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 795 | SetConnectionState(s); |
| 796 | })); |
| 797 | }); |
| 798 | transport_controller_->SubscribeStandardizedIceConnectionState( |
| 799 | [this](PeerConnectionInterface::IceConnectionState s) { |
| 800 | RTC_DCHECK_RUN_ON(network_thread()); |
| 801 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 802 | SafeTask(signaling_thread_safety_.flag(), [this, s]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 803 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 804 | SetStandardizedIceConnectionState(s); |
| 805 | })); |
| 806 | }); |
| 807 | transport_controller_->SubscribeIceGatheringState( |
| 808 | [this](cricket::IceGatheringState s) { |
| 809 | RTC_DCHECK_RUN_ON(network_thread()); |
| 810 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 811 | SafeTask(signaling_thread_safety_.flag(), [this, s]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 812 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 813 | OnTransportControllerGatheringState(s); |
| 814 | })); |
| 815 | }); |
| 816 | transport_controller_->SubscribeIceCandidateGathered( |
| 817 | [this](const std::string& transport, |
| 818 | const std::vector<cricket::Candidate>& candidates) { |
| 819 | RTC_DCHECK_RUN_ON(network_thread()); |
| 820 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 821 | SafeTask(signaling_thread_safety_.flag(), |
| 822 | [this, t = transport, c = candidates]() { |
| 823 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 824 | OnTransportControllerCandidatesGathered(t, c); |
| 825 | })); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 826 | }); |
| 827 | transport_controller_->SubscribeIceCandidateError( |
| 828 | [this](const cricket::IceCandidateErrorEvent& event) { |
| 829 | RTC_DCHECK_RUN_ON(network_thread()); |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 830 | signaling_thread()->PostTask( |
| 831 | SafeTask(signaling_thread_safety_.flag(), [this, event = event]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 832 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 833 | OnTransportControllerCandidateError(event); |
| 834 | })); |
| 835 | }); |
| 836 | transport_controller_->SubscribeIceCandidatesRemoved( |
| 837 | [this](const std::vector<cricket::Candidate>& c) { |
| 838 | RTC_DCHECK_RUN_ON(network_thread()); |
| 839 | signaling_thread()->PostTask( |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 840 | SafeTask(signaling_thread_safety_.flag(), [this, c = c]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 841 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 842 | OnTransportControllerCandidatesRemoved(c); |
| 843 | })); |
| 844 | }); |
| 845 | transport_controller_->SubscribeIceCandidatePairChanged( |
| 846 | [this](const cricket::CandidatePairChangeEvent& event) { |
| 847 | RTC_DCHECK_RUN_ON(network_thread()); |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 848 | signaling_thread()->PostTask( |
| 849 | SafeTask(signaling_thread_safety_.flag(), [this, event = event]() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 850 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 851 | OnTransportControllerCandidateChanged(event); |
| 852 | })); |
| 853 | }); |
| 854 | |
| 855 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 856 | return transport_controller_.get(); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 857 | } |
| 858 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 859 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 860 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 861 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 862 | "Plan SdpSemantics. Please use GetSenders " |
| 863 | "instead."; |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 864 | return sdp_handler_->local_streams(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 865 | } |
| 866 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 867 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 868 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 869 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 870 | "Plan SdpSemantics. Please use GetReceivers " |
| 871 | "instead."; |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 872 | return sdp_handler_->remote_streams(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 873 | } |
| 874 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 | [diff] [blame] | 875 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 876 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 877 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 878 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 879 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 880 | if (!ConfiguredForMedia()) { |
| 881 | RTC_LOG(LS_ERROR) << "AddStream: Not configured for media"; |
| 882 | return false; |
| 883 | } |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 884 | return sdp_handler_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 888 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 889 | RTC_DCHECK(ConfiguredForMedia()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 890 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 891 | "Plan SdpSemantics. Please use RemoveTrack " |
| 892 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 893 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 894 | sdp_handler_->RemoveStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 895 | } |
| 896 | |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 897 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 898 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 899 | const std::vector<std::string>& stream_ids) { |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 900 | return AddTrack(std::move(track), stream_ids, nullptr); |
| 901 | } |
| 902 | |
| 903 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
| 904 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 905 | const std::vector<std::string>& stream_ids, |
| 906 | const std::vector<RtpEncodingParameters>& init_send_encodings) { |
| 907 | return AddTrack(std::move(track), stream_ids, &init_send_encodings); |
| 908 | } |
| 909 | |
| 910 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
| 911 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 912 | const std::vector<std::string>& stream_ids, |
| 913 | const std::vector<RtpEncodingParameters>* init_send_encodings) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 914 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 915 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 916 | if (!ConfiguredForMedia()) { |
| 917 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 918 | "Not configured for media"); |
| 919 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 920 | if (!track) { |
| 921 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 922 | } |
| 923 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 924 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 925 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 926 | "Track has invalid kind: " + track->kind()); |
| 927 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 928 | if (IsClosed()) { |
| 929 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 930 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 931 | } |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 932 | if (rtp_manager()->FindSenderForTrack(track.get())) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 933 | LOG_AND_RETURN_ERROR( |
| 934 | RTCErrorType::INVALID_PARAMETER, |
| 935 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 936 | } |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 937 | auto sender_or_error = |
| 938 | rtp_manager()->AddTrack(track, stream_ids, init_send_encodings); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 939 | if (sender_or_error.ok()) { |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 940 | sdp_handler_->UpdateNegotiationNeeded(); |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 941 | legacy_stats_->AddTrack(track.get()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 942 | } |
| 943 | return sender_or_error; |
| 944 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 945 | |
Harald Alvestrand | 09a0d01 | 2022-01-04 19:42:07 | [diff] [blame] | 946 | RTCError PeerConnection::RemoveTrackOrError( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 947 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 948 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 949 | if (!ConfiguredForMedia()) { |
| 950 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 951 | "Not configured for media"); |
| 952 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 953 | if (!sender) { |
| 954 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 955 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 956 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 957 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 958 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 959 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 960 | if (IsUnifiedPlan()) { |
| 961 | auto transceiver = FindTransceiverBySender(sender); |
| 962 | if (!transceiver || !sender->track()) { |
| 963 | return RTCError::OK(); |
| 964 | } |
| 965 | sender->SetTrack(nullptr); |
| 966 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 967 | transceiver->internal()->set_direction( |
| 968 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 969 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 970 | transceiver->internal()->set_direction( |
| 971 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 972 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 973 | } else { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 974 | bool removed; |
| 975 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 976 | removed = rtp_manager()->GetAudioTransceiver()->internal()->RemoveSender( |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 977 | sender.get()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 978 | } else { |
| 979 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 980 | removed = rtp_manager()->GetVideoTransceiver()->internal()->RemoveSender( |
Niels Möller | afb246b | 2022-04-20 12:26:50 | [diff] [blame] | 981 | sender.get()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 982 | } |
| 983 | if (!removed) { |
| 984 | LOG_AND_RETURN_ERROR( |
| 985 | RTCErrorType::INVALID_PARAMETER, |
| 986 | "Couldn't find sender " + sender->id() + " to remove."); |
| 987 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 988 | } |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 989 | sdp_handler_->UpdateNegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 990 | return RTCError::OK(); |
| 991 | } |
| 992 | |
| 993 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 994 | PeerConnection::FindTransceiverBySender( |
| 995 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 996 | return rtp_manager()->transceivers()->FindBySender(sender); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 997 | } |
| 998 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 999 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1000 | PeerConnection::AddTransceiver( |
| 1001 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1002 | if (!ConfiguredForMedia()) { |
| 1003 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1004 | "Not configured for media"); |
| 1005 | } |
| 1006 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1007 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1008 | } |
| 1009 | |
| 1010 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1011 | PeerConnection::AddTransceiver( |
| 1012 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1013 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1014 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1015 | if (!ConfiguredForMedia()) { |
| 1016 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1017 | "Not configured for media"); |
| 1018 | } |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1019 | RTC_CHECK(IsUnifiedPlan()) |
| 1020 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1021 | if (!track) { |
| 1022 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1023 | } |
| 1024 | cricket::MediaType media_type; |
| 1025 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1026 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1027 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1028 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1029 | } else { |
| 1030 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1031 | "Track kind is not audio or video"); |
| 1032 | } |
| 1033 | return AddTransceiver(media_type, track, init); |
| 1034 | } |
| 1035 | |
| 1036 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1037 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1038 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1039 | } |
| 1040 | |
| 1041 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1042 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1043 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1044 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1045 | if (!ConfiguredForMedia()) { |
| 1046 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1047 | "Not configured for media"); |
| 1048 | } |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1049 | RTC_CHECK(IsUnifiedPlan()) |
| 1050 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1051 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1052 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1053 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1054 | "media type is not audio or video"); |
| 1055 | } |
| 1056 | return AddTransceiver(media_type, nullptr, init); |
| 1057 | } |
| 1058 | |
| 1059 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1060 | PeerConnection::AddTransceiver( |
| 1061 | cricket::MediaType media_type, |
| 1062 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1063 | const RtpTransceiverInit& init, |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1064 | bool update_negotiation_needed) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1065 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1066 | if (!ConfiguredForMedia()) { |
| 1067 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1068 | "Not configured for media"); |
| 1069 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1070 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1071 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1072 | if (track) { |
| 1073 | RTC_DCHECK_EQ(media_type, |
| 1074 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1075 | ? cricket::MEDIA_TYPE_AUDIO |
| 1076 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1077 | } |
| 1078 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1079 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1080 | [](const RtpEncodingParameters& encoding) { |
| 1081 | return !encoding.rid.empty(); |
| 1082 | }); |
| 1083 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-07 01:09:52 | [diff] [blame] | 1084 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1085 | RTCErrorType::INVALID_PARAMETER, |
| 1086 | "RIDs must be provided for either all or none of the send encodings."); |
Emircan Uysaler | 7832343 | 2019-02-08 20:41:39 | [diff] [blame] | 1087 | } |
| 1088 | |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 1089 | if (num_rids > 0 && absl::c_any_of(init.send_encodings, |
| 1090 | [](const RtpEncodingParameters& encoding) { |
| 1091 | return !IsLegalRsidName(encoding.rid); |
| 1092 | })) { |
| 1093 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1094 | "Invalid RID value provided."); |
| 1095 | } |
| 1096 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1097 | if (absl::c_any_of(init.send_encodings, |
| 1098 | [](const RtpEncodingParameters& encoding) { |
| 1099 | return encoding.ssrc.has_value(); |
| 1100 | })) { |
| 1101 | LOG_AND_RETURN_ERROR( |
| 1102 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1103 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | RtpParameters parameters; |
| 1107 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1108 | |
| 1109 | // Encodings are dropped from the tail if too many are provided. |
Florent Castelli | e1b685a | 2021-04-30 17:11:37 | [diff] [blame] | 1110 | size_t max_simulcast_streams = |
| 1111 | media_type == cricket::MEDIA_TYPE_VIDEO ? kMaxSimulcastStreams : 1u; |
| 1112 | if (parameters.encodings.size() > max_simulcast_streams) { |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1113 | parameters.encodings.erase( |
Florent Castelli | e1b685a | 2021-04-30 17:11:37 | [diff] [blame] | 1114 | parameters.encodings.begin() + max_simulcast_streams, |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1115 | parameters.encodings.end()); |
| 1116 | } |
| 1117 | |
| 1118 | // Single RID should be removed. |
| 1119 | if (parameters.encodings.size() == 1 && |
| 1120 | !parameters.encodings[0].rid.empty()) { |
| 1121 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1122 | parameters.encodings[0].rid.clear(); |
| 1123 | } |
| 1124 | |
| 1125 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1126 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1127 | rtc::UniqueStringGenerator rid_generator; |
| 1128 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
Harald Alvestrand | 5ad491e | 2023-02-10 11:28:04 | [diff] [blame] | 1129 | encoding.rid = rid_generator.GenerateString(); |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
Florent Castelli | bd1e5d5 | 2023-01-17 17:08:26 | [diff] [blame] | 1133 | // If no encoding parameters were provided, a default entry is created. |
| 1134 | if (parameters.encodings.empty()) { |
| 1135 | parameters.encodings.push_back({}); |
| 1136 | } |
| 1137 | |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1138 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1139 | LOG_AND_RETURN_ERROR( |
| 1140 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1141 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1142 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1143 | |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 1144 | std::vector<cricket::VideoCodec> codecs; |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 1145 | // Gather the current codec capabilities to allow checking scalabilityMode and |
| 1146 | // codec selection against supported values. |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 1147 | if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 1148 | codecs = context_->media_engine()->video().send_codecs(false); |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 1149 | } else { |
| 1150 | codecs = context_->media_engine()->voice().send_codecs(); |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 1151 | } |
| 1152 | |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 1153 | auto result = |
| 1154 | cricket::CheckRtpParametersValues(parameters, codecs, absl::nullopt); |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 1155 | if (!result.ok()) { |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 1156 | if (result.type() == RTCErrorType::INVALID_MODIFICATION) { |
| 1157 | result.set_type(RTCErrorType::UNSUPPORTED_OPERATION); |
| 1158 | } |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 1159 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1160 | } |
| 1161 | |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1162 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1163 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1164 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1165 | // that sender ID is already in use. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1166 | std::string sender_id = (track && !rtp_manager()->FindSenderById(track->id()) |
| 1167 | ? track->id() |
| 1168 | : rtc::CreateRandomUuid()); |
| 1169 | auto sender = rtp_manager()->CreateSender( |
| 1170 | media_type, sender_id, track, init.stream_ids, parameters.encodings); |
| 1171 | auto receiver = |
| 1172 | rtp_manager()->CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1173 | auto transceiver = rtp_manager()->CreateAndAddTransceiver(sender, receiver); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1174 | transceiver->internal()->set_direction(init.direction); |
| 1175 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1176 | if (update_negotiation_needed) { |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1177 | sdp_handler_->UpdateNegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1178 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1179 | |
| 1180 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1181 | } |
| 1182 | |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1183 | void PeerConnection::OnNegotiationNeeded() { |
| 1184 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1185 | RTC_DCHECK(!IsClosed()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1186 | sdp_handler_->UpdateNegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1187 | } |
| 1188 | |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1189 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-19 00:58:44 | [diff] [blame] | 1190 | const std::string& kind, |
| 1191 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1192 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1193 | if (!ConfiguredForMedia()) { |
| 1194 | RTC_LOG(LS_ERROR) << "Not configured for media"; |
| 1195 | return nullptr; |
| 1196 | } |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1197 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1198 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1199 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1200 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 1201 | if (IsClosed()) { |
| 1202 | return nullptr; |
| 1203 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1204 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1205 | // Internally we need to have one stream with Plan B semantics, so we |
| 1206 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1207 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1208 | if (stream_id.empty()) { |
| 1209 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1210 | RTC_LOG(LS_INFO) |
| 1211 | << "No stream_id specified for sender. Generated stream ID: " |
| 1212 | << stream_ids[0]; |
| 1213 | } else { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1214 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1215 | } |
| 1216 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1217 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1218 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1219 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1220 | auto audio_sender = |
| 1221 | AudioRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), |
| 1222 | legacy_stats_.get(), rtp_manager()); |
Harald Alvestrand | c0d44d9 | 2022-12-13 12:57:24 | [diff] [blame] | 1223 | audio_sender->SetMediaChannel(rtp_manager()->voice_media_send_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1224 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1225 | signaling_thread(), audio_sender); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1226 | rtp_manager()->GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1227 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1228 | auto video_sender = VideoRtpSender::Create( |
| 1229 | worker_thread(), rtc::CreateRandomUuid(), rtp_manager()); |
Harald Alvestrand | c0d44d9 | 2022-12-13 12:57:24 | [diff] [blame] | 1230 | video_sender->SetMediaChannel(rtp_manager()->video_media_send_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1231 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1232 | signaling_thread(), video_sender); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1233 | rtp_manager()->GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1234 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1235 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1236 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1237 | } |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 1238 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1239 | |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1240 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1241 | } |
| 1242 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1243 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1244 | const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1245 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1246 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1247 | if (ConfiguredForMedia()) { |
| 1248 | for (const auto& sender : rtp_manager()->GetSendersInternal()) { |
| 1249 | ret.push_back(sender); |
| 1250 | } |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1251 | } |
| 1252 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1256 | PeerConnection::GetReceivers() const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1257 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1258 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1259 | if (ConfiguredForMedia()) { |
| 1260 | for (const auto& receiver : rtp_manager()->GetReceiversInternal()) { |
| 1261 | ret.push_back(receiver); |
| 1262 | } |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1263 | } |
| 1264 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1265 | } |
| 1266 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1267 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1268 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1269 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1270 | RTC_CHECK(IsUnifiedPlan()) |
| 1271 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1272 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1273 | if (ConfiguredForMedia()) { |
| 1274 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
| 1275 | all_transceivers.push_back(transceiver); |
| 1276 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1277 | } |
| 1278 | return all_transceivers; |
| 1279 | } |
| 1280 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1281 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 | [diff] [blame] | 1282 | MediaStreamTrackInterface* track, |
| 1283 | StatsOutputLevel level) { |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1284 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats (legacy)"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1285 | RTC_DCHECK_RUN_ON(signaling_thread()); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 1286 | if (!observer) { |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1287 | RTC_LOG(LS_ERROR) << "Legacy GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1288 | return false; |
| 1289 | } |
| 1290 | |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1291 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 1292 | |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1293 | legacy_stats_->UpdateStats(level); |
Tommi | 1901551 | 2022-02-02 10:49:35 | [diff] [blame] | 1294 | |
| 1295 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(4); |
| 1296 | |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1297 | // The LegacyStatsCollector is used to tell if a track is valid because it may |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 1298 | // remember tracks that the PeerConnection previously removed. |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1299 | if (track && !legacy_stats_->IsValidTrack(track->id())) { |
| 1300 | RTC_LOG(LS_WARNING) << "Legacy GetStats is called with an invalid track: " |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1301 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 1302 | return false; |
| 1303 | } |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1304 | message_handler_.PostGetStats(observer, legacy_stats_.get(), track); |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1305 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1306 | return true; |
| 1307 | } |
| 1308 | |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1309 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1310 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1311 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1312 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1313 | RTC_DCHECK(callback); |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1314 | RTC_LOG_THREAD_BLOCK_COUNT(); |
Niels Möller | e7cc883 | 2022-01-04 14:20:03 | [diff] [blame] | 1315 | stats_collector_->GetStatsReport( |
| 1316 | rtc::scoped_refptr<RTCStatsCollectorCallback>(callback)); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1317 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1318 | } |
| 1319 | |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1320 | void PeerConnection::GetStats( |
| 1321 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 1322 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1323 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1324 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1325 | RTC_DCHECK(callback); |
| 1326 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1327 | RTC_LOG_THREAD_BLOCK_COUNT(); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1328 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 1329 | if (selector) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1330 | for (const auto& proxy_transceiver : |
| 1331 | rtp_manager()->transceivers()->List()) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1332 | for (const auto& proxy_sender : |
| 1333 | proxy_transceiver->internal()->senders()) { |
| 1334 | if (proxy_sender == selector) { |
| 1335 | internal_sender = proxy_sender->internal(); |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | if (internal_sender) |
| 1340 | break; |
| 1341 | } |
| 1342 | } |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 1343 | // 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] | 1344 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 1345 | // PeerConnection). This means that "all the stats objects representing the |
| 1346 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 1347 | // produces an empty stats report. |
| 1348 | stats_collector_->GetStatsReport(internal_sender, callback); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1349 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | void PeerConnection::GetStats( |
| 1353 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 1354 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 1355 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1356 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1357 | RTC_DCHECK(callback); |
| 1358 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1359 | RTC_LOG_THREAD_BLOCK_COUNT(); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1360 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 1361 | if (selector) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1362 | for (const auto& proxy_transceiver : |
| 1363 | rtp_manager()->transceivers()->List()) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1364 | for (const auto& proxy_receiver : |
| 1365 | proxy_transceiver->internal()->receivers()) { |
| 1366 | if (proxy_receiver == selector) { |
| 1367 | internal_receiver = proxy_receiver->internal(); |
| 1368 | break; |
| 1369 | } |
| 1370 | } |
| 1371 | if (internal_receiver) |
| 1372 | break; |
| 1373 | } |
| 1374 | } |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 1375 | // 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] | 1376 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 1377 | // the PeerConnection). This means that "all the stats objects representing |
| 1378 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 1379 | // selector produces an empty stats report. |
| 1380 | stats_collector_->GetStatsReport(internal_receiver, callback); |
Henrik Boström | 5023ffb | 2022-07-04 13:47:25 | [diff] [blame] | 1381 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 1382 | } |
| 1383 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1384 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1385 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1386 | return sdp_handler_->signaling_state(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1387 | } |
| 1388 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1389 | PeerConnectionInterface::IceConnectionState |
| 1390 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1391 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1392 | return ice_connection_state_; |
| 1393 | } |
| 1394 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1395 | PeerConnectionInterface::IceConnectionState |
| 1396 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1397 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1398 | return standardized_ice_connection_state_; |
| 1399 | } |
| 1400 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1401 | PeerConnectionInterface::PeerConnectionState |
| 1402 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1403 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1404 | return connection_state_; |
| 1405 | } |
| 1406 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1407 | PeerConnectionInterface::IceGatheringState |
| 1408 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 1409 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1410 | return ice_gathering_state_; |
| 1411 | } |
| 1412 | |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 1413 | absl::optional<bool> PeerConnection::can_trickle_ice_candidates() { |
| 1414 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1415 | const SessionDescriptionInterface* description = current_remote_description(); |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 1416 | if (!description) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1417 | description = pending_remote_description(); |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 1418 | } |
| 1419 | if (!description) { |
| 1420 | return absl::nullopt; |
| 1421 | } |
| 1422 | // TODO(bugs.webrtc.org/7443): Change to retrieve from session-level option. |
| 1423 | if (description->description()->transport_infos().size() < 1) { |
| 1424 | return absl::nullopt; |
| 1425 | } |
| 1426 | return description->description()->transport_infos()[0].description.HasOption( |
| 1427 | "trickle"); |
| 1428 | } |
| 1429 | |
Harald Alvestrand | a9af50f | 2021-05-21 13:33:51 | [diff] [blame] | 1430 | RTCErrorOr<rtc::scoped_refptr<DataChannelInterface>> |
| 1431 | PeerConnection::CreateDataChannelOrError(const std::string& label, |
| 1432 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 09:17:47 | [diff] [blame] | 1433 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1434 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 18:14:50 | [diff] [blame] | 1435 | |
Tommi | 4f7ade5 | 2023-03-29 18:46:59 | [diff] [blame] | 1436 | if (IsClosed()) { |
| 1437 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1438 | "CreateDataChannelOrError: PeerConnection is closed."); |
| 1439 | } |
| 1440 | |
Harald Alvestrand | 5da3eb0 | 2023-03-15 20:39:42 | [diff] [blame] | 1441 | bool first_datachannel = !data_channel_controller_.HasUsedDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1442 | |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 1443 | InternalDataChannelInit internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 1444 | if (config) { |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 1445 | internal_config = InternalDataChannelInit(*config); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 1446 | } |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 1447 | |
| 1448 | internal_config.fallback_ssl_role = sdp_handler_->GuessSslRole(); |
Tommi | 4f7ade5 | 2023-03-29 18:46:59 | [diff] [blame] | 1449 | RTCErrorOr<rtc::scoped_refptr<DataChannelInterface>> ret = |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 1450 | data_channel_controller_.InternalCreateDataChannelWithProxy( |
Tommi | 4f7ade5 | 2023-03-29 18:46:59 | [diff] [blame] | 1451 | label, internal_config); |
| 1452 | if (!ret.ok()) { |
| 1453 | return ret.MoveError(); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1454 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1455 | |
Tommi | 4f7ade5 | 2023-03-29 18:46:59 | [diff] [blame] | 1456 | rtc::scoped_refptr<DataChannelInterface> channel = ret.MoveValue(); |
| 1457 | |
Philipp Hancke | 522380f | 2023-05-09 07:41:03 | [diff] [blame] | 1458 | // Check the onRenegotiationNeeded event (with plan-b backward compat) |
| 1459 | if (configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan || |
| 1460 | (configuration_.sdp_semantics == SdpSemantics::kPlanB_DEPRECATED && |
| 1461 | first_datachannel)) { |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1462 | sdp_handler_->UpdateNegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 1463 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1464 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 1465 | return channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1466 | } |
| 1467 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 1468 | void PeerConnection::RestartIce() { |
| 1469 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1470 | sdp_handler_->RestartIce(); |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 1471 | } |
| 1472 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1473 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 1474 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1475 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1476 | sdp_handler_->CreateOffer(observer, options); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1477 | } |
| 1478 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1479 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1480 | const RTCOfferAnswerOptions& options) { |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1481 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1482 | sdp_handler_->CreateAnswer(observer, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1483 | } |
| 1484 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1485 | void PeerConnection::SetLocalDescription( |
| 1486 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 1487 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1488 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1489 | sdp_handler_->SetLocalDescription(observer, desc_ptr); |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | void PeerConnection::SetLocalDescription( |
| 1493 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1494 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 1495 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1496 | sdp_handler_->SetLocalDescription(std::move(desc), observer); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1497 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 1498 | |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 1499 | void PeerConnection::SetLocalDescription( |
| 1500 | SetSessionDescriptionObserver* observer) { |
| 1501 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1502 | sdp_handler_->SetLocalDescription(observer); |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | void PeerConnection::SetLocalDescription( |
| 1506 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 1507 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1508 | sdp_handler_->SetLocalDescription(observer); |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 1509 | } |
| 1510 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1511 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 | [diff] [blame] | 1512 | SetSessionDescriptionObserver* observer, |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1513 | SessionDescriptionInterface* desc_ptr) { |
| 1514 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1515 | sdp_handler_->SetRemoteDescription(observer, desc_ptr); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | void PeerConnection::SetRemoteDescription( |
| 1519 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1520 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1521 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1522 | sdp_handler_->SetRemoteDescription(std::move(desc), observer); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 1523 | } |
| 1524 | |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 1525 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1526 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 1527 | return configuration_; |
| 1528 | } |
| 1529 | |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1530 | RTCError PeerConnection::SetConfiguration( |
| 1531 | const RTCConfiguration& configuration) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1532 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1533 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 1534 | if (IsClosed()) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1535 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1536 | "SetConfiguration: PeerConnection is closed."); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 1537 | } |
| 1538 | |
Tommi | aea49c9 | 2023-10-22 11:00:14 | [diff] [blame] | 1539 | const bool has_local_description = local_description() != nullptr; |
| 1540 | |
| 1541 | RTCError validate_error = ValidateIceCandidatePoolSize( |
| 1542 | configuration.ice_candidate_pool_size, |
| 1543 | has_local_description |
| 1544 | ? absl::optional<int>(configuration_.ice_candidate_pool_size) |
| 1545 | : absl::nullopt); |
| 1546 | if (!validate_error.ok()) { |
| 1547 | return validate_error; |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 1548 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1549 | |
Tommi | aea49c9 | 2023-10-22 11:00:14 | [diff] [blame] | 1550 | if (has_local_description && |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1551 | configuration.crypto_options != configuration_.crypto_options) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1552 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 1553 | "Can't change crypto_options after calling " |
| 1554 | "SetLocalDescription."); |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1555 | } |
| 1556 | |
Tommi | aea49c9 | 2023-10-22 11:00:14 | [diff] [blame] | 1557 | // Create a new, configuration object whose Ice config will have been |
| 1558 | // validated.. |
| 1559 | RTCErrorOr<RTCConfiguration> validated_config = |
| 1560 | ApplyConfiguration(configuration, configuration_); |
| 1561 | if (!validated_config.ok()) { |
| 1562 | return validated_config.error(); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | // Parse ICE servers before hopping to network thread. |
| 1566 | cricket::ServerAddresses stun_servers; |
| 1567 | std::vector<cricket::RelayServerConfig> turn_servers; |
Tommi | aea49c9 | 2023-10-22 11:00:14 | [diff] [blame] | 1568 | validate_error = ParseAndValidateIceServersFromConfiguration( |
| 1569 | configuration, stun_servers, turn_servers, usage_pattern_); |
| 1570 | if (!validate_error.ok()) { |
| 1571 | return validate_error; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1572 | } |
Philipp Hancke | 41a8357 | 2022-11-28 13:47:45 | [diff] [blame] | 1573 | |
Tommi | aea49c9 | 2023-10-22 11:00:14 | [diff] [blame] | 1574 | const RTCConfiguration& modified_config = validated_config.value(); |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 1575 | const bool needs_ice_restart = |
| 1576 | modified_config.servers != configuration_.servers || |
| 1577 | NeedIceRestart( |
| 1578 | configuration_.surface_ice_candidates_on_ice_transport_type_changed, |
| 1579 | configuration_.type, modified_config.type) || |
| 1580 | modified_config.GetTurnPortPrunePolicy() != |
| 1581 | configuration_.GetTurnPortPrunePolicy(); |
| 1582 | cricket::IceConfig ice_config = ParseIceConfig(modified_config); |
| 1583 | |
| 1584 | // Apply part of the configuration on the network thread. In theory this |
| 1585 | // shouldn't fail. |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1586 | if (!network_thread()->BlockingCall( |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 1587 | [this, needs_ice_restart, &ice_config, &stun_servers, &turn_servers, |
| 1588 | &modified_config, has_local_description] { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1589 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 1590 | // As described in JSEP, calling setConfiguration with new ICE |
| 1591 | // servers or candidate policy must set a "needs-ice-restart" bit so |
| 1592 | // that the next offer triggers an ICE restart which will pick up |
| 1593 | // the changes. |
| 1594 | if (needs_ice_restart) |
| 1595 | transport_controller_->SetNeedsIceRestartFlag(); |
| 1596 | |
| 1597 | transport_controller_->SetIceConfig(ice_config); |
Tommi | aea49c9 | 2023-10-22 11:00:14 | [diff] [blame] | 1598 | transport_controller_->SetActiveResetSrtpParams( |
| 1599 | modified_config.active_reset_srtp_params); |
Niels Möller | 4bab23f | 2021-01-18 08:24:33 | [diff] [blame] | 1600 | return ReconfigurePortAllocator_n( |
| 1601 | stun_servers, turn_servers, modified_config.type, |
| 1602 | modified_config.ice_candidate_pool_size, |
| 1603 | modified_config.GetTurnPortPrunePolicy(), |
| 1604 | modified_config.turn_customizer, |
| 1605 | modified_config.stun_candidate_keepalive_interval, |
| 1606 | has_local_description); |
| 1607 | })) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1608 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 1609 | "Failed to apply configuration to PortAllocator."); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1610 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 15:18:11 | [diff] [blame] | 1611 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1612 | configuration_ = modified_config; |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 1613 | return RTCError::OK(); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 1614 | } |
| 1615 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1616 | bool PeerConnection::AddIceCandidate( |
| 1617 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1618 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Philipp Hancke | a09b921 | 2022-06-22 05:41:22 | [diff] [blame] | 1619 | ClearStatsCache(); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1620 | return sdp_handler_->AddIceCandidate(ice_candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1621 | } |
| 1622 | |
Henrik Boström | ee6f4f6 | 2019-11-06 11:36:12 | [diff] [blame] | 1623 | void PeerConnection::AddIceCandidate( |
| 1624 | std::unique_ptr<IceCandidateInterface> candidate, |
| 1625 | std::function<void(RTCError)> callback) { |
| 1626 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Philipp Hancke | a09b921 | 2022-06-22 05:41:22 | [diff] [blame] | 1627 | sdp_handler_->AddIceCandidate(std::move(candidate), |
Harald Alvestrand | a654437 | 2023-11-13 09:33:56 | [diff] [blame] | 1628 | [this, callback](RTCError result) { |
Philipp Hancke | a09b921 | 2022-06-22 05:41:22 | [diff] [blame] | 1629 | ClearStatsCache(); |
| 1630 | callback(result); |
| 1631 | }); |
Henrik Boström | ee6f4f6 | 2019-11-06 11:36:12 | [diff] [blame] | 1632 | } |
| 1633 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 1634 | bool PeerConnection::RemoveIceCandidates( |
| 1635 | const std::vector<cricket::Candidate>& candidates) { |
| 1636 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 1637 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1638 | return sdp_handler_->RemoveIceCandidates(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 1639 | } |
| 1640 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1641 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 1642 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1643 | return worker_thread()->BlockingCall([&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1644 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1645 | RTC_DCHECK_RUN_ON(worker_thread()); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1646 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1647 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 1648 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 1649 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1650 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 1651 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1652 | "min_bitrate_bps <= 0"); |
| 1653 | } |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1654 | if (has_start) { |
| 1655 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1656 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1657 | "start_bitrate_bps < min_bitrate_bps"); |
| 1658 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1659 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1660 | "curent_bitrate_bps < 0"); |
| 1661 | } |
| 1662 | } |
| 1663 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1664 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1665 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 1666 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1667 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 1668 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1669 | "max_bitrate_bps < min_bitrate_bps"); |
| 1670 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 1671 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1672 | "max_bitrate_bps < 0"); |
| 1673 | } |
| 1674 | } |
| 1675 | |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1676 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 17:31:54 | [diff] [blame] | 1677 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 1678 | |
| 1679 | return RTCError::OK(); |
| 1680 | } |
| 1681 | |
Per K | 39ac25d | 2024-02-07 13:16:20 | [diff] [blame] | 1682 | void PeerConnection::ReconfigureBandwidthEstimation( |
| 1683 | const BandwidthEstimationSettings& settings) { |
| 1684 | worker_thread()->PostTask(SafeTask(worker_thread_safety_, [this, settings]() { |
| 1685 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1686 | call_->GetTransportControllerSend()->ReconfigureBandwidthEstimation( |
| 1687 | settings); |
| 1688 | })); |
| 1689 | } |
| 1690 | |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1691 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 1692 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1693 | worker_thread()->BlockingCall( |
| 1694 | [this, playout] { SetAudioPlayout(playout); }); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1695 | return; |
| 1696 | } |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 1697 | auto audio_state = context_->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1698 | audio_state->SetPlayout(playout); |
| 1699 | } |
| 1700 | |
| 1701 | void PeerConnection::SetAudioRecording(bool recording) { |
| 1702 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1703 | worker_thread()->BlockingCall( |
| 1704 | [this, recording] { SetAudioRecording(recording); }); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1705 | return; |
| 1706 | } |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 1707 | auto audio_state = context_->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 1708 | audio_state->SetRecording(recording); |
| 1709 | } |
| 1710 | |
Henrik Boström | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 1711 | void PeerConnection::AddAdaptationResource( |
| 1712 | rtc::scoped_refptr<Resource> resource) { |
| 1713 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1714 | return worker_thread()->BlockingCall( |
| 1715 | [this, resource]() { return AddAdaptationResource(resource); }); |
Henrik Boström | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 1716 | } |
| 1717 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1718 | if (!call_) { |
| 1719 | // The PeerConnection has been closed. |
| 1720 | return; |
| 1721 | } |
| 1722 | call_->AddAdaptationResource(resource); |
| 1723 | } |
| 1724 | |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1725 | bool PeerConnection::ConfiguredForMedia() const { |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 1726 | return context_->media_engine(); |
Harald Alvestrand | 35ba0c5 | 2022-05-05 07:37:41 | [diff] [blame] | 1727 | } |
| 1728 | |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 1729 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 1730 | int64_t output_period_ms) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1731 | return worker_thread()->BlockingCall( |
Danil Chapovalov | 116ffe7 | 2019-09-05 08:21:11 | [diff] [blame] | 1732 | [this, output = std::move(output), output_period_ms]() mutable { |
| 1733 | return StartRtcEventLog_w(std::move(output), output_period_ms); |
| 1734 | }); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 1735 | } |
| 1736 | |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 1737 | bool PeerConnection::StartRtcEventLog( |
| 1738 | std::unique_ptr<RtcEventLogOutput> output) { |
Lionel Koenig | 0606eaf | 2023-06-27 08:46:19 | [diff] [blame] | 1739 | int64_t output_period_ms = 5000; |
| 1740 | if (trials().IsDisabled("WebRTC-RtcEventLogNewFormat")) { |
Harald Alvestrand | a654437 | 2023-11-13 09:33:56 | [diff] [blame] | 1741 | output_period_ms = RtcEventLog::kImmediateOutput; |
Niels Möller | 695cf6a | 2019-05-13 10:27:23 | [diff] [blame] | 1742 | } |
| 1743 | return StartRtcEventLog(std::move(output), output_period_ms); |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 1744 | } |
| 1745 | |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 1746 | void PeerConnection::StopRtcEventLog() { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1747 | worker_thread()->BlockingCall([this] { StopRtcEventLog_w(); }); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 1748 | } |
| 1749 | |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 1750 | rtc::scoped_refptr<DtlsTransportInterface> |
| 1751 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
Tomas Gunnarsson | 2aeab5e | 2021-02-23 20:36:14 | [diff] [blame] | 1752 | RTC_DCHECK_RUN_ON(network_thread()); |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 1753 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 1754 | } |
| 1755 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 1756 | rtc::scoped_refptr<DtlsTransport> |
| 1757 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 1758 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1759 | // TODO(bugs.webrtc.org/9987): Avoid the thread jump. |
| 1760 | // This might be done by caching the value on the signaling thread. |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1761 | return network_thread()->BlockingCall([this, mid]() { |
| 1762 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1763 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 1764 | }); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 1765 | } |
| 1766 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 1767 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 1768 | const { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1769 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1770 | if (!sctp_mid_n_) |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 1771 | return nullptr; |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1772 | |
| 1773 | return transport_controller_->GetSctpTransport(*sctp_mid_n_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 1774 | } |
| 1775 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1776 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1777 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1778 | return sdp_handler_->local_description(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1782 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1783 | return sdp_handler_->remote_description(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1784 | } |
| 1785 | |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1786 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 1787 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1788 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1789 | return sdp_handler_->current_local_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 1793 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1794 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1795 | return sdp_handler_->current_remote_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 1799 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1800 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1801 | return sdp_handler_->pending_local_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 1805 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 1806 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1807 | return sdp_handler_->pending_remote_description(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 1808 | } |
| 1809 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1810 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1811 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1812 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1813 | |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 1814 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 1815 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1816 | if (IsClosed()) { |
| 1817 | return; |
| 1818 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1819 | // Update stats here so that we have the most recent stats for tracks and |
| 1820 | // streams before the channels are closed. |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 1821 | legacy_stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1822 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1823 | ice_connection_state_ = PeerConnectionInterface::kIceConnectionClosed; |
| 1824 | Observer()->OnIceConnectionChange(ice_connection_state_); |
| 1825 | standardized_ice_connection_state_ = |
| 1826 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
| 1827 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 1828 | Observer()->OnConnectionChange(connection_state_); |
| 1829 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1830 | sdp_handler_->Close(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1831 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1832 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1833 | |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1834 | if (ConfiguredForMedia()) { |
| 1835 | for (const auto& transceiver : rtp_manager()->transceivers()->List()) { |
| 1836 | transceiver->internal()->SetPeerConnectionClosed(); |
| 1837 | if (!transceiver->stopped()) |
| 1838 | transceiver->StopInternal(); |
| 1839 | } |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1840 | } |
Steve Anton | 25cfeb9 | 2018-04-26 18:44:00 | [diff] [blame] | 1841 | // Ensure that all asynchronous stats requests are completed before destroying |
| 1842 | // the transport controller below. |
| 1843 | if (stats_collector_) { |
| 1844 | stats_collector_->WaitForPendingRequest(); |
| 1845 | } |
| 1846 | |
| 1847 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 1848 | // the last stats request can still read from the channels. |
Tommi | 2919075 | 2023-10-21 14:50:09 | [diff] [blame] | 1849 | // TODO(tommi): The voice/video channels will be partially uninitialized on |
| 1850 | // the network thread (see `RtpTransceiver::ClearChannel`), partially on the |
| 1851 | // worker thread (see `PushNewMediaChannelAndDeleteChannel`) and then |
| 1852 | // eventually freed on the signaling thread. |
| 1853 | // It would be good to combine those steps with the teardown steps here. |
| 1854 | sdp_handler_->DestroyMediaChannels(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1855 | |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 1856 | // The event log is used in the transport controller, which must be outlived |
| 1857 | // by the former. CreateOffer by the peer connection is implemented |
| 1858 | // asynchronously and if the peer connection is closed without resetting the |
| 1859 | // WebRTC session description factory, the session description factory would |
| 1860 | // call the transport controller. |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1861 | sdp_handler_->ResetSessionDescFactory(); |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 1862 | if (ConfiguredForMedia()) { |
| 1863 | rtp_manager_->Close(); |
| 1864 | } |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 1865 | |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1866 | network_thread()->BlockingCall([this] { |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 1867 | RTC_DCHECK_RUN_ON(network_thread()); |
Tommi | 2919075 | 2023-10-21 14:50:09 | [diff] [blame] | 1868 | TeardownDataChannelTransport_n({}); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1869 | transport_controller_.reset(); |
| 1870 | port_allocator_->DiscardCandidatePool(); |
| 1871 | if (network_thread_safety_) { |
| 1872 | network_thread_safety_->SetNotAlive(); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 1873 | } |
| 1874 | }); |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 1875 | |
Tommi | 2919075 | 2023-10-21 14:50:09 | [diff] [blame] | 1876 | sctp_mid_s_.reset(); |
| 1877 | SetSctpTransportName(""); |
| 1878 | |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 1879 | worker_thread()->BlockingCall([this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1880 | RTC_DCHECK_RUN_ON(worker_thread()); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 1881 | worker_thread_safety_->SetNotAlive(); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1882 | call_.reset(); |
Danil Chapovalov | 49c35d3 | 2023-11-27 11:55:39 | [diff] [blame] | 1883 | StopRtcEventLog_w(); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1884 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1885 | ReportUsagePattern(); |
Tomas Gunnarsson | 2efb8a5 | 2021-04-01 14:26:57 | [diff] [blame] | 1886 | |
| 1887 | // Signal shutdown to the sdp handler. This invalidates weak pointers for |
| 1888 | // internal pending callbacks. |
| 1889 | sdp_handler_->PrepareForShutdown(); |
Tommi | 1f708ef | 2023-03-31 16:40:50 | [diff] [blame] | 1890 | data_channel_controller_.PrepareForShutdown(); |
| 1891 | |
| 1892 | // The .h file says that observer can be discarded after close() returns. |
| 1893 | // Make sure this is true. |
| 1894 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1895 | } |
| 1896 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1897 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1898 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1899 | if (ice_connection_state_ == new_state) { |
| 1900 | return; |
| 1901 | } |
| 1902 | |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 1903 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1904 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1905 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 1906 | return; |
| 1907 | } |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1908 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1909 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 1910 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1911 | RTC_DCHECK(ice_connection_state_ != |
| 1912 | PeerConnectionInterface::kIceConnectionClosed); |
| 1913 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1914 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 1915 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1916 | } |
| 1917 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1918 | void PeerConnection::SetStandardizedIceConnectionState( |
| 1919 | PeerConnectionInterface::IceConnectionState new_state) { |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 1920 | if (standardized_ice_connection_state_ == new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1921 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | if (IsClosed()) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1925 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState " |
| 1929 | << standardized_ice_connection_state_ << " => " << new_state; |
| 1930 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1931 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 10:25:14 | [diff] [blame] | 1932 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1933 | } |
| 1934 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1935 | void PeerConnection::SetConnectionState( |
| 1936 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1937 | if (connection_state_ == new_state) |
| 1938 | return; |
| 1939 | if (IsClosed()) |
| 1940 | return; |
| 1941 | connection_state_ = new_state; |
| 1942 | Observer()->OnConnectionChange(new_state); |
Philipp Hancke | bb8f32f | 2021-02-04 20:50:50 | [diff] [blame] | 1943 | |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 1944 | // The first connection state change to connected happens once per |
| 1945 | // connection which makes it a good point to report metrics. |
Philipp Hancke | cd0373f | 2021-02-24 10:04:30 | [diff] [blame] | 1946 | if (new_state == PeerConnectionState::kConnected && !was_ever_connected_) { |
| 1947 | was_ever_connected_ = true; |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 1948 | ReportFirstConnectUsageMetrics(); |
Philipp Hancke | bb8f32f | 2021-02-04 20:50:50 | [diff] [blame] | 1949 | } |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1950 | } |
| 1951 | |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 1952 | void PeerConnection::ReportFirstConnectUsageMetrics() { |
| 1953 | // Record bundle-policy from configuration. Done here from |
| 1954 | // connectionStateChange to limit to actually established connections. |
| 1955 | BundlePolicyUsage policy = kBundlePolicyUsageMax; |
| 1956 | switch (configuration_.bundle_policy) { |
| 1957 | case kBundlePolicyBalanced: |
| 1958 | policy = kBundlePolicyUsageBalanced; |
| 1959 | break; |
| 1960 | case kBundlePolicyMaxBundle: |
| 1961 | policy = kBundlePolicyUsageMaxBundle; |
| 1962 | break; |
| 1963 | case kBundlePolicyMaxCompat: |
| 1964 | policy = kBundlePolicyUsageMaxCompat; |
| 1965 | break; |
| 1966 | } |
| 1967 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.BundlePolicy", policy, |
| 1968 | kBundlePolicyUsageMax); |
| 1969 | |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 1970 | // Record whether there was a local or remote provisional answer. |
| 1971 | ProvisionalAnswerUsage pranswer = kProvisionalAnswerNotUsed; |
| 1972 | if (local_description()->GetType() == SdpType::kPrAnswer) { |
| 1973 | pranswer = kProvisionalAnswerLocal; |
| 1974 | } else if (remote_description()->GetType() == SdpType::kPrAnswer) { |
| 1975 | pranswer = kProvisionalAnswerRemote; |
| 1976 | } |
| 1977 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.ProvisionalAnswer", pranswer, |
| 1978 | kProvisionalAnswerMax); |
| 1979 | |
Philipp Hancke | 6f22eb5 | 2022-07-14 09:38:44 | [diff] [blame] | 1980 | auto transport_infos = remote_description()->description()->transport_infos(); |
Philipp Hancke | 0967247 | 2023-11-15 14:23:06 | [diff] [blame] | 1981 | if (!transport_infos.empty()) { |
| 1982 | // Record the number of valid / invalid ice-ufrag. We do allow certain |
| 1983 | // non-spec ice-char for backward-compat reasons. At this point we know |
| 1984 | // that the ufrag/pwd consists of a valid ice-char or one of the four |
| 1985 | // not allowed characters since we have passed the IsIceChar check done |
| 1986 | // by the p2p transport description on setRemoteDescription calls. |
Philipp Hancke | 6f22eb5 | 2022-07-14 09:38:44 | [diff] [blame] | 1987 | auto ice_parameters = transport_infos[0].description.GetIceParameters(); |
| 1988 | auto is_invalid_char = [](char c) { |
| 1989 | return c == '-' || c == '=' || c == '#' || c == '_'; |
| 1990 | }; |
| 1991 | bool isUsingInvalidIceCharInUfrag = |
| 1992 | absl::c_any_of(ice_parameters.ufrag, is_invalid_char); |
| 1993 | bool isUsingInvalidIceCharInPwd = |
| 1994 | absl::c_any_of(ice_parameters.pwd, is_invalid_char); |
| 1995 | RTC_HISTOGRAM_BOOLEAN( |
| 1996 | "WebRTC.PeerConnection.ValidIceChars", |
| 1997 | !(isUsingInvalidIceCharInUfrag || isUsingInvalidIceCharInPwd)); |
Philipp Hancke | 0967247 | 2023-11-15 14:23:06 | [diff] [blame] | 1998 | |
| 1999 | // Record whether the hash algorithm of the first transport's |
| 2000 | // DTLS fingerprint is still using SHA-1. |
| 2001 | if (transport_infos[0].description.identity_fingerprint) { |
| 2002 | RTC_HISTOGRAM_BOOLEAN( |
| 2003 | "WebRTC.PeerConnection.DtlsFingerprintLegacySha1", |
| 2004 | absl::EqualsIgnoreCase( |
| 2005 | transport_infos[0].description.identity_fingerprint->algorithm, |
| 2006 | "sha-1")); |
| 2007 | } |
Philipp Hancke | 6f22eb5 | 2022-07-14 09:38:44 | [diff] [blame] | 2008 | } |
Philipp Hancke | 66efab2 | 2023-01-25 10:37:45 | [diff] [blame] | 2009 | |
| 2010 | // Record RtcpMuxPolicy setting. |
| 2011 | RtcpMuxPolicyUsage rtcp_mux_policy = kRtcpMuxPolicyUsageMax; |
| 2012 | switch (configuration_.rtcp_mux_policy) { |
| 2013 | case kRtcpMuxPolicyNegotiate: |
| 2014 | rtcp_mux_policy = kRtcpMuxPolicyUsageNegotiate; |
| 2015 | break; |
| 2016 | case kRtcpMuxPolicyRequire: |
| 2017 | rtcp_mux_policy = kRtcpMuxPolicyUsageRequire; |
| 2018 | break; |
| 2019 | } |
| 2020 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.RtcpMuxPolicy", |
| 2021 | rtcp_mux_policy, kRtcpMuxPolicyUsageMax); |
Philipp Hancke | 9799fe0 | 2022-07-06 07:26:41 | [diff] [blame] | 2022 | } |
| 2023 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2024 | void PeerConnection::OnIceGatheringChange( |
| 2025 | PeerConnectionInterface::IceGatheringState new_state) { |
| 2026 | if (IsClosed()) { |
| 2027 | return; |
| 2028 | } |
| 2029 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2030 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2031 | } |
| 2032 | |
jbauch | 81bf7b0 | 2017-03-25 15:31:12 | [diff] [blame] | 2033 | void PeerConnection::OnIceCandidate( |
| 2034 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 2035 | if (IsClosed()) { |
| 2036 | return; |
| 2037 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2038 | ReportIceCandidateCollected(candidate->candidate()); |
Philipp Hancke | 1fe14f2 | 2022-06-17 09:34:31 | [diff] [blame] | 2039 | ClearStatsCache(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2040 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2041 | } |
| 2042 | |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 2043 | void PeerConnection::OnIceCandidateError(const std::string& address, |
| 2044 | int port, |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2045 | const std::string& url, |
| 2046 | int error_code, |
| 2047 | const std::string& error_text) { |
| 2048 | if (IsClosed()) { |
| 2049 | return; |
| 2050 | } |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 2051 | Observer()->OnIceCandidateError(address, port, url, error_code, error_text); |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2052 | } |
| 2053 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 2054 | void PeerConnection::OnIceCandidatesRemoved( |
| 2055 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 2056 | if (IsClosed()) { |
| 2057 | return; |
| 2058 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2059 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 2060 | } |
| 2061 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 2062 | void PeerConnection::OnSelectedCandidatePairChanged( |
| 2063 | const cricket::CandidatePairChangeEvent& event) { |
| 2064 | if (IsClosed()) { |
| 2065 | return; |
| 2066 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2067 | |
Tommi | 3b2b2af | 2024-01-17 13:19:41 | [diff] [blame] | 2068 | if (event.selected_candidate_pair.local_candidate().is_local() && |
| 2069 | event.selected_candidate_pair.remote_candidate().is_local()) { |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 2070 | NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED); |
| 2071 | } |
| 2072 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 2073 | Observer()->OnIceSelectedCandidatePairChanged(event); |
| 2074 | } |
| 2075 | |
Tommi | 840cf78 | 2023-10-21 14:47:56 | [diff] [blame] | 2076 | bool PeerConnection::CreateDataChannelTransport(absl::string_view mid) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2077 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Tommi | 840cf78 | 2023-10-21 14:47:56 | [diff] [blame] | 2078 | RTC_DCHECK(!sctp_mid().has_value() || mid == sctp_mid().value()); |
| 2079 | RTC_LOG(LS_INFO) << "Creating data channel, mid=" << mid; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 2080 | |
Tommi | 840cf78 | 2023-10-21 14:47:56 | [diff] [blame] | 2081 | absl::optional<std::string> transport_name = |
| 2082 | network_thread()->BlockingCall([&] { |
| 2083 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2084 | return SetupDataChannelTransport_n(mid); |
| 2085 | }); |
| 2086 | if (!transport_name) |
| 2087 | return false; |
| 2088 | |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2089 | sctp_mid_s_ = std::string(mid); |
Tommi | 840cf78 | 2023-10-21 14:47:56 | [diff] [blame] | 2090 | SetSctpTransportName(transport_name.value()); |
| 2091 | |
| 2092 | return true; |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2093 | } |
| 2094 | |
Tommi | 840cf78 | 2023-10-21 14:47:56 | [diff] [blame] | 2095 | void PeerConnection::DestroyDataChannelTransport(RTCError error) { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2096 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Tommi | 840cf78 | 2023-10-21 14:47:56 | [diff] [blame] | 2097 | network_thread()->BlockingCall([&] { |
| 2098 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2099 | TeardownDataChannelTransport_n(error); |
| 2100 | }); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2101 | sctp_mid_s_.reset(); |
Henrik Boström | 46053e4 | 2023-01-20 12:18:53 | [diff] [blame] | 2102 | SetSctpTransportName(""); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2103 | } |
| 2104 | |
Tommi | d2afbaf | 2023-03-02 09:51:16 | [diff] [blame] | 2105 | void PeerConnection::OnSctpDataChannelStateChanged( |
Tommi | 5654898 | 2023-03-27 16:07:34 | [diff] [blame] | 2106 | int channel_id, |
Tommi | d2afbaf | 2023-03-02 09:51:16 | [diff] [blame] | 2107 | DataChannelInterface::DataState state) { |
| 2108 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2109 | if (stats_collector_) |
Tommi | 5654898 | 2023-03-27 16:07:34 | [diff] [blame] | 2110 | stats_collector_->OnSctpDataChannelStateChanged(channel_id, state); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2111 | } |
| 2112 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2113 | PeerConnection::InitializePortAllocatorResult |
| 2114 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 2115 | const cricket::ServerAddresses& stun_servers, |
| 2116 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2117 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2118 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2119 | |
Taylor Brandstetter | f8e6577 | 2016-06-28 00:20:15 | [diff] [blame] | 2120 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2121 | // To handle both internal and externally created port allocator, we will |
| 2122 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2123 | int port_allocator_flags = port_allocator_->flags(); |
| 2124 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 2125 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 2126 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Henrik Boström | a445e6a | 2022-12-01 09:25:12 | [diff] [blame] | 2127 | if (trials().IsDisabled("WebRTC-IPv6Default")) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2128 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2129 | } |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 2130 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2131 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2132 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 2133 | } |
| 2134 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2135 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2136 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2137 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2138 | } |
| 2139 | |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 2140 | if (configuration.candidate_network_policy == |
| 2141 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2142 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2143 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 2144 | } |
| 2145 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 2146 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2147 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 2148 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 2149 | } |
| 2150 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2151 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2152 | // No step delay is used while allocating ports. |
| 2153 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2154 | port_allocator_->SetCandidateFilter( |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2155 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 2156 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2157 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 2158 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2159 | for (auto& turn_server : turn_servers_copy) { |
| 2160 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 2161 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2162 | // Call this last since it may create pooled allocator sessions using the |
| 2163 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 2164 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2165 | stun_servers, std::move(turn_servers_copy), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2166 | configuration.ice_candidate_pool_size, |
| 2167 | configuration.GetTurnPortPrunePolicy(), configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 2168 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 2169 | |
| 2170 | InitializePortAllocatorResult res; |
| 2171 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 2172 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2173 | } |
| 2174 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 2175 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 2176 | const cricket::ServerAddresses& stun_servers, |
| 2177 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 2178 | IceTransportsType type, |
| 2179 | int candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2180 | PortPrunePolicy turn_port_prune_policy, |
Harald Alvestrand | a654437 | 2023-11-13 09:33:56 | [diff] [blame] | 2181 | TurnCustomizer* turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 2182 | absl::optional<int> stun_candidate_keepalive_interval, |
| 2183 | bool have_local_description) { |
Harald Alvestrand | f598e49 | 2020-11-04 05:54:10 | [diff] [blame] | 2184 | RTC_DCHECK_RUN_ON(network_thread()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2185 | port_allocator_->SetCandidateFilter( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 2186 | ConvertIceTransportTypeToCandidateFilter(type)); |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2187 | // Add the custom tls turn servers if they exist. |
| 2188 | auto turn_servers_copy = turn_servers; |
| 2189 | for (auto& turn_server : turn_servers_copy) { |
| 2190 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 2191 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2192 | // Call this last since it may create pooled allocator sessions using the |
| 2193 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-13 02:49:32 | [diff] [blame] | 2194 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 2195 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2196 | turn_port_prune_policy, turn_customizer, |
| 2197 | stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2198 | } |
| 2199 | |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 2200 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 2201 | std::unique_ptr<RtcEventLogOutput> output, |
| 2202 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 2203 | RTC_DCHECK_RUN_ON(worker_thread()); |
Danil Chapovalov | 49c35d3 | 2023-11-27 11:55:39 | [diff] [blame] | 2204 | if (!worker_thread_safety_->alive()) { |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 2205 | return false; |
| 2206 | } |
Danil Chapovalov | 3bdb49b | 2023-11-30 07:59:39 | [diff] [blame] | 2207 | return env_.event_log().StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 2211 | RTC_DCHECK_RUN_ON(worker_thread()); |
Danil Chapovalov | 3bdb49b | 2023-11-30 07:59:39 | [diff] [blame] | 2212 | env_.event_log().StopLogging(); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 2213 | } |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 2214 | |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 2215 | absl::optional<rtc::SSLRole> PeerConnection::GetSctpSslRole_n() { |
Tommi | c61eee2 | 2023-03-22 07:25:38 | [diff] [blame] | 2216 | RTC_DCHECK_RUN_ON(network_thread()); |
Tommi | 335d084 | 2023-03-25 09:56:18 | [diff] [blame] | 2217 | return sctp_mid_n_ ? transport_controller_->GetDtlsRole(*sctp_mid_n_) |
| 2218 | : absl::nullopt; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 2222 | rtc::SSLRole* role) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 2223 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2224 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2225 | RTC_LOG(LS_INFO) |
| 2226 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 2227 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2228 | return false; |
| 2229 | } |
| 2230 | |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 2231 | auto dtls_role = network_thread()->BlockingCall([this, content_name]() { |
| 2232 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2233 | return transport_controller_->GetDtlsRole(content_name); |
| 2234 | }); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2235 | if (dtls_role) { |
| 2236 | *role = *dtls_role; |
| 2237 | return true; |
| 2238 | } |
| 2239 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2240 | } |
| 2241 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2242 | bool PeerConnection::GetTransportDescription( |
| 2243 | const SessionDescription* description, |
| 2244 | const std::string& content_name, |
| 2245 | cricket::TransportDescription* tdesc) { |
| 2246 | if (!description || !tdesc) { |
| 2247 | return false; |
| 2248 | } |
| 2249 | const TransportInfo* transport_info = |
| 2250 | description->GetTransportInfoByName(content_name); |
| 2251 | if (!transport_info) { |
| 2252 | return false; |
| 2253 | } |
| 2254 | *tdesc = transport_info->description; |
| 2255 | return true; |
| 2256 | } |
| 2257 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 2258 | std::vector<DataChannelStats> PeerConnection::GetDataChannelStats() const { |
Tommi | f9e13f8 | 2023-04-06 19:21:45 | [diff] [blame] | 2259 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 2260 | return data_channel_controller_.GetDataChannelStats(); |
| 2261 | } |
| 2262 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 2263 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 2264 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 2265 | if (sctp_mid_s_ && transport_controller_copy_) |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2266 | return sctp_transport_name_s_; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 2267 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2268 | } |
| 2269 | |
Henrik Boström | 46053e4 | 2023-01-20 12:18:53 | [diff] [blame] | 2270 | void PeerConnection::SetSctpTransportName(std::string sctp_transport_name) { |
| 2271 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2272 | sctp_transport_name_s_ = std::move(sctp_transport_name); |
| 2273 | ClearStatsCache(); |
| 2274 | } |
| 2275 | |
Tomas Gunnarsson | bfd9ba8 | 2021-04-18 09:55:57 | [diff] [blame] | 2276 | absl::optional<std::string> PeerConnection::sctp_mid() const { |
| 2277 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2278 | return sctp_mid_s_; |
| 2279 | } |
| 2280 | |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 2281 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2282 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2283 | if (!network_thread_safety_->alive()) |
| 2284 | return {}; |
Philipp Hancke | 6e57ca2 | 2022-06-09 13:58:18 | [diff] [blame] | 2285 | cricket::CandidateStatsList candidate_stats_list; |
| 2286 | port_allocator_->GetCandidateStatsFromPooledSessions(&candidate_stats_list); |
| 2287 | return candidate_stats_list; |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 2288 | } |
| 2289 | |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 2290 | std::map<std::string, cricket::TransportStats> |
| 2291 | PeerConnection::GetTransportStatsByNames( |
| 2292 | const std::set<std::string>& transport_names) { |
Markus Handell | 518669d | 2021-06-07 11:30:46 | [diff] [blame] | 2293 | TRACE_EVENT0("webrtc", "PeerConnection::GetTransportStatsByNames"); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 2294 | RTC_DCHECK_RUN_ON(network_thread()); |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2295 | if (!network_thread_safety_->alive()) |
| 2296 | return {}; |
| 2297 | |
| 2298 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 2299 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 2300 | for (const std::string& transport_name : transport_names) { |
| 2301 | cricket::TransportStats transport_stats; |
| 2302 | bool success = |
| 2303 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 2304 | if (success) { |
| 2305 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 2306 | } else { |
| 2307 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 2308 | << transport_name; |
| 2309 | } |
| 2310 | } |
| 2311 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | bool PeerConnection::GetLocalCertificate( |
| 2315 | const std::string& transport_name, |
| 2316 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2317 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2318 | if (!network_thread_safety_->alive() || !certificate) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2319 | return false; |
| 2320 | } |
| 2321 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 2322 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2323 | } |
| 2324 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 2325 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2326 | const std::string& transport_name) { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2327 | RTC_DCHECK_RUN_ON(network_thread()); |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 2328 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2329 | } |
| 2330 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2331 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
Karl Wiberg | f73f7d6 | 2019-04-08 13:36:53 | [diff] [blame] | 2332 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2333 | return sdp_handler_->IceRestartPending(content_name); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2334 | } |
| 2335 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2336 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 2337 | return network_thread()->BlockingCall([this, &content_name] { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2338 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2339 | return transport_controller_->NeedsIceRestart(content_name); |
| 2340 | }); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2341 | } |
| 2342 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2343 | void PeerConnection::OnTransportControllerConnectionState( |
| 2344 | cricket::IceConnectionState state) { |
| 2345 | switch (state) { |
| 2346 | case cricket::kIceConnectionConnecting: |
| 2347 | // If the current state is Connected or Completed, then there were |
| 2348 | // writable channels but now there are not, so the next state must |
| 2349 | // be Disconnected. |
| 2350 | // kIceConnectionConnecting is currently used as the default, |
| 2351 | // un-connected state by the TransportController, so its only use is |
| 2352 | // detecting disconnections. |
| 2353 | if (ice_connection_state_ == |
| 2354 | PeerConnectionInterface::kIceConnectionConnected || |
| 2355 | ice_connection_state_ == |
| 2356 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 2357 | SetIceConnectionState( |
| 2358 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 2359 | } |
| 2360 | break; |
| 2361 | case cricket::kIceConnectionFailed: |
| 2362 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 2363 | break; |
| 2364 | case cricket::kIceConnectionConnected: |
| 2365 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 2366 | "all transports are writable."; |
Tommi | dba22d3 | 2023-06-01 14:08:52 | [diff] [blame] | 2367 | { |
| 2368 | std::vector<RtpTransceiverProxyRefPtr> transceivers; |
| 2369 | if (ConfiguredForMedia()) { |
| 2370 | transceivers = rtp_manager()->transceivers()->List(); |
| 2371 | } |
| 2372 | |
| 2373 | network_thread()->PostTask( |
| 2374 | SafeTask(network_thread_safety_, |
| 2375 | [this, transceivers = std::move(transceivers)] { |
| 2376 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2377 | ReportTransportStats(std::move(transceivers)); |
| 2378 | })); |
| 2379 | } |
| 2380 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2381 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 2382 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 2383 | break; |
| 2384 | case cricket::kIceConnectionCompleted: |
| 2385 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 2386 | "all transports are complete."; |
| 2387 | if (ice_connection_state_ != |
| 2388 | PeerConnectionInterface::kIceConnectionConnected) { |
| 2389 | // If jumping directly from "checking" to "connected", |
| 2390 | // signal "connected" first. |
| 2391 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 2392 | } |
| 2393 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2394 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2395 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2396 | break; |
| 2397 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2398 | RTC_DCHECK_NOTREACHED(); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2399 | } |
| 2400 | } |
| 2401 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2402 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 2403 | const std::string& transport_name, |
| 2404 | const cricket::Candidates& candidates) { |
Tomas Gunnarsson | 20f7456 | 2021-02-04 09:22:50 | [diff] [blame] | 2405 | // TODO(bugs.webrtc.org/12427): Expect this to come in on the network thread |
| 2406 | // (not signaling as it currently does), handle appropriately. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2407 | int sdp_mline_index; |
| 2408 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2409 | RTC_LOG(LS_ERROR) |
| 2410 | << "OnTransportControllerCandidatesGathered: content name " |
| 2411 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2412 | return; |
| 2413 | } |
| 2414 | |
| 2415 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 2416 | citer != candidates.end(); ++citer) { |
| 2417 | // Use transport_name as the candidate media id. |
| 2418 | std::unique_ptr<JsepIceCandidate> candidate( |
| 2419 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2420 | sdp_handler_->AddLocalIceCandidate(candidate.get()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2421 | OnIceCandidate(std::move(candidate)); |
| 2422 | } |
| 2423 | } |
| 2424 | |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2425 | void PeerConnection::OnTransportControllerCandidateError( |
| 2426 | const cricket::IceCandidateErrorEvent& event) { |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 2427 | OnIceCandidateError(event.address, event.port, event.url, event.error_code, |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 2428 | event.error_text); |
| 2429 | } |
| 2430 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2431 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 2432 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2433 | // Sanity check. |
| 2434 | for (const cricket::Candidate& candidate : candidates) { |
| 2435 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2436 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 2437 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2438 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2439 | return; |
| 2440 | } |
| 2441 | } |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2442 | sdp_handler_->RemoveLocalIceCandidates(candidates); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2443 | OnIceCandidatesRemoved(candidates); |
| 2444 | } |
| 2445 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 2446 | void PeerConnection::OnTransportControllerCandidateChanged( |
| 2447 | const cricket::CandidatePairChangeEvent& event) { |
| 2448 | OnSelectedCandidatePairChanged(event); |
| 2449 | } |
| 2450 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2451 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 2452 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2453 | RTC_HISTOGRAM_ENUMERATION( |
| 2454 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 2455 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2456 | } |
| 2457 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2458 | // Returns the media index for a local ice candidate given the content name. |
| 2459 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 2460 | const std::string& content_name, |
| 2461 | int* sdp_mline_index) { |
| 2462 | if (!local_description() || !sdp_mline_index) { |
| 2463 | return false; |
| 2464 | } |
| 2465 | |
| 2466 | bool content_found = false; |
| 2467 | const ContentInfos& contents = local_description()->description()->contents(); |
| 2468 | for (size_t index = 0; index < contents.size(); ++index) { |
| 2469 | if (contents[index].name == content_name) { |
| 2470 | *sdp_mline_index = static_cast<int>(index); |
| 2471 | content_found = true; |
| 2472 | break; |
| 2473 | } |
| 2474 | } |
| 2475 | return content_found; |
| 2476 | } |
| 2477 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2478 | Call::Stats PeerConnection::GetCallStats() { |
| 2479 | if (!worker_thread()->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 2480 | return worker_thread()->BlockingCall([this] { return GetCallStats(); }); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2481 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2482 | RTC_DCHECK_RUN_ON(worker_thread()); |
Henrik Boström | e88c95e | 2020-07-08 09:18:50 | [diff] [blame] | 2483 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2484 | if (call_) { |
| 2485 | return call_->GetStats(); |
| 2486 | } else { |
| 2487 | return Call::Stats(); |
| 2488 | } |
| 2489 | } |
| 2490 | |
Fredrik Hernqvist | efbe753 | 2023-01-13 15:42:36 | [diff] [blame] | 2491 | absl::optional<AudioDeviceModule::Stats> PeerConnection::GetAudioDeviceStats() { |
| 2492 | if (context_->media_engine()) { |
| 2493 | return context_->media_engine()->voice().GetAudioDeviceStats(); |
| 2494 | } |
| 2495 | return absl::nullopt; |
| 2496 | } |
| 2497 | |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2498 | absl::optional<std::string> PeerConnection::SetupDataChannelTransport_n( |
| 2499 | absl::string_view mid) { |
| 2500 | sctp_mid_n_ = std::string(mid); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2501 | DataChannelTransportInterface* transport = |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2502 | transport_controller_->GetDataChannelTransport(*sctp_mid_n_); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2503 | if (!transport) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 2504 | RTC_LOG(LS_ERROR) |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 2505 | << "Data channel transport is not available for data channels, mid=" |
| 2506 | << mid; |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2507 | sctp_mid_n_ = absl::nullopt; |
| 2508 | return absl::nullopt; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2509 | } |
| 2510 | |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2511 | absl::optional<std::string> transport_name; |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2512 | cricket::DtlsTransportInternal* dtls_transport = |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2513 | transport_controller_->GetDtlsTransport(*sctp_mid_n_); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2514 | if (dtls_transport) { |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2515 | transport_name = dtls_transport->transport_name(); |
| 2516 | } else { |
| 2517 | // Make sure we still set a valid string. |
| 2518 | transport_name = std::string(""); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2519 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2520 | |
Tommi | aa3c9f2 | 2023-04-18 10:19:19 | [diff] [blame] | 2521 | data_channel_controller_.SetupDataChannelTransport_n(transport); |
| 2522 | |
| 2523 | return transport_name; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2524 | } |
| 2525 | |
Tommi | b00d63c | 2023-04-12 17:49:53 | [diff] [blame] | 2526 | void PeerConnection::TeardownDataChannelTransport_n(RTCError error) { |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 2527 | if (sctp_mid_n_) { |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 2528 | // `sctp_mid_` may still be active through an SCTP transport. If not, unset |
Tomas Gunnarsson | d69e070 | 2021-04-07 13:14:43 | [diff] [blame] | 2529 | // it. |
| 2530 | RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid=" |
| 2531 | << *sctp_mid_n_; |
| 2532 | sctp_mid_n_.reset(); |
| 2533 | } |
| 2534 | |
Tommi | b00d63c | 2023-04-12 17:49:53 | [diff] [blame] | 2535 | data_channel_controller_.TeardownDataChannelTransport_n(error); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2536 | } |
| 2537 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2538 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
Henrik Boström | f8187e0 | 2021-04-26 19:04:26 | [diff] [blame] | 2539 | bool PeerConnection::ValidateBundleSettings( |
| 2540 | const SessionDescription* desc, |
| 2541 | const std::map<std::string, const cricket::ContentGroup*>& |
| 2542 | bundle_groups_by_mid) { |
| 2543 | if (bundle_groups_by_mid.empty()) |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2544 | return true; |
| 2545 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2546 | const cricket::ContentInfos& contents = desc->contents(); |
| 2547 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 2548 | citer != contents.end(); ++citer) { |
| 2549 | const cricket::ContentInfo* content = (&*citer); |
| 2550 | RTC_DCHECK(content != NULL); |
Henrik Boström | f8187e0 | 2021-04-26 19:04:26 | [diff] [blame] | 2551 | auto it = bundle_groups_by_mid.find(content->name); |
Philipp Hancke | 32dae4b | 2023-05-12 04:53:55 | [diff] [blame] | 2552 | if (it != bundle_groups_by_mid.end() && |
| 2553 | !(content->rejected || content->bundle_only) && |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 2554 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2555 | if (!HasRtcpMuxEnabled(content)) |
| 2556 | return false; |
| 2557 | } |
| 2558 | } |
| 2559 | // RTCP-MUX is enabled in all the contents. |
| 2560 | return true; |
| 2561 | } |
| 2562 | |
Philipp Hancke | 54b925c | 2021-01-28 08:56:39 | [diff] [blame] | 2563 | void PeerConnection::ReportSdpBundleUsage( |
| 2564 | const SessionDescriptionInterface& remote_description) { |
| 2565 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2566 | |
| 2567 | bool using_bundle = |
| 2568 | remote_description.description()->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 2569 | int num_audio_mlines = 0; |
| 2570 | int num_video_mlines = 0; |
| 2571 | int num_data_mlines = 0; |
| 2572 | for (const ContentInfo& content : |
| 2573 | remote_description.description()->contents()) { |
| 2574 | cricket::MediaType media_type = content.media_description()->type(); |
| 2575 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 2576 | num_audio_mlines += 1; |
| 2577 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 2578 | num_video_mlines += 1; |
| 2579 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
| 2580 | num_data_mlines += 1; |
| 2581 | } |
| 2582 | } |
| 2583 | bool simple = num_audio_mlines <= 1 && num_video_mlines <= 1; |
| 2584 | BundleUsage usage = kBundleUsageMax; |
| 2585 | if (num_audio_mlines == 0 && num_video_mlines == 0) { |
| 2586 | if (num_data_mlines > 0) { |
| 2587 | usage = using_bundle ? kBundleUsageBundleDatachannelOnly |
| 2588 | : kBundleUsageNoBundleDatachannelOnly; |
| 2589 | } else { |
| 2590 | usage = kBundleUsageEmpty; |
| 2591 | } |
Harald Alvestrand | fa67aef | 2021-12-08 14:30:55 | [diff] [blame] | 2592 | } else if (configuration_.sdp_semantics == SdpSemantics::kPlanB_DEPRECATED) { |
Philipp Hancke | 54b925c | 2021-01-28 08:56:39 | [diff] [blame] | 2593 | // In plan-b, simple/complex usage will not show up in the number of |
| 2594 | // m-lines or BUNDLE. |
| 2595 | usage = using_bundle ? kBundleUsageBundlePlanB : kBundleUsageNoBundlePlanB; |
| 2596 | } else { |
| 2597 | if (simple) { |
| 2598 | usage = |
| 2599 | using_bundle ? kBundleUsageBundleSimple : kBundleUsageNoBundleSimple; |
| 2600 | } else { |
| 2601 | usage = using_bundle ? kBundleUsageBundleComplex |
| 2602 | : kBundleUsageNoBundleComplex; |
| 2603 | } |
| 2604 | } |
| 2605 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.BundleUsage", usage, |
| 2606 | kBundleUsageMax); |
| 2607 | } |
| 2608 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 2609 | void PeerConnection::ReportIceCandidateCollected( |
| 2610 | const cricket::Candidate& candidate) { |
| 2611 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
| 2612 | if (candidate.address().IsPrivateIP()) { |
| 2613 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 2614 | } |
| 2615 | if (candidate.address().IsUnresolvedIP()) { |
| 2616 | NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED); |
| 2617 | } |
| 2618 | if (candidate.address().family() == AF_INET6) { |
| 2619 | NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED); |
| 2620 | } |
| 2621 | } |
| 2622 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2623 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 2624 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 2625 | usage_pattern_.NoteUsageEvent(event); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2626 | } |
| 2627 | |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2628 | // Asynchronously adds remote candidates on the network thread. |
| 2629 | void PeerConnection::AddRemoteCandidate(const std::string& mid, |
| 2630 | const cricket::Candidate& candidate) { |
| 2631 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2632 | |
Byoungchan Lee | 5a92577 | 2022-10-18 17:43:18 | [diff] [blame] | 2633 | if (candidate.network_type() != rtc::ADAPTER_TYPE_UNKNOWN) { |
| 2634 | RTC_DLOG(LS_WARNING) << "Using candidate with adapter type set - this " |
| 2635 | "should only happen in test"; |
| 2636 | } |
| 2637 | |
| 2638 | // Clear fields that do not make sense as remote candidates. |
| 2639 | cricket::Candidate new_candidate(candidate); |
| 2640 | new_candidate.set_network_type(rtc::ADAPTER_TYPE_UNKNOWN); |
| 2641 | new_candidate.set_relay_protocol(""); |
| 2642 | new_candidate.set_underlying_type_for_vpn(rtc::ADAPTER_TYPE_UNKNOWN); |
| 2643 | |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 2644 | network_thread()->PostTask(SafeTask( |
Byoungchan Lee | 5a92577 | 2022-10-18 17:43:18 | [diff] [blame] | 2645 | network_thread_safety_, [this, mid = mid, candidate = new_candidate] { |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2646 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2647 | std::vector<cricket::Candidate> candidates = {candidate}; |
| 2648 | RTCError error = |
| 2649 | transport_controller_->AddRemoteCandidates(mid, candidates); |
| 2650 | if (error.ok()) { |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 2651 | signaling_thread()->PostTask(SafeTask( |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2652 | signaling_thread_safety_.flag(), |
| 2653 | [this, candidate = std::move(candidate)] { |
| 2654 | ReportRemoteIceCandidateAdded(candidate); |
| 2655 | // Candidates successfully submitted for checking. |
| 2656 | if (ice_connection_state() == |
| 2657 | PeerConnectionInterface::kIceConnectionNew || |
| 2658 | ice_connection_state() == |
| 2659 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 2660 | // If state is New, then the session has just gotten its first |
| 2661 | // remote ICE candidates, so go to Checking. If state is |
| 2662 | // Disconnected, the session is re-using old candidates or |
| 2663 | // receiving additional ones, so go to Checking. If state is |
| 2664 | // Connected, stay Connected. |
| 2665 | // TODO(bemasc): If state is Connected, and the new candidates |
| 2666 | // are for a newly added transport, then the state actually |
| 2667 | // _should_ move to checking. Add a way to distinguish that |
| 2668 | // case. |
| 2669 | SetIceConnectionState( |
| 2670 | PeerConnectionInterface::kIceConnectionChecking); |
| 2671 | } |
| 2672 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 2673 | })); |
| 2674 | } else { |
| 2675 | RTC_LOG(LS_WARNING) << error.message(); |
| 2676 | } |
| 2677 | })); |
| 2678 | } |
| 2679 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2680 | void PeerConnection::ReportUsagePattern() const { |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 2681 | usage_pattern_.ReportUsagePattern(observer_); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2682 | } |
| 2683 | |
Tomas Gunnarsson | 8cb9706 | 2021-02-08 17:57:04 | [diff] [blame] | 2684 | void PeerConnection::ReportRemoteIceCandidateAdded( |
| 2685 | const cricket::Candidate& candidate) { |
| 2686 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2687 | |
| 2688 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 2689 | |
| 2690 | if (candidate.address().IsPrivateIP()) { |
| 2691 | NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
| 2692 | } |
| 2693 | if (candidate.address().IsUnresolvedIP()) { |
| 2694 | NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
| 2695 | } |
| 2696 | if (candidate.address().family() == AF_INET6) { |
| 2697 | NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED); |
| 2698 | } |
| 2699 | } |
| 2700 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2701 | bool PeerConnection::SrtpRequired() const { |
Tomas Gunnarsson | 3278a71 | 2021-03-30 15:23:42 | [diff] [blame] | 2702 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 974044e | 2024-02-08 13:15:51 | [diff] [blame] | 2703 | return dtls_enabled_; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | void PeerConnection::OnTransportControllerGatheringState( |
| 2707 | cricket::IceGatheringState state) { |
| 2708 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 2709 | if (state == cricket::kIceGatheringGathering) { |
| 2710 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 2711 | } else if (state == cricket::kIceGatheringComplete) { |
| 2712 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
Harald Alvestrand | bedb605 | 2020-08-20 12:50:10 | [diff] [blame] | 2713 | } else if (state == cricket::kIceGatheringNew) { |
| 2714 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringNew); |
| 2715 | } else { |
| 2716 | RTC_LOG(LS_ERROR) << "Unknown state received: " << state; |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2717 | RTC_DCHECK_NOTREACHED(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2718 | } |
| 2719 | } |
| 2720 | |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2721 | // Runs on network_thread(). |
Tommi | dba22d3 | 2023-06-01 14:08:52 | [diff] [blame] | 2722 | void PeerConnection::ReportTransportStats( |
| 2723 | std::vector<RtpTransceiverProxyRefPtr> transceivers) { |
Markus Handell | 518669d | 2021-06-07 11:30:46 | [diff] [blame] | 2724 | TRACE_EVENT0("webrtc", "PeerConnection::ReportTransportStats"); |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2725 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2726 | std::map<std::string, std::set<cricket::MediaType>> |
| 2727 | media_types_by_transport_name; |
Tommi | dba22d3 | 2023-06-01 14:08:52 | [diff] [blame] | 2728 | for (const auto& transceiver : transceivers) { |
| 2729 | if (transceiver->internal()->channel()) { |
| 2730 | std::string transport_name( |
| 2731 | transceiver->internal()->channel()->transport_name()); |
| 2732 | media_types_by_transport_name[transport_name].insert( |
| 2733 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2734 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2735 | } |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2736 | |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2737 | if (sctp_mid_n_) { |
Tomas Gunnarsson | e1c8a43 | 2021-04-08 13:15:28 | [diff] [blame] | 2738 | cricket::DtlsTransportInternal* dtls_transport = |
| 2739 | transport_controller_->GetDtlsTransport(*sctp_mid_n_); |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2740 | if (dtls_transport) { |
| 2741 | media_types_by_transport_name[dtls_transport->transport_name()].insert( |
| 2742 | cricket::MEDIA_TYPE_DATA); |
| 2743 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2744 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2745 | |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2746 | for (const auto& entry : media_types_by_transport_name) { |
| 2747 | const std::string& transport_name = entry.first; |
| 2748 | const std::set<cricket::MediaType> media_types = entry.second; |
| 2749 | cricket::TransportStats stats; |
| 2750 | if (transport_controller_->GetStats(transport_name, &stats)) { |
| 2751 | ReportBestConnectionState(stats); |
| 2752 | ReportNegotiatedCiphers(dtls_enabled_, stats, media_types); |
| 2753 | } |
| 2754 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2755 | } |
Tomas Gunnarsson | 2001dc3 | 2021-04-06 09:36:00 | [diff] [blame] | 2756 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2757 | // Walk through the ConnectionInfos to gather best connection usage |
| 2758 | // for IPv4 and IPv6. |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2759 | // static (no member state required) |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2760 | void PeerConnection::ReportBestConnectionState( |
| 2761 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2762 | for (const cricket::TransportChannelStats& channel_stats : |
| 2763 | stats.channel_stats) { |
| 2764 | for (const cricket::ConnectionInfo& connection_info : |
Jonas Oreland | 149dc72 | 2019-08-28 06:10:27 | [diff] [blame] | 2765 | channel_stats.ice_transport_stats.connection_infos) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2766 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2767 | continue; |
| 2768 | } |
| 2769 | |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2770 | const cricket::Candidate& local = connection_info.local_candidate; |
| 2771 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2772 | |
| 2773 | // Increment the counter for IceCandidatePairType. |
| 2774 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
Tommi | 3b2b2af | 2024-01-17 13:19:41 | [diff] [blame] | 2775 | (local.is_relay() && |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2776 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2777 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 2778 | GetIceCandidatePairCounter(local, remote), |
| 2779 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2780 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2781 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 2782 | GetIceCandidatePairCounter(local, remote), |
| 2783 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2784 | } else { |
Karl Wiberg | c95b939 | 2020-11-07 23:49:37 | [diff] [blame] | 2785 | RTC_CHECK_NOTREACHED(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2786 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2787 | |
| 2788 | // Increment the counter for IP type. |
| 2789 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2790 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 2791 | kBestConnections_IPv4, |
| 2792 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2793 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2794 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 2795 | kBestConnections_IPv6, |
| 2796 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2797 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 2798 | RTC_CHECK(!local.address().hostname().empty() && |
| 2799 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | return; |
| 2803 | } |
| 2804 | } |
| 2805 | } |
| 2806 | |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2807 | // static |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2808 | void PeerConnection::ReportNegotiatedCiphers( |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2809 | bool dtls_enabled, |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2810 | const cricket::TransportStats& stats, |
| 2811 | const std::set<cricket::MediaType>& media_types) { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2812 | if (!dtls_enabled || stats.channel_stats.empty()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2813 | return; |
| 2814 | } |
| 2815 | |
| 2816 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 2817 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2818 | if (srtp_crypto_suite == rtc::kSrtpInvalidCryptoSuite && |
| 2819 | ssl_cipher_suite == rtc::kTlsNullWithNullNull) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2820 | return; |
| 2821 | } |
| 2822 | |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2823 | if (ssl_cipher_suite != rtc::kTlsNullWithNullNull) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2824 | for (cricket::MediaType media_type : media_types) { |
| 2825 | switch (media_type) { |
| 2826 | case cricket::MEDIA_TYPE_AUDIO: |
| 2827 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2828 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2829 | rtc::kSslCipherSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2830 | break; |
| 2831 | case cricket::MEDIA_TYPE_VIDEO: |
| 2832 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2833 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2834 | rtc::kSslCipherSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2835 | break; |
| 2836 | case cricket::MEDIA_TYPE_DATA: |
| 2837 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2838 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 2839 | rtc::kSslCipherSuiteMaxValue); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2840 | break; |
| 2841 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 2842 | RTC_DCHECK_NOTREACHED(); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 2843 | continue; |
| 2844 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 2845 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2846 | } |
Philipp Hancke | 36e4dd2 | 2023-09-28 09:02:21 | [diff] [blame] | 2847 | |
| 2848 | uint16_t ssl_peer_signature_algorithm = |
| 2849 | stats.channel_stats[0].ssl_peer_signature_algorithm; |
| 2850 | if (ssl_peer_signature_algorithm != rtc::kSslSignatureAlgorithmUnknown) { |
| 2851 | for (cricket::MediaType media_type : media_types) { |
| 2852 | switch (media_type) { |
| 2853 | case cricket::MEDIA_TYPE_AUDIO: |
| 2854 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2855 | "WebRTC.PeerConnection.SslPeerSignatureAlgorithm.Audio", |
| 2856 | ssl_peer_signature_algorithm, |
| 2857 | rtc::kSslSignatureAlgorithmMaxValue); |
| 2858 | break; |
| 2859 | case cricket::MEDIA_TYPE_VIDEO: |
| 2860 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2861 | "WebRTC.PeerConnection.SslPeerSignatureAlgorithm.Video", |
| 2862 | ssl_peer_signature_algorithm, |
| 2863 | rtc::kSslSignatureAlgorithmMaxValue); |
| 2864 | break; |
| 2865 | case cricket::MEDIA_TYPE_DATA: |
| 2866 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 2867 | "WebRTC.PeerConnection.SslPeerSignatureAlgorithm.Data", |
| 2868 | ssl_peer_signature_algorithm, |
| 2869 | rtc::kSslSignatureAlgorithmMaxValue); |
| 2870 | break; |
| 2871 | default: |
| 2872 | RTC_DCHECK_NOTREACHED(); |
| 2873 | continue; |
| 2874 | } |
| 2875 | } |
| 2876 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2877 | } |
| 2878 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2879 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2880 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2881 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 2882 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 2883 | DataChannelTransportInterface* data_channel_transport) { |
Karl Wiberg | ac02589 | 2019-03-26 12:08:37 | [diff] [blame] | 2884 | RTC_DCHECK_RUN_ON(network_thread()); |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2885 | bool ret = true; |
Harald Alvestrand | 8101e7b | 2022-05-23 14:57:47 | [diff] [blame] | 2886 | if (ConfiguredForMedia()) { |
| 2887 | for (const auto& transceiver : |
| 2888 | rtp_manager()->transceivers()->UnsafeList()) { |
| 2889 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2890 | if (channel && channel->mid() == mid) { |
| 2891 | ret = channel->SetRtpTransport(rtp_transport); |
| 2892 | } |
Harald Alvestrand | 19ebabc | 2022-04-28 13:31:17 | [diff] [blame] | 2893 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2894 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2895 | |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 2896 | if (mid == sctp_mid_n_) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2897 | data_channel_controller_.OnTransportChanged(data_channel_transport); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2898 | if (dtls_transport) { |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 2899 | signaling_thread()->PostTask(SafeTask( |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2900 | signaling_thread_safety_.flag(), |
Tomas Gunnarsson | 94f0194 | 2022-01-03 14:59:12 | [diff] [blame] | 2901 | [this, |
| 2902 | name = std::string(dtls_transport->internal()->transport_name())] { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2903 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 46053e4 | 2023-01-20 12:18:53 | [diff] [blame] | 2904 | SetSctpTransportName(std::move(name)); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2905 | })); |
| 2906 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 2907 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2908 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 2909 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2910 | } |
| 2911 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2912 | PeerConnectionObserver* PeerConnection::Observer() const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 2913 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2914 | RTC_DCHECK(observer_); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2915 | return observer_; |
| 2916 | } |
| 2917 | |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2918 | void PeerConnection::StartSctpTransport(int local_port, |
| 2919 | int remote_port, |
| 2920 | int max_message_size) { |
| 2921 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2922 | if (!sctp_mid_s_) |
| 2923 | return; |
| 2924 | |
Danil Chapovalov | a30439b | 2022-07-07 08:08:49 | [diff] [blame] | 2925 | network_thread()->PostTask(SafeTask( |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2926 | network_thread_safety_, |
| 2927 | [this, mid = *sctp_mid_s_, local_port, remote_port, max_message_size] { |
| 2928 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
Harald Alvestrand | bc32c56 | 2022-02-09 12:08:47 | [diff] [blame] | 2929 | transport_controller_n()->GetSctpTransport(mid); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 2930 | if (sctp_transport) |
| 2931 | sctp_transport->Start(local_port, remote_port, max_message_size); |
| 2932 | })); |
| 2933 | } |
| 2934 | |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 2935 | CryptoOptions PeerConnection::GetCryptoOptions() { |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 2936 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 2937 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 2938 | // after it has been removed. |
| 2939 | return configuration_.crypto_options.has_value() |
| 2940 | ? *configuration_.crypto_options |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 2941 | : options_.crypto_options; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 2942 | } |
| 2943 | |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 2944 | void PeerConnection::ClearStatsCache() { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2945 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Philipp Hancke | 7baa63f | 2022-09-01 13:39:50 | [diff] [blame] | 2946 | if (legacy_stats_) { |
| 2947 | legacy_stats_->InvalidateCache(); |
| 2948 | } |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 2949 | if (stats_collector_) { |
| 2950 | stats_collector_->ClearCachedStatsReport(); |
| 2951 | } |
| 2952 | } |
| 2953 | |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 2954 | bool PeerConnection::ShouldFireNegotiationNeededEvent(uint32_t event_id) { |
| 2955 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 2956 | return sdp_handler_->ShouldFireNegotiationNeededEvent(event_id); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 2957 | } |
| 2958 | |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2959 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Harald Alvestrand | 00c62ed | 2021-10-20 08:52:12 | [diff] [blame] | 2960 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2961 | message_handler_.RequestUsagePatternReport( |
| 2962 | [this]() { |
| 2963 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2964 | ReportUsagePattern(); |
| 2965 | }, |
| 2966 | /* delay_ms= */ 0); |
| 2967 | } |
| 2968 | |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 08:39:31 | [diff] [blame] | 2969 | std::function<void(const rtc::CopyOnWriteBuffer& packet, |
| 2970 | int64_t packet_time_us)> |
| 2971 | PeerConnection::InitializeRtcpCallback() { |
Tommi | c3257d0 | 2021-02-10 17:40:08 | [diff] [blame] | 2972 | RTC_DCHECK_RUN_ON(network_thread()); |
Per K | cf439a0 | 2023-01-05 13:01:39 | [diff] [blame] | 2973 | return [this](const rtc::CopyOnWriteBuffer& packet, |
| 2974 | int64_t /*packet_time_us*/) { |
| 2975 | worker_thread()->PostTask(SafeTask(worker_thread_safety_, [this, packet]() { |
| 2976 | call_ptr_->Receiver()->DeliverRtcpPacket(packet); |
| 2977 | })); |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 08:39:31 | [diff] [blame] | 2978 | }; |
| 2979 | } |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2980 | |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 2981 | std::function<void(const RtpPacketReceived& parsed_packet)> |
| 2982 | PeerConnection::InitializeUnDemuxablePacketHandler() { |
| 2983 | RTC_DCHECK_RUN_ON(network_thread()); |
| 2984 | return [this](const RtpPacketReceived& parsed_packet) { |
| 2985 | worker_thread()->PostTask( |
| 2986 | SafeTask(worker_thread_safety_, [this, parsed_packet]() { |
| 2987 | // Deliver the packet anyway to Call to allow Call to do BWE. |
| 2988 | // Even if there is no media receiver, the packet has still |
| 2989 | // been received on the network and has been correcly parsed. |
| 2990 | call_ptr_->Receiver()->DeliverRtpPacket( |
| 2991 | MediaType::ANY, parsed_packet, |
| 2992 | /*undemuxable_packet_handler=*/ |
| 2993 | [](const RtpPacketReceived& packet) { return false; }); |
| 2994 | })); |
| 2995 | }; |
| 2996 | } |
| 2997 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2998 | } // namespace webrtc |