blob: b3ded2be8e276ddfbbe269c699fe6c87d17463a9 [file] [log] [blame]
sergeyua7749062015-10-28 21:57:451// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_
6#define REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_
7
8#include <string>
9
10#include "base/callback_forward.h"
11#include "base/macros.h"
12#include "base/threading/thread_checker.h"
13#include "base/timer/timer.h"
14#include "remoting/protocol/network_settings.h"
15#include "remoting/protocol/transport.h"
zhihuang1aa292f32017-01-03 19:37:5116// TODO(zhihuang):Replace #include by forward declaration once proper
17// inheritance is defined for cricket::IceTransportInternal and
18// cricket::P2PTransportChannel.
Steve Anton0c3c2a82019-01-15 01:51:5419#include "third_party/webrtc/p2p/base/ice_transport_internal.h"
sprangd72236c2016-10-21 08:36:0420// TODO(johan): Replace #include by forward declaration once proper inheritance
21// is defined for rtc::PacketTransportInterface and cricket::TransportChannel.
Steve Anton0c3c2a82019-01-15 01:51:5422#include "third_party/webrtc/p2p/base/packet_transport_interface.h"
Artem Titov68b05be2018-07-31 08:06:1423#include "third_party/webrtc/rtc_base/third_party/sigslot/sigslot.h"
sprangd72236c2016-10-21 08:36:0424
sergeyua7749062015-10-28 21:57:4525namespace cricket {
26class Candidate;
27class P2PTransportChannel;
28class PortAllocator;
sergeyua7749062015-10-28 21:57:4529} // namespace cricket
30
31namespace remoting {
32namespace protocol {
33
jbriancee9b2ece2016-12-01 07:29:5234class P2PDatagramSocket;
sergeyu86030f382015-12-16 22:45:0035class TransportContext;
36
sergeyua7749062015-10-28 21:57:4537class IceTransportChannel : public sigslot::has_slots<> {
38 public:
39 class Delegate {
40 public:
Nico Weber7452f1c2019-02-11 15:06:1741 Delegate() {}
42 virtual ~Delegate() {}
sergeyua7749062015-10-28 21:57:4543
44 // Called to pass ICE credentials to the session. Used only for STANDARD
45 // version of ICE, see SetIceVersion().
sergeyue5767602015-12-28 19:49:0346 virtual void OnChannelIceCredentials(IceTransportChannel* transport,
sergeyua7749062015-10-28 21:57:4547 const std::string& ufrag,
48 const std::string& password) = 0;
49
50 // Called when the transport generates a new candidate that needs
51 // to be passed to the AddRemoteCandidate() method on the remote
52 // end of the connection.
sergeyue5767602015-12-28 19:49:0353 virtual void OnChannelCandidate(IceTransportChannel* transport,
sergeyua7749062015-10-28 21:57:4554 const cricket::Candidate& candidate) = 0;
55
56 // Called when transport route changes. Can be called even before
57 // the transport is connected.
sergeyue5767602015-12-28 19:49:0358 virtual void OnChannelRouteChange(IceTransportChannel* transport,
sergeyua7749062015-10-28 21:57:4559 const TransportRoute& route) = 0;
60
sergeyue5767602015-12-28 19:49:0361 // Called when when the channel has failed to connect or reconnect.
62 virtual void OnChannelFailed(IceTransportChannel* transport) = 0;
sergeyua7749062015-10-28 21:57:4563
sergeyue5767602015-12-28 19:49:0364 // Called when the channel is about to be deleted.
65 virtual void OnChannelDeleted(IceTransportChannel* transport) = 0;
sergeyua7749062015-10-28 21:57:4566 };
67
dcheng0765c492016-04-06 22:41:5368 typedef base::Callback<void(std::unique_ptr<P2PDatagramSocket>)>
69 ConnectedCallback;
sergeyua7749062015-10-28 21:57:4570
sergeyu86030f382015-12-16 22:45:0071 explicit IceTransportChannel(
72 scoped_refptr<TransportContext> transport_context);
sergeyua7749062015-10-28 21:57:4573 ~IceTransportChannel() override;
74
sergeyua7749062015-10-28 21:57:4575 // Connects the channel and calls the |callback| after that.
76 void Connect(const std::string& name,
77 Delegate* delegate,
78 const ConnectedCallback& callback);
79
80 // Sets remote ICE credentials.
81 void SetRemoteCredentials(const std::string& ufrag,
82 const std::string& password);
83
84 // Adds |candidate| received from the peer.
85 void AddRemoteCandidate(const cricket::Candidate& candidate);
86
87 // Name of the channel. Used to identify the channel and disambiguate
88 // candidates it generates from candidates generated by parallel connections.
89 const std::string& name() const;
90
91 // Returns true if the channel is already connected.
92 bool is_connected() const;
93
94 private:
sergeyu86030f382015-12-16 22:45:0095 void OnPortAllocatorCreated(
dcheng0765c492016-04-06 22:41:5396 std::unique_ptr<cricket::PortAllocator> port_allocator);
sergeyu86030f382015-12-16 22:45:0097
sergeyua7749062015-10-28 21:57:4598 void NotifyConnected();
99
zhihuang1aa292f32017-01-03 19:37:51100 // Signal handlers for cricket::IceTransportInternal.
zhihuangf6a93f22017-01-16 02:11:12101 void OnCandidateGathered(cricket::IceTransportInternal* ice_transport,
sergeyua7749062015-10-28 21:57:45102 const cricket::Candidate& candidate);
zhihuang1aa292f32017-01-03 19:37:51103 void OnRouteChange(cricket::IceTransportInternal* ice_transport,
sergeyua7749062015-10-28 21:57:45104 const cricket::Candidate& candidate);
sprangd72236c2016-10-21 08:36:04105 void OnWritableState(rtc::PacketTransportInterface* transport);
sergeyua7749062015-10-28 21:57:45106
107 // Callback for TransportChannelSocketAdapter to notify when the socket is
108 // destroyed.
109 void OnChannelDestroyed();
110
111 void NotifyRouteChanged();
112
113 // Tries to connect by restarting ICE. Called by |reconnect_timer_|.
114 void TryReconnect();
115
sergeyu86030f382015-12-16 22:45:00116 scoped_refptr<TransportContext> transport_context_;
sergeyua7749062015-10-28 21:57:45117
118 std::string name_;
sergeyu86030f382015-12-16 22:45:00119 Delegate* delegate_ = nullptr;
sergeyua7749062015-10-28 21:57:45120 ConnectedCallback callback_;
121 std::string ice_username_fragment_;
122
dcheng0765c492016-04-06 22:41:53123 std::unique_ptr<cricket::PortAllocator> port_allocator_;
sergeyua7749062015-10-28 21:57:45124
125 std::string remote_ice_username_fragment_;
126 std::string remote_ice_password_;
127 std::list<cricket::Candidate> pending_candidates_;
dcheng0765c492016-04-06 22:41:53128 std::unique_ptr<cricket::P2PTransportChannel> channel_;
sergeyua7749062015-10-28 21:57:45129 int connect_attempts_left_;
130 base::RepeatingTimer reconnect_timer_;
131
132 base::ThreadChecker thread_checker_;
133
Jeremy Roman7c5cfabd2019-08-12 15:45:27134 base::WeakPtrFactory<IceTransportChannel> weak_factory_{this};
sergeyua7749062015-10-28 21:57:45135
136 DISALLOW_COPY_AND_ASSIGN(IceTransportChannel);
137};
138
139} // namespace protocol
140} // namespace remoting
141
142#endif // REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_