ice_transport_channel.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // 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. #ifndef REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_
  5. #define REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_
  6. #include <string>
  7. #include "base/callback_forward.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/threading/thread_checker.h"
  10. #include "base/timer/timer.h"
  11. #include "remoting/protocol/network_settings.h"
  12. #include "remoting/protocol/transport.h"
  13. // TODO(zhihuang):Replace #include by forward declaration once proper
  14. // inheritance is defined for cricket::IceTransportInternal and
  15. // cricket::P2PTransportChannel.
  16. #include "third_party/webrtc/p2p/base/ice_transport_internal.h"
  17. // TODO(johan): Replace #include by forward declaration once proper inheritance
  18. // is defined for rtc::PacketTransportInterface and cricket::TransportChannel.
  19. #include "third_party/webrtc/p2p/base/packet_transport_internal.h"
  20. #include "third_party/webrtc/rtc_base/third_party/sigslot/sigslot.h"
  21. namespace cricket {
  22. class Candidate;
  23. class P2PTransportChannel;
  24. class PortAllocator;
  25. } // namespace cricket
  26. namespace remoting {
  27. namespace protocol {
  28. class P2PDatagramSocket;
  29. class TransportContext;
  30. class IceTransportChannel : public sigslot::has_slots<> {
  31. public:
  32. class Delegate {
  33. public:
  34. Delegate() {}
  35. virtual ~Delegate() {}
  36. // Called to pass ICE credentials to the session. Used only for STANDARD
  37. // version of ICE, see SetIceVersion().
  38. virtual void OnChannelIceCredentials(IceTransportChannel* transport,
  39. const std::string& ufrag,
  40. const std::string& password) = 0;
  41. // Called when the transport generates a new candidate that needs
  42. // to be passed to the AddRemoteCandidate() method on the remote
  43. // end of the connection.
  44. virtual void OnChannelCandidate(IceTransportChannel* transport,
  45. const cricket::Candidate& candidate) = 0;
  46. // Called when transport route changes. Can be called even before
  47. // the transport is connected.
  48. virtual void OnChannelRouteChange(IceTransportChannel* transport,
  49. const TransportRoute& route) = 0;
  50. // Called when when the channel has failed to connect or reconnect.
  51. virtual void OnChannelFailed(IceTransportChannel* transport) = 0;
  52. // Called when the channel is about to be deleted.
  53. virtual void OnChannelDeleted(IceTransportChannel* transport) = 0;
  54. };
  55. typedef base::OnceCallback<void(std::unique_ptr<P2PDatagramSocket>)>
  56. ConnectedCallback;
  57. explicit IceTransportChannel(
  58. scoped_refptr<TransportContext> transport_context);
  59. IceTransportChannel(const IceTransportChannel&) = delete;
  60. IceTransportChannel& operator=(const IceTransportChannel&) = delete;
  61. ~IceTransportChannel() override;
  62. // Connects the channel and calls the |callback| after that.
  63. void Connect(const std::string& name,
  64. Delegate* delegate,
  65. ConnectedCallback callback);
  66. // Sets remote ICE credentials.
  67. void SetRemoteCredentials(const std::string& ufrag,
  68. const std::string& password);
  69. // Adds |candidate| received from the peer.
  70. void AddRemoteCandidate(const cricket::Candidate& candidate);
  71. // Name of the channel. Used to identify the channel and disambiguate
  72. // candidates it generates from candidates generated by parallel connections.
  73. const std::string& name() const;
  74. // Returns true if the channel is already connected.
  75. bool is_connected() const;
  76. private:
  77. void OnPortAllocatorCreated(
  78. std::unique_ptr<cricket::PortAllocator> port_allocator);
  79. void NotifyConnected();
  80. // Signal handlers for cricket::IceTransportInternal.
  81. void OnCandidateGathered(cricket::IceTransportInternal* ice_transport,
  82. const cricket::Candidate& candidate);
  83. void OnRouteChange(cricket::IceTransportInternal* ice_transport,
  84. const cricket::Candidate& candidate);
  85. void OnWritableState(rtc::PacketTransportInternal* transport);
  86. // Callback for TransportChannelSocketAdapter to notify when the socket is
  87. // destroyed.
  88. void OnChannelDestroyed();
  89. void NotifyRouteChanged();
  90. // Tries to connect by restarting ICE. Called by |reconnect_timer_|.
  91. void TryReconnect();
  92. scoped_refptr<TransportContext> transport_context_;
  93. std::string name_;
  94. raw_ptr<Delegate> delegate_ = nullptr;
  95. ConnectedCallback callback_;
  96. std::string ice_username_fragment_;
  97. std::unique_ptr<cricket::PortAllocator> port_allocator_;
  98. std::string remote_ice_username_fragment_;
  99. std::string remote_ice_password_;
  100. std::list<cricket::Candidate> pending_candidates_;
  101. std::unique_ptr<cricket::P2PTransportChannel> channel_;
  102. int connect_attempts_left_;
  103. base::RepeatingTimer reconnect_timer_;
  104. base::ThreadChecker thread_checker_;
  105. base::WeakPtrFactory<IceTransportChannel> weak_factory_{this};
  106. };
  107. } // namespace protocol
  108. } // namespace remoting
  109. #endif // REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_