blob: 6c7178af55cd78aacd7cdaae4d8921de782b5a86 [file] [log] [blame]
Donald E Curtisa8736442015-08-05 15:48:13 -07001/*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Niels Möller0da11562019-05-10 12:47:25 +020011// clang-format off
12// clang formating would change include order.
13#include <windows.h>
14#include <shellapi.h> // must come after windows.h
15// clang-format on
16
17#include <string>
18#include <vector>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "examples/peerconnection/client/conductor.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "examples/peerconnection/client/flag_defs.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "examples/peerconnection/client/main_wnd.h"
23#include "examples/peerconnection/client/peer_connection_client.h"
24#include "rtc_base/checks.h"
Niels Möller0da11562019-05-10 12:47:25 +020025#include "rtc_base/constructor_magic.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/ssl_adapter.h"
Niels Möller0da11562019-05-10 12:47:25 +020027#include "rtc_base/string_utils.h" // For ToUtf8
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/win32_socket_init.h"
29#include "rtc_base/win32_socket_server.h"
Bjorn Terelius3e676762018-10-29 15:26:27 +010030#include "system_wrappers/include/field_trial.h"
31#include "test/field_trial.h"
Donald E Curtisa8736442015-08-05 15:48:13 -070032
Niels Möller0da11562019-05-10 12:47:25 +020033namespace {
34// A helper class to translate Windows command line arguments into UTF8,
35// which then allows us to just pass them to the flags system.
36// This encapsulates all the work of getting the command line and translating
37// it to an array of 8-bit strings; all you have to do is create one of these,
38// and then call argc() and argv().
39class WindowsCommandLineArguments {
40 public:
41 WindowsCommandLineArguments();
42
43 int argc() { return argv_.size(); }
44 const char** argv() { return argv_.data(); }
45
46 private:
47 // Owned argument strings.
48 std::vector<std::string> args_;
49 // Pointers, to get layout compatible with char** argv.
50 std::vector<const char*> argv_;
51
52 private:
53 RTC_DISALLOW_COPY_AND_ASSIGN(WindowsCommandLineArguments);
54};
55
56WindowsCommandLineArguments::WindowsCommandLineArguments() {
57 // start by getting the command line.
58 LPCWSTR command_line = ::GetCommandLineW();
59 // now, convert it to a list of wide char strings.
60 int argc;
61 LPWSTR* wide_argv = ::CommandLineToArgvW(command_line, &argc);
62
63 // iterate over the returned wide strings;
64 for (int i = 0; i < argc; ++i) {
65 args_.push_back(rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i])));
66 // make sure the argv array points to the string data.
67 argv_.push_back(args_.back().c_str());
68 }
69 LocalFree(wide_argv);
70}
71
72} // namespace
Yves Gerey665174f2018-06-19 15:03:05 +020073int PASCAL wWinMain(HINSTANCE instance,
74 HINSTANCE prev_instance,
75 wchar_t* cmd_line,
76 int cmd_show) {
Mirko Bonadeiba5eaee2018-09-17 12:20:10 +020077 rtc::WinsockInitializer winsock_init;
nisse7eaa4ea2017-05-08 05:25:41 -070078 rtc::Win32SocketServer w32_ss;
79 rtc::Win32Thread w32_thread(&w32_ss);
Donald E Curtisa8736442015-08-05 15:48:13 -070080 rtc::ThreadManager::Instance()->SetCurrentThread(&w32_thread);
81
Niels Möller0da11562019-05-10 12:47:25 +020082 WindowsCommandLineArguments win_args;
Donald E Curtisa8736442015-08-05 15:48:13 -070083 int argc = win_args.argc();
Niels Möller0da11562019-05-10 12:47:25 +020084 const char** argv = win_args.argv();
Donald E Curtisa8736442015-08-05 15:48:13 -070085
86 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
87 if (FLAG_help) {
88 rtc::FlagList::Print(NULL, false);
89 return 0;
90 }
91
Bjorn Terelius3e676762018-10-29 15:26:27 +010092 webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
93 // InitFieldTrialsFromString stores the char*, so the char array must outlive
94 // the application.
95 webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
96
Donald E Curtisa8736442015-08-05 15:48:13 -070097 // Abort if the user specifies a port that is outside the allowed
98 // range [1, 65535].
99 if ((FLAG_port < 1) || (FLAG_port > 65535)) {
100 printf("Error: %i is not a valid port.\n", FLAG_port);
101 return -1;
102 }
103
104 MainWnd wnd(FLAG_server, FLAG_port, FLAG_autoconnect, FLAG_autocall);
105 if (!wnd.Create()) {
nissec80e7412017-01-11 05:56:46 -0800106 RTC_NOTREACHED();
Donald E Curtisa8736442015-08-05 15:48:13 -0700107 return -1;
108 }
109
110 rtc::InitializeSSL();
111 PeerConnectionClient client;
112 rtc::scoped_refptr<Conductor> conductor(
Yves Gerey665174f2018-06-19 15:03:05 +0200113 new rtc::RefCountedObject<Conductor>(&client, &wnd));
Donald E Curtisa8736442015-08-05 15:48:13 -0700114
115 // Main loop.
116 MSG msg;
117 BOOL gm;
118 while ((gm = ::GetMessage(&msg, NULL, 0, 0)) != 0 && gm != -1) {
119 if (!wnd.PreTranslateMessage(&msg)) {
120 ::TranslateMessage(&msg);
121 ::DispatchMessage(&msg);
122 }
123 }
124
125 if (conductor->connection_active() || client.is_connected()) {
126 while ((conductor->connection_active() || client.is_connected()) &&
127 (gm = ::GetMessage(&msg, NULL, 0, 0)) != 0 && gm != -1) {
128 if (!wnd.PreTranslateMessage(&msg)) {
129 ::TranslateMessage(&msg);
130 ::DispatchMessage(&msg);
131 }
132 }
133 }
134
135 rtc::CleanupSSL();
136 return 0;
137}