| 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_connection_to_client.h" | 5 #include "remoting/protocol/ice_connection_to_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "remoting/codec/video_encoder.h" | 10 #include "remoting/codec/video_encoder.h" |
| 11 #include "remoting/codec/video_encoder_verbatim.h" | 11 #include "remoting/codec/video_encoder_verbatim.h" |
| 12 #include "remoting/codec/video_encoder_vpx.h" | 12 #include "remoting/codec/video_encoder_vpx.h" |
| 13 #include "remoting/protocol/audio_writer.h" | 13 #include "remoting/protocol/audio_writer.h" |
| 14 #include "remoting/protocol/clipboard_stub.h" | 14 #include "remoting/protocol/clipboard_stub.h" |
| 15 #include "remoting/protocol/host_control_dispatcher.h" | 15 #include "remoting/protocol/host_control_dispatcher.h" |
| 16 #include "remoting/protocol/host_event_dispatcher.h" | 16 #include "remoting/protocol/host_event_dispatcher.h" |
| 17 #include "remoting/protocol/host_stub.h" | 17 #include "remoting/protocol/host_stub.h" |
| 18 #include "remoting/protocol/host_video_dispatcher.h" | 18 #include "remoting/protocol/host_video_dispatcher.h" |
| 19 #include "remoting/protocol/input_stub.h" | 19 #include "remoting/protocol/input_stub.h" |
| 20 #include "remoting/protocol/transport_context.h" |
| 20 #include "remoting/protocol/video_frame_pump.h" | 21 #include "remoting/protocol/video_frame_pump.h" |
| 21 | 22 |
| 22 namespace remoting { | 23 namespace remoting { |
| 23 namespace protocol { | 24 namespace protocol { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 scoped_ptr<VideoEncoder> CreateVideoEncoder( | 28 scoped_ptr<VideoEncoder> CreateVideoEncoder( |
| 28 const protocol::SessionConfig& config) { | 29 const protocol::SessionConfig& config) { |
| 29 const protocol::ChannelConfig& video_config = config.video_config(); | 30 const protocol::ChannelConfig& video_config = config.video_config(); |
| 30 | 31 |
| 31 if (video_config.codec == protocol::ChannelConfig::CODEC_VP8) { | 32 if (video_config.codec == protocol::ChannelConfig::CODEC_VP8) { |
| 32 return VideoEncoderVpx::CreateForVP8(); | 33 return VideoEncoderVpx::CreateForVP8(); |
| 33 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VP9) { | 34 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VP9) { |
| 34 return VideoEncoderVpx::CreateForVP9(); | 35 return VideoEncoderVpx::CreateForVP9(); |
| 35 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) { | 36 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) { |
| 36 return make_scoped_ptr(new VideoEncoderVerbatim()); | 37 return make_scoped_ptr(new VideoEncoderVerbatim()); |
| 37 } | 38 } |
| 38 | 39 |
| 39 NOTREACHED(); | 40 NOTREACHED(); |
| 40 return nullptr; | 41 return nullptr; |
| 41 } | 42 } |
| 42 | 43 |
| 43 } // namespace | 44 } // namespace |
| 44 | 45 |
| 45 IceConnectionToClient::IceConnectionToClient( | 46 IceConnectionToClient::IceConnectionToClient( |
| 46 scoped_ptr<protocol::Session> session, | 47 scoped_ptr<protocol::Session> session, |
| 48 scoped_refptr<TransportContext> transport_context, |
| 47 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner) | 49 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner) |
| 48 : event_handler_(nullptr), | 50 : event_handler_(nullptr), |
| 49 session_(std::move(session)), | 51 session_(std::move(session)), |
| 50 video_encode_task_runner_(video_encode_task_runner), | 52 video_encode_task_runner_(video_encode_task_runner), |
| 53 transport_(transport_context, this), |
| 51 control_dispatcher_(new HostControlDispatcher()), | 54 control_dispatcher_(new HostControlDispatcher()), |
| 52 event_dispatcher_(new HostEventDispatcher()), | 55 event_dispatcher_(new HostEventDispatcher()), |
| 53 video_dispatcher_(new HostVideoDispatcher()) { | 56 video_dispatcher_(new HostVideoDispatcher()) { |
| 54 session_->SetEventHandler(this); | 57 session_->SetEventHandler(this); |
| 58 session_->SetTransport(&transport_); |
| 55 } | 59 } |
| 56 | 60 |
| 57 IceConnectionToClient::~IceConnectionToClient() {} | 61 IceConnectionToClient::~IceConnectionToClient() {} |
| 58 | 62 |
| 59 void IceConnectionToClient::SetEventHandler( | 63 void IceConnectionToClient::SetEventHandler( |
| 60 ConnectionToClient::EventHandler* event_handler) { | 64 ConnectionToClient::EventHandler* event_handler) { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | 65 DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 event_handler_ = event_handler; | 66 event_handler_ = event_handler; |
| 63 } | 67 } |
| 64 | 68 |
| 65 protocol::Session* IceConnectionToClient::session() { | 69 protocol::Session* IceConnectionToClient::session() { |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 67 return session_.get(); | 71 return session_.get(); |
| 68 } | 72 } |
| 69 | 73 |
| 70 void IceConnectionToClient::Disconnect(ErrorCode error) { | 74 void IceConnectionToClient::Disconnect(ErrorCode error) { |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); | 75 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 | 76 |
| 73 CloseChannels(); | |
| 74 | |
| 75 // This should trigger OnConnectionClosed() event and this object | 77 // This should trigger OnConnectionClosed() event and this object |
| 76 // may be destroyed as the result. | 78 // may be destroyed as the result. |
| 77 session_->Close(error); | 79 session_->Close(error); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void IceConnectionToClient::OnInputEventReceived(int64_t timestamp) { | 82 void IceConnectionToClient::OnInputEventReceived(int64_t timestamp) { |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 82 event_handler_->OnInputEventReceived(this, timestamp); | 84 event_handler_->OnInputEventReceived(this, timestamp); |
| 83 } | 85 } |
| 84 | 86 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 | 129 |
| 128 void IceConnectionToClient::OnSessionStateChange(Session::State state) { | 130 void IceConnectionToClient::OnSessionStateChange(Session::State state) { |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | 131 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 | 132 |
| 131 DCHECK(event_handler_); | 133 DCHECK(event_handler_); |
| 132 switch (state) { | 134 switch (state) { |
| 133 case Session::INITIALIZING: | 135 case Session::INITIALIZING: |
| 134 case Session::CONNECTING: | 136 case Session::CONNECTING: |
| 135 case Session::ACCEPTING: | 137 case Session::ACCEPTING: |
| 136 case Session::ACCEPTED: | 138 case Session::ACCEPTED: |
| 137 case Session::CONNECTED: | |
| 138 // Don't care about these events. | 139 // Don't care about these events. |
| 139 break; | 140 break; |
| 140 case Session::AUTHENTICATING: | 141 case Session::AUTHENTICATING: |
| 141 event_handler_->OnConnectionAuthenticating(this); | 142 event_handler_->OnConnectionAuthenticating(this); |
| 142 break; | 143 break; |
| 143 case Session::AUTHENTICATED: | 144 case Session::AUTHENTICATED: |
| 144 // Initialize channels. | 145 // Initialize channels. |
| 145 control_dispatcher_->Init( | 146 control_dispatcher_->Init(transport_.GetMultiplexedChannelFactory(), |
| 146 session_->GetTransport()->GetMultiplexedChannelFactory(), this); | 147 this); |
| 147 | 148 |
| 148 event_dispatcher_->Init( | 149 event_dispatcher_->Init(transport_.GetMultiplexedChannelFactory(), this); |
| 149 session_->GetTransport()->GetMultiplexedChannelFactory(), this); | |
| 150 event_dispatcher_->set_on_input_event_callback( | 150 event_dispatcher_->set_on_input_event_callback( |
| 151 base::Bind(&IceConnectionToClient::OnInputEventReceived, | 151 base::Bind(&IceConnectionToClient::OnInputEventReceived, |
| 152 base::Unretained(this))); | 152 base::Unretained(this))); |
| 153 | 153 |
| 154 video_dispatcher_->Init( | 154 video_dispatcher_->Init(transport_.GetStreamChannelFactory(), this); |
| 155 session_->GetTransport()->GetStreamChannelFactory(), this); | |
| 156 | 155 |
| 157 audio_writer_ = AudioWriter::Create(session_->config()); | 156 audio_writer_ = AudioWriter::Create(session_->config()); |
| 158 if (audio_writer_.get()) { | 157 if (audio_writer_) |
| 159 audio_writer_->Init( | 158 audio_writer_->Init(transport_.GetMultiplexedChannelFactory(), this); |
| 160 session_->GetTransport()->GetMultiplexedChannelFactory(), this); | |
| 161 } | |
| 162 | 159 |
| 163 // Notify the handler after initializing the channels, so that | 160 // Notify the handler after initializing the channels, so that |
| 164 // ClientSession can get a client clipboard stub. | 161 // ClientSession can get a client clipboard stub. |
| 165 event_handler_->OnConnectionAuthenticated(this); | 162 event_handler_->OnConnectionAuthenticated(this); |
| 166 break; | 163 break; |
| 167 | 164 |
| 168 case Session::CLOSED: | 165 case Session::CLOSED: |
| 169 Close(OK); | |
| 170 break; | |
| 171 | |
| 172 case Session::FAILED: | 166 case Session::FAILED: |
| 173 Close(session_->error()); | 167 CloseChannels(); |
| 168 event_handler_->OnConnectionClosed( |
| 169 this, state == Session::FAILED ? session_->error() : OK); |
| 174 break; | 170 break; |
| 175 } | 171 } |
| 176 } | 172 } |
| 177 | 173 |
| 178 void IceConnectionToClient::OnSessionRouteChange( | 174 |
| 175 void IceConnectionToClient::OnIceTransportRouteChange( |
| 179 const std::string& channel_name, | 176 const std::string& channel_name, |
| 180 const TransportRoute& route) { | 177 const TransportRoute& route) { |
| 181 event_handler_->OnRouteChange(this, channel_name, route); | 178 event_handler_->OnRouteChange(this, channel_name, route); |
| 182 } | 179 } |
| 183 | 180 |
| 181 void IceConnectionToClient::OnIceTransportError(ErrorCode error) { |
| 182 DCHECK(thread_checker_.CalledOnValidThread()); |
| 183 |
| 184 Disconnect(error); |
| 185 } |
| 186 |
| 184 void IceConnectionToClient::OnChannelInitialized( | 187 void IceConnectionToClient::OnChannelInitialized( |
| 185 ChannelDispatcherBase* channel_dispatcher) { | 188 ChannelDispatcherBase* channel_dispatcher) { |
| 186 DCHECK(thread_checker_.CalledOnValidThread()); | 189 DCHECK(thread_checker_.CalledOnValidThread()); |
| 187 | 190 |
| 188 NotifyIfChannelsReady(); | 191 NotifyIfChannelsReady(); |
| 189 } | 192 } |
| 190 | 193 |
| 191 void IceConnectionToClient::OnChannelError( | 194 void IceConnectionToClient::OnChannelError( |
| 192 ChannelDispatcherBase* channel_dispatcher, | 195 ChannelDispatcherBase* channel_dispatcher, |
| 193 ErrorCode error) { | 196 ErrorCode error) { |
| 194 DCHECK(thread_checker_.CalledOnValidThread()); | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 195 | 198 |
| 196 LOG(ERROR) << "Failed to connect channel " | 199 LOG(ERROR) << "Failed to connect channel " |
| 197 << channel_dispatcher->channel_name(); | 200 << channel_dispatcher->channel_name(); |
| 198 Close(CHANNEL_CONNECTION_ERROR); | 201 Disconnect(error); |
| 199 } | 202 } |
| 200 | 203 |
| 201 void IceConnectionToClient::NotifyIfChannelsReady() { | 204 void IceConnectionToClient::NotifyIfChannelsReady() { |
| 202 DCHECK(thread_checker_.CalledOnValidThread()); | 205 DCHECK(thread_checker_.CalledOnValidThread()); |
| 203 | 206 |
| 204 if (!control_dispatcher_ || !control_dispatcher_->is_connected()) | 207 if (!control_dispatcher_ || !control_dispatcher_->is_connected()) |
| 205 return; | 208 return; |
| 206 if (!event_dispatcher_ || !event_dispatcher_->is_connected()) | 209 if (!event_dispatcher_ || !event_dispatcher_->is_connected()) |
| 207 return; | 210 return; |
| 208 if (!video_dispatcher_ || !video_dispatcher_->is_connected()) | 211 if (!video_dispatcher_ || !video_dispatcher_->is_connected()) |
| 209 return; | 212 return; |
| 210 if ((!audio_writer_ || !audio_writer_->is_connected()) && | 213 if ((!audio_writer_ || !audio_writer_->is_connected()) && |
| 211 session_->config().is_audio_enabled()) { | 214 session_->config().is_audio_enabled()) { |
| 212 return; | 215 return; |
| 213 } | 216 } |
| 214 event_handler_->OnConnectionChannelsConnected(this); | 217 event_handler_->OnConnectionChannelsConnected(this); |
| 215 } | 218 } |
| 216 | 219 |
| 217 void IceConnectionToClient::Close(ErrorCode error) { | |
| 218 CloseChannels(); | |
| 219 event_handler_->OnConnectionClosed(this, error); | |
| 220 } | |
| 221 | |
| 222 void IceConnectionToClient::CloseChannels() { | 220 void IceConnectionToClient::CloseChannels() { |
| 223 control_dispatcher_.reset(); | 221 control_dispatcher_.reset(); |
| 224 event_dispatcher_.reset(); | 222 event_dispatcher_.reset(); |
| 225 video_dispatcher_.reset(); | 223 video_dispatcher_.reset(); |
| 226 audio_writer_.reset(); | 224 audio_writer_.reset(); |
| 227 } | 225 } |
| 228 | 226 |
| 229 } // namespace protocol | 227 } // namespace protocol |
| 230 } // namespace remoting | 228 } // namespace remoting |
| OLD | NEW |