deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame^] | 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 | |
| 23 | namespace rtc { |
| 24 | class Thread; |
| 25 | } |
| 26 | |
| 27 | namespace cricket { |
| 28 | |
| 29 | class 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 SetIceConnectionReceivingTimeout(int timeout_ms); |
| 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 | virtual TransportChannel* CreateTransportChannel_w( |
| 85 | const std::string& transport_name, |
| 86 | int component); |
| 87 | virtual void DestroyTransportChannel_w(const std::string& transport_name, |
| 88 | int component); |
| 89 | |
| 90 | // All of these signals are fired on the signalling thread. |
| 91 | |
| 92 | // If any transport failed => failed, |
| 93 | // Else if all completed => completed, |
| 94 | // Else if all connected => connected, |
| 95 | // Else => connecting |
| 96 | sigslot::signal1<IceConnectionState> SignalConnectionState; |
| 97 | |
| 98 | // Receiving if any transport is receiving |
| 99 | sigslot::signal1<bool> SignalReceiving; |
| 100 | |
| 101 | // If all transports done gathering => complete, |
| 102 | // Else if any are gathering => gathering, |
| 103 | // Else => new |
| 104 | sigslot::signal1<IceGatheringState> SignalGatheringState; |
| 105 | |
| 106 | // (transport_name, candidates) |
| 107 | sigslot::signal2<const std::string&, const Candidates&> |
| 108 | SignalCandidatesGathered; |
| 109 | |
| 110 | // for unit test |
| 111 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing(); |
| 112 | |
| 113 | protected: |
| 114 | // Protected and virtual so we can override it in unit tests. |
| 115 | virtual Transport* CreateTransport_w(const std::string& transport_name); |
| 116 | |
| 117 | // For unit tests |
| 118 | const std::map<std::string, Transport*>& transports() { return transports_; } |
| 119 | Transport* GetTransport_w(const std::string& transport_name); |
| 120 | |
| 121 | private: |
| 122 | void OnMessage(rtc::Message* pmsg) override; |
| 123 | |
| 124 | Transport* GetOrCreateTransport_w(const std::string& transport_name); |
| 125 | void DestroyTransport_w(const std::string& transport_name); |
| 126 | void DestroyAllTransports_w(); |
| 127 | |
| 128 | bool SetSslMaxProtocolVersion_w(rtc::SSLProtocolVersion version); |
| 129 | void SetIceConnectionReceivingTimeout_w(int timeout_ms); |
| 130 | void SetIceRole_w(IceRole ice_role); |
| 131 | bool GetSslRole_w(rtc::SSLRole* role); |
| 132 | bool SetLocalCertificate_w( |
| 133 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
| 134 | bool GetLocalCertificate_w( |
| 135 | const std::string& transport_name, |
| 136 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate); |
| 137 | bool GetRemoteSSLCertificate_w(const std::string& transport_name, |
| 138 | rtc::SSLCertificate** cert); |
| 139 | bool SetLocalTransportDescription_w(const std::string& transport_name, |
| 140 | const TransportDescription& tdesc, |
| 141 | ContentAction action, |
| 142 | std::string* err); |
| 143 | bool SetRemoteTransportDescription_w(const std::string& transport_name, |
| 144 | const TransportDescription& tdesc, |
| 145 | ContentAction action, |
| 146 | std::string* err); |
| 147 | void MaybeStartGathering_w(); |
| 148 | bool AddRemoteCandidates_w(const std::string& transport_name, |
| 149 | const Candidates& candidates, |
| 150 | std::string* err); |
| 151 | bool ReadyForRemoteCandidates_w(const std::string& transport_name); |
| 152 | bool GetStats_w(const std::string& transport_name, TransportStats* stats); |
| 153 | |
| 154 | // Handlers for signals from Transport. |
| 155 | void OnTransportConnecting_w(Transport* transport); |
| 156 | void OnTransportWritableState_w(Transport* transport); |
| 157 | void OnTransportReceivingState_w(Transport* transport); |
| 158 | void OnTransportCompleted_w(Transport* transport); |
| 159 | void OnTransportFailed_w(Transport* transport); |
| 160 | void OnTransportGatheringState_w(Transport* transport); |
| 161 | void OnTransportCandidatesGathered_w( |
| 162 | Transport* transport, |
| 163 | const std::vector<Candidate>& candidates); |
| 164 | void OnTransportRoleConflict_w(); |
| 165 | |
| 166 | void UpdateAggregateStates_w(); |
| 167 | bool HasChannels_w(); |
| 168 | |
| 169 | rtc::Thread* const signaling_thread_ = nullptr; |
| 170 | rtc::Thread* const worker_thread_ = nullptr; |
| 171 | typedef std::map<std::string, Transport*> TransportMap; |
| 172 | TransportMap transports_; |
| 173 | |
| 174 | PortAllocator* const port_allocator_ = nullptr; |
| 175 | rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_10; |
| 176 | |
| 177 | // Aggregate state for Transports |
| 178 | IceConnectionState connection_state_ = kIceConnectionConnecting; |
| 179 | bool receiving_ = false; |
| 180 | IceGatheringState gathering_state_ = kIceGatheringNew; |
| 181 | |
| 182 | // TODO(deadbeef): Move the fields below down to the transports themselves |
| 183 | |
| 184 | // Timeout value in milliseconds for which no ICE connection receives |
| 185 | // any packets |
| 186 | int ice_receiving_timeout_ms_ = -1; |
| 187 | IceRole ice_role_ = ICEROLE_CONTROLLING; |
| 188 | // Flag which will be set to true after the first role switch |
| 189 | bool ice_role_switch_ = false; |
| 190 | uint64 ice_tiebreaker_ = rtc::CreateRandomId64(); |
| 191 | rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
| 192 | }; |
| 193 | |
| 194 | } // namespace cricket |
| 195 | |
| 196 | #endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ |