1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/protocol/webrtc_connection_to_host.h"
#include <utility>
#include "jingle/glue/thread_wrapper.h"
#include "remoting/protocol/client_control_dispatcher.h"
#include "remoting/protocol/client_event_dispatcher.h"
#include "remoting/protocol/client_stub.h"
#include "remoting/protocol/clipboard_stub.h"
#include "remoting/protocol/transport_context.h"
#include "remoting/protocol/video_renderer.h"
#include "remoting/protocol/webrtc_transport.h"
#include "remoting/protocol/webrtc_video_renderer_adapter.h"
namespace remoting {
namespace protocol {
WebrtcConnectionToHost::WebrtcConnectionToHost() {}
WebrtcConnectionToHost::~WebrtcConnectionToHost() {}
void WebrtcConnectionToHost::Connect(
scoped_ptr<Session> session,
scoped_refptr<TransportContext> transport_context,
HostEventCallback* event_callback) {
DCHECK(client_stub_);
DCHECK(clipboard_stub_);
transport_.reset(new WebrtcTransport(
jingle_glue::JingleThreadWrapper::current(), transport_context, this));
session_ = std::move(session);
session_->SetEventHandler(this);
session_->SetTransport(transport_.get());
event_callback_ = event_callback;
SetState(CONNECTING, OK);
}
const SessionConfig& WebrtcConnectionToHost::config() {
return session_->config();
}
ClipboardStub* WebrtcConnectionToHost::clipboard_forwarder() {
return &clipboard_forwarder_;
}
HostStub* WebrtcConnectionToHost::host_stub() {
return control_dispatcher_.get();
}
InputStub* WebrtcConnectionToHost::input_stub() {
return &event_forwarder_;
}
void WebrtcConnectionToHost::set_client_stub(ClientStub* client_stub) {
client_stub_ = client_stub;
}
void WebrtcConnectionToHost::set_clipboard_stub(ClipboardStub* clipboard_stub) {
clipboard_stub_ = clipboard_stub;
}
void WebrtcConnectionToHost::set_video_renderer(VideoRenderer* video_renderer) {
video_renderer_ = video_renderer;
}
void WebrtcConnectionToHost::set_audio_stub(AudioStub* audio_stub) {
NOTIMPLEMENTED();
}
void WebrtcConnectionToHost::OnSessionStateChange(Session::State state) {
DCHECK(event_callback_);
switch (state) {
case Session::INITIALIZING:
case Session::CONNECTING:
case Session::ACCEPTING:
case Session::ACCEPTED:
case Session::AUTHENTICATING:
// Don't care about these events.
break;
case Session::AUTHENTICATED:
SetState(AUTHENTICATED, OK);
break;
case Session::CLOSED:
CloseChannels();
SetState(CLOSED, OK);
break;
case Session::FAILED:
CloseChannels();
SetState(FAILED, session_->error());
break;
}
}
void WebrtcConnectionToHost::OnWebrtcTransportConnecting() {
control_dispatcher_.reset(new ClientControlDispatcher());
control_dispatcher_->Init(transport_->incoming_channel_factory(), this);
control_dispatcher_->set_client_stub(client_stub_);
control_dispatcher_->set_clipboard_stub(clipboard_stub_);
event_dispatcher_.reset(new ClientEventDispatcher());
event_dispatcher_->Init(transport_->outgoing_channel_factory(), this);
}
void WebrtcConnectionToHost::OnWebrtcTransportConnected() {}
void WebrtcConnectionToHost::OnWebrtcTransportError(ErrorCode error) {
CloseChannels();
SetState(FAILED, error);
}
void WebrtcConnectionToHost::OnWebrtcTransportMediaStreamAdded(
scoped_refptr<webrtc::MediaStreamInterface> stream) {
if (video_adapter_) {
LOG(WARNING)
<< "Received multiple media streams. Ignoring all except the last one.";
}
video_adapter_.reset(new WebrtcVideoRendererAdapter(
stream, video_renderer_->GetFrameConsumer()));
}
void WebrtcConnectionToHost::OnWebrtcTransportMediaStreamRemoved(
scoped_refptr<webrtc::MediaStreamInterface> stream) {
if (video_adapter_ && video_adapter_->label() == stream->label())
video_adapter_.reset();
}
void WebrtcConnectionToHost::OnChannelInitialized(
ChannelDispatcherBase* channel_dispatcher) {
NotifyIfChannelsReady();
}
void WebrtcConnectionToHost::OnChannelError(
ChannelDispatcherBase* channel_dispatcher,
ErrorCode error) {
LOG(ERROR) << "Failed to connect channel " << channel_dispatcher;
CloseChannels();
SetState(FAILED, CHANNEL_CONNECTION_ERROR);
}
ConnectionToHost::State WebrtcConnectionToHost::state() const {
return state_;
}
void WebrtcConnectionToHost::NotifyIfChannelsReady() {
if (!control_dispatcher_.get() || !control_dispatcher_->is_connected())
return;
if (!event_dispatcher_.get() || !event_dispatcher_->is_connected())
return;
// Start forwarding clipboard and input events.
clipboard_forwarder_.set_clipboard_stub(control_dispatcher_.get());
event_forwarder_.set_input_stub(event_dispatcher_.get());
SetState(CONNECTED, OK);
}
void WebrtcConnectionToHost::CloseChannels() {
clipboard_forwarder_.set_clipboard_stub(nullptr);
control_dispatcher_.reset();
event_forwarder_.set_input_stub(nullptr);
event_dispatcher_.reset();
}
void WebrtcConnectionToHost::SetState(State state, ErrorCode error) {
// |error| should be specified only when |state| is set to FAILED.
DCHECK(state == FAILED || error == OK);
if (state != state_) {
state_ = state;
error_ = error;
event_callback_->OnConnectionState(state_, error_);
}
}
} // namespace protocol
} // namespace remoting
|