blob: d979e83f97ba589662a999c313b12752dfba042a [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]34b99632011-01-01 01:01:0629#include "base/threading/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]1fdc9df2011-02-23 11:33:5935#include "remoting/host/desktop_environment.h"
[email protected]1e72daa2011-01-28 21:25:4236#include "remoting/host/event_executor.h"
[email protected]76207352010-06-17 23:43:0037#include "remoting/host/json_host_config.h"
[email protected]04b36142010-11-02 01:08:1938#include "remoting/proto/video.pb.h"
[email protected]cb3b1f9312010-06-07 19:58:2339
[email protected]7b398be32011-02-19 00:32:4440#if defined(TOOLKIT_USES_GTK)
41#include "ui/gfx/gtk_util.h"
42#endif
43
[email protected]f901a072010-11-23 22:18:1244using remoting::ChromotingHost;
[email protected]1fdc9df2011-02-23 11:33:5945using remoting::DesktopEnvironment;
[email protected]f901a072010-11-23 22:18:1246using remoting::protocol::CandidateSessionConfig;
47using remoting::protocol::ChannelConfig;
48using std::string;
49using std::wstring;
50
[email protected]cb3b1f9312010-06-07 19:58:2351#if defined(OS_WIN)
[email protected]f901a072010-11-23 22:18:1252const wchar_t kDefaultConfigPath[] = L".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5053const wchar_t kHomeDrive[] = L"HOMEDRIVE";
[email protected]7de8b182010-06-23 15:38:2954const wchar_t kHomePath[] = L"HOMEPATH";
[email protected]f901a072010-11-23 22:18:1255// TODO(sergeyu): Use environment utils from base/environment.h.
[email protected]7de8b182010-06-23 15:38:2956const wchar_t* GetEnvironmentVar(const wchar_t* x) { return _wgetenv(x); }
[email protected]76207352010-06-17 23:43:0057#else
[email protected]f901a072010-11-23 22:18:1258const char kDefaultConfigPath[] = ".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5059static char* GetEnvironmentVar(const char* x) { return getenv(x); }
[email protected]76207352010-06-17 23:43:0060#endif
61
[email protected]92698ce2010-06-28 21:49:3062void ShutdownTask(MessageLoop* message_loop) {
63 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
64}
65
[email protected]f901a072010-11-23 22:18:1266const char kFakeSwitchName[] = "fake";
67const char kConfigSwitchName[] = "config";
68const char kVideoSwitchName[] = "video";
69
70const char kVideoSwitchValueVerbatim[] = "verbatim";
71const char kVideoSwitchValueZip[] = "zip";
72const char kVideoSwitchValueVp8[] = "vp8";
73const char kVideoSwitchValueVp8Rtp[] = "vp8rtp";
74
[email protected]76207352010-06-17 23:43:0075
[email protected]cb3b1f9312010-06-07 19:58:2376int main(int argc, char** argv) {
[email protected]7de8b182010-06-23 15:38:2977 // Needed for the Mac, so we don't leak objects when threads are created.
[email protected]c2818d42010-10-18 02:47:3978 base::mac::ScopedNSAutoreleasePool pool;
[email protected]7de8b182010-06-23 15:38:2979
[email protected]76207352010-06-17 23:43:0080 CommandLine::Init(argc, argv);
81 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
82
[email protected]cb3b1f9312010-06-07 19:58:2383 base::AtExitManager exit_manager;
[email protected]81565e952010-06-30 22:51:0684 base::EnsureNSPRInit();
85
[email protected]6fd3d6a12010-11-16 19:53:3586 // Allocate a chromoting context and starts it.
[email protected]7b398be32011-02-19 00:32:4487#if defined(TOOLKIT_USES_GTK)
88 gfx::GtkInitFromCommandLine(*cmd_line);
89#endif
90 MessageLoopForUI message_loop;
91 remoting::ChromotingHostContext context(&message_loop);
[email protected]6fd3d6a12010-11-16 19:53:3592 context.Start();
93
[email protected]76207352010-06-17 23:43:0094
[email protected]65b6c1aa2010-06-25 01:19:5095#if defined(OS_WIN)
[email protected]f901a072010-11-23 22:18:1296 wstring home_path = GetEnvironmentVar(kHomeDrive);
[email protected]04b36142010-11-02 01:08:1997 home_path += GetEnvironmentVar(kHomePath);
[email protected]65b6c1aa2010-06-25 01:19:5098#else
[email protected]f901a072010-11-23 22:18:1299 string home_path = GetEnvironmentVar(base::env_vars::kHome);
[email protected]65b6c1aa2010-06-25 01:19:50100#endif
[email protected]04b36142010-11-02 01:08:19101 FilePath config_path(home_path);
[email protected]7de8b182010-06-23 15:38:29102 config_path = config_path.Append(kDefaultConfigPath);
[email protected]76207352010-06-17 23:43:00103 if (cmd_line->HasSwitch(kConfigSwitchName)) {
104 config_path = cmd_line->GetSwitchValuePath(kConfigSwitchName);
105 }
106
[email protected]76207352010-06-17 23:43:00107 base::Thread file_io_thread("FileIO");
108 file_io_thread.Start();
[email protected]34f09f1a2010-06-15 23:00:26109
[email protected]76207352010-06-17 23:43:00110 scoped_refptr<remoting::JsonHostConfig> config(
111 new remoting::JsonHostConfig(
112 config_path, file_io_thread.message_loop_proxy()));
113
114 if (!config->Read()) {
115 LOG(ERROR) << "Failed to read configuration file " << config_path.value();
[email protected]6fd3d6a12010-11-16 19:53:35116 context.Stop();
[email protected]76207352010-06-17 23:43:00117 return 1;
118 }
119
[email protected]04b36142010-11-02 01:08:19120 FilePath module_path;
121 PathService::Get(base::DIR_MODULE, &module_path);
122 CHECK(media::InitializeMediaLibrary(module_path))
123 << "Cannot load media library";
124
[email protected]92698ce2010-06-28 21:49:30125 // Construct a chromoting host.
[email protected]f901a072010-11-23 22:18:12126 scoped_refptr<ChromotingHost> host;
[email protected]c3ba9b32010-11-17 05:24:30127
128 bool fake = cmd_line->HasSwitch(kFakeSwitchName);
129 if (fake) {
[email protected]1e72daa2011-01-28 21:25:42130 remoting::Capturer* capturer =
131 new remoting::CapturerFake(context.main_message_loop());
132 remoting::protocol::InputStub* input_stub =
[email protected]7b398be32011-02-19 00:32:44133 CreateEventExecutor(context.ui_message_loop(), capturer);
[email protected]f901a072010-11-23 22:18:12134 host = ChromotingHost::Create(
[email protected]1fdc9df2011-02-23 11:33:59135 &context, config, new DesktopEnvironment(capturer, input_stub));
[email protected]c3ba9b32010-11-17 05:24:30136 } else {
[email protected]f901a072010-11-23 22:18:12137 host = ChromotingHost::Create(&context, config);
138 }
139
140 if (cmd_line->HasSwitch(kVideoSwitchName)) {
141 string video_codec = cmd_line->GetSwitchValueASCII(kVideoSwitchName);
142 scoped_ptr<CandidateSessionConfig> config(
143 CandidateSessionConfig::CreateDefault());
144 config->mutable_video_configs()->clear();
145
146 ChannelConfig::TransportType transport = ChannelConfig::TRANSPORT_STREAM;
147 ChannelConfig::Codec codec;
148 if (video_codec == kVideoSwitchValueVerbatim) {
149 codec = ChannelConfig::CODEC_VERBATIM;
150 } else if (video_codec == kVideoSwitchValueZip) {
151 codec = ChannelConfig::CODEC_ZIP;
152 } else if (video_codec == kVideoSwitchValueVp8) {
153 codec = ChannelConfig::CODEC_VP8;
154 } else if (video_codec == kVideoSwitchValueVp8Rtp) {
155 transport = ChannelConfig::TRANSPORT_SRTP;
156 codec = ChannelConfig::CODEC_VP8;
157 } else {
158 LOG(ERROR) << "Unknown video codec: " << video_codec;
159 context.Stop();
160 return 1;
161 }
162 config->mutable_video_configs()->push_back(ChannelConfig(
163 transport, remoting::protocol::kDefaultStreamVersion, codec));
164 host->set_protocol_config(config.release());
[email protected]c3ba9b32010-11-17 05:24:30165 }
[email protected]76207352010-06-17 23:43:00166
[email protected]88552a92010-08-06 22:50:00167 // Let the chromoting host run until the shutdown task is executed.
[email protected]92698ce2010-06-28 21:49:30168 host->Start(NewRunnableFunction(&ShutdownTask, &message_loop));
[email protected]7b398be32011-02-19 00:32:44169 message_loop.MessageLoop::Run();
[email protected]92698ce2010-06-28 21:49:30170
171 // And then stop the chromoting context.
172 context.Stop();
[email protected]76207352010-06-17 23:43:00173 file_io_thread.Stop();
[email protected]cb3b1f9312010-06-07 19:58:23174 return 0;
175}