[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 1 | // 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] | 7de8b18 | 2010-06-23 15:38:29 | [diff] [blame] | 17 | #include <stdlib.h> |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 18 | |
| 19 | #include "build/build_config.h" |
| 20 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 21 | #include "base/at_exit.h" |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 22 | #include "base/command_line.h" |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 23 | #include "base/environment.h" |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 24 | #include "base/file_path.h" |
| 25 | #include "base/logging.h" |
[email protected] | c2818d4 | 2010-10-18 02:47:39 | [diff] [blame] | 26 | #include "base/mac/scoped_nsautorelease_pool.h" |
[email protected] | 81565e95 | 2010-06-30 22:51:06 | [diff] [blame] | 27 | #include "base/nss_util.h" |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 28 | #include "base/path_service.h" |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 29 | #include "base/thread.h" |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 30 | #include "media/base/media.h" |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 31 | #include "remoting/base/tracer.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 32 | #include "remoting/host/capturer_fake.h" |
[email protected] | 2a4f988 | 2010-06-15 00:20:38 | [diff] [blame] | 33 | #include "remoting/host/chromoting_host.h" |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 34 | #include "remoting/host/chromoting_host_context.h" |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 35 | #include "remoting/host/json_host_config.h" |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 36 | #include "remoting/proto/video.pb.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 37 | |
| 38 | #if defined(OS_WIN) |
| 39 | #include "remoting/host/capturer_gdi.h" |
| 40 | #include "remoting/host/event_executor_win.h" |
| 41 | #elif defined(OS_LINUX) |
| 42 | #include "remoting/host/capturer_linux.h" |
| 43 | #include "remoting/host/event_executor_linux.h" |
[email protected] | 9ec5273 | 2010-06-09 23:47:18 | [diff] [blame] | 44 | #elif defined(OS_MACOSX) |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 45 | #include "remoting/host/capturer_mac.h" |
| 46 | #include "remoting/host/event_executor_mac.h" |
| 47 | #endif |
| 48 | |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 49 | #if defined(OS_WIN) |
[email protected] | 7de8b18 | 2010-06-23 15:38:29 | [diff] [blame] | 50 | const std::wstring kDefaultConfigPath = L".ChromotingConfig.json"; |
[email protected] | 65b6c1aa | 2010-06-25 01:19:50 | [diff] [blame] | 51 | const wchar_t kHomeDrive[] = L"HOMEDRIVE"; |
[email protected] | 7de8b18 | 2010-06-23 15:38:29 | [diff] [blame] | 52 | const wchar_t kHomePath[] = L"HOMEPATH"; |
| 53 | const wchar_t* GetEnvironmentVar(const wchar_t* x) { return _wgetenv(x); } |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 54 | #else |
[email protected] | 7de8b18 | 2010-06-23 15:38:29 | [diff] [blame] | 55 | const std::string kDefaultConfigPath = ".ChromotingConfig.json"; |
[email protected] | 65b6c1aa | 2010-06-25 01:19:50 | [diff] [blame] | 56 | static char* GetEnvironmentVar(const char* x) { return getenv(x); } |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 57 | #endif |
| 58 | |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 59 | void ShutdownTask(MessageLoop* message_loop) { |
| 60 | message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 61 | } |
| 62 | |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 63 | const std::string kFakeSwitchName = "fake"; |
| 64 | const std::string kConfigSwitchName = "config"; |
| 65 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 66 | int main(int argc, char** argv) { |
[email protected] | 7de8b18 | 2010-06-23 15:38:29 | [diff] [blame] | 67 | // Needed for the Mac, so we don't leak objects when threads are created. |
[email protected] | c2818d4 | 2010-10-18 02:47:39 | [diff] [blame] | 68 | base::mac::ScopedNSAutoreleasePool pool; |
[email protected] | 7de8b18 | 2010-06-23 15:38:29 | [diff] [blame] | 69 | |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 70 | CommandLine::Init(argc, argv); |
| 71 | const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 72 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 73 | base::AtExitManager exit_manager; |
[email protected] | 81565e95 | 2010-06-30 22:51:06 | [diff] [blame] | 74 | base::EnsureNSPRInit(); |
| 75 | |
[email protected] | 6fd3d6a1 | 2010-11-16 19:53:35 | [diff] [blame^] | 76 | // Allocate a chromoting context and starts it. |
| 77 | remoting::ChromotingHostContext context; |
| 78 | context.Start(); |
| 79 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 80 | scoped_ptr<remoting::Capturer> capturer; |
[email protected] | 6fd3d6a1 | 2010-11-16 19:53:35 | [diff] [blame^] | 81 | scoped_ptr<remoting::protocol::InputStub> input_stub; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 82 | #if defined(OS_WIN) |
| 83 | capturer.reset(new remoting::CapturerGdi()); |
[email protected] | 6fd3d6a1 | 2010-11-16 19:53:35 | [diff] [blame^] | 84 | input_stub.reset(new remoting::EventExecutorWin( |
| 85 | context.capture_message_loop(), capturer.get())); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 86 | #elif defined(OS_LINUX) |
| 87 | capturer.reset(new remoting::CapturerLinux()); |
[email protected] | 6fd3d6a1 | 2010-11-16 19:53:35 | [diff] [blame^] | 88 | input_stub.reset(new remoting::EventExecutorLinux( |
| 89 | context.capture_message_loop(), capturer.get())); |
[email protected] | 9ec5273 | 2010-06-09 23:47:18 | [diff] [blame] | 90 | #elif defined(OS_MACOSX) |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 91 | capturer.reset(new remoting::CapturerMac()); |
[email protected] | 6fd3d6a1 | 2010-11-16 19:53:35 | [diff] [blame^] | 92 | input_stub.reset(new remoting::EventExecutorMac( |
| 93 | context.capture_message_loop(), capturer.get())); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 94 | #endif |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 95 | |
[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 96 | // Check the argument to see if we should use a fake capturer. |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 97 | bool fake = cmd_line->HasSwitch(kFakeSwitchName); |
| 98 | |
[email protected] | 65b6c1aa | 2010-06-25 01:19:50 | [diff] [blame] | 99 | #if defined(OS_WIN) |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 100 | std::wstring home_path = GetEnvironmentVar(kHomeDrive); |
| 101 | home_path += GetEnvironmentVar(kHomePath); |
[email protected] | 65b6c1aa | 2010-06-25 01:19:50 | [diff] [blame] | 102 | #else |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 103 | std::string home_path = GetEnvironmentVar(base::env_vars::kHome); |
[email protected] | 65b6c1aa | 2010-06-25 01:19:50 | [diff] [blame] | 104 | #endif |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 105 | FilePath config_path(home_path); |
[email protected] | 7de8b18 | 2010-06-23 15:38:29 | [diff] [blame] | 106 | config_path = config_path.Append(kDefaultConfigPath); |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 107 | if (cmd_line->HasSwitch(kConfigSwitchName)) { |
| 108 | config_path = cmd_line->GetSwitchValuePath(kConfigSwitchName); |
| 109 | } |
| 110 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 111 | if (fake) { |
| 112 | // Inject a fake capturer. |
[email protected] | 88552a9 | 2010-08-06 22:50:00 | [diff] [blame] | 113 | LOG(INFO) << "Using a fake capturer."; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 114 | capturer.reset(new remoting::CapturerFake()); |
| 115 | } |
| 116 | |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 117 | base::Thread file_io_thread("FileIO"); |
| 118 | file_io_thread.Start(); |
[email protected] | 34f09f1a | 2010-06-15 23:00:26 | [diff] [blame] | 119 | |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 120 | scoped_refptr<remoting::JsonHostConfig> config( |
| 121 | new remoting::JsonHostConfig( |
| 122 | config_path, file_io_thread.message_loop_proxy())); |
| 123 | |
| 124 | if (!config->Read()) { |
| 125 | LOG(ERROR) << "Failed to read configuration file " << config_path.value(); |
[email protected] | 6fd3d6a1 | 2010-11-16 19:53:35 | [diff] [blame^] | 126 | context.Stop(); |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 127 | return 1; |
| 128 | } |
| 129 | |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 130 | FilePath module_path; |
| 131 | PathService::Get(base::DIR_MODULE, &module_path); |
| 132 | CHECK(media::InitializeMediaLibrary(module_path)) |
| 133 | << "Cannot load media library"; |
| 134 | |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 135 | // Construct a chromoting host. |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 136 | scoped_refptr<remoting::ChromotingHost> host( |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 137 | new remoting::ChromotingHost(&context, |
| 138 | config, |
[email protected] | 34f09f1a | 2010-06-15 23:00:26 | [diff] [blame] | 139 | capturer.release(), |
[email protected] | 6fd3d6a1 | 2010-11-16 19:53:35 | [diff] [blame^] | 140 | input_stub.release())); |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 141 | |
[email protected] | 88552a9 | 2010-08-06 22:50:00 | [diff] [blame] | 142 | // Let the chromoting host run until the shutdown task is executed. |
[email protected] | 81565e95 | 2010-06-30 22:51:06 | [diff] [blame] | 143 | MessageLoop message_loop(MessageLoop::TYPE_UI); |
[email protected] | 92698ce | 2010-06-28 21:49:30 | [diff] [blame] | 144 | host->Start(NewRunnableFunction(&ShutdownTask, &message_loop)); |
| 145 | message_loop.Run(); |
| 146 | |
| 147 | // And then stop the chromoting context. |
| 148 | context.Stop(); |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 149 | file_io_thread.Stop(); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 150 | return 0; |
| 151 | } |