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