| 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/webrtc_video_encoder_vpx.h" | 14 #include "remoting/codec/webrtc_video_encoder_vpx.h" |
| 15 #include "remoting/protocol/audio_source.h" | 15 #include "remoting/protocol/audio_source.h" |
| 16 #include "remoting/protocol/audio_stream.h" | 16 #include "remoting/protocol/audio_stream.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/message_pipe.h" | 22 #include "remoting/protocol/message_pipe.h" |
| 23 #include "remoting/protocol/transport_context.h" | 23 #include "remoting/protocol/transport_context.h" |
| 24 #include "remoting/protocol/webrtc_audio_stream.h" |
| 24 #include "remoting/protocol/webrtc_transport.h" | 25 #include "remoting/protocol/webrtc_transport.h" |
| 25 #include "remoting/protocol/webrtc_video_stream.h" | 26 #include "remoting/protocol/webrtc_video_stream.h" |
| 26 #include "third_party/webrtc/api/mediastreaminterface.h" | 27 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 27 #include "third_party/webrtc/api/peerconnectioninterface.h" | 28 #include "third_party/webrtc/api/peerconnectioninterface.h" |
| 28 #include "third_party/webrtc/api/test/fakeconstraints.h" | 29 #include "third_party/webrtc/api/test/fakeconstraints.h" |
| 29 | 30 |
| 30 namespace remoting { | 31 namespace remoting { |
| 31 namespace protocol { | 32 namespace protocol { |
| 32 | 33 |
| 33 // Currently the network thread is also used as worker thread for webrtc. | 34 // Currently the network thread is also used as worker thread for webrtc. |
| 34 // | 35 // |
| 35 // TODO(sergeyu): Figure out if we would benefit from using a separate | 36 // TODO(sergeyu): Figure out if we would benefit from using a separate |
| 36 // thread as a worker thread. | 37 // thread as a worker thread. |
| 37 WebrtcConnectionToClient::WebrtcConnectionToClient( | 38 WebrtcConnectionToClient::WebrtcConnectionToClient( |
| 38 std::unique_ptr<protocol::Session> session, | 39 std::unique_ptr<protocol::Session> session, |
| 39 scoped_refptr<protocol::TransportContext> transport_context, | 40 scoped_refptr<protocol::TransportContext> transport_context, |
| 40 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner) | 41 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner, |
| 42 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner) |
| 41 : transport_( | 43 : transport_( |
| 42 new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(), | 44 new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(), |
| 43 transport_context, | 45 transport_context, |
| 44 this)), | 46 this)), |
| 45 session_(std::move(session)), | 47 session_(std::move(session)), |
| 46 video_encode_task_runner_(video_encode_task_runner), | 48 video_encode_task_runner_(video_encode_task_runner), |
| 49 audio_task_runner_(audio_task_runner), |
| 47 control_dispatcher_(new HostControlDispatcher()), | 50 control_dispatcher_(new HostControlDispatcher()), |
| 48 event_dispatcher_(new HostEventDispatcher()), | 51 event_dispatcher_(new HostEventDispatcher()), |
| 49 weak_factory_(this) { | 52 weak_factory_(this) { |
| 50 session_->SetEventHandler(this); | 53 session_->SetEventHandler(this); |
| 51 session_->SetTransport(transport_.get()); | 54 session_->SetTransport(transport_.get()); |
| 52 } | 55 } |
| 53 | 56 |
| 54 WebrtcConnectionToClient::~WebrtcConnectionToClient() {} | 57 WebrtcConnectionToClient::~WebrtcConnectionToClient() {} |
| 55 | 58 |
| 56 void WebrtcConnectionToClient::SetEventHandler( | 59 void WebrtcConnectionToClient::SetEventHandler( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 67 void WebrtcConnectionToClient::Disconnect(ErrorCode error) { | 70 void WebrtcConnectionToClient::Disconnect(ErrorCode error) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 | 72 |
| 70 // This should trigger OnConnectionClosed() event and this object | 73 // This should trigger OnConnectionClosed() event and this object |
| 71 // may be destroyed as the result. | 74 // may be destroyed as the result. |
| 72 session_->Close(error); | 75 session_->Close(error); |
| 73 } | 76 } |
| 74 | 77 |
| 75 std::unique_ptr<VideoStream> WebrtcConnectionToClient::StartVideoStream( | 78 std::unique_ptr<VideoStream> WebrtcConnectionToClient::StartVideoStream( |
| 76 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer) { | 79 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer) { |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); |
| 81 DCHECK(transport_); |
| 82 |
| 77 std::unique_ptr<WebrtcVideoStream> stream(new WebrtcVideoStream()); | 83 std::unique_ptr<WebrtcVideoStream> stream(new WebrtcVideoStream()); |
| 78 if (!stream->Start(std::move(desktop_capturer), transport_.get(), | 84 stream->Start(std::move(desktop_capturer), transport_.get(), |
| 79 video_encode_task_runner_)) { | 85 video_encode_task_runner_); |
| 80 return nullptr; | |
| 81 } | |
| 82 return std::move(stream); | 86 return std::move(stream); |
| 83 } | 87 } |
| 84 | 88 |
| 85 std::unique_ptr<AudioStream> WebrtcConnectionToClient::StartAudioStream( | 89 std::unique_ptr<AudioStream> WebrtcConnectionToClient::StartAudioStream( |
| 86 std::unique_ptr<AudioSource> audio_source) { | 90 std::unique_ptr<AudioSource> audio_source) { |
| 87 NOTIMPLEMENTED(); | 91 DCHECK(thread_checker_.CalledOnValidThread()); |
| 88 return nullptr; | 92 DCHECK(transport_); |
| 93 |
| 94 std::unique_ptr<WebrtcAudioStream> stream(new WebrtcAudioStream()); |
| 95 stream->Start(audio_task_runner_, std::move(audio_source), transport_.get()); |
| 96 return std::move(stream); |
| 89 } | 97 } |
| 90 | 98 |
| 91 // Return pointer to ClientStub. | 99 // Return pointer to ClientStub. |
| 92 ClientStub* WebrtcConnectionToClient::client_stub() { | 100 ClientStub* WebrtcConnectionToClient::client_stub() { |
| 93 DCHECK(thread_checker_.CalledOnValidThread()); | 101 DCHECK(thread_checker_.CalledOnValidThread()); |
| 94 return control_dispatcher_.get(); | 102 return control_dispatcher_.get(); |
| 95 } | 103 } |
| 96 | 104 |
| 97 void WebrtcConnectionToClient::set_clipboard_stub( | 105 void WebrtcConnectionToClient::set_clipboard_stub( |
| 98 protocol::ClipboardStub* clipboard_stub) { | 106 protocol::ClipboardStub* clipboard_stub) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 if (self) | 143 if (self) |
| 136 event_handler_->CreateMediaStreams(); | 144 event_handler_->CreateMediaStreams(); |
| 137 break; | 145 break; |
| 138 } | 146 } |
| 139 | 147 |
| 140 case Session::CLOSED: | 148 case Session::CLOSED: |
| 141 case Session::FAILED: | 149 case Session::FAILED: |
| 142 control_dispatcher_.reset(); | 150 control_dispatcher_.reset(); |
| 143 event_dispatcher_.reset(); | 151 event_dispatcher_.reset(); |
| 144 transport_->Close(state == Session::CLOSED ? OK : session_->error()); | 152 transport_->Close(state == Session::CLOSED ? OK : session_->error()); |
| 153 transport_.reset(); |
| 145 event_handler_->OnConnectionClosed( | 154 event_handler_->OnConnectionClosed( |
| 146 state == Session::CLOSED ? OK : session_->error()); | 155 state == Session::CLOSED ? OK : session_->error()); |
| 147 break; | 156 break; |
| 148 } | 157 } |
| 149 } | 158 } |
| 150 | 159 |
| 151 void WebrtcConnectionToClient::OnWebrtcTransportConnecting() { | 160 void WebrtcConnectionToClient::OnWebrtcTransportConnecting() { |
| 161 DCHECK(thread_checker_.CalledOnValidThread()); |
| 152 // Create outgoing control channel. |event_dispatcher_| is initialized later | 162 // Create outgoing control channel. |event_dispatcher_| is initialized later |
| 153 // because event channel is expected to be created by the client. | 163 // because event channel is expected to be created by the client. |
| 154 control_dispatcher_->Init( | 164 control_dispatcher_->Init( |
| 155 transport_->CreateOutgoingChannel(control_dispatcher_->channel_name()), | 165 transport_->CreateOutgoingChannel(control_dispatcher_->channel_name()), |
| 156 this); | 166 this); |
| 157 } | 167 } |
| 158 | 168 |
| 159 void WebrtcConnectionToClient::OnWebrtcTransportConnected() { | 169 void WebrtcConnectionToClient::OnWebrtcTransportConnected() { |
| 160 DCHECK(thread_checker_.CalledOnValidThread()); | 170 DCHECK(thread_checker_.CalledOnValidThread()); |
| 161 } | 171 } |
| 162 | 172 |
| 163 void WebrtcConnectionToClient::OnWebrtcTransportError(ErrorCode error) { | 173 void WebrtcConnectionToClient::OnWebrtcTransportError(ErrorCode error) { |
| 164 DCHECK(thread_checker_.CalledOnValidThread()); | 174 DCHECK(thread_checker_.CalledOnValidThread()); |
| 165 Disconnect(error); | 175 Disconnect(error); |
| 166 } | 176 } |
| 167 | 177 |
| 168 void WebrtcConnectionToClient::OnWebrtcTransportIncomingDataChannel( | 178 void WebrtcConnectionToClient::OnWebrtcTransportIncomingDataChannel( |
| 169 const std::string& name, | 179 const std::string& name, |
| 170 std::unique_ptr<MessagePipe> pipe) { | 180 std::unique_ptr<MessagePipe> pipe) { |
| 181 DCHECK(thread_checker_.CalledOnValidThread()); |
| 171 if (name == event_dispatcher_->channel_name() && | 182 if (name == event_dispatcher_->channel_name() && |
| 172 !event_dispatcher_->is_connected()) { | 183 !event_dispatcher_->is_connected()) { |
| 173 event_dispatcher_->set_on_input_event_callback( | 184 event_dispatcher_->set_on_input_event_callback( |
| 174 base::Bind(&WebrtcConnectionToClient::OnInputEventReceived, | 185 base::Bind(&WebrtcConnectionToClient::OnInputEventReceived, |
| 175 base::Unretained(this))); | 186 base::Unretained(this))); |
| 176 event_dispatcher_->Init(std::move(pipe), this); | 187 event_dispatcher_->Init(std::move(pipe), this); |
| 177 } | 188 } |
| 178 } | 189 } |
| 179 | 190 |
| 180 void WebrtcConnectionToClient::OnWebrtcTransportMediaStreamAdded( | 191 void WebrtcConnectionToClient::OnWebrtcTransportMediaStreamAdded( |
| 181 scoped_refptr<webrtc::MediaStreamInterface> stream) { | 192 scoped_refptr<webrtc::MediaStreamInterface> stream) { |
| 193 DCHECK(thread_checker_.CalledOnValidThread()); |
| 182 LOG(WARNING) << "The client created an unexpected media stream."; | 194 LOG(WARNING) << "The client created an unexpected media stream."; |
| 183 } | 195 } |
| 184 | 196 |
| 185 void WebrtcConnectionToClient::OnWebrtcTransportMediaStreamRemoved( | 197 void WebrtcConnectionToClient::OnWebrtcTransportMediaStreamRemoved( |
| 186 scoped_refptr<webrtc::MediaStreamInterface> stream) {} | 198 scoped_refptr<webrtc::MediaStreamInterface> stream) { |
| 199 DCHECK(thread_checker_.CalledOnValidThread()); |
| 200 } |
| 187 | 201 |
| 188 void WebrtcConnectionToClient::OnChannelInitialized( | 202 void WebrtcConnectionToClient::OnChannelInitialized( |
| 189 ChannelDispatcherBase* channel_dispatcher) { | 203 ChannelDispatcherBase* channel_dispatcher) { |
| 190 DCHECK(thread_checker_.CalledOnValidThread()); | 204 DCHECK(thread_checker_.CalledOnValidThread()); |
| 191 | 205 |
| 192 if (control_dispatcher_ && control_dispatcher_->is_connected() && | 206 if (control_dispatcher_ && control_dispatcher_->is_connected() && |
| 193 event_dispatcher_ && event_dispatcher_->is_connected()) { | 207 event_dispatcher_ && event_dispatcher_->is_connected()) { |
| 194 event_handler_->OnConnectionChannelsConnected(); | 208 event_handler_->OnConnectionChannelsConnected(); |
| 195 } | 209 } |
| 196 } | 210 } |
| 197 | 211 |
| 198 void WebrtcConnectionToClient::OnChannelClosed( | 212 void WebrtcConnectionToClient::OnChannelClosed( |
| 199 ChannelDispatcherBase* channel_dispatcher) { | 213 ChannelDispatcherBase* channel_dispatcher) { |
| 200 DCHECK(thread_checker_.CalledOnValidThread()); | 214 DCHECK(thread_checker_.CalledOnValidThread()); |
| 201 | 215 |
| 202 LOG(ERROR) << "Channel " << channel_dispatcher->channel_name() | 216 LOG(ERROR) << "Channel " << channel_dispatcher->channel_name() |
| 203 << " was closed unexpectedly."; | 217 << " was closed unexpectedly."; |
| 204 Disconnect(INCOMPATIBLE_PROTOCOL); | 218 Disconnect(INCOMPATIBLE_PROTOCOL); |
| 205 } | 219 } |
| 206 | 220 |
| 207 void WebrtcConnectionToClient::OnInputEventReceived(int64_t timestamp) { | 221 void WebrtcConnectionToClient::OnInputEventReceived(int64_t timestamp) { |
| 208 DCHECK(thread_checker_.CalledOnValidThread()); | 222 DCHECK(thread_checker_.CalledOnValidThread()); |
| 209 event_handler_->OnInputEventReceived(timestamp); | 223 event_handler_->OnInputEventReceived(timestamp); |
| 210 } | 224 } |
| 211 | 225 |
| 212 } // namespace protocol | 226 } // namespace protocol |
| 213 } // namespace remoting | 227 } // namespace remoting |
| OLD | NEW |