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