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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
// Copyright (c) 2010 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/jingle_session.h"
#include "base/message_loop.h"
#include "net/base/net_errors.h"
#include "remoting/base/constants.h"
#include "remoting/jingle_glue/channel_socket_adapter.h"
#include "remoting/jingle_glue/jingle_thread.h"
#include "remoting/jingle_glue/stream_socket_adapter.h"
#include "remoting/protocol/jingle_session_manager.h"
#include "third_party/libjingle/source/talk/base/thread.h"
#include "third_party/libjingle/source/talk/p2p/base/session.h"
#include "third_party/libjingle/source/talk/session/tunnel/pseudotcpchannel.h"
using cricket::BaseSession;
using cricket::PseudoTcpChannel;
namespace remoting {
namespace protocol {
namespace {
const char kControlChannelName[] = "control";
const char kEventChannelName[] = "event";
const char kVideoChannelName[] = "video";
const char kVideoRtpChannelName[] = "videortp";
const char kVideoRtcpChannelName[] = "videortcp";
} // namespace
const char JingleSession::kChromotingContentName[] = "chromoting";
JingleSession::JingleSession(
JingleSessionManager* jingle_session_manager)
: jingle_session_manager_(jingle_session_manager),
state_(INITIALIZING),
closed_(false),
cricket_session_(NULL),
event_channel_(NULL),
video_channel_(NULL) {
}
JingleSession::~JingleSession() {
DCHECK(closed_);
}
void JingleSession::Init(cricket::Session* cricket_session) {
DCHECK_EQ(jingle_session_manager_->message_loop(), MessageLoop::current());
cricket_session_ = cricket_session;
jid_ = cricket_session_->remote_name();
cricket_session_->SignalState.connect(
this, &JingleSession::OnSessionState);
}
bool JingleSession::HasSession(cricket::Session* cricket_session) {
return cricket_session_ == cricket_session;
}
cricket::Session* JingleSession::ReleaseSession() {
DCHECK_EQ(jingle_session_manager_->message_loop(), MessageLoop::current());
SetState(CLOSED);
cricket::Session* session = cricket_session_;
if (cricket_session_)
cricket_session_->SignalState.disconnect(this);
cricket_session_ = NULL;
closed_ = true;
return session;
}
void JingleSession::SetStateChangeCallback(
StateChangeCallback* callback) {
DCHECK_EQ(jingle_session_manager_->message_loop(), MessageLoop::current());
DCHECK(callback);
state_change_callback_.reset(callback);
}
net::Socket* JingleSession::control_channel() {
DCHECK_EQ(jingle_session_manager_->message_loop(), MessageLoop::current());
return control_channel_adapter_.get();
}
net::Socket* JingleSession::event_channel() {
DCHECK_EQ(jingle_session_manager_->message_loop(), MessageLoop::current());
return event_channel_adapter_.get();
}
// TODO(sergeyu): Remove this method after we switch to RTP.
net::Socket* JingleSession::video_channel() {
DCHECK_EQ(jingle_session_manager_->message_loop(), MessageLoop::current());
return video_channel_adapter_.get();
}
net::Socket* JingleSession::video_rtp_channel() {
DCHECK_EQ(jingle_session_manager_->message_loop(), MessageLoop::current());
return video_rtp_channel_.get();
}
net::Socket* JingleSession::video_rtcp_channel() {
DCHECK_EQ(jingle_session_manager_->message_loop(), MessageLoop::current());
return video_rtcp_channel_.get();
}
const std::string& JingleSession::jid() {
// No synchronization is needed because jid_ is not changed
// after new connection is passed to JingleChromotocolServer callback.
return jid_;
}
MessageLoop* JingleSession::message_loop() {
return jingle_session_manager_->message_loop();
}
const CandidateSessionConfig*
JingleSession::candidate_config() {
DCHECK(candidate_config_.get());
return candidate_config_.get();
}
void JingleSession::set_candidate_config(
const CandidateSessionConfig* candidate_config) {
DCHECK(!candidate_config_.get());
DCHECK(candidate_config);
candidate_config_.reset(candidate_config);
}
const SessionConfig* JingleSession::config() {
DCHECK(config_.get());
return config_.get();
}
void JingleSession::set_config(const SessionConfig* config) {
DCHECK(!config_.get());
DCHECK(config);
config_.reset(config);
}
const std::string& JingleSession::initiator_token() {
return initiator_token_;
}
void JingleSession::set_initiator_token(const std::string& initiator_token) {
initiator_token_ = initiator_token;
}
const std::string& JingleSession::receiver_token() {
return receiver_token_;
}
void JingleSession::set_receiver_token(const std::string& receiver_token) {
receiver_token_ = receiver_token;
}
void JingleSession::Close(Task* closed_task) {
if (MessageLoop::current() != jingle_session_manager_->message_loop()) {
jingle_session_manager_->message_loop()->PostTask(
FROM_HERE, NewRunnableMethod(this, &JingleSession::Close,
closed_task));
return;
}
if (!closed_) {
if (control_channel_adapter_.get())
control_channel_adapter_->Close(net::ERR_CONNECTION_CLOSED);
if (control_channel_) {
control_channel_->OnSessionTerminate(cricket_session_);
control_channel_ = NULL;
}
if (event_channel_adapter_.get())
event_channel_adapter_->Close(net::ERR_CONNECTION_CLOSED);
if (event_channel_) {
event_channel_->OnSessionTerminate(cricket_session_);
event_channel_ = NULL;
}
if (video_channel_adapter_.get())
video_channel_adapter_->Close(net::ERR_CONNECTION_CLOSED);
if (video_channel_) {
video_channel_->OnSessionTerminate(cricket_session_);
video_channel_ = NULL;
}
if (video_rtp_channel_.get())
video_rtp_channel_->Close(net::ERR_CONNECTION_CLOSED);
if (video_rtcp_channel_.get())
video_rtcp_channel_->Close(net::ERR_CONNECTION_CLOSED);
if (cricket_session_)
cricket_session_->Terminate();
SetState(CLOSED);
closed_ = true;
}
closed_task->Run();
delete closed_task;
}
void JingleSession::OnSessionState(
BaseSession* session, BaseSession::State state) {
DCHECK_EQ(cricket_session_, session);
switch (state) {
case cricket::Session::STATE_SENTINITIATE:
case cricket::Session::STATE_RECEIVEDINITIATE:
OnInitiate();
break;
case cricket::Session::STATE_SENTACCEPT:
case cricket::Session::STATE_RECEIVEDACCEPT:
OnAccept();
break;
case cricket::Session::STATE_SENTTERMINATE:
case cricket::Session::STATE_RECEIVEDTERMINATE:
case cricket::Session::STATE_SENTREJECT:
case cricket::Session::STATE_RECEIVEDREJECT:
OnTerminate();
break;
case cricket::Session::STATE_DEINIT:
// Close() must have been called before this.
NOTREACHED();
break;
default:
// We don't care about other steates.
break;
}
}
void JingleSession::OnInitiate() {
jid_ = cricket_session_->remote_name();
std::string content_name;
// If we initiate the session, we get to specify the content name. When
// accepting one, the remote end specifies it.
if (cricket_session_->initiator()) {
content_name = kChromotingContentName;
} else {
const cricket::ContentInfo* content;
content = cricket_session_->remote_description()->FirstContentByType(
kChromotingXmlNamespace);
CHECK(content);
content_name = content->name;
}
// Create video RTP channels.
video_rtp_channel_.reset(new TransportChannelSocketAdapter(
cricket_session_->CreateChannel(content_name, kVideoRtpChannelName)));
video_rtcp_channel_.reset(new TransportChannelSocketAdapter(
cricket_session_->CreateChannel(content_name, kVideoRtcpChannelName)));
// Create control channel.
control_channel_ = new PseudoTcpChannel(
jingle_session_manager_->jingle_thread(), cricket_session_);
control_channel_->Connect(content_name, kControlChannelName);
control_channel_adapter_.reset(new StreamSocketAdapter(
control_channel_->GetStream()));
// Create event channel.
event_channel_ = new PseudoTcpChannel(
jingle_session_manager_->jingle_thread(), cricket_session_);
event_channel_->Connect(content_name, kEventChannelName);
event_channel_adapter_.reset(new StreamSocketAdapter(
event_channel_->GetStream()));
// Create video channel.
// TODO(sergeyu): Remove video channel when we are ready to switch to RTP.
video_channel_ = new PseudoTcpChannel(
jingle_session_manager_->jingle_thread(), cricket_session_);
video_channel_->Connect(content_name, kVideoChannelName);
video_channel_adapter_.reset(new StreamSocketAdapter(
video_channel_->GetStream()));
if (!cricket_session_->initiator())
jingle_session_manager_->AcceptConnection(this, cricket_session_);
SetState(CONNECTING);
}
void JingleSession::OnAccept() {
// Set the config if we are the one who initiated the session.
if (cricket_session_->initiator()) {
const cricket::ContentInfo* content =
cricket_session_->remote_description()->FirstContentByType(
kChromotingXmlNamespace);
CHECK(content);
const protocol::ContentDescription* content_description =
static_cast<const protocol::ContentDescription*>(content->description);
SessionConfig* config = content_description->config()->GetFinalConfig();
// Terminate the session if the config we received is invalid.
if (!config || !candidate_config()->IsSupported(config)) {
// TODO(sergeyu): Inform the user that the host is misbehaving?
LOG(ERROR) << "Terminating outgoing session after an "
"invalid session description has been received.";
cricket_session_->Terminate();
return;
}
set_config(config);
}
SetState(CONNECTED);
}
void JingleSession::OnTerminate() {
if (control_channel_adapter_.get())
control_channel_adapter_->Close(net::ERR_CONNECTION_ABORTED);
if (control_channel_) {
control_channel_->OnSessionTerminate(cricket_session_);
control_channel_ = NULL;
}
if (event_channel_adapter_.get())
event_channel_adapter_->Close(net::ERR_CONNECTION_ABORTED);
if (event_channel_) {
event_channel_->OnSessionTerminate(cricket_session_);
event_channel_ = NULL;
}
if (video_channel_adapter_.get())
video_channel_adapter_->Close(net::ERR_CONNECTION_ABORTED);
if (video_channel_) {
video_channel_->OnSessionTerminate(cricket_session_);
video_channel_ = NULL;
}
if (video_rtp_channel_.get())
video_rtp_channel_->Close(net::ERR_CONNECTION_ABORTED);
if (video_rtcp_channel_.get())
video_rtcp_channel_->Close(net::ERR_CONNECTION_ABORTED);
SetState(CLOSED);
closed_ = true;
}
void JingleSession::SetState(State new_state) {
if (new_state != state_) {
state_ = new_state;
if (!closed_ && state_change_callback_.get())
state_change_callback_->Run(new_state);
}
}
} // namespace protocol
} // namespace remoting
|