blob: 5decb81dd8fe1c16226878fcd831d0991c280b29 [file] [log] [blame]
[email protected]cb3b1f9312010-06-07 19:58:231// Copyright (c) 2010 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// 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]76207352010-06-17 23:43:0029#include "base/thread.h"
[email protected]04b36142010-11-02 01:08:1930#include "media/base/media.h"
[email protected]04b36142010-11-02 01:08:1931#include "remoting/base/tracer.h"
[email protected]cb3b1f9312010-06-07 19:58:2332#include "remoting/host/capturer_fake.h"
[email protected]2a4f9882010-06-15 00:20:3833#include "remoting/host/chromoting_host.h"
[email protected]92698ce2010-06-28 21:49:3034#include "remoting/host/chromoting_host_context.h"
[email protected]76207352010-06-17 23:43:0035#include "remoting/host/json_host_config.h"
[email protected]04b36142010-11-02 01:08:1936#include "remoting/proto/video.pb.h"
[email protected]cb3b1f9312010-06-07 19:58:2337
38#if defined(OS_WIN)
[email protected]7de8b182010-06-23 15:38:2939const std::wstring kDefaultConfigPath = L".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5040const wchar_t kHomeDrive[] = L"HOMEDRIVE";
[email protected]7de8b182010-06-23 15:38:2941const wchar_t kHomePath[] = L"HOMEPATH";
42const wchar_t* GetEnvironmentVar(const wchar_t* x) { return _wgetenv(x); }
[email protected]76207352010-06-17 23:43:0043#else
[email protected]7de8b182010-06-23 15:38:2944const std::string kDefaultConfigPath = ".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5045static char* GetEnvironmentVar(const char* x) { return getenv(x); }
[email protected]76207352010-06-17 23:43:0046#endif
47
[email protected]92698ce2010-06-28 21:49:3048void ShutdownTask(MessageLoop* message_loop) {
49 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
50}
51
[email protected]76207352010-06-17 23:43:0052const std::string kFakeSwitchName = "fake";
53const std::string kConfigSwitchName = "config";
54
[email protected]cb3b1f9312010-06-07 19:58:2355int main(int argc, char** argv) {
[email protected]7de8b182010-06-23 15:38:2956 // Needed for the Mac, so we don't leak objects when threads are created.
[email protected]c2818d42010-10-18 02:47:3957 base::mac::ScopedNSAutoreleasePool pool;
[email protected]7de8b182010-06-23 15:38:2958
[email protected]76207352010-06-17 23:43:0059 CommandLine::Init(argc, argv);
60 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
61
[email protected]cb3b1f9312010-06-07 19:58:2362 base::AtExitManager exit_manager;
[email protected]81565e952010-06-30 22:51:0663 base::EnsureNSPRInit();
64
[email protected]6fd3d6a12010-11-16 19:53:3565 // Allocate a chromoting context and starts it.
66 remoting::ChromotingHostContext context;
67 context.Start();
68
[email protected]76207352010-06-17 23:43:0069
[email protected]65b6c1aa2010-06-25 01:19:5070#if defined(OS_WIN)
[email protected]04b36142010-11-02 01:08:1971 std::wstring home_path = GetEnvironmentVar(kHomeDrive);
72 home_path += GetEnvironmentVar(kHomePath);
[email protected]65b6c1aa2010-06-25 01:19:5073#else
[email protected]04b36142010-11-02 01:08:1974 std::string home_path = GetEnvironmentVar(base::env_vars::kHome);
[email protected]65b6c1aa2010-06-25 01:19:5075#endif
[email protected]04b36142010-11-02 01:08:1976 FilePath config_path(home_path);
[email protected]7de8b182010-06-23 15:38:2977 config_path = config_path.Append(kDefaultConfigPath);
[email protected]76207352010-06-17 23:43:0078 if (cmd_line->HasSwitch(kConfigSwitchName)) {
79 config_path = cmd_line->GetSwitchValuePath(kConfigSwitchName);
80 }
81
[email protected]76207352010-06-17 23:43:0082 base::Thread file_io_thread("FileIO");
83 file_io_thread.Start();
[email protected]34f09f1a2010-06-15 23:00:2684
[email protected]76207352010-06-17 23:43:0085 scoped_refptr<remoting::JsonHostConfig> config(
86 new remoting::JsonHostConfig(
87 config_path, file_io_thread.message_loop_proxy()));
88
89 if (!config->Read()) {
90 LOG(ERROR) << "Failed to read configuration file " << config_path.value();
[email protected]6fd3d6a12010-11-16 19:53:3591 context.Stop();
[email protected]76207352010-06-17 23:43:0092 return 1;
93 }
94
[email protected]04b36142010-11-02 01:08:1995 FilePath module_path;
96 PathService::Get(base::DIR_MODULE, &module_path);
97 CHECK(media::InitializeMediaLibrary(module_path))
98 << "Cannot load media library";
99
[email protected]92698ce2010-06-28 21:49:30100 // Construct a chromoting host.
[email protected]c3ba9b32010-11-17 05:24:30101 scoped_refptr<remoting::ChromotingHost> host;
102
103 bool fake = cmd_line->HasSwitch(kFakeSwitchName);
104 if (fake) {
105 host = new remoting::ChromotingHost(
106 &context, config,
107 new remoting::CapturerFake(context.capture_message_loop()));
108 } else {
109 host = new remoting::ChromotingHost(&context, config);
110 }
[email protected]76207352010-06-17 23:43:00111
[email protected]88552a92010-08-06 22:50:00112 // Let the chromoting host run until the shutdown task is executed.
[email protected]81565e952010-06-30 22:51:06113 MessageLoop message_loop(MessageLoop::TYPE_UI);
[email protected]92698ce2010-06-28 21:49:30114 host->Start(NewRunnableFunction(&ShutdownTask, &message_loop));
115 message_loop.Run();
116
117 // And then stop the chromoting context.
118 context.Stop();
[email protected]76207352010-06-17 23:43:00119 file_io_thread.Stop();
[email protected]cb3b1f9312010-06-07 19:58:23120 return 0;
121}