blob: 32bc52bda48e5febfb9b68e2aedf9d4738d722eb [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 Bonadei0be40bf2019-07-16 18:40:05 +020020#include "absl/flags/parse.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "examples/peerconnection/client/conductor.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "examples/peerconnection/client/flag_defs.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "examples/peerconnection/client/main_wnd.h"
24#include "examples/peerconnection/client/peer_connection_client.h"
25#include "rtc_base/checks.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"
Bjorn Terelius3e676762018-10-29 15:26:27 +010029#include "system_wrappers/include/field_trial.h"
30#include "test/field_trial.h"
Donald E Curtisa8736442015-08-05 15:48:13 -070031
Niels Möller0da11562019-05-10 12:47:25 +020032namespace {
33// A helper class to translate Windows command line arguments into UTF8,
34// which then allows us to just pass them to the flags system.
35// This encapsulates all the work of getting the command line and translating
36// it to an array of 8-bit strings; all you have to do is create one of these,
37// and then call argc() and argv().
38class WindowsCommandLineArguments {
39 public:
40 WindowsCommandLineArguments();
41
Artem Titov6cae2d52022-01-26 15:01:10 +000042 WindowsCommandLineArguments(const WindowsCommandLineArguments&) = delete;
43 WindowsCommandLineArguments& operator=(WindowsCommandLineArguments&) = delete;
44
Niels Möller0da11562019-05-10 12:47:25 +020045 int argc() { return argv_.size(); }
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020046 char** argv() { return argv_.data(); }
Niels Möller0da11562019-05-10 12:47:25 +020047
48 private:
49 // Owned argument strings.
50 std::vector<std::string> args_;
51 // Pointers, to get layout compatible with char** argv.
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020052 std::vector<char*> argv_;
Niels Möller0da11562019-05-10 12:47:25 +020053};
54
55WindowsCommandLineArguments::WindowsCommandLineArguments() {
56 // start by getting the command line.
57 LPCWSTR command_line = ::GetCommandLineW();
58 // now, convert it to a list of wide char strings.
59 int argc;
60 LPWSTR* wide_argv = ::CommandLineToArgvW(command_line, &argc);
61
62 // iterate over the returned wide strings;
63 for (int i = 0; i < argc; ++i) {
64 args_.push_back(rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i])));
65 // make sure the argv array points to the string data.
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020066 argv_.push_back(const_cast<char*>(args_.back().c_str()));
Niels Möller0da11562019-05-10 12:47:25 +020067 }
68 LocalFree(wide_argv);
69}
70
71} // namespace
Yves Gerey665174f2018-06-19 15:03:05 +020072int PASCAL wWinMain(HINSTANCE instance,
73 HINSTANCE prev_instance,
74 wchar_t* cmd_line,
75 int cmd_show) {
Mirko Bonadeiba5eaee2018-09-17 12:20:10 +020076 rtc::WinsockInitializer winsock_init;
Niels Mölleref83d152021-06-23 13:57:48 +020077 rtc::PhysicalSocketServer ss;
78 rtc::AutoSocketServerThread main_thread(&ss);
Donald E Curtisa8736442015-08-05 15:48:13 -070079
Niels Möller0da11562019-05-10 12:47:25 +020080 WindowsCommandLineArguments win_args;
Donald E Curtisa8736442015-08-05 15:48:13 -070081 int argc = win_args.argc();
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020082 char** argv = win_args.argv();
Donald E Curtisa8736442015-08-05 15:48:13 -070083
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020084 absl::ParseCommandLine(argc, argv);
Donald E Curtisa8736442015-08-05 15:48:13 -070085
Bjorn Terelius3e676762018-10-29 15:26:27 +010086 // InitFieldTrialsFromString stores the char*, so the char array must outlive
87 // the application.
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020088 const std::string forced_field_trials =
89 absl::GetFlag(FLAGS_force_fieldtrials);
90 webrtc::field_trial::InitFieldTrialsFromString(forced_field_trials.c_str());
Bjorn Terelius3e676762018-10-29 15:26:27 +010091
Donald E Curtisa8736442015-08-05 15:48:13 -070092 // Abort if the user specifies a port that is outside the allowed
93 // range [1, 65535].
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020094 if ((absl::GetFlag(FLAGS_port) < 1) || (absl::GetFlag(FLAGS_port) > 65535)) {
95 printf("Error: %i is not a valid port.\n", absl::GetFlag(FLAGS_port));
Donald E Curtisa8736442015-08-05 15:48:13 -070096 return -1;
97 }
98
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020099 const std::string server = absl::GetFlag(FLAGS_server);
100 MainWnd wnd(server.c_str(), absl::GetFlag(FLAGS_port),
101 absl::GetFlag(FLAGS_autoconnect), absl::GetFlag(FLAGS_autocall));
Donald E Curtisa8736442015-08-05 15:48:13 -0700102 if (!wnd.Create()) {
Artem Titovd3251962021-11-15 16:57:07 +0100103 RTC_DCHECK_NOTREACHED();
Donald E Curtisa8736442015-08-05 15:48:13 -0700104 return -1;
105 }
106
107 rtc::InitializeSSL();
108 PeerConnectionClient client;
Niels Möller027c7932022-01-25 13:56:07 +0100109 auto conductor = rtc::make_ref_counted<Conductor>(&client, &wnd);
Donald E Curtisa8736442015-08-05 15:48:13 -0700110
111 // Main loop.
112 MSG msg;
113 BOOL gm;
114 while ((gm = ::GetMessage(&msg, NULL, 0, 0)) != 0 && gm != -1) {
115 if (!wnd.PreTranslateMessage(&msg)) {
116 ::TranslateMessage(&msg);
117 ::DispatchMessage(&msg);
118 }
119 }
120
121 if (conductor->connection_active() || client.is_connected()) {
122 while ((conductor->connection_active() || client.is_connected()) &&
123 (gm = ::GetMessage(&msg, NULL, 0, 0)) != 0 && gm != -1) {
124 if (!wnd.PreTranslateMessage(&msg)) {
125 ::TranslateMessage(&msg);
126 ::DispatchMessage(&msg);
127 }
128 }
129 }
130
131 rtc::CleanupSSL();
132 return 0;
133}