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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
// Copyright 2014 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 "base/base64.h"
#include "base/files/file_util.h"
#include "base/message_loop/message_loop.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/thread_task_runner_handle.h"
#include "jingle/glue/thread_wrapper.h"
#include "net/base/test_data_directory.h"
#include "net/url_request/url_request_context_getter.h"
#include "remoting/base/rsa_key_pair.h"
#include "remoting/client/audio_player.h"
#include "remoting/client/chromoting_client.h"
#include "remoting/client/client_context.h"
#include "remoting/client/client_user_interface.h"
#include "remoting/client/video_renderer.h"
#include "remoting/host/chromoting_host.h"
#include "remoting/host/chromoting_host_context.h"
#include "remoting/host/fake_desktop_environment.h"
#include "remoting/host/video_frame_pump.h"
#include "remoting/protocol/jingle_session_manager.h"
#include "remoting/protocol/libjingle_transport_factory.h"
#include "remoting/protocol/me2me_host_authenticator_factory.h"
#include "remoting/protocol/negotiating_client_authenticator.h"
#include "remoting/protocol/session_config.h"
#include "remoting/signaling/fake_signal_strategy.h"
#include "remoting/test/fake_network_dispatcher.h"
#include "remoting/test/fake_port_allocator.h"
#include "remoting/test/fake_socket_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
using protocol::ChannelConfig;
const char kHostJid[] = "host_jid@example.com/host";
const char kHostOwner[] = "jane.doe@example.com";
const char kClientJid[] = "jane.doe@example.com/client";
struct NetworkPerformanceParams {
NetworkPerformanceParams(int bandwidth,
int max_buffers,
double latency_average_ms,
double latency_stddev_ms,
double out_of_order_rate)
: bandwidth(bandwidth),
max_buffers(max_buffers),
latency_average(base::TimeDelta::FromMillisecondsD(latency_average_ms)),
latency_stddev(base::TimeDelta::FromMillisecondsD(latency_stddev_ms)),
out_of_order_rate(out_of_order_rate) {}
int bandwidth;
int max_buffers;
base::TimeDelta latency_average;
base::TimeDelta latency_stddev;
double out_of_order_rate;
};
class FakeCursorShapeStub : public protocol::CursorShapeStub {
public:
FakeCursorShapeStub() {}
~FakeCursorShapeStub() override {}
// protocol::CursorShapeStub interface.
void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override{};
};
class ProtocolPerfTest
: public testing::Test,
public testing::WithParamInterface<NetworkPerformanceParams>,
public ClientUserInterface,
public VideoRenderer,
public HostStatusObserver {
public:
ProtocolPerfTest()
: host_thread_("host"),
capture_thread_("capture"),
encode_thread_("encode") {
VideoFramePump::EnableTimestampsForTests();
host_thread_.StartWithOptions(
base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
capture_thread_.Start();
encode_thread_.Start();
}
virtual ~ProtocolPerfTest() {
host_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, host_.release());
host_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE,
host_signaling_.release());
message_loop_.RunUntilIdle();
}
// ClientUserInterface interface.
void OnConnectionState(protocol::ConnectionToHost::State state,
protocol::ErrorCode error) override {
if (state == protocol::ConnectionToHost::CONNECTED) {
client_connected_ = true;
if (host_connected_)
connecting_loop_->Quit();
}
}
void OnConnectionReady(bool ready) override {}
void OnRouteChanged(const std::string& channel_name,
const protocol::TransportRoute& route) override {}
void SetCapabilities(const std::string& capabilities) override {}
void SetPairingResponse(
const protocol::PairingResponse& pairing_response) override {}
void DeliverHostMessage(const protocol::ExtensionMessage& message) override {}
protocol::ClipboardStub* GetClipboardStub() override { return nullptr; }
protocol::CursorShapeStub* GetCursorShapeStub() override {
return &cursor_shape_stub_;
}
// VideoRenderer interface.
void OnSessionConfig(const protocol::SessionConfig& config) override {}
ChromotingStats* GetStats() override { return nullptr; }
void ProcessVideoPacket(scoped_ptr<VideoPacket> video_packet,
const base::Closure& done) override {
if (video_packet->data().empty()) {
// Ignore keep-alive packets
done.Run();
return;
}
last_video_packet_ = video_packet.Pass();
if (!on_frame_task_.is_null())
on_frame_task_.Run();
done.Run();
}
// HostStatusObserver interface.
void OnClientConnected(const std::string& jid) override {
message_loop_.PostTask(
FROM_HERE,
base::Bind(&ProtocolPerfTest::OnHostConnectedMainThread,
base::Unretained(this)));
}
protected:
void WaitConnected() {
client_connected_ = false;
host_connected_ = false;
connecting_loop_.reset(new base::RunLoop());
connecting_loop_->Run();
ASSERT_TRUE(client_connected_ && host_connected_);
}
void OnHostConnectedMainThread() {
host_connected_ = true;
if (client_connected_)
connecting_loop_->Quit();
}
void ReceiveFrame(base::TimeDelta* latency) {
waiting_frames_loop_.reset(new base::RunLoop());
on_frame_task_ = waiting_frames_loop_->QuitClosure();
waiting_frames_loop_->Run();
if (latency) {
base::TimeTicks timestamp =
base::TimeTicks::FromInternalValue(last_video_packet_->timestamp());
*latency = base::TimeTicks::Now() - timestamp;
}
}
void ReceiveFrames(int frames, base::TimeDelta* max_latency) {
if (max_latency)
*max_latency = base::TimeDelta();
for (int i = 0; i < frames; ++i) {
base::TimeDelta latency;
ReceiveFrame(&latency);
if (max_latency && latency > *max_latency) {
*max_latency = latency;
}
}
}
// Creates test host and client and starts connection between them. Caller
// should call WaitConnected() to wait until connection is established. The
// host is started on |host_thread_| while the client works on the main
// thread.
void StartHostAndClient(protocol::ChannelConfig::Codec video_codec) {
fake_network_dispatcher_ = new FakeNetworkDispatcher();
client_signaling_.reset(new FakeSignalStrategy(kClientJid));
jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
protocol_config_ = protocol::CandidateSessionConfig::CreateDefault();
protocol_config_->DisableAudioChannel();
protocol_config_->mutable_video_configs()->clear();
protocol_config_->mutable_video_configs()->push_back(
protocol::ChannelConfig(
protocol::ChannelConfig::TRANSPORT_STREAM, 2, video_codec));
host_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(&ProtocolPerfTest::StartHost, base::Unretained(this)));
}
void StartHost() {
DCHECK(host_thread_.message_loop_proxy()->BelongsToCurrentThread());
jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
host_signaling_.reset(new FakeSignalStrategy(kHostJid));
host_signaling_->ConnectTo(client_signaling_.get());
protocol::NetworkSettings network_settings(
protocol::NetworkSettings::NAT_TRAVERSAL_OUTGOING);
scoped_ptr<FakePortAllocator> port_allocator(
FakePortAllocator::Create(fake_network_dispatcher_));
port_allocator->socket_factory()->SetBandwidth(GetParam().bandwidth,
GetParam().max_buffers);
port_allocator->socket_factory()->SetLatency(GetParam().latency_average,
GetParam().latency_stddev);
port_allocator->socket_factory()->set_out_of_order_rate(
GetParam().out_of_order_rate);
scoped_ptr<protocol::TransportFactory> host_transport_factory(
new protocol::LibjingleTransportFactory(
host_signaling_.get(),
port_allocator.Pass(),
network_settings));
scoped_ptr<protocol::SessionManager> session_manager(
new protocol::JingleSessionManager(host_transport_factory.Pass()));
// Encoder runs on a separate thread, main thread is used for everything
// else.
host_.reset(new ChromotingHost(host_signaling_.get(),
&desktop_environment_factory_,
session_manager.Pass(),
host_thread_.message_loop_proxy(),
host_thread_.message_loop_proxy(),
capture_thread_.message_loop_proxy(),
encode_thread_.message_loop_proxy(),
host_thread_.message_loop_proxy(),
host_thread_.message_loop_proxy()));
base::FilePath certs_dir(net::GetTestCertsDirectory());
std::string host_cert;
ASSERT_TRUE(base::ReadFileToString(
certs_dir.AppendASCII("unittest.selfsigned.der"), &host_cert));
base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
std::string key_string;
ASSERT_TRUE(base::ReadFileToString(key_path, &key_string));
std::string key_base64;
base::Base64Encode(key_string, &key_base64);
scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(key_base64);
ASSERT_TRUE(key_pair.get());
protocol::SharedSecretHash host_secret;
host_secret.hash_function = protocol::AuthenticationMethod::NONE;
host_secret.value = "123456";
scoped_ptr<protocol::AuthenticatorFactory> auth_factory =
protocol::Me2MeHostAuthenticatorFactory::CreateWithSharedSecret(
true, kHostOwner, host_cert, key_pair, host_secret, nullptr);
host_->SetAuthenticatorFactory(auth_factory.Pass());
host_->AddStatusObserver(this);
host_->set_protocol_config(protocol_config_->Clone());
host_->Start(kHostOwner);
message_loop_.PostTask(FROM_HERE,
base::Bind(&ProtocolPerfTest::StartClientAfterHost,
base::Unretained(this)));
}
void StartClientAfterHost() {
client_signaling_->ConnectTo(host_signaling_.get());
protocol::NetworkSettings network_settings(
protocol::NetworkSettings::NAT_TRAVERSAL_OUTGOING);
// Initialize client.
client_context_.reset(
new ClientContext(base::ThreadTaskRunnerHandle::Get()));
scoped_ptr<FakePortAllocator> port_allocator(
FakePortAllocator::Create(fake_network_dispatcher_));
port_allocator->socket_factory()->SetBandwidth(GetParam().bandwidth,
GetParam().max_buffers);
port_allocator->socket_factory()->SetLatency(GetParam().latency_average,
GetParam().latency_stddev);
port_allocator->socket_factory()->set_out_of_order_rate(
GetParam().out_of_order_rate);
scoped_ptr<protocol::TransportFactory> client_transport_factory(
new protocol::LibjingleTransportFactory(
client_signaling_.get(),
port_allocator.Pass(),
network_settings));
std::vector<protocol::AuthenticationMethod> auth_methods;
auth_methods.push_back(protocol::AuthenticationMethod::Spake2(
protocol::AuthenticationMethod::NONE));
scoped_ptr<protocol::Authenticator> client_authenticator(
new protocol::NegotiatingClientAuthenticator(
std::string(), // client_pairing_id
std::string(), // client_pairing_secret
std::string(), // authentication_tag
base::Bind(&ProtocolPerfTest::FetchPin, base::Unretained(this)),
nullptr,
auth_methods));
client_.reset(
new ChromotingClient(client_context_.get(), this, this, nullptr));
client_->SetProtocolConfigForTests(protocol_config_->Clone());
client_->Start(
client_signaling_.get(), client_authenticator.Pass(),
client_transport_factory.Pass(), kHostJid, std::string());
}
void FetchPin(
bool pairing_supported,
const protocol::SecretFetchedCallback& secret_fetched_callback) {
secret_fetched_callback.Run("123456");
}
base::MessageLoopForIO message_loop_;
scoped_refptr<FakeNetworkDispatcher> fake_network_dispatcher_;
base::Thread host_thread_;
base::Thread capture_thread_;
base::Thread encode_thread_;
FakeDesktopEnvironmentFactory desktop_environment_factory_;
FakeCursorShapeStub cursor_shape_stub_;
scoped_ptr<protocol::CandidateSessionConfig> protocol_config_;
scoped_ptr<FakeSignalStrategy> host_signaling_;
scoped_ptr<FakeSignalStrategy> client_signaling_;
scoped_ptr<ChromotingHost> host_;
scoped_ptr<ClientContext> client_context_;
scoped_ptr<ChromotingClient> client_;
scoped_ptr<base::RunLoop> connecting_loop_;
scoped_ptr<base::RunLoop> waiting_frames_loop_;
bool client_connected_;
bool host_connected_;
base::Closure on_frame_task_;
scoped_ptr<VideoPacket> last_video_packet_;
DISALLOW_COPY_AND_ASSIGN(ProtocolPerfTest);
};
INSTANTIATE_TEST_CASE_P(
NoDelay,
ProtocolPerfTest,
::testing::Values(NetworkPerformanceParams(0, 0, 0, 0, 0.0)));
INSTANTIATE_TEST_CASE_P(
HighLatency,
ProtocolPerfTest,
::testing::Values(NetworkPerformanceParams(0, 0, 300, 30, 0.0),
NetworkPerformanceParams(0, 0, 30, 10, 0.0)));
INSTANTIATE_TEST_CASE_P(
OutOfOrder,
ProtocolPerfTest,
::testing::Values(NetworkPerformanceParams(0, 0, 2, 0, 0.01),
NetworkPerformanceParams(0, 0, 30, 1, 0.01),
NetworkPerformanceParams(0, 0, 30, 1, 0.1),
NetworkPerformanceParams(0, 0, 300, 20, 0.01),
NetworkPerformanceParams(0, 0, 300, 20, 0.1)));
INSTANTIATE_TEST_CASE_P(
LimitedBandwidth,
ProtocolPerfTest,
::testing::Values(
// 100 MBps
NetworkPerformanceParams(800000000, 800000000, 2, 1, 0.0),
// 8 MBps
NetworkPerformanceParams(1000000, 300000, 30, 5, 0.01),
NetworkPerformanceParams(1000000, 2000000, 30, 5, 0.01),
// 800 kBps
NetworkPerformanceParams(100000, 30000, 130, 5, 0.01),
NetworkPerformanceParams(100000, 200000, 130, 5, 0.01)));
TEST_P(ProtocolPerfTest, StreamFrameRate) {
StartHostAndClient(protocol::ChannelConfig::CODEC_VP8);
ASSERT_NO_FATAL_FAILURE(WaitConnected());
base::TimeDelta latency;
ReceiveFrame(&latency);
LOG(INFO) << "First frame latency: " << latency.InMillisecondsF() << "ms";
ReceiveFrames(20, nullptr);
base::TimeTicks started = base::TimeTicks::Now();
ReceiveFrames(40, &latency);
base::TimeDelta elapsed = base::TimeTicks::Now() - started;
LOG(INFO) << "Frame rate: " << (40.0 / elapsed.InSecondsF());
LOG(INFO) << "Maximum latency: " << latency.InMillisecondsF() << "ms";
}
const int kIntermittentFrameSize = 100 * 1000;
// Frame generator that rewrites the whole screen every 60th frame. Should only
// be used with the VERBATIM codec as the allocated frame may contain arbitrary
// data.
class IntermittentChangeFrameGenerator
: public base::RefCountedThreadSafe<IntermittentChangeFrameGenerator> {
public:
IntermittentChangeFrameGenerator()
: frame_index_(0) {}
scoped_ptr<webrtc::DesktopFrame> GenerateFrame(
webrtc::DesktopCapturer::Callback* callback) {
const int kWidth = 1000;
const int kHeight = kIntermittentFrameSize / kWidth / 4;
bool fresh_frame = false;
if (frame_index_ % 60 == 0 || !current_frame_) {
current_frame_.reset(webrtc::SharedDesktopFrame::Wrap(
new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth, kHeight))));
fresh_frame = true;
}
++frame_index_;
scoped_ptr<webrtc::DesktopFrame> result(current_frame_->Share());
result->mutable_updated_region()->Clear();
if (fresh_frame) {
result->mutable_updated_region()->AddRect(
webrtc::DesktopRect::MakeXYWH(0, 0, kWidth, kHeight));
}
return result.Pass();
}
private:
~IntermittentChangeFrameGenerator() {}
friend class base::RefCountedThreadSafe<IntermittentChangeFrameGenerator>;
int frame_index_;
scoped_ptr<webrtc::SharedDesktopFrame> current_frame_;
DISALLOW_COPY_AND_ASSIGN(IntermittentChangeFrameGenerator);
};
TEST_P(ProtocolPerfTest, IntermittentChanges) {
desktop_environment_factory_.set_frame_generator(
base::Bind(&IntermittentChangeFrameGenerator::GenerateFrame,
new IntermittentChangeFrameGenerator()));
StartHostAndClient(protocol::ChannelConfig::CODEC_VERBATIM);
ASSERT_NO_FATAL_FAILURE(WaitConnected());
ReceiveFrame(nullptr);
base::TimeDelta expected = GetParam().latency_average;
if (GetParam().bandwidth > 0) {
expected += base::TimeDelta::FromSecondsD(kIntermittentFrameSize /
GetParam().bandwidth);
}
LOG(INFO) << "Expected: " << expected.InMillisecondsF() << "ms";
base::TimeDelta sum;
const int kFrames = 5;
for (int i = 0; i < kFrames; ++i) {
base::TimeDelta latency;
ReceiveFrame(&latency);
LOG(INFO) << "Latency: " << latency.InMillisecondsF()
<< "ms Encode: " << last_video_packet_->encode_time_ms()
<< "ms Capture: " << last_video_packet_->capture_time_ms()
<< "ms";
sum += latency;
}
LOG(INFO) << "Average: " << (sum / kFrames).InMillisecondsF();
}
} // namespace remoting
|