| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/ice_transport_session.h" | 5 #include "remoting/protocol/ice_transport_session.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "remoting/protocol/channel_authenticator.h" | 8 #include "remoting/protocol/channel_authenticator.h" |
| 8 #include "remoting/protocol/channel_multiplexer.h" | 9 #include "remoting/protocol/channel_multiplexer.h" |
| 9 #include "remoting/protocol/libjingle_transport_factory.h" | |
| 10 #include "remoting/protocol/pseudotcp_channel_factory.h" | 10 #include "remoting/protocol/pseudotcp_channel_factory.h" |
| 11 #include "remoting/protocol/secure_channel_factory.h" | 11 #include "remoting/protocol/secure_channel_factory.h" |
| 12 #include "remoting/protocol/stream_channel_factory.h" | 12 #include "remoting/protocol/stream_channel_factory.h" |
| 13 | 13 |
| 14 namespace remoting { | 14 namespace remoting { |
| 15 namespace protocol { | 15 namespace protocol { |
| 16 | 16 |
| 17 // Delay after candidate creation before sending transport-info message to | 17 // Delay after candidate creation before sending transport-info message to |
| 18 // accumulate multiple candidates. This is an optimization to reduce number of | 18 // accumulate multiple candidates. This is an optimization to reduce number of |
| 19 // transport-info messages. | 19 // transport-info messages. |
| 20 const int kTransportInfoSendDelayMs = 20; | 20 const int kTransportInfoSendDelayMs = 20; |
| 21 | 21 |
| 22 // Name of the multiplexed channel. | 22 // Name of the multiplexed channel. |
| 23 static const char kMuxChannelName[] = "mux"; | 23 static const char kMuxChannelName[] = "mux"; |
| 24 | 24 |
| 25 IceTransportSession::IceTransportSession( | 25 IceTransportSession::IceTransportSession( |
| 26 LibjingleTransportFactory* libjingle_transport_factory) | 26 cricket::PortAllocator* port_allocator, |
| 27 : libjingle_transport_factory_(libjingle_transport_factory) {} | 27 const NetworkSettings& network_settings, |
| 28 TransportRole role) |
| 29 : port_allocator_(port_allocator), |
| 30 network_settings_(network_settings), |
| 31 role_(role), |
| 32 weak_factory_(this) {} |
| 28 | 33 |
| 29 IceTransportSession::~IceTransportSession() { | 34 IceTransportSession::~IceTransportSession() { |
| 30 channel_multiplexer_.reset(); | 35 channel_multiplexer_.reset(); |
| 31 DCHECK(channels_.empty()); | 36 DCHECK(channels_.empty()); |
| 32 } | 37 } |
| 33 | 38 |
| 39 base::Closure IceTransportSession::GetCanStartClosure() { |
| 40 return base::Bind(&IceTransportSession::OnCanStart, |
| 41 weak_factory_.GetWeakPtr()); |
| 42 } |
| 43 |
| 34 void IceTransportSession::Start(TransportSession::EventHandler* event_handler, | 44 void IceTransportSession::Start(TransportSession::EventHandler* event_handler, |
| 35 Authenticator* authenticator) { | 45 Authenticator* authenticator) { |
| 46 DCHECK(event_handler); |
| 47 DCHECK(!event_handler_); |
| 48 |
| 36 event_handler_ = event_handler; | 49 event_handler_ = event_handler; |
| 37 pseudotcp_channel_factory_.reset(new PseudoTcpChannelFactory(this)); | 50 pseudotcp_channel_factory_.reset(new PseudoTcpChannelFactory(this)); |
| 38 secure_channel_factory_.reset(new SecureChannelFactory( | 51 secure_channel_factory_.reset(new SecureChannelFactory( |
| 39 pseudotcp_channel_factory_.get(), authenticator)); | 52 pseudotcp_channel_factory_.get(), authenticator)); |
| 40 } | 53 } |
| 41 | 54 |
| 42 bool IceTransportSession::ProcessTransportInfo( | 55 bool IceTransportSession::ProcessTransportInfo( |
| 43 buzz::XmlElement* transport_info_xml) { | 56 buzz::XmlElement* transport_info_xml) { |
| 44 IceTransportInfo transport_info; | 57 IceTransportInfo transport_info; |
| 45 if (!transport_info.ParseXml(transport_info_xml)) | 58 if (!transport_info.ParseXml(transport_info_xml)) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 } | 94 } |
| 82 | 95 |
| 83 StreamChannelFactory* IceTransportSession::GetMultiplexedChannelFactory() { | 96 StreamChannelFactory* IceTransportSession::GetMultiplexedChannelFactory() { |
| 84 if (!channel_multiplexer_.get()) { | 97 if (!channel_multiplexer_.get()) { |
| 85 channel_multiplexer_.reset( | 98 channel_multiplexer_.reset( |
| 86 new ChannelMultiplexer(GetStreamChannelFactory(), kMuxChannelName)); | 99 new ChannelMultiplexer(GetStreamChannelFactory(), kMuxChannelName)); |
| 87 } | 100 } |
| 88 return channel_multiplexer_.get(); | 101 return channel_multiplexer_.get(); |
| 89 } | 102 } |
| 90 | 103 |
| 104 void IceTransportSession::OnCanStart() { |
| 105 DCHECK(!can_start_); |
| 106 |
| 107 can_start_ = true; |
| 108 for (ChannelsMap::iterator it = channels_.begin(); it != channels_.end(); |
| 109 ++it) { |
| 110 it->second->OnCanStart(); |
| 111 } |
| 112 } |
| 113 |
| 91 void IceTransportSession::CreateChannel( | 114 void IceTransportSession::CreateChannel( |
| 92 const std::string& name, | 115 const std::string& name, |
| 93 const ChannelCreatedCallback& callback) { | 116 const ChannelCreatedCallback& callback) { |
| 94 DCHECK(!channels_[name]); | 117 DCHECK(!channels_[name]); |
| 95 | 118 |
| 96 scoped_ptr<Transport> channel = | 119 scoped_ptr<IceTransportChannel> channel( |
| 97 libjingle_transport_factory_->CreateTransport(); | 120 new IceTransportChannel(port_allocator_, network_settings_, role_)); |
| 121 if (can_start_) |
| 122 channel->OnCanStart(); |
| 98 channel->Connect(name, this, callback); | 123 channel->Connect(name, this, callback); |
| 99 AddPendingRemoteTransportInfo(channel.get()); | 124 AddPendingRemoteTransportInfo(channel.get()); |
| 100 channels_[name] = channel.release(); | 125 channels_[name] = channel.release(); |
| 101 } | 126 } |
| 102 | 127 |
| 103 void IceTransportSession::CancelChannelCreation(const std::string& name) { | 128 void IceTransportSession::CancelChannelCreation(const std::string& name) { |
| 104 ChannelsMap::iterator it = channels_.find(name); | 129 ChannelsMap::iterator it = channels_.find(name); |
| 105 if (it != channels_.end()) { | 130 if (it != channels_.end()) { |
| 106 DCHECK(!it->second->is_connected()); | 131 DCHECK(!it->second->is_connected()); |
| 107 delete it->second; | 132 delete it->second; |
| 108 DCHECK(channels_.find(name) == channels_.end()); | 133 DCHECK(channels_.find(name) == channels_.end()); |
| 109 } | 134 } |
| 110 } | 135 } |
| 111 | 136 |
| 112 void IceTransportSession::AddPendingRemoteTransportInfo(Transport* channel) { | 137 void IceTransportSession::AddPendingRemoteTransportInfo( |
| 138 IceTransportChannel* channel) { |
| 113 std::list<IceTransportInfo::IceCredentials>::iterator credentials = | 139 std::list<IceTransportInfo::IceCredentials>::iterator credentials = |
| 114 pending_remote_ice_credentials_.begin(); | 140 pending_remote_ice_credentials_.begin(); |
| 115 while (credentials != pending_remote_ice_credentials_.end()) { | 141 while (credentials != pending_remote_ice_credentials_.end()) { |
| 116 if (credentials->channel == channel->name()) { | 142 if (credentials->channel == channel->name()) { |
| 117 channel->SetRemoteCredentials(credentials->ufrag, credentials->password); | 143 channel->SetRemoteCredentials(credentials->ufrag, credentials->password); |
| 118 credentials = pending_remote_ice_credentials_.erase(credentials); | 144 credentials = pending_remote_ice_credentials_.erase(credentials); |
| 119 } else { | 145 } else { |
| 120 ++credentials; | 146 ++credentials; |
| 121 } | 147 } |
| 122 } | 148 } |
| 123 | 149 |
| 124 std::list<IceTransportInfo::NamedCandidate>::iterator candidate = | 150 std::list<IceTransportInfo::NamedCandidate>::iterator candidate = |
| 125 pending_remote_candidates_.begin(); | 151 pending_remote_candidates_.begin(); |
| 126 while (candidate != pending_remote_candidates_.end()) { | 152 while (candidate != pending_remote_candidates_.end()) { |
| 127 if (candidate->name == channel->name()) { | 153 if (candidate->name == channel->name()) { |
| 128 channel->AddRemoteCandidate(candidate->candidate); | 154 channel->AddRemoteCandidate(candidate->candidate); |
| 129 candidate = pending_remote_candidates_.erase(candidate); | 155 candidate = pending_remote_candidates_.erase(candidate); |
| 130 } else { | 156 } else { |
| 131 ++candidate; | 157 ++candidate; |
| 132 } | 158 } |
| 133 } | 159 } |
| 134 } | 160 } |
| 135 | 161 |
| 136 void IceTransportSession::OnTransportIceCredentials( | 162 void IceTransportSession::OnTransportIceCredentials( |
| 137 Transport* transport, | 163 IceTransportChannel* channel, |
| 138 const std::string& ufrag, | 164 const std::string& ufrag, |
| 139 const std::string& password) { | 165 const std::string& password) { |
| 140 EnsurePendingTransportInfoMessage(); | 166 EnsurePendingTransportInfoMessage(); |
| 141 pending_transport_info_message_->ice_credentials.push_back( | 167 pending_transport_info_message_->ice_credentials.push_back( |
| 142 IceTransportInfo::IceCredentials(transport->name(), ufrag, password)); | 168 IceTransportInfo::IceCredentials(channel->name(), ufrag, password)); |
| 143 } | 169 } |
| 144 | 170 |
| 145 void IceTransportSession::OnTransportCandidate( | 171 void IceTransportSession::OnTransportCandidate( |
| 146 Transport* transport, | 172 IceTransportChannel* channel, |
| 147 const cricket::Candidate& candidate) { | 173 const cricket::Candidate& candidate) { |
| 148 EnsurePendingTransportInfoMessage(); | 174 EnsurePendingTransportInfoMessage(); |
| 149 pending_transport_info_message_->candidates.push_back( | 175 pending_transport_info_message_->candidates.push_back( |
| 150 IceTransportInfo::NamedCandidate(transport->name(), candidate)); | 176 IceTransportInfo::NamedCandidate(channel->name(), candidate)); |
| 151 } | 177 } |
| 152 | 178 |
| 153 void IceTransportSession::OnTransportRouteChange(Transport* transport, | 179 void IceTransportSession::OnTransportRouteChange(IceTransportChannel* channel, |
| 154 const TransportRoute& route) { | 180 const TransportRoute& route) { |
| 155 if (event_handler_) | 181 if (event_handler_) |
| 156 event_handler_->OnTransportRouteChange(transport->name(), route); | 182 event_handler_->OnTransportRouteChange(channel->name(), route); |
| 157 } | 183 } |
| 158 | 184 |
| 159 void IceTransportSession::OnTransportFailed(Transport* transport) { | 185 void IceTransportSession::OnTransportFailed(IceTransportChannel* channel) { |
| 160 event_handler_->OnTransportError(CHANNEL_CONNECTION_ERROR); | 186 event_handler_->OnTransportError(CHANNEL_CONNECTION_ERROR); |
| 161 } | 187 } |
| 162 | 188 |
| 163 void IceTransportSession::OnTransportDeleted(Transport* transport) { | 189 void IceTransportSession::OnTransportDeleted(IceTransportChannel* channel) { |
| 164 ChannelsMap::iterator it = channels_.find(transport->name()); | 190 ChannelsMap::iterator it = channels_.find(channel->name()); |
| 165 DCHECK_EQ(it->second, transport); | 191 DCHECK_EQ(it->second, channel); |
| 166 channels_.erase(it); | 192 channels_.erase(it); |
| 167 } | 193 } |
| 168 | 194 |
| 169 void IceTransportSession::EnsurePendingTransportInfoMessage() { | 195 void IceTransportSession::EnsurePendingTransportInfoMessage() { |
| 170 // |transport_info_timer_| must be running iff | 196 // |transport_info_timer_| must be running iff |
| 171 // |pending_transport_info_message_| exists. | 197 // |pending_transport_info_message_| exists. |
| 172 DCHECK_EQ(pending_transport_info_message_ != nullptr, | 198 DCHECK_EQ(pending_transport_info_message_ != nullptr, |
| 173 transport_info_timer_.IsRunning()); | 199 transport_info_timer_.IsRunning()); |
| 174 | 200 |
| 175 if (!pending_transport_info_message_) { | 201 if (!pending_transport_info_message_) { |
| 176 pending_transport_info_message_.reset(new IceTransportInfo()); | 202 pending_transport_info_message_.reset(new IceTransportInfo()); |
| 177 // Delay sending the new candidates in case we get more candidates | 203 // Delay sending the new candidates in case we get more candidates |
| 178 // that we can send in one message. | 204 // that we can send in one message. |
| 179 transport_info_timer_.Start( | 205 transport_info_timer_.Start( |
| 180 FROM_HERE, base::TimeDelta::FromMilliseconds(kTransportInfoSendDelayMs), | 206 FROM_HERE, base::TimeDelta::FromMilliseconds(kTransportInfoSendDelayMs), |
| 181 this, &IceTransportSession::SendTransportInfo); | 207 this, &IceTransportSession::SendTransportInfo); |
| 182 } | 208 } |
| 183 } | 209 } |
| 184 | 210 |
| 185 void IceTransportSession::SendTransportInfo() { | 211 void IceTransportSession::SendTransportInfo() { |
| 186 DCHECK(pending_transport_info_message_); | 212 DCHECK(pending_transport_info_message_); |
| 187 event_handler_->OnOutgoingTransportInfo( | 213 event_handler_->OnOutgoingTransportInfo( |
| 188 pending_transport_info_message_->ToXml()); | 214 pending_transport_info_message_->ToXml()); |
| 189 pending_transport_info_message_.reset(); | 215 pending_transport_info_message_.reset(); |
| 190 } | 216 } |
| 191 | 217 |
| 192 } // namespace protocol | 218 } // namespace protocol |
| 193 } // namespace remoting | 219 } // namespace remoting |
| OLD | NEW |