blob: 59034892f2c75c783031a874ae932780e15b34c5 [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]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]1e72daa2011-01-28 21:25:4235#include "remoting/host/event_executor.h"
[email protected]76207352010-06-17 23:43:0036#include "remoting/host/json_host_config.h"
[email protected]04b36142010-11-02 01:08:1937#include "remoting/proto/video.pb.h"
[email protected]cb3b1f9312010-06-07 19:58:2338
[email protected]7b398be32011-02-19 00:32:4439#if defined(TOOLKIT_USES_GTK)
40#include "ui/gfx/gtk_util.h"
41#endif
42
[email protected]f901a072010-11-23 22:18:1243using remoting::ChromotingHost;
44using remoting::protocol::CandidateSessionConfig;
45using remoting::protocol::ChannelConfig;
46using std::string;
47using std::wstring;
48
[email protected]cb3b1f9312010-06-07 19:58:2349#if defined(OS_WIN)
[email protected]f901a072010-11-23 22:18:1250const wchar_t kDefaultConfigPath[] = L".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5051const wchar_t kHomeDrive[] = L"HOMEDRIVE";
[email protected]7de8b182010-06-23 15:38:2952const wchar_t kHomePath[] = L"HOMEPATH";
[email protected]f901a072010-11-23 22:18:1253// TODO(sergeyu): Use environment utils from base/environment.h.
[email protected]7de8b182010-06-23 15:38:2954const wchar_t* GetEnvironmentVar(const wchar_t* x) { return _wgetenv(x); }
[email protected]76207352010-06-17 23:43:0055#else
[email protected]f901a072010-11-23 22:18:1256const char kDefaultConfigPath[] = ".ChromotingConfig.json";
[email protected]65b6c1aa2010-06-25 01:19:5057static char* GetEnvironmentVar(const char* x) { return getenv(x); }
[email protected]76207352010-06-17 23:43:0058#endif
59
[email protected]92698ce2010-06-28 21:49:3060void ShutdownTask(MessageLoop* message_loop) {
61 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
62}
63
[email protected]f901a072010-11-23 22:18:1264const char kFakeSwitchName[] = "fake";
65const char kConfigSwitchName[] = "config";
66const char kVideoSwitchName[] = "video";
67
68const char kVideoSwitchValueVerbatim[] = "verbatim";
69const char kVideoSwitchValueZip[] = "zip";
70const char kVideoSwitchValueVp8[] = "vp8";
71const char kVideoSwitchValueVp8Rtp[] = "vp8rtp";
72
[email protected]76207352010-06-17 23:43:0073
[email protected]cb3b1f9312010-06-07 19:58:2374int main(int argc, char** argv) {
[email protected]7de8b182010-06-23 15:38:2975 // Needed for the Mac, so we don't leak objects when threads are created.
[email protected]c2818d42010-10-18 02:47:3976 base::mac::ScopedNSAutoreleasePool pool;
[email protected]7de8b182010-06-23 15:38:2977
[email protected]76207352010-06-17 23:43:0078 CommandLine::Init(argc, argv);
79 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
80
[email protected]cb3b1f9312010-06-07 19:58:2381 base::AtExitManager exit_manager;
[email protected]81565e952010-06-30 22:51:0682 base::EnsureNSPRInit();
83
[email protected]6fd3d6a12010-11-16 19:53:3584 // Allocate a chromoting context and starts it.
[email protected]7b398be32011-02-19 00:32:4485#if defined(TOOLKIT_USES_GTK)
86 gfx::GtkInitFromCommandLine(*cmd_line);
87#endif
88 MessageLoopForUI message_loop;
89 remoting::ChromotingHostContext context(&message_loop);
[email protected]6fd3d6a12010-11-16 19:53:3590 context.Start();
91
[email protected]76207352010-06-17 23:43:0092
[email protected]65b6c1aa2010-06-25 01:19:5093#if defined(OS_WIN)
[email protected]f901a072010-11-23 22:18:1294 wstring home_path = GetEnvironmentVar(kHomeDrive);
[email protected]04b36142010-11-02 01:08:1995 home_path += GetEnvironmentVar(kHomePath);
[email protected]65b6c1aa2010-06-25 01:19:5096#else
[email protected]f901a072010-11-23 22:18:1297 string home_path = GetEnvironmentVar(base::env_vars::kHome);
[email protected]65b6c1aa2010-06-25 01:19:5098#endif
[email protected]04b36142010-11-02 01:08:1999 FilePath config_path(home_path);
[email protected]7de8b182010-06-23 15:38:29100 config_path = config_path.Append(kDefaultConfigPath);
[email protected]76207352010-06-17 23:43:00101 if (cmd_line->HasSwitch(kConfigSwitchName)) {
102 config_path = cmd_line->GetSwitchValuePath(kConfigSwitchName);
103 }
104
[email protected]76207352010-06-17 23:43:00105 base::Thread file_io_thread("FileIO");
106 file_io_thread.Start();
[email protected]34f09f1a2010-06-15 23:00:26107
[email protected]76207352010-06-17 23:43:00108 scoped_refptr<remoting::JsonHostConfig> config(
109 new remoting::JsonHostConfig(
110 config_path, file_io_thread.message_loop_proxy()));
111
112 if (!config->Read()) {
113 LOG(ERROR) << "Failed to read configuration file " << config_path.value();
[email protected]6fd3d6a12010-11-16 19:53:35114 context.Stop();
[email protected]76207352010-06-17 23:43:00115 return 1;
116 }
117
[email protected]04b36142010-11-02 01:08:19118 FilePath module_path;
119 PathService::Get(base::DIR_MODULE, &module_path);
120 CHECK(media::InitializeMediaLibrary(module_path))
121 << "Cannot load media library";
122
[email protected]92698ce2010-06-28 21:49:30123 // Construct a chromoting host.
[email protected]f901a072010-11-23 22:18:12124 scoped_refptr<ChromotingHost> host;
[email protected]c3ba9b32010-11-17 05:24:30125
126 bool fake = cmd_line->HasSwitch(kFakeSwitchName);
127 if (fake) {
[email protected]1e72daa2011-01-28 21:25:42128 remoting::Capturer* capturer =
129 new remoting::CapturerFake(context.main_message_loop());
130 remoting::protocol::InputStub* input_stub =
[email protected]7b398be32011-02-19 00:32:44131 CreateEventExecutor(context.ui_message_loop(), capturer);
[email protected]f901a072010-11-23 22:18:12132 host = ChromotingHost::Create(
[email protected]1e72daa2011-01-28 21:25:42133 &context, config, capturer, input_stub);
[email protected]c3ba9b32010-11-17 05:24:30134 } else {
[email protected]f901a072010-11-23 22:18:12135 host = ChromotingHost::Create(&context, config);
136 }
137
138 if (cmd_line->HasSwitch(kVideoSwitchName)) {
139 string video_codec = cmd_line->GetSwitchValueASCII(kVideoSwitchName);
140 scoped_ptr<CandidateSessionConfig> config(
141 CandidateSessionConfig::CreateDefault());
142 config->mutable_video_configs()->clear();
143
144 ChannelConfig::TransportType transport = ChannelConfig::TRANSPORT_STREAM;
145 ChannelConfig::Codec codec;
146 if (video_codec == kVideoSwitchValueVerbatim) {
147 codec = ChannelConfig::CODEC_VERBATIM;
148 } else if (video_codec == kVideoSwitchValueZip) {
149 codec = ChannelConfig::CODEC_ZIP;
150 } else if (video_codec == kVideoSwitchValueVp8) {
151 codec = ChannelConfig::CODEC_VP8;
152 } else if (video_codec == kVideoSwitchValueVp8Rtp) {
153 transport = ChannelConfig::TRANSPORT_SRTP;
154 codec = ChannelConfig::CODEC_VP8;
155 } else {
156 LOG(ERROR) << "Unknown video codec: " << video_codec;
157 context.Stop();
158 return 1;
159 }
160 config->mutable_video_configs()->push_back(ChannelConfig(
161 transport, remoting::protocol::kDefaultStreamVersion, codec));
162 host->set_protocol_config(config.release());
[email protected]c3ba9b32010-11-17 05:24:30163 }
[email protected]76207352010-06-17 23:43:00164
[email protected]88552a92010-08-06 22:50:00165 // Let the chromoting host run until the shutdown task is executed.
[email protected]92698ce2010-06-28 21:49:30166 host->Start(NewRunnableFunction(&ShutdownTask, &message_loop));
[email protected]7b398be32011-02-19 00:32:44167 message_loop.MessageLoop::Run();
[email protected]92698ce2010-06-28 21:49:30168
169 // And then stop the chromoting context.
170 context.Stop();
[email protected]76207352010-06-17 23:43:00171 file_io_thread.Stop();
[email protected]cb3b1f9312010-06-07 19:58:23172 return 0;
173}