1 // Copyright 2016 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/test/it2me_standalone_host.h"
6 
7 #include <functional>
8 #include <iostream>
9 #include <vector>
10 
11 #include "base/bind.h"
12 #include "base/location.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "base/time/time.h"
17 #include "remoting/base/auto_thread_task_runner.h"
18 #include "remoting/host/chromoting_host_context.h"
19 #include "remoting/host/host_extension.h"
20 #include "remoting/protocol/pairing_registry.h"
21 #include "remoting/protocol/protocol_mock_objects.h"
22 #include "remoting/protocol/session_config.h"
23 
24 namespace remoting {
25 namespace test {
26 
27 namespace {
28 
OutputFakeConnectionEventLogger(const FakeConnectionEventLogger & logger)29 void OutputFakeConnectionEventLogger(const FakeConnectionEventLogger& logger) {
30   std::cout << logger;
31 }
32 
33 constexpr char kSessionJid[] = "user@domain/rest-of-jid";
34 
35 }  // namespace
36 
37 using ::remoting::protocol::MockSession;
38 
It2MeStandaloneHost()39 It2MeStandaloneHost::It2MeStandaloneHost()
40     : task_environment_(
41           base::test::SingleThreadTaskEnvironment::MainThreadType::UI),
42       context_(ChromotingHostContext::Create(
43           new AutoThreadTaskRunner(base::ThreadTaskRunnerHandle::Get(),
44                                    run_loop_.QuitClosure()))),
45       main_task_runner_(context_->file_task_runner()),
46       factory_(main_task_runner_,
47                context_->video_capture_task_runner(),
48                context_->input_task_runner(),
49                context_->ui_task_runner()),
50       connection_(base::WrapUnique(new testing::NiceMock<MockSession>())),
51       session_jid_(kSessionJid),
52 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
53       // We cannot support audio capturing for linux, since a pipe name is
54       // needed to initialize AudioCapturerLinux.
55       config_(protocol::SessionConfig::ForTest()),
56 #else
57       config_(protocol::SessionConfig::ForTestWithAudio()),
58 #endif
59       event_logger_(&connection_) {
60   EXPECT_CALL(*static_cast<MockSession*>(connection_.session()), jid())
61       .WillRepeatedly(testing::ReturnRef(session_jid_));
62   EXPECT_CALL(*static_cast<MockSession*>(connection_.session()), config())
63       .WillRepeatedly(testing::ReturnRef(*config_));
64   connection_.set_video_stub(event_logger_.video_stub());
65   connection_.set_client_stub(event_logger_.client_stub());
66   connection_.set_host_stub(event_logger_.host_stub());
67   connection_.set_video_encode_task_runner(
68       context_->video_encode_task_runner());
69 }
70 
~It2MeStandaloneHost()71 It2MeStandaloneHost::~It2MeStandaloneHost() {}
72 
Run()73 void It2MeStandaloneHost::Run() {
74   main_task_runner_->PostTask(
75       FROM_HERE,
76       base::BindOnce(&It2MeStandaloneHost::Connect, base::Unretained(this)));
77   run_loop_.Run();
78 }
79 
StartOutputTimer()80 void It2MeStandaloneHost::StartOutputTimer() {
81   timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1),
82                base::BindRepeating(&OutputFakeConnectionEventLogger,
83                                    std::cref(event_logger_)));
84 }
85 
Connect()86 void It2MeStandaloneHost::Connect() {
87   DesktopEnvironmentOptions options =
88       DesktopEnvironmentOptions::CreateDefault();
89   options.set_enable_user_interface(false);
90   session_.reset(new ClientSession(
91       &handler_, std::unique_ptr<protocol::ConnectionToClient>(&connection_),
92       &factory_, options, base::TimeDelta(),
93       scoped_refptr<protocol::PairingRegistry>(),
94       std::vector<HostExtension*>()));
95   session_->OnConnectionAuthenticated();
96   session_->OnConnectionChannelsConnected();
97   session_->CreateMediaStreams();
98 }
99 
100 }  // namespace test
101 }  // namespace remoting
102