xref: /aosp_15_r20/external/webrtc/p2p/base/p2p_transport_channel.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
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.
9  */
10 
11 // P2PTransportChannel wraps up the state management of the connection between
12 // two P2P clients.  Clients have candidate ports for connecting, and
13 // connections which are combinations of candidates from each end (Alice and
14 // Bob each have candidates, one candidate from Alice and one candidate from
15 // Bob are used to make a connection, repeat to make many connections).
16 //
17 // When all of the available connections become invalid (non-writable), we
18 // kick off a process of determining more candidates and more connections.
19 //
20 #ifndef P2P_BASE_P2P_TRANSPORT_CHANNEL_H_
21 #define P2P_BASE_P2P_TRANSPORT_CHANNEL_H_
22 
23 #include <stddef.h>
24 #include <stdint.h>
25 
26 #include <algorithm>
27 #include <map>
28 #include <memory>
29 #include <set>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 #include "absl/base/attributes.h"
35 #include "absl/strings/string_view.h"
36 #include "absl/types/optional.h"
37 #include "api/array_view.h"
38 #include "api/async_dns_resolver.h"
39 #include "api/async_resolver_factory.h"
40 #include "api/candidate.h"
41 #include "api/ice_transport_interface.h"
42 #include "api/rtc_error.h"
43 #include "api/sequence_checker.h"
44 #include "api/task_queue/pending_task_safety_flag.h"
45 #include "api/transport/enums.h"
46 #include "api/transport/stun.h"
47 #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h"
48 #include "logging/rtc_event_log/ice_logger.h"
49 #include "p2p/base/active_ice_controller_factory_interface.h"
50 #include "p2p/base/basic_async_resolver_factory.h"
51 #include "p2p/base/candidate_pair_interface.h"
52 #include "p2p/base/connection.h"
53 #include "p2p/base/ice_agent_interface.h"
54 #include "p2p/base/ice_controller_factory_interface.h"
55 #include "p2p/base/ice_controller_interface.h"
56 #include "p2p/base/ice_switch_reason.h"
57 #include "p2p/base/ice_transport_internal.h"
58 #include "p2p/base/p2p_constants.h"
59 #include "p2p/base/p2p_transport_channel_ice_field_trials.h"
60 #include "p2p/base/port.h"
61 #include "p2p/base/port_allocator.h"
62 #include "p2p/base/port_interface.h"
63 #include "p2p/base/regathering_controller.h"
64 #include "p2p/base/transport_description.h"
65 #include "rtc_base/async_packet_socket.h"
66 #include "rtc_base/checks.h"
67 #include "rtc_base/dscp.h"
68 #include "rtc_base/network/sent_packet.h"
69 #include "rtc_base/network_route.h"
70 #include "rtc_base/socket.h"
71 #include "rtc_base/socket_address.h"
72 #include "rtc_base/strings/string_builder.h"
73 #include "rtc_base/system/rtc_export.h"
74 #include "rtc_base/third_party/sigslot/sigslot.h"
75 #include "rtc_base/thread.h"
76 #include "rtc_base/thread_annotations.h"
77 
78 namespace webrtc {
79 class RtcEventLog;
80 }  // namespace webrtc
81 
82 namespace cricket {
83 
84 // Enum for UMA metrics, used to record whether the channel is
85 // connected/connecting/disconnected when ICE restart happens.
86 enum class IceRestartState { CONNECTING, CONNECTED, DISCONNECTED, MAX_VALUE };
87 
88 static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3;
89 
90 bool IceCredentialsChanged(absl::string_view old_ufrag,
91                            absl::string_view old_pwd,
92                            absl::string_view new_ufrag,
93                            absl::string_view new_pwd);
94 
95 // Adds the port on which the candidate originated.
96 class RemoteCandidate : public Candidate {
97  public:
RemoteCandidate(const Candidate & c,PortInterface * origin_port)98   RemoteCandidate(const Candidate& c, PortInterface* origin_port)
99       : Candidate(c), origin_port_(origin_port) {}
100 
origin_port()101   PortInterface* origin_port() { return origin_port_; }
102 
103  private:
104   PortInterface* origin_port_;
105 };
106 
107 // P2PTransportChannel manages the candidates and connection process to keep
108 // two P2P clients connected to each other.
109 class RTC_EXPORT P2PTransportChannel : public IceTransportInternal,
110                                        public IceAgentInterface {
111  public:
112   static std::unique_ptr<P2PTransportChannel> Create(
113       absl::string_view transport_name,
114       int component,
115       webrtc::IceTransportInit init);
116 
117   // For testing only.
118   // TODO(zstein): Remove once AsyncDnsResolverFactory is required.
119   P2PTransportChannel(absl::string_view transport_name,
120                       int component,
121                       PortAllocator* allocator,
122                       const webrtc::FieldTrialsView* field_trials = nullptr);
123 
124   ~P2PTransportChannel() override;
125 
126   P2PTransportChannel(const P2PTransportChannel&) = delete;
127   P2PTransportChannel& operator=(const P2PTransportChannel&) = delete;
128 
129   // From TransportChannelImpl:
130   IceTransportState GetState() const override;
131   webrtc::IceTransportState GetIceTransportState() const override;
132 
133   const std::string& transport_name() const override;
134   int component() const override;
135   bool writable() const override;
136   bool receiving() const override;
137   void SetIceRole(IceRole role) override;
138   IceRole GetIceRole() const override;
139   void SetIceTiebreaker(uint64_t tiebreaker) override;
140   void SetIceParameters(const IceParameters& ice_params) override;
141   void SetRemoteIceParameters(const IceParameters& ice_params) override;
142   void SetRemoteIceMode(IceMode mode) override;
143   // TODO(deadbeef): Deprecated. Remove when Chromium's
144   // IceTransportChannel does not depend on this.
Connect()145   void Connect() {}
146   void MaybeStartGathering() override;
147   IceGatheringState gathering_state() const override;
148   void ResolveHostnameCandidate(const Candidate& candidate);
149   void AddRemoteCandidate(const Candidate& candidate) override;
150   void RemoveRemoteCandidate(const Candidate& candidate) override;
151   void RemoveAllRemoteCandidates() override;
152   // Sets the parameters in IceConfig. We do not set them blindly. Instead, we
153   // only update the parameter if it is considered set in `config`. For example,
154   // a negative value of receiving_timeout will be considered "not set" and we
155   // will not use it to update the respective parameter in `config_`.
156   // TODO(deadbeef): Use absl::optional instead of negative values.
157   void SetIceConfig(const IceConfig& config) override;
158   const IceConfig& config() const;
159   static webrtc::RTCError ValidateIceConfig(const IceConfig& config);
160 
161   // From TransportChannel:
162   int SendPacket(const char* data,
163                  size_t len,
164                  const rtc::PacketOptions& options,
165                  int flags) override;
166   int SetOption(rtc::Socket::Option opt, int value) override;
167   bool GetOption(rtc::Socket::Option opt, int* value) override;
168   int GetError() override;
169   bool GetStats(IceTransportStats* ice_transport_stats) override;
170   absl::optional<int> GetRttEstimate() override;
171   const Connection* selected_connection() const override;
172   absl::optional<const CandidatePair> GetSelectedCandidatePair() const override;
173 
174   // From IceAgentInterface
175   void OnStartedPinging() override;
176   int64_t GetLastPingSentMs() const override;
177   void UpdateConnectionStates() override;
178   void UpdateState() override;
179   void SendPingRequest(const Connection* connection) override;
180   void SwitchSelectedConnection(const Connection* connection,
181                                 IceSwitchReason reason) override;
182   void ForgetLearnedStateForConnections(
183       rtc::ArrayView<const Connection* const> connections) override;
184   bool PruneConnections(
185       rtc::ArrayView<const Connection* const> connections) override;
186 
187   // TODO(honghaiz): Remove this method once the reference of it in
188   // Chromoting is removed.
best_connection()189   const Connection* best_connection() const {
190     RTC_DCHECK_RUN_ON(network_thread_);
191     return selected_connection_;
192   }
193 
set_incoming_only(bool value)194   void set_incoming_only(bool value) {
195     RTC_DCHECK_RUN_ON(network_thread_);
196     incoming_only_ = value;
197   }
198 
199   // Note: These are only for testing purpose.
200   // `ports_` and `pruned_ports` should not be changed from outside.
ports()201   const std::vector<PortInterface*>& ports() {
202     RTC_DCHECK_RUN_ON(network_thread_);
203     return ports_;
204   }
pruned_ports()205   const std::vector<PortInterface*>& pruned_ports() {
206     RTC_DCHECK_RUN_ON(network_thread_);
207     return pruned_ports_;
208   }
209 
remote_ice_mode()210   IceMode remote_ice_mode() const {
211     RTC_DCHECK_RUN_ON(network_thread_);
212     return remote_ice_mode_;
213   }
214 
215   void PruneAllPorts();
216   int check_receiving_interval() const;
217   absl::optional<rtc::NetworkRoute> network_route() const override;
218 
219   void RemoveConnection(const Connection* connection);
220 
221   // Helper method used only in unittest.
222   rtc::DiffServCodePoint DefaultDscpValue() const;
223 
224   // Public for unit tests.
225   Connection* FindNextPingableConnection();
226   void MarkConnectionPinged(Connection* conn);
227 
228   // Public for unit tests.
229   rtc::ArrayView<Connection*> connections() const;
230   void RemoveConnectionForTest(Connection* connection);
231 
232   // Public for unit tests.
allocator_session()233   PortAllocatorSession* allocator_session() const {
234     RTC_DCHECK_RUN_ON(network_thread_);
235     if (allocator_sessions_.empty()) {
236       return nullptr;
237     }
238     return allocator_sessions_.back().get();
239   }
240 
241   // Public for unit tests.
remote_candidates()242   const std::vector<RemoteCandidate>& remote_candidates() const {
243     RTC_DCHECK_RUN_ON(network_thread_);
244     return remote_candidates_;
245   }
246 
ToString()247   std::string ToString() const {
248     RTC_DCHECK_RUN_ON(network_thread_);
249     const std::string RECEIVING_ABBREV[2] = {"_", "R"};
250     const std::string WRITABLE_ABBREV[2] = {"_", "W"};
251     rtc::StringBuilder ss;
252     ss << "Channel[" << transport_name_ << "|" << component_ << "|"
253        << RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]";
254     return ss.Release();
255   }
256 
257  private:
258   P2PTransportChannel(
259       absl::string_view transport_name,
260       int component,
261       PortAllocator* allocator,
262       // DNS resolver factory
263       webrtc::AsyncDnsResolverFactoryInterface* async_dns_resolver_factory,
264       // If the P2PTransportChannel has to delete the DNS resolver factory
265       // on release, this pointer is set.
266       std::unique_ptr<webrtc::AsyncDnsResolverFactoryInterface>
267           owned_dns_resolver_factory,
268       webrtc::RtcEventLog* event_log,
269       IceControllerFactoryInterface* ice_controller_factory,
270       ActiveIceControllerFactoryInterface* active_ice_controller_factory,
271       const webrtc::FieldTrialsView* field_trials);
272 
IsGettingPorts()273   bool IsGettingPorts() {
274     RTC_DCHECK_RUN_ON(network_thread_);
275     return allocator_session()->IsGettingPorts();
276   }
277 
278   // Returns true if it's possible to send packets on `connection`.
279   bool ReadyToSend(const Connection* connection) const;
280   bool PresumedWritable(const Connection* conn) const;
281   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
282   void RequestSortAndStateUpdate(IceSwitchReason reason_to_sort);
283   // Start pinging if we haven't already started, and we now have a connection
284   // that's pingable.
285   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
286   void MaybeStartPinging();
287   void SendPingRequestInternal(Connection* connection);
288 
289   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
290   void SortConnectionsAndUpdateState(IceSwitchReason reason_to_sort);
291   rtc::NetworkRoute ConfigureNetworkRoute(const Connection* conn);
292   void SwitchSelectedConnectionInternal(Connection* conn,
293                                         IceSwitchReason reason);
294   void UpdateTransportState();
295   void HandleAllTimedOut();
296   void MaybeStopPortAllocatorSessions();
297   void OnSelectedConnectionDestroyed() RTC_RUN_ON(network_thread_);
298 
299   // ComputeIceTransportState computes the RTCIceTransportState as described in
300   // https://w3c.github.io/webrtc-pc/#dom-rtcicetransportstate. ComputeState
301   // computes the value we currently export as RTCIceTransportState.
302   // TODO(bugs.webrtc.org/9308): Remove ComputeState once it's no longer used.
303   IceTransportState ComputeState() const;
304   webrtc::IceTransportState ComputeIceTransportState() const;
305 
306   bool CreateConnections(const Candidate& remote_candidate,
307                          PortInterface* origin_port);
308   bool CreateConnection(PortInterface* port,
309                         const Candidate& remote_candidate,
310                         PortInterface* origin_port);
311   bool FindConnection(const Connection* connection) const;
312 
313   uint32_t GetRemoteCandidateGeneration(const Candidate& candidate);
314   bool IsDuplicateRemoteCandidate(const Candidate& candidate);
315   void RememberRemoteCandidate(const Candidate& remote_candidate,
316                                PortInterface* origin_port);
317   void PingConnection(Connection* conn);
318   void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session);
319   void AddConnection(Connection* connection);
320 
321   void OnPortReady(PortAllocatorSession* session, PortInterface* port);
322   void OnPortsPruned(PortAllocatorSession* session,
323                      const std::vector<PortInterface*>& ports);
324   void OnCandidatesReady(PortAllocatorSession* session,
325                          const std::vector<Candidate>& candidates);
326   void OnCandidateError(PortAllocatorSession* session,
327                         const IceCandidateErrorEvent& event);
328   void OnCandidatesRemoved(PortAllocatorSession* session,
329                            const std::vector<Candidate>& candidates);
330   void OnCandidatesAllocationDone(PortAllocatorSession* session);
331   void OnUnknownAddress(PortInterface* port,
332                         const rtc::SocketAddress& addr,
333                         ProtocolType proto,
334                         IceMessage* stun_msg,
335                         const std::string& remote_username,
336                         bool port_muxed);
337   void OnCandidateFilterChanged(uint32_t prev_filter, uint32_t cur_filter);
338 
339   // When a port is destroyed, remove it from both lists `ports_`
340   // and `pruned_ports_`.
341   void OnPortDestroyed(PortInterface* port);
342   // When pruning a port, move it from `ports_` to `pruned_ports_`.
343   // Returns true if the port is found and removed from `ports_`.
344   bool PrunePort(PortInterface* port);
345   void OnRoleConflict(PortInterface* port);
346 
347   void OnConnectionStateChange(Connection* connection);
348   void OnReadPacket(Connection* connection,
349                     const char* data,
350                     size_t len,
351                     int64_t packet_time_us);
352   void OnSentPacket(const rtc::SentPacket& sent_packet);
353   void OnReadyToSend(Connection* connection);
354   void OnConnectionDestroyed(Connection* connection);
355 
356   void OnNominated(Connection* conn);
357 
358   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
359   void CheckAndPing();
360 
361   void LogCandidatePairConfig(Connection* conn,
362                               webrtc::IceCandidatePairConfigType type);
363 
364   uint32_t GetNominationAttr(Connection* conn) const;
365   bool GetUseCandidateAttr(Connection* conn) const;
366 
367   // Returns true if the new_connection is selected for transmission.
368   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
369   bool MaybeSwitchSelectedConnection(const Connection* new_connection,
370                                      IceSwitchReason reason);
371   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
372   bool MaybeSwitchSelectedConnection(
373       IceSwitchReason reason,
374       IceControllerInterface::SwitchResult result);
375   bool AllowedToPruneConnections() const;
376   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
377   void PruneConnections();
378 
379   // Returns the latest remote ICE parameters or nullptr if there are no remote
380   // ICE parameters yet.
remote_ice()381   IceParameters* remote_ice() {
382     RTC_DCHECK_RUN_ON(network_thread_);
383     return remote_ice_parameters_.empty() ? nullptr
384                                           : &remote_ice_parameters_.back();
385   }
386   // Returns the remote IceParameters and generation that match `ufrag`
387   // if found, and returns nullptr otherwise.
388   const IceParameters* FindRemoteIceFromUfrag(absl::string_view ufrag,
389                                               uint32_t* generation);
390   // Returns the index of the latest remote ICE parameters, or 0 if no remote
391   // ICE parameters have been received.
remote_ice_generation()392   uint32_t remote_ice_generation() {
393     RTC_DCHECK_RUN_ON(network_thread_);
394     return remote_ice_parameters_.empty()
395                ? 0
396                : static_cast<uint32_t>(remote_ice_parameters_.size() - 1);
397   }
398 
399   // Indicates if the given local port has been pruned.
400   bool IsPortPruned(const Port* port) const;
401 
402   // Indicates if the given remote candidate has been pruned.
403   bool IsRemoteCandidatePruned(const Candidate& cand) const;
404 
405   // Sets the writable state, signaling if necessary.
406   void SetWritable(bool writable);
407   // Sets the receiving state, signaling if necessary.
408   void SetReceiving(bool receiving);
409   // Clears the address and the related address fields of a local candidate to
410   // avoid IP leakage. This is applicable in several scenarios as commented in
411   // `PortAllocator::SanitizeCandidate`.
412   Candidate SanitizeLocalCandidate(const Candidate& c) const;
413   // Clears the address field of a remote candidate to avoid IP leakage. This is
414   // applicable in the following scenarios:
415   // 1. mDNS candidates are received.
416   // 2. Peer-reflexive remote candidates.
417   Candidate SanitizeRemoteCandidate(const Candidate& c) const;
418 
419   // Cast a Connection returned from IceController and verify that it exists.
420   // (P2P owns all Connections, and only gives const pointers to IceController,
421   // see IceControllerInterface).
FromIceController(const Connection * conn)422   Connection* FromIceController(const Connection* conn) {
423     // Verify that IceController does not return a connection
424     // that we have destroyed.
425     RTC_DCHECK(FindConnection(conn));
426     return const_cast<Connection*>(conn);
427   }
428 
429   int64_t ComputeEstimatedDisconnectedTimeMs(int64_t now,
430                                              Connection* old_connection);
431 
432   void ParseFieldTrials(const webrtc::FieldTrialsView* field_trials);
433 
434   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
435   webrtc::ScopedTaskSafety task_safety_;
436   std::string transport_name_ RTC_GUARDED_BY(network_thread_);
437   int component_ RTC_GUARDED_BY(network_thread_);
438   PortAllocator* allocator_ RTC_GUARDED_BY(network_thread_);
439   webrtc::AsyncDnsResolverFactoryInterface* const async_dns_resolver_factory_
440       RTC_GUARDED_BY(network_thread_);
441   const std::unique_ptr<webrtc::AsyncDnsResolverFactoryInterface>
442       owned_dns_resolver_factory_;
443   rtc::Thread* const network_thread_;
444   bool incoming_only_ RTC_GUARDED_BY(network_thread_);
445   int error_ RTC_GUARDED_BY(network_thread_);
446   std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_
447       RTC_GUARDED_BY(network_thread_);
448   // `ports_` contains ports that are used to form new connections when
449   // new remote candidates are added.
450   std::vector<PortInterface*> ports_ RTC_GUARDED_BY(network_thread_);
451   // `pruned_ports_` contains ports that have been removed from `ports_` and
452   // are not being used to form new connections, but that aren't yet destroyed.
453   // They may have existing connections, and they still fire signals such as
454   // SignalUnknownAddress.
455   std::vector<PortInterface*> pruned_ports_ RTC_GUARDED_BY(network_thread_);
456 
457   Connection* selected_connection_ RTC_GUARDED_BY(network_thread_) = nullptr;
458   std::vector<Connection*> connections_ RTC_GUARDED_BY(network_thread_);
459 
460   std::vector<RemoteCandidate> remote_candidates_
461       RTC_GUARDED_BY(network_thread_);
462   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
463   bool sort_dirty_ RTC_GUARDED_BY(
464       network_thread_);  // indicates whether another sort is needed right now
465   bool had_connection_ RTC_GUARDED_BY(network_thread_) =
466       false;  // if connections_ has ever been nonempty
467   typedef std::map<rtc::Socket::Option, int> OptionMap;
468   OptionMap options_ RTC_GUARDED_BY(network_thread_);
469   IceParameters ice_parameters_ RTC_GUARDED_BY(network_thread_);
470   std::vector<IceParameters> remote_ice_parameters_
471       RTC_GUARDED_BY(network_thread_);
472   IceMode remote_ice_mode_ RTC_GUARDED_BY(network_thread_);
473   IceRole ice_role_ RTC_GUARDED_BY(network_thread_);
474   uint64_t tiebreaker_ RTC_GUARDED_BY(network_thread_);
475   IceGatheringState gathering_state_ RTC_GUARDED_BY(network_thread_);
476   std::unique_ptr<webrtc::BasicRegatheringController> regathering_controller_
477       RTC_GUARDED_BY(network_thread_);
478   int64_t last_ping_sent_ms_ RTC_GUARDED_BY(network_thread_) = 0;
479   int weak_ping_interval_ RTC_GUARDED_BY(network_thread_) = WEAK_PING_INTERVAL;
480   // TODO(jonasolsson): Remove state_ and rename standardized_state_ once state_
481   // is no longer used to compute the ICE connection state.
482   IceTransportState state_ RTC_GUARDED_BY(network_thread_) =
483       IceTransportState::STATE_INIT;
484   webrtc::IceTransportState standardized_state_
485       RTC_GUARDED_BY(network_thread_) = webrtc::IceTransportState::kNew;
486   IceConfig config_ RTC_GUARDED_BY(network_thread_);
487   int last_sent_packet_id_ RTC_GUARDED_BY(network_thread_) =
488       -1;  // -1 indicates no packet was sent before.
489   // TODO(bugs.webrtc.org/14367) remove once refactor lands.
490   bool started_pinging_ RTC_GUARDED_BY(network_thread_) = false;
491   // The value put in the "nomination" attribute for the next nominated
492   // connection. A zero-value indicates the connection will not be nominated.
493   uint32_t nomination_ RTC_GUARDED_BY(network_thread_) = 0;
494   bool receiving_ RTC_GUARDED_BY(network_thread_) = false;
495   bool writable_ RTC_GUARDED_BY(network_thread_) = false;
496   bool has_been_writable_ RTC_GUARDED_BY(network_thread_) =
497       false;  // if writable_ has ever been true
498 
499   absl::optional<rtc::NetworkRoute> network_route_
500       RTC_GUARDED_BY(network_thread_);
501   webrtc::IceEventLog ice_event_log_ RTC_GUARDED_BY(network_thread_);
502 
503   // The adapter transparently delegates ICE controller interactions to either
504   // the legacy or the active ICE controller depending on field trials.
505   // TODO(bugs.webrtc.org/14367) replace with active ICE controller eventually.
506   class IceControllerAdapter : public ActiveIceControllerInterface {
507    public:
508     IceControllerAdapter(
509         const IceControllerFactoryArgs& args,
510         IceControllerFactoryInterface* ice_controller_factory,
511         ActiveIceControllerFactoryInterface* active_ice_controller_factory,
512         const webrtc::FieldTrialsView* field_trials,
513         P2PTransportChannel* transport);
514     ~IceControllerAdapter() override;
515 
516     // ActiveIceControllerInterface overrides
517     void SetIceConfig(const IceConfig& config) override;
518     void OnConnectionAdded(const Connection* connection) override;
519     void OnConnectionSwitched(const Connection* connection) override;
520     void OnConnectionPinged(const Connection* connection) override;
521     void OnConnectionDestroyed(const Connection* connection) override;
522     void OnConnectionUpdated(const Connection* connection) override;
523     void OnSortAndSwitchRequest(IceSwitchReason reason) override;
524     void OnImmediateSortAndSwitchRequest(IceSwitchReason reason) override;
525     bool OnImmediateSwitchRequest(IceSwitchReason reason,
526                                   const Connection* connection) override;
527     bool GetUseCandidateAttribute(const Connection* connection,
528                                   NominationMode mode,
529                                   IceMode remote_ice_mode) const override;
530     const Connection* FindNextPingableConnection() override;
531 
532     // Methods only available with legacy ICE controller.
533     rtc::ArrayView<Connection*> LegacyConnections() const;
534     bool LegacyHasPingableConnection() const;
535     IceControllerInterface::PingResult LegacySelectConnectionToPing(
536         int64_t last_ping_sent_ms);
537     IceControllerInterface::SwitchResult LegacyShouldSwitchConnection(
538         IceSwitchReason reason,
539         const Connection* connection);
540     IceControllerInterface::SwitchResult LegacySortAndSwitchConnection(
541         IceSwitchReason reason);
542     std::vector<const Connection*> LegacyPruneConnections();
543 
544    private:
545     P2PTransportChannel* transport_;
546     std::unique_ptr<IceControllerInterface> legacy_ice_controller_;
547     std::unique_ptr<ActiveIceControllerInterface> active_ice_controller_;
548   };
549   std::unique_ptr<IceControllerAdapter> ice_adapter_
550       RTC_GUARDED_BY(network_thread_);
551 
552   struct CandidateAndResolver final {
553     CandidateAndResolver(
554         const Candidate& candidate,
555         std::unique_ptr<webrtc::AsyncDnsResolverInterface>&& resolver);
556     ~CandidateAndResolver();
557     // Moveable, but not copyable.
558     CandidateAndResolver(CandidateAndResolver&&) = default;
559     CandidateAndResolver& operator=(CandidateAndResolver&&) = default;
560 
561     Candidate candidate_;
562     std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver_;
563   };
564   std::vector<CandidateAndResolver> resolvers_ RTC_GUARDED_BY(network_thread_);
565   void FinishAddingRemoteCandidate(const Candidate& new_remote_candidate);
566   void OnCandidateResolved(webrtc::AsyncDnsResolverInterface* resolver);
567   void AddRemoteCandidateWithResult(
568       Candidate candidate,
569       const webrtc::AsyncDnsResolverResult& result);
570 
571   // Bytes/packets sent/received on this channel.
572   uint64_t bytes_sent_ = 0;
573   uint64_t bytes_received_ = 0;
574   uint64_t packets_sent_ = 0;
575   uint64_t packets_received_ = 0;
576 
577   // Number of times the selected_connection_ has been modified.
578   uint32_t selected_candidate_pair_changes_ = 0;
579 
580   // When was last data received on a existing connection,
581   // from connection->last_data_received() that uses rtc::TimeMillis().
582   int64_t last_data_received_ms_ = 0;
583 
584   // Parsed field trials.
585   IceFieldTrials ice_field_trials_;
586 };
587 
588 }  // namespace cricket
589 
590 #endif  // P2P_BASE_P2P_TRANSPORT_CHANNEL_H_
591