blob: e28c1c0c698e26093dfa6cb4820932c2f0c5a607 [file] [log] [blame]
[email protected]1fdc9df2011-02-23 11:33:591// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]cb3b1f9312010-06-07 19:58:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// This is an application of a minimal host process in a Chromoting
6// system. It serves the purpose of gluing different pieces together
7// to make a functional host process for testing.
8//
9// It peforms the following functionality:
10// 1. Connect to the GTalk network and register the machine as a host.
11// 2. Accepts connection through libjingle.
12// 3. Receive mouse / keyboard events through libjingle.
13// 4. Sends screen capture through libjingle.
14
15#include <iostream>
16#include <string>
[email protected]7de8b182010-06-23 15:38:2917#include <stdlib.h>
[email protected]cb3b1f9312010-06-07 19:58:2318
19#include "build/build_config.h"
20
[email protected]cb3b1f9312010-06-07 19:58:2321#include "base/at_exit.h"
[email protected]76207352010-06-17 23:43:0022#include "base/command_line.h"
[email protected]76b90d312010-08-03 03:00:5023#include "base/environment.h"
[email protected]76207352010-06-17 23:43:0024#include "base/file_path.h"
25#include "base/logging.h"
[email protected]c2818d42010-10-18 02:47:3926#include "base/mac/scoped_nsautorelease_pool.h"
[email protected]81565e952010-06-30 22:51:0627#include "base/nss_util.h"
[email protected]04b36142010-11-02 01:08:1928#include "base/path_service.h"
[email protected]26b627e2011-03-02 21:53:1229#include "base/test/mock_chrome_application_mac.h"
[email protected]34b99632011-01-01 01:01:0630#include "base/threading/thread.h"
[email protected]04b36142010-11-02 01:08:1931#include "media/base/media.h"
[email protected]04b36142010-11-02 01:08:1932#include "remoting/base/tracer.h"
[email protected]cb3b1f9312010-06-07 19:58:2333#include "remoting/host/capturer_fake.h"
[email protected]2a4f9882010-06-15 00:20:3834#include "remoting/host/chromoting_host.h"
[email protected]92698ce2010-06-28 21:49:3035#include "remoting/host/chromoting_host_context.h"
[email protected]1fdc9df2011-02-23 11:33:5936#include "remoting/host/desktop_environment.h"
[email protected]1e72daa2011-01-28 21:25:4237#include "remoting/host/event_executor.h"
[email protected]76207352010-06-17 23:43:0038#include "remoting/host/json_host_config.h"
[email protected]04b36142010-11-02 01:08:1939#include "remoting/proto/video.pb.h"
[email protected]cb3b1f9312010-06-07 19:58:2340
[email protected]7b398be32011-02-19 00:32:4441#if defined(TOOLKIT_USES_GTK)
42#include "ui/gfx/gtk_util.h"
43#endif
44
[email protected]f901a072010-11-23 22:18:1245using remoting::ChromotingHost;
[email protected]1fdc9df2011-02-23 11:33:5946using remoting::DesktopEnvironment;
[email protected]f901a072010-11-23 22:18:1247using remoting::protocol::CandidateSessionConfig;
48using remoting::protocol::ChannelConfig;
49using std::string;
50using std::wstring;
51
[email protected]cb3b1f9312010-06-07 19:58:2352#if defined(OS_WIN)
[email protected]f901a072010-11-23 22:18:1253const wchar_t kDefaultConfigPath[] = L".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5054const wchar_t kHomeDrive[] = L"HOMEDRIVE";
[email protected]7de8b182010-06-23 15:38:2955const wchar_t kHomePath[] = L"HOMEPATH";
[email protected]f901a072010-11-23 22:18:1256// TODO(sergeyu): Use environment utils from base/environment.h.
[email protected]7de8b182010-06-23 15:38:2957const wchar_t* GetEnvironmentVar(const wchar_t* x) { return _wgetenv(x); }
[email protected]76207352010-06-17 23:43:0058#else
[email protected]f901a072010-11-23 22:18:1259const char kDefaultConfigPath[] = ".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5060static char* GetEnvironmentVar(const char* x) { return getenv(x); }
[email protected]76207352010-06-17 23:43:0061#endif
62
[email protected]92698ce2010-06-28 21:49:3063void ShutdownTask(MessageLoop* message_loop) {
64 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
65}
66
[email protected]f901a072010-11-23 22:18:1267const char kFakeSwitchName[] = "fake";
68const char kConfigSwitchName[] = "config";
69const char kVideoSwitchName[] = "video";
70
71const char kVideoSwitchValueVerbatim[] = "verbatim";
72const char kVideoSwitchValueZip[] = "zip";
73const char kVideoSwitchValueVp8[] = "vp8";
74const char kVideoSwitchValueVp8Rtp[] = "vp8rtp";
75
[email protected]76207352010-06-17 23:43:0076
[email protected]cb3b1f9312010-06-07 19:58:2377int main(int argc, char** argv) {
[email protected]7de8b182010-06-23 15:38:2978 // Needed for the Mac, so we don't leak objects when threads are created.
[email protected]c2818d42010-10-18 02:47:3979 base::mac::ScopedNSAutoreleasePool pool;
[email protected]7de8b182010-06-23 15:38:2980
[email protected]76207352010-06-17 23:43:0081 CommandLine::Init(argc, argv);
82 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
83
[email protected]cb3b1f9312010-06-07 19:58:2384 base::AtExitManager exit_manager;
[email protected]81565e952010-06-30 22:51:0685 base::EnsureNSPRInit();
86
[email protected]6fd3d6a12010-11-16 19:53:3587 // Allocate a chromoting context and starts it.
[email protected]7b398be32011-02-19 00:32:4488#if defined(TOOLKIT_USES_GTK)
89 gfx::GtkInitFromCommandLine(*cmd_line);
90#endif
91 MessageLoopForUI message_loop;
92 remoting::ChromotingHostContext context(&message_loop);
[email protected]6fd3d6a12010-11-16 19:53:3593 context.Start();
94
[email protected]76207352010-06-17 23:43:0095
[email protected]65b6c1aa2010-06-25 01:19:5096#if defined(OS_WIN)
[email protected]f901a072010-11-23 22:18:1297 wstring home_path = GetEnvironmentVar(kHomeDrive);
[email protected]04b36142010-11-02 01:08:1998 home_path += GetEnvironmentVar(kHomePath);
[email protected]65b6c1aa2010-06-25 01:19:5099#else
[email protected]f901a072010-11-23 22:18:12100 string home_path = GetEnvironmentVar(base::env_vars::kHome);
[email protected]65b6c1aa2010-06-25 01:19:50101#endif
[email protected]04b36142010-11-02 01:08:19102 FilePath config_path(home_path);
[email protected]7de8b182010-06-23 15:38:29103 config_path = config_path.Append(kDefaultConfigPath);
[email protected]76207352010-06-17 23:43:00104 if (cmd_line->HasSwitch(kConfigSwitchName)) {
105 config_path = cmd_line->GetSwitchValuePath(kConfigSwitchName);
106 }
107
[email protected]76207352010-06-17 23:43:00108 base::Thread file_io_thread("FileIO");
109 file_io_thread.Start();
[email protected]34f09f1a2010-06-15 23:00:26110
[email protected]76207352010-06-17 23:43:00111 scoped_refptr<remoting::JsonHostConfig> config(
112 new remoting::JsonHostConfig(
113 config_path, file_io_thread.message_loop_proxy()));
114
115 if (!config->Read()) {
116 LOG(ERROR) << "Failed to read configuration file " << config_path.value();
[email protected]6fd3d6a12010-11-16 19:53:35117 context.Stop();
[email protected]76207352010-06-17 23:43:00118 return 1;
119 }
120
[email protected]04b36142010-11-02 01:08:19121 FilePath module_path;
122 PathService::Get(base::DIR_MODULE, &module_path);
123 CHECK(media::InitializeMediaLibrary(module_path))
124 << "Cannot load media library";
125
[email protected]92698ce2010-06-28 21:49:30126 // Construct a chromoting host.
[email protected]f901a072010-11-23 22:18:12127 scoped_refptr<ChromotingHost> host;
[email protected]c3ba9b32010-11-17 05:24:30128
129 bool fake = cmd_line->HasSwitch(kFakeSwitchName);
130 if (fake) {
[email protected]1e72daa2011-01-28 21:25:42131 remoting::Capturer* capturer =
[email protected]bab76b9b2011-03-29 14:40:20132 new remoting::CapturerFake();
[email protected]1e72daa2011-01-28 21:25:42133 remoting::protocol::InputStub* input_stub =
[email protected]7b398be32011-02-19 00:32:44134 CreateEventExecutor(context.ui_message_loop(), capturer);
[email protected]f901a072010-11-23 22:18:12135 host = ChromotingHost::Create(
[email protected]1fdc9df2011-02-23 11:33:59136 &context, config, new DesktopEnvironment(capturer, input_stub));
[email protected]c3ba9b32010-11-17 05:24:30137 } else {
[email protected]f901a072010-11-23 22:18:12138 host = ChromotingHost::Create(&context, config);
139 }
140
141 if (cmd_line->HasSwitch(kVideoSwitchName)) {
142 string video_codec = cmd_line->GetSwitchValueASCII(kVideoSwitchName);
143 scoped_ptr<CandidateSessionConfig> config(
144 CandidateSessionConfig::CreateDefault());
145 config->mutable_video_configs()->clear();
146
147 ChannelConfig::TransportType transport = ChannelConfig::TRANSPORT_STREAM;
148 ChannelConfig::Codec codec;
149 if (video_codec == kVideoSwitchValueVerbatim) {
150 codec = ChannelConfig::CODEC_VERBATIM;
151 } else if (video_codec == kVideoSwitchValueZip) {
152 codec = ChannelConfig::CODEC_ZIP;
153 } else if (video_codec == kVideoSwitchValueVp8) {
154 codec = ChannelConfig::CODEC_VP8;
155 } else if (video_codec == kVideoSwitchValueVp8Rtp) {
156 transport = ChannelConfig::TRANSPORT_SRTP;
157 codec = ChannelConfig::CODEC_VP8;
158 } else {
159 LOG(ERROR) << "Unknown video codec: " << video_codec;
160 context.Stop();
161 return 1;
162 }
163 config->mutable_video_configs()->push_back(ChannelConfig(
164 transport, remoting::protocol::kDefaultStreamVersion, codec));
165 host->set_protocol_config(config.release());
[email protected]c3ba9b32010-11-17 05:24:30166 }
[email protected]76207352010-06-17 23:43:00167
[email protected]26b627e2011-03-02 21:53:12168#if defined(OS_MACOSX)
169 mock_cr_app::RegisterMockCrApp();
170#endif // OS_MACOSX
171
[email protected]88552a92010-08-06 22:50:00172 // Let the chromoting host run until the shutdown task is executed.
[email protected]92698ce2010-06-28 21:49:30173 host->Start(NewRunnableFunction(&ShutdownTask, &message_loop));
[email protected]7b398be32011-02-19 00:32:44174 message_loop.MessageLoop::Run();
[email protected]92698ce2010-06-28 21:49:30175
176 // And then stop the chromoting context.
177 context.Stop();
[email protected]76207352010-06-17 23:43:00178 file_io_thread.Stop();
[email protected]cb3b1f9312010-06-07 19:58:23179 return 0;
180}