| 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/webrtc_connection_to_client.h" | 5 #include "remoting/protocol/webrtc_connection_to_client.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "jingle/glue/thread_wrapper.h" |
| 11 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 12 #include "remoting/codec/video_encoder.h" | 13 #include "remoting/codec/video_encoder.h" |
| 13 #include "remoting/codec/video_encoder_verbatim.h" | 14 #include "remoting/codec/video_encoder_verbatim.h" |
| 14 #include "remoting/codec/video_encoder_vpx.h" | 15 #include "remoting/codec/video_encoder_vpx.h" |
| 15 #include "remoting/protocol/audio_writer.h" | 16 #include "remoting/protocol/audio_writer.h" |
| 16 #include "remoting/protocol/clipboard_stub.h" | 17 #include "remoting/protocol/clipboard_stub.h" |
| 17 #include "remoting/protocol/host_control_dispatcher.h" | 18 #include "remoting/protocol/host_control_dispatcher.h" |
| 18 #include "remoting/protocol/host_event_dispatcher.h" | 19 #include "remoting/protocol/host_event_dispatcher.h" |
| 19 #include "remoting/protocol/host_stub.h" | 20 #include "remoting/protocol/host_stub.h" |
| 20 #include "remoting/protocol/input_stub.h" | 21 #include "remoting/protocol/input_stub.h" |
| 22 #include "remoting/protocol/transport_context.h" |
| 21 #include "remoting/protocol/webrtc_transport.h" | 23 #include "remoting/protocol/webrtc_transport.h" |
| 22 #include "remoting/protocol/webrtc_video_capturer_adapter.h" | 24 #include "remoting/protocol/webrtc_video_capturer_adapter.h" |
| 23 #include "remoting/protocol/webrtc_video_stream.h" | 25 #include "remoting/protocol/webrtc_video_stream.h" |
| 24 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h" | 26 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h" |
| 25 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h
" | 27 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h
" |
| 26 #include "third_party/libjingle/source/talk/app/webrtc/test/fakeconstraints.h" | 28 #include "third_party/libjingle/source/talk/app/webrtc/test/fakeconstraints.h" |
| 27 #include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h" | 29 #include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h" |
| 28 | 30 |
| 29 namespace remoting { | 31 namespace remoting { |
| 30 namespace protocol { | 32 namespace protocol { |
| 31 | 33 |
| 32 const char kStreamLabel[] = "screen_stream"; | 34 const char kStreamLabel[] = "screen_stream"; |
| 33 const char kVideoLabel[] = "screen_video"; | 35 const char kVideoLabel[] = "screen_video"; |
| 34 | 36 |
| 37 // Currently the network thread is also used as worker thread for webrtc. |
| 38 // |
| 39 // TODO(sergeyu): Figure out if we would benefit from using a separate |
| 40 // thread as a worker thread. |
| 35 WebrtcConnectionToClient::WebrtcConnectionToClient( | 41 WebrtcConnectionToClient::WebrtcConnectionToClient( |
| 36 scoped_ptr<protocol::Session> session) | 42 scoped_ptr<protocol::Session> session, |
| 37 : session_(std::move(session)), | 43 scoped_refptr<protocol::TransportContext> transport_context) |
| 44 : transport_(jingle_glue::JingleThreadWrapper::current(), |
| 45 transport_context, |
| 46 this), |
| 47 session_(std::move(session)), |
| 38 control_dispatcher_(new HostControlDispatcher()), | 48 control_dispatcher_(new HostControlDispatcher()), |
| 39 event_dispatcher_(new HostEventDispatcher()) { | 49 event_dispatcher_(new HostEventDispatcher()) { |
| 40 session_->SetEventHandler(this); | 50 session_->SetEventHandler(this); |
| 51 session_->SetTransport(&transport_); |
| 41 } | 52 } |
| 42 | 53 |
| 43 WebrtcConnectionToClient::~WebrtcConnectionToClient() {} | 54 WebrtcConnectionToClient::~WebrtcConnectionToClient() {} |
| 44 | 55 |
| 45 void WebrtcConnectionToClient::SetEventHandler( | 56 void WebrtcConnectionToClient::SetEventHandler( |
| 46 ConnectionToClient::EventHandler* event_handler) { | 57 ConnectionToClient::EventHandler* event_handler) { |
| 47 DCHECK(thread_checker_.CalledOnValidThread()); | 58 DCHECK(thread_checker_.CalledOnValidThread()); |
| 48 event_handler_ = event_handler; | 59 event_handler_ = event_handler; |
| 49 } | 60 } |
| 50 | 61 |
| 51 protocol::Session* WebrtcConnectionToClient::session() { | 62 protocol::Session* WebrtcConnectionToClient::session() { |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 return session_.get(); | 64 return session_.get(); |
| 54 } | 65 } |
| 55 | 66 |
| 56 void WebrtcConnectionToClient::Disconnect(ErrorCode error) { | 67 void WebrtcConnectionToClient::Disconnect(ErrorCode error) { |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 | 69 |
| 59 control_dispatcher_.reset(); | |
| 60 event_dispatcher_.reset(); | |
| 61 | |
| 62 // This should trigger OnConnectionClosed() event and this object | 70 // This should trigger OnConnectionClosed() event and this object |
| 63 // may be destroyed as the result. | 71 // may be destroyed as the result. |
| 64 session_->Close(error); | 72 session_->Close(error); |
| 65 } | 73 } |
| 66 | 74 |
| 67 void WebrtcConnectionToClient::OnInputEventReceived(int64_t timestamp) { | 75 void WebrtcConnectionToClient::OnInputEventReceived(int64_t timestamp) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 76 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 event_handler_->OnInputEventReceived(this, timestamp); | 77 event_handler_->OnInputEventReceived(this, timestamp); |
| 70 } | 78 } |
| 71 | 79 |
| 72 scoped_ptr<VideoStream> WebrtcConnectionToClient::StartVideoStream( | 80 scoped_ptr<VideoStream> WebrtcConnectionToClient::StartVideoStream( |
| 73 scoped_ptr<webrtc::DesktopCapturer> desktop_capturer) { | 81 scoped_ptr<webrtc::DesktopCapturer> desktop_capturer) { |
| 74 // TODO(sergeyu): Reconsider Transport interface and how it's used here. | |
| 75 WebrtcTransport* transport = session_->GetTransport()->AsWebrtcTransport(); | |
| 76 CHECK(transport); | |
| 77 | |
| 78 scoped_ptr<WebrtcVideoCapturerAdapter> video_capturer_adapter( | 82 scoped_ptr<WebrtcVideoCapturerAdapter> video_capturer_adapter( |
| 79 new WebrtcVideoCapturerAdapter(std::move(desktop_capturer))); | 83 new WebrtcVideoCapturerAdapter(std::move(desktop_capturer))); |
| 80 | 84 |
| 81 // Set video stream constraints. | 85 // Set video stream constraints. |
| 82 webrtc::FakeConstraints video_constraints; | 86 webrtc::FakeConstraints video_constraints; |
| 83 video_constraints.AddMandatory( | 87 video_constraints.AddMandatory( |
| 84 webrtc::MediaConstraintsInterface::kMinFrameRate, 5); | 88 webrtc::MediaConstraintsInterface::kMinFrameRate, 5); |
| 85 | 89 |
| 86 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track = | 90 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track = |
| 87 transport->peer_connection_factory()->CreateVideoTrack( | 91 transport_.peer_connection_factory()->CreateVideoTrack( |
| 88 kVideoLabel, | 92 kVideoLabel, |
| 89 transport->peer_connection_factory()->CreateVideoSource( | 93 transport_.peer_connection_factory()->CreateVideoSource( |
| 90 video_capturer_adapter.release(), &video_constraints)); | 94 video_capturer_adapter.release(), &video_constraints)); |
| 91 | 95 |
| 92 rtc::scoped_refptr<webrtc::MediaStreamInterface> video_stream = | 96 rtc::scoped_refptr<webrtc::MediaStreamInterface> video_stream = |
| 93 transport->peer_connection_factory()->CreateLocalMediaStream( | 97 transport_.peer_connection_factory()->CreateLocalMediaStream( |
| 94 kStreamLabel); | 98 kStreamLabel); |
| 95 | 99 |
| 96 if (!video_stream->AddTrack(video_track) || | 100 if (!video_stream->AddTrack(video_track) || |
| 97 !transport->peer_connection()->AddStream(video_stream)) { | 101 !transport_.peer_connection()->AddStream(video_stream)) { |
| 98 return nullptr; | 102 return nullptr; |
| 99 } | 103 } |
| 100 | 104 |
| 101 return make_scoped_ptr( | 105 return make_scoped_ptr( |
| 102 new WebrtcVideoStream(transport->peer_connection(), video_stream)); | 106 new WebrtcVideoStream(transport_.peer_connection(), video_stream)); |
| 103 } | 107 } |
| 104 | 108 |
| 105 AudioStub* WebrtcConnectionToClient::audio_stub() { | 109 AudioStub* WebrtcConnectionToClient::audio_stub() { |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | 110 DCHECK(thread_checker_.CalledOnValidThread()); |
| 107 return nullptr; | 111 return nullptr; |
| 108 } | 112 } |
| 109 | 113 |
| 110 // Return pointer to ClientStub. | 114 // Return pointer to ClientStub. |
| 111 ClientStub* WebrtcConnectionToClient::client_stub() { | 115 ClientStub* WebrtcConnectionToClient::client_stub() { |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 138 case Session::CONNECTING: | 142 case Session::CONNECTING: |
| 139 case Session::ACCEPTING: | 143 case Session::ACCEPTING: |
| 140 case Session::ACCEPTED: | 144 case Session::ACCEPTED: |
| 141 // Don't care about these events. | 145 // Don't care about these events. |
| 142 break; | 146 break; |
| 143 case Session::AUTHENTICATING: | 147 case Session::AUTHENTICATING: |
| 144 event_handler_->OnConnectionAuthenticating(this); | 148 event_handler_->OnConnectionAuthenticating(this); |
| 145 break; | 149 break; |
| 146 case Session::AUTHENTICATED: { | 150 case Session::AUTHENTICATED: { |
| 147 // Initialize channels. | 151 // Initialize channels. |
| 148 control_dispatcher_->Init( | 152 control_dispatcher_->Init(transport_.GetStreamChannelFactory(), this); |
| 149 session_->GetTransport()->GetStreamChannelFactory(), | |
| 150 this); | |
| 151 | 153 |
| 152 event_dispatcher_->Init( | 154 event_dispatcher_->Init(transport_.GetStreamChannelFactory(), this); |
| 153 session_->GetTransport()->GetStreamChannelFactory(), this); | |
| 154 event_dispatcher_->set_on_input_event_callback(base::Bind( | 155 event_dispatcher_->set_on_input_event_callback(base::Bind( |
| 155 &ConnectionToClient::OnInputEventReceived, base::Unretained(this))); | 156 &ConnectionToClient::OnInputEventReceived, base::Unretained(this))); |
| 156 | 157 |
| 157 // Notify the handler after initializing the channels, so that | 158 // Notify the handler after initializing the channels, so that |
| 158 // ClientSession can get a client clipboard stub. | 159 // ClientSession can get a client clipboard stub. |
| 159 event_handler_->OnConnectionAuthenticated(this); | 160 event_handler_->OnConnectionAuthenticated(this); |
| 160 break; | 161 break; |
| 161 } | 162 } |
| 162 | 163 |
| 163 case Session::CONNECTED: | |
| 164 event_handler_->OnConnectionChannelsConnected(this); | |
| 165 break; | |
| 166 | |
| 167 case Session::CLOSED: | 164 case Session::CLOSED: |
| 168 case Session::FAILED: | 165 case Session::FAILED: |
| 169 control_dispatcher_.reset(); | 166 control_dispatcher_.reset(); |
| 170 event_dispatcher_.reset(); | 167 event_dispatcher_.reset(); |
| 171 event_handler_->OnConnectionClosed( | 168 event_handler_->OnConnectionClosed( |
| 172 this, state == Session::CLOSED ? OK : session_->error()); | 169 this, state == Session::CLOSED ? OK : session_->error()); |
| 173 break; | 170 break; |
| 174 } | 171 } |
| 175 } | 172 } |
| 176 | 173 |
| 177 void WebrtcConnectionToClient::OnSessionRouteChange( | 174 void WebrtcConnectionToClient::OnWebrtcTransportConnected() { |
| 178 const std::string& channel_name, | 175 event_handler_->OnConnectionChannelsConnected(this); |
| 179 const TransportRoute& route) { | 176 } |
| 180 event_handler_->OnRouteChange(this, channel_name, route); | 177 |
| 178 void WebrtcConnectionToClient::OnWebrtcTransportError(ErrorCode error) { |
| 179 DCHECK(thread_checker_.CalledOnValidThread()); |
| 180 Disconnect(error); |
| 181 } | 181 } |
| 182 | 182 |
| 183 void WebrtcConnectionToClient::OnChannelInitialized( | 183 void WebrtcConnectionToClient::OnChannelInitialized( |
| 184 ChannelDispatcherBase* channel_dispatcher) { | 184 ChannelDispatcherBase* channel_dispatcher) { |
| 185 DCHECK(thread_checker_.CalledOnValidThread()); | 185 DCHECK(thread_checker_.CalledOnValidThread()); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void WebrtcConnectionToClient::OnChannelError( | 188 void WebrtcConnectionToClient::OnChannelError( |
| 189 ChannelDispatcherBase* channel_dispatcher, | 189 ChannelDispatcherBase* channel_dispatcher, |
| 190 ErrorCode error) { | 190 ErrorCode error) { |
| 191 DCHECK(thread_checker_.CalledOnValidThread()); | 191 DCHECK(thread_checker_.CalledOnValidThread()); |
| 192 | 192 |
| 193 LOG(ERROR) << "Failed to connect channel " | 193 LOG(ERROR) << "Failed to connect channel " |
| 194 << channel_dispatcher->channel_name(); | 194 << channel_dispatcher->channel_name(); |
| 195 session_->Close(CHANNEL_CONNECTION_ERROR); | 195 Disconnect(error); |
| 196 } | 196 } |
| 197 | 197 |
| 198 } // namespace protocol | 198 } // namespace protocol |
| 199 } // namespace remoting | 199 } // namespace remoting |
| OLD | NEW |