transportcontroller.h revision c4d3a5d44c25fb42c26393b6ddc0feadd52e5e2f
1/* 2 * Copyright 2015 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#ifndef WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ 12#define WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ 13 14#include <map> 15#include <string> 16#include <vector> 17 18#include "webrtc/base/sigslot.h" 19#include "webrtc/base/sslstreamadapter.h" 20#include "webrtc/p2p/base/candidate.h" 21#include "webrtc/p2p/base/transport.h" 22 23namespace rtc { 24class Thread; 25} 26 27namespace cricket { 28 29class TransportController : public sigslot::has_slots<>, 30 public rtc::MessageHandler { 31 public: 32 TransportController(rtc::Thread* signaling_thread, 33 rtc::Thread* worker_thread, 34 PortAllocator* port_allocator); 35 36 virtual ~TransportController(); 37 38 rtc::Thread* signaling_thread() const { return signaling_thread_; } 39 rtc::Thread* worker_thread() const { return worker_thread_; } 40 41 PortAllocator* port_allocator() const { return port_allocator_; } 42 43 // Can only be set before transports are created. 44 // TODO(deadbeef): Make this an argument to the constructor once BaseSession 45 // and WebRtcSession are combined 46 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); 47 48 void SetIceConfig(const IceConfig& config); 49 void SetIceRole(IceRole ice_role); 50 51 // TODO(deadbeef) - Return role of each transport, as role may differ from 52 // one another. 53 // In current implementaion we just return the role of the first transport 54 // alphabetically. 55 bool GetSslRole(rtc::SSLRole* role); 56 57 // Specifies the identity to use in this session. 58 // Can only be called once. 59 bool SetLocalCertificate( 60 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); 61 bool GetLocalCertificate( 62 const std::string& transport_name, 63 rtc::scoped_refptr<rtc::RTCCertificate>* certificate); 64 // Caller owns returned certificate 65 bool GetRemoteSSLCertificate(const std::string& transport_name, 66 rtc::SSLCertificate** cert); 67 bool SetLocalTransportDescription(const std::string& transport_name, 68 const TransportDescription& tdesc, 69 ContentAction action, 70 std::string* err); 71 bool SetRemoteTransportDescription(const std::string& transport_name, 72 const TransportDescription& tdesc, 73 ContentAction action, 74 std::string* err); 75 // Start gathering candidates for any new transports, or transports doing an 76 // ICE restart. 77 void MaybeStartGathering(); 78 bool AddRemoteCandidates(const std::string& transport_name, 79 const Candidates& candidates, 80 std::string* err); 81 bool ReadyForRemoteCandidates(const std::string& transport_name); 82 bool GetStats(const std::string& transport_name, TransportStats* stats); 83 84 // Creates a channel if it doesn't exist. Otherwise, increments a reference 85 // count and returns an existing channel. 86 virtual TransportChannel* CreateTransportChannel_w( 87 const std::string& transport_name, 88 int component); 89 90 // Decrements a channel's reference count, and destroys the channel if 91 // nothing is referencing it. 92 virtual void DestroyTransportChannel_w(const std::string& transport_name, 93 int component); 94 95 // All of these signals are fired on the signalling thread. 96 97 // If any transport failed => failed, 98 // Else if all completed => completed, 99 // Else if all connected => connected, 100 // Else => connecting 101 sigslot::signal1<IceConnectionState> SignalConnectionState; 102 103 // Receiving if any transport is receiving 104 sigslot::signal1<bool> SignalReceiving; 105 106 // If all transports done gathering => complete, 107 // Else if any are gathering => gathering, 108 // Else => new 109 sigslot::signal1<IceGatheringState> SignalGatheringState; 110 111 // (transport_name, candidates) 112 sigslot::signal2<const std::string&, const Candidates&> 113 SignalCandidatesGathered; 114 115 // for unit test 116 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing(); 117 118 protected: 119 // Protected and virtual so we can override it in unit tests. 120 virtual Transport* CreateTransport_w(const std::string& transport_name); 121 122 // For unit tests 123 const std::map<std::string, Transport*>& transports() { return transports_; } 124 Transport* GetTransport_w(const std::string& transport_name); 125 126 private: 127 void OnMessage(rtc::Message* pmsg) override; 128 129 // It's the Transport that's currently responsible for creating/destroying 130 // channels, but the TransportController keeps track of how many external 131 // objects (BaseChannels) reference each channel. 132 struct RefCountedChannel { 133 RefCountedChannel() : impl_(nullptr), ref_(0) {} 134 explicit RefCountedChannel(TransportChannelImpl* impl) 135 : impl_(impl), ref_(0) {} 136 137 void AddRef() { ++ref_; } 138 void DecRef() { 139 ASSERT(ref_ > 0); 140 --ref_; 141 } 142 int ref() const { return ref_; } 143 144 TransportChannelImpl* get() const { return impl_; } 145 TransportChannelImpl* operator->() const { return impl_; } 146 147 private: 148 TransportChannelImpl* impl_; 149 int ref_; 150 }; 151 152 std::vector<RefCountedChannel>::iterator FindChannel_w( 153 const std::string& transport_name, 154 int component); 155 156 Transport* GetOrCreateTransport_w(const std::string& transport_name); 157 void DestroyTransport_w(const std::string& transport_name); 158 void DestroyAllTransports_w(); 159 160 bool SetSslMaxProtocolVersion_w(rtc::SSLProtocolVersion version); 161 void SetIceConfig_w(const IceConfig& config); 162 void SetIceRole_w(IceRole ice_role); 163 bool GetSslRole_w(rtc::SSLRole* role); 164 bool SetLocalCertificate_w( 165 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); 166 bool GetLocalCertificate_w( 167 const std::string& transport_name, 168 rtc::scoped_refptr<rtc::RTCCertificate>* certificate); 169 bool GetRemoteSSLCertificate_w(const std::string& transport_name, 170 rtc::SSLCertificate** cert); 171 bool SetLocalTransportDescription_w(const std::string& transport_name, 172 const TransportDescription& tdesc, 173 ContentAction action, 174 std::string* err); 175 bool SetRemoteTransportDescription_w(const std::string& transport_name, 176 const TransportDescription& tdesc, 177 ContentAction action, 178 std::string* err); 179 void MaybeStartGathering_w(); 180 bool AddRemoteCandidates_w(const std::string& transport_name, 181 const Candidates& candidates, 182 std::string* err); 183 bool ReadyForRemoteCandidates_w(const std::string& transport_name); 184 bool GetStats_w(const std::string& transport_name, TransportStats* stats); 185 186 // Handlers for signals from Transport. 187 void OnChannelWritableState_w(TransportChannel* channel); 188 void OnChannelReceivingState_w(TransportChannel* channel); 189 void OnChannelGatheringState_w(TransportChannelImpl* channel); 190 void OnChannelCandidateGathered_w(TransportChannelImpl* channel, 191 const Candidate& candidate); 192 void OnChannelRoleConflict_w(TransportChannelImpl* channel); 193 void OnChannelConnectionRemoved_w(TransportChannelImpl* channel); 194 195 void UpdateAggregateStates_w(); 196 197 rtc::Thread* const signaling_thread_ = nullptr; 198 rtc::Thread* const worker_thread_ = nullptr; 199 typedef std::map<std::string, Transport*> TransportMap; 200 TransportMap transports_; 201 202 std::vector<RefCountedChannel> channels_; 203 204 PortAllocator* const port_allocator_ = nullptr; 205 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_10; 206 207 // Aggregate state for TransportChannelImpls. 208 IceConnectionState connection_state_ = kIceConnectionConnecting; 209 bool receiving_ = false; 210 IceGatheringState gathering_state_ = kIceGatheringNew; 211 212 // TODO(deadbeef): Move the fields below down to the transports themselves 213 IceConfig ice_config_; 214 IceRole ice_role_ = ICEROLE_CONTROLLING; 215 // Flag which will be set to true after the first role switch 216 bool ice_role_switch_ = false; 217 uint64 ice_tiebreaker_ = rtc::CreateRandomId64(); 218 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; 219}; 220 221} // namespace cricket 222 223#endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ 224