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 "jingle/glue/thread_wrapper.h" |
12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
13 #include "remoting/codec/video_encoder.h" | 13 #include "remoting/codec/video_encoder.h" |
14 #include "remoting/codec/video_encoder_verbatim.h" | 14 #include "remoting/codec/video_encoder_verbatim.h" |
15 #include "remoting/codec/video_encoder_vpx.h" | 15 #include "remoting/codec/video_encoder_vpx.h" |
16 #include "remoting/protocol/audio_writer.h" | 16 #include "remoting/protocol/audio_writer.h" |
17 #include "remoting/protocol/clipboard_stub.h" | 17 #include "remoting/protocol/clipboard_stub.h" |
18 #include "remoting/protocol/host_control_dispatcher.h" | 18 #include "remoting/protocol/host_control_dispatcher.h" |
19 #include "remoting/protocol/host_event_dispatcher.h" | 19 #include "remoting/protocol/host_event_dispatcher.h" |
20 #include "remoting/protocol/host_stub.h" | 20 #include "remoting/protocol/host_stub.h" |
21 #include "remoting/protocol/input_stub.h" | 21 #include "remoting/protocol/input_stub.h" |
22 #include "remoting/protocol/transport_context.h" | 22 #include "remoting/protocol/transport_context.h" |
23 #include "remoting/protocol/webrtc_transport.h" | 23 #include "remoting/protocol/webrtc_transport.h" |
24 #include "remoting/protocol/webrtc_video_capturer_adapter.h" | |
25 #include "remoting/protocol/webrtc_video_stream.h" | 24 #include "remoting/protocol/webrtc_video_stream.h" |
26 #include "third_party/webrtc/api/mediastreaminterface.h" | 25 #include "third_party/webrtc/api/mediastreaminterface.h" |
27 #include "third_party/webrtc/api/peerconnectioninterface.h" | 26 #include "third_party/webrtc/api/peerconnectioninterface.h" |
28 #include "third_party/webrtc/api/test/fakeconstraints.h" | 27 #include "third_party/webrtc/api/test/fakeconstraints.h" |
29 | 28 |
30 namespace remoting { | 29 namespace remoting { |
31 namespace protocol { | 30 namespace protocol { |
32 | 31 |
33 // Currently the network thread is also used as worker thread for webrtc. | 32 // Currently the network thread is also used as worker thread for webrtc. |
34 // | 33 // |
35 // TODO(sergeyu): Figure out if we would benefit from using a separate | 34 // TODO(sergeyu): Figure out if we would benefit from using a separate |
36 // thread as a worker thread. | 35 // thread as a worker thread. |
37 WebrtcConnectionToClient::WebrtcConnectionToClient( | 36 WebrtcConnectionToClient::WebrtcConnectionToClient( |
38 std::unique_ptr<protocol::Session> session, | 37 std::unique_ptr<protocol::Session> session, |
39 scoped_refptr<protocol::TransportContext> transport_context) | 38 scoped_refptr<protocol::TransportContext> transport_context, |
40 : transport_(jingle_glue::JingleThreadWrapper::current(), | 39 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner) |
41 transport_context, | 40 : transport_( |
42 this), | 41 new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(), |
| 42 transport_context, |
| 43 this)), |
43 session_(std::move(session)), | 44 session_(std::move(session)), |
| 45 video_encode_task_runner_(video_encode_task_runner), |
44 control_dispatcher_(new HostControlDispatcher()), | 46 control_dispatcher_(new HostControlDispatcher()), |
45 event_dispatcher_(new HostEventDispatcher()), | 47 event_dispatcher_(new HostEventDispatcher()), |
46 weak_factory_(this) { | 48 weak_factory_(this) { |
47 session_->SetEventHandler(this); | 49 session_->SetEventHandler(this); |
48 session_->SetTransport(&transport_); | 50 session_->SetTransport(transport_.get()); |
49 } | 51 } |
50 | 52 |
51 WebrtcConnectionToClient::~WebrtcConnectionToClient() {} | 53 WebrtcConnectionToClient::~WebrtcConnectionToClient() {} |
52 | 54 |
53 void WebrtcConnectionToClient::SetEventHandler( | 55 void WebrtcConnectionToClient::SetEventHandler( |
54 ConnectionToClient::EventHandler* event_handler) { | 56 ConnectionToClient::EventHandler* event_handler) { |
55 DCHECK(thread_checker_.CalledOnValidThread()); | 57 DCHECK(thread_checker_.CalledOnValidThread()); |
56 event_handler_ = event_handler; | 58 event_handler_ = event_handler; |
57 } | 59 } |
58 | 60 |
(...skipping 10 matching lines...) Expand all Loading... |
69 session_->Close(error); | 71 session_->Close(error); |
70 } | 72 } |
71 | 73 |
72 void WebrtcConnectionToClient::OnInputEventReceived(int64_t timestamp) { | 74 void WebrtcConnectionToClient::OnInputEventReceived(int64_t timestamp) { |
73 DCHECK(thread_checker_.CalledOnValidThread()); | 75 DCHECK(thread_checker_.CalledOnValidThread()); |
74 event_handler_->OnInputEventReceived(this, timestamp); | 76 event_handler_->OnInputEventReceived(this, timestamp); |
75 } | 77 } |
76 | 78 |
77 std::unique_ptr<VideoStream> WebrtcConnectionToClient::StartVideoStream( | 79 std::unique_ptr<VideoStream> WebrtcConnectionToClient::StartVideoStream( |
78 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer) { | 80 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer) { |
| 81 // TODO(isheriff): make this codec independent |
| 82 std::unique_ptr<VideoEncoder> video_encoder = VideoEncoderVpx::CreateForVP8(); |
79 std::unique_ptr<WebrtcVideoStream> stream(new WebrtcVideoStream()); | 83 std::unique_ptr<WebrtcVideoStream> stream(new WebrtcVideoStream()); |
80 if (!stream->Start(std::move(desktop_capturer), transport_.peer_connection(), | 84 if (!stream->Start(std::move(desktop_capturer), transport_.get(), |
81 transport_.peer_connection_factory())) { | 85 video_encode_task_runner_, std::move(video_encoder))) { |
82 return nullptr; | 86 return nullptr; |
83 } | 87 } |
84 return std::move(stream); | 88 return std::move(stream); |
85 } | 89 } |
86 | 90 |
87 AudioStub* WebrtcConnectionToClient::audio_stub() { | 91 AudioStub* WebrtcConnectionToClient::audio_stub() { |
88 DCHECK(thread_checker_.CalledOnValidThread()); | 92 DCHECK(thread_checker_.CalledOnValidThread()); |
89 return nullptr; | 93 return nullptr; |
90 } | 94 } |
91 | 95 |
(...skipping 16 matching lines...) Expand all Loading... |
108 | 112 |
109 void WebrtcConnectionToClient::set_input_stub(protocol::InputStub* input_stub) { | 113 void WebrtcConnectionToClient::set_input_stub(protocol::InputStub* input_stub) { |
110 DCHECK(thread_checker_.CalledOnValidThread()); | 114 DCHECK(thread_checker_.CalledOnValidThread()); |
111 event_dispatcher_->set_input_stub(input_stub); | 115 event_dispatcher_->set_input_stub(input_stub); |
112 } | 116 } |
113 | 117 |
114 void WebrtcConnectionToClient::OnSessionStateChange(Session::State state) { | 118 void WebrtcConnectionToClient::OnSessionStateChange(Session::State state) { |
115 DCHECK(thread_checker_.CalledOnValidThread()); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
116 | 120 |
117 DCHECK(event_handler_); | 121 DCHECK(event_handler_); |
118 switch(state) { | 122 switch (state) { |
119 case Session::INITIALIZING: | 123 case Session::INITIALIZING: |
120 case Session::CONNECTING: | 124 case Session::CONNECTING: |
121 case Session::ACCEPTING: | 125 case Session::ACCEPTING: |
122 case Session::ACCEPTED: | 126 case Session::ACCEPTED: |
123 // Don't care about these events. | 127 // Don't care about these events. |
124 break; | 128 break; |
125 | 129 |
126 case Session::AUTHENTICATING: | 130 case Session::AUTHENTICATING: |
127 event_handler_->OnConnectionAuthenticating(this); | 131 event_handler_->OnConnectionAuthenticating(this); |
128 break; | 132 break; |
129 | 133 |
130 case Session::AUTHENTICATED: { | 134 case Session::AUTHENTICATED: { |
131 base::WeakPtr<WebrtcConnectionToClient> self = weak_factory_.GetWeakPtr(); | 135 base::WeakPtr<WebrtcConnectionToClient> self = weak_factory_.GetWeakPtr(); |
132 event_handler_->OnConnectionAuthenticated(this); | 136 event_handler_->OnConnectionAuthenticated(this); |
133 | 137 |
134 // OnConnectionAuthenticated() call above may result in the connection | 138 // OnConnectionAuthenticated() call above may result in the connection |
135 // being torn down. | 139 // being torn down. |
136 if (self) | 140 if (self) |
137 event_handler_->CreateVideoStreams(this); | 141 event_handler_->CreateVideoStreams(this); |
138 break; | 142 break; |
139 } | 143 } |
140 | 144 |
141 case Session::CLOSED: | 145 case Session::CLOSED: |
142 case Session::FAILED: | 146 case Session::FAILED: |
| 147 transport_->Close(state == Session::CLOSED ? OK : session_->error()); |
143 control_dispatcher_.reset(); | 148 control_dispatcher_.reset(); |
144 event_dispatcher_.reset(); | 149 event_dispatcher_.reset(); |
145 event_handler_->OnConnectionClosed( | 150 event_handler_->OnConnectionClosed( |
146 this, state == Session::CLOSED ? OK : session_->error()); | 151 this, state == Session::CLOSED ? OK : session_->error()); |
147 break; | 152 break; |
148 } | 153 } |
149 } | 154 } |
150 | 155 |
151 void WebrtcConnectionToClient::OnWebrtcTransportConnecting() { | 156 void WebrtcConnectionToClient::OnWebrtcTransportConnecting() { |
152 control_dispatcher_->Init(transport_.outgoing_channel_factory(), this); | 157 control_dispatcher_->Init(transport_->outgoing_channel_factory(), this); |
153 | 158 |
154 event_dispatcher_->Init(transport_.incoming_channel_factory(), this); | 159 event_dispatcher_->Init(transport_->incoming_channel_factory(), this); |
155 event_dispatcher_->set_on_input_event_callback(base::Bind( | 160 event_dispatcher_->set_on_input_event_callback(base::Bind( |
156 &ConnectionToClient::OnInputEventReceived, base::Unretained(this))); | 161 &ConnectionToClient::OnInputEventReceived, base::Unretained(this))); |
157 } | 162 } |
158 | 163 |
159 void WebrtcConnectionToClient::OnWebrtcTransportConnected() { | 164 void WebrtcConnectionToClient::OnWebrtcTransportConnected() { |
160 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
161 } | 166 } |
162 | 167 |
163 void WebrtcConnectionToClient::OnWebrtcTransportError(ErrorCode error) { | 168 void WebrtcConnectionToClient::OnWebrtcTransportError(ErrorCode error) { |
164 DCHECK(thread_checker_.CalledOnValidThread()); | 169 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 13 matching lines...) Expand all Loading... |
178 DCHECK(thread_checker_.CalledOnValidThread()); | 183 DCHECK(thread_checker_.CalledOnValidThread()); |
179 | 184 |
180 if (control_dispatcher_ && control_dispatcher_->is_connected() && | 185 if (control_dispatcher_ && control_dispatcher_->is_connected() && |
181 event_dispatcher_ && event_dispatcher_->is_connected()) { | 186 event_dispatcher_ && event_dispatcher_->is_connected()) { |
182 event_handler_->OnConnectionChannelsConnected(this); | 187 event_handler_->OnConnectionChannelsConnected(this); |
183 } | 188 } |
184 } | 189 } |
185 | 190 |
186 } // namespace protocol | 191 } // namespace protocol |
187 } // namespace remoting | 192 } // namespace remoting |
OLD | NEW |