blob: c10c2c576529518c37e377b548dc5aa402233422 [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]04b36142010-11-02 01:08:1927#include "base/path_service.h"
[email protected]26b627e2011-03-02 21:53:1228#include "base/test/mock_chrome_application_mac.h"
[email protected]34b99632011-01-01 01:01:0629#include "base/threading/thread.h"
[email protected]4b559b4d2011-04-14 17:37:1430#include "crypto/nss_util.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]37961b12011-03-30 12:39:0036#include "remoting/host/curtain.h"
[email protected]1fdc9df2011-02-23 11:33:5937#include "remoting/host/desktop_environment.h"
[email protected]1e72daa2011-01-28 21:25:4238#include "remoting/host/event_executor.h"
[email protected]76207352010-06-17 23:43:0039#include "remoting/host/json_host_config.h"
[email protected]04b36142010-11-02 01:08:1940#include "remoting/proto/video.pb.h"
[email protected]cb3b1f9312010-06-07 19:58:2341
[email protected]7b398be32011-02-19 00:32:4442#if defined(TOOLKIT_USES_GTK)
43#include "ui/gfx/gtk_util.h"
44#endif
45
[email protected]f901a072010-11-23 22:18:1246using remoting::ChromotingHost;
[email protected]1fdc9df2011-02-23 11:33:5947using remoting::DesktopEnvironment;
[email protected]f901a072010-11-23 22:18:1248using remoting::protocol::CandidateSessionConfig;
49using remoting::protocol::ChannelConfig;
50using std::string;
51using std::wstring;
52
[email protected]cb3b1f9312010-06-07 19:58:2353#if defined(OS_WIN)
[email protected]f901a072010-11-23 22:18:1254const wchar_t kDefaultConfigPath[] = L".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5055const wchar_t kHomeDrive[] = L"HOMEDRIVE";
[email protected]7de8b182010-06-23 15:38:2956const wchar_t kHomePath[] = L"HOMEPATH";
[email protected]f901a072010-11-23 22:18:1257// TODO(sergeyu): Use environment utils from base/environment.h.
[email protected]7de8b182010-06-23 15:38:2958const wchar_t* GetEnvironmentVar(const wchar_t* x) { return _wgetenv(x); }
[email protected]76207352010-06-17 23:43:0059#else
[email protected]f901a072010-11-23 22:18:1260const char kDefaultConfigPath[] = ".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5061static char* GetEnvironmentVar(const char* x) { return getenv(x); }
[email protected]76207352010-06-17 23:43:0062#endif
63
[email protected]92698ce2010-06-28 21:49:3064void ShutdownTask(MessageLoop* message_loop) {
65 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
66}
67
[email protected]f901a072010-11-23 22:18:1268const char kFakeSwitchName[] = "fake";
69const char kConfigSwitchName[] = "config";
70const char kVideoSwitchName[] = "video";
71
72const char kVideoSwitchValueVerbatim[] = "verbatim";
73const char kVideoSwitchValueZip[] = "zip";
74const char kVideoSwitchValueVp8[] = "vp8";
75const char kVideoSwitchValueVp8Rtp[] = "vp8rtp";
76
[email protected]76207352010-06-17 23:43:0077
[email protected]cb3b1f9312010-06-07 19:58:2378int main(int argc, char** argv) {
[email protected]7de8b182010-06-23 15:38:2979 // Needed for the Mac, so we don't leak objects when threads are created.
[email protected]c2818d42010-10-18 02:47:3980 base::mac::ScopedNSAutoreleasePool pool;
[email protected]7de8b182010-06-23 15:38:2981
[email protected]76207352010-06-17 23:43:0082 CommandLine::Init(argc, argv);
83 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
84
[email protected]cb3b1f9312010-06-07 19:58:2385 base::AtExitManager exit_manager;
[email protected]4b559b4d2011-04-14 17:37:1486 crypto::EnsureNSPRInit();
[email protected]81565e952010-06-30 22:51:0687
[email protected]6fd3d6a12010-11-16 19:53:3588 // Allocate a chromoting context and starts it.
[email protected]7b398be32011-02-19 00:32:4489#if defined(TOOLKIT_USES_GTK)
90 gfx::GtkInitFromCommandLine(*cmd_line);
91#endif
92 MessageLoopForUI message_loop;
93 remoting::ChromotingHostContext context(&message_loop);
[email protected]6fd3d6a12010-11-16 19:53:3594 context.Start();
95
[email protected]76207352010-06-17 23:43:0096
[email protected]65b6c1aa2010-06-25 01:19:5097#if defined(OS_WIN)
[email protected]f901a072010-11-23 22:18:1298 wstring home_path = GetEnvironmentVar(kHomeDrive);
[email protected]04b36142010-11-02 01:08:1999 home_path += GetEnvironmentVar(kHomePath);
[email protected]65b6c1aa2010-06-25 01:19:50100#else
[email protected]f901a072010-11-23 22:18:12101 string home_path = GetEnvironmentVar(base::env_vars::kHome);
[email protected]65b6c1aa2010-06-25 01:19:50102#endif
[email protected]04b36142010-11-02 01:08:19103 FilePath config_path(home_path);
[email protected]7de8b182010-06-23 15:38:29104 config_path = config_path.Append(kDefaultConfigPath);
[email protected]76207352010-06-17 23:43:00105 if (cmd_line->HasSwitch(kConfigSwitchName)) {
106 config_path = cmd_line->GetSwitchValuePath(kConfigSwitchName);
107 }
108
[email protected]76207352010-06-17 23:43:00109 base::Thread file_io_thread("FileIO");
110 file_io_thread.Start();
[email protected]34f09f1a2010-06-15 23:00:26111
[email protected]76207352010-06-17 23:43:00112 scoped_refptr<remoting::JsonHostConfig> config(
113 new remoting::JsonHostConfig(
114 config_path, file_io_thread.message_loop_proxy()));
115
116 if (!config->Read()) {
117 LOG(ERROR) << "Failed to read configuration file " << config_path.value();
[email protected]6fd3d6a12010-11-16 19:53:35118 context.Stop();
[email protected]76207352010-06-17 23:43:00119 return 1;
120 }
121
[email protected]04b36142010-11-02 01:08:19122 FilePath module_path;
123 PathService::Get(base::DIR_MODULE, &module_path);
124 CHECK(media::InitializeMediaLibrary(module_path))
125 << "Cannot load media library";
126
[email protected]92698ce2010-06-28 21:49:30127 // Construct a chromoting host.
[email protected]f901a072010-11-23 22:18:12128 scoped_refptr<ChromotingHost> host;
[email protected]c3ba9b32010-11-17 05:24:30129
130 bool fake = cmd_line->HasSwitch(kFakeSwitchName);
131 if (fake) {
[email protected]1e72daa2011-01-28 21:25:42132 remoting::Capturer* capturer =
[email protected]bab76b9b2011-03-29 14:40:20133 new remoting::CapturerFake();
[email protected]0b7e4282011-04-04 22:44:11134 remoting::EventExecutor* event_executor =
135 remoting::EventExecutor::Create(context.ui_message_loop(), capturer);
[email protected]37961b12011-03-30 12:39:00136 remoting::Curtain* curtain = remoting::Curtain::Create();
[email protected]f901a072010-11-23 22:18:12137 host = ChromotingHost::Create(
[email protected]37961b12011-03-30 12:39:00138 &context, config,
[email protected]0b7e4282011-04-04 22:44:11139 new DesktopEnvironment(capturer, event_executor, curtain));
[email protected]c3ba9b32010-11-17 05:24:30140 } else {
[email protected]f901a072010-11-23 22:18:12141 host = ChromotingHost::Create(&context, config);
142 }
143
144 if (cmd_line->HasSwitch(kVideoSwitchName)) {
145 string video_codec = cmd_line->GetSwitchValueASCII(kVideoSwitchName);
146 scoped_ptr<CandidateSessionConfig> config(
147 CandidateSessionConfig::CreateDefault());
148 config->mutable_video_configs()->clear();
149
150 ChannelConfig::TransportType transport = ChannelConfig::TRANSPORT_STREAM;
151 ChannelConfig::Codec codec;
152 if (video_codec == kVideoSwitchValueVerbatim) {
153 codec = ChannelConfig::CODEC_VERBATIM;
154 } else if (video_codec == kVideoSwitchValueZip) {
155 codec = ChannelConfig::CODEC_ZIP;
156 } else if (video_codec == kVideoSwitchValueVp8) {
157 codec = ChannelConfig::CODEC_VP8;
158 } else if (video_codec == kVideoSwitchValueVp8Rtp) {
159 transport = ChannelConfig::TRANSPORT_SRTP;
160 codec = ChannelConfig::CODEC_VP8;
161 } else {
162 LOG(ERROR) << "Unknown video codec: " << video_codec;
163 context.Stop();
164 return 1;
165 }
166 config->mutable_video_configs()->push_back(ChannelConfig(
167 transport, remoting::protocol::kDefaultStreamVersion, codec));
168 host->set_protocol_config(config.release());
[email protected]c3ba9b32010-11-17 05:24:30169 }
[email protected]76207352010-06-17 23:43:00170
[email protected]26b627e2011-03-02 21:53:12171#if defined(OS_MACOSX)
172 mock_cr_app::RegisterMockCrApp();
173#endif // OS_MACOSX
174
[email protected]88552a92010-08-06 22:50:00175 // Let the chromoting host run until the shutdown task is executed.
[email protected]92698ce2010-06-28 21:49:30176 host->Start(NewRunnableFunction(&ShutdownTask, &message_loop));
[email protected]7b398be32011-02-19 00:32:44177 message_loop.MessageLoop::Run();
[email protected]92698ce2010-06-28 21:49:30178
179 // And then stop the chromoting context.
180 context.Stop();
[email protected]76207352010-06-17 23:43:00181 file_io_thread.Stop();
[email protected]cb3b1f9312010-06-07 19:58:23182 return 0;
183}