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