Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 1 | /* |
| 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öller | 0da1156 | 2019-05-10 12:47:25 +0200 | [diff] [blame] | 11 | // 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 Bonadei | 0be40bf | 2019-07-16 18:40:05 +0200 | [diff] [blame] | 20 | #include "absl/flags/parse.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "examples/peerconnection/client/conductor.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "examples/peerconnection/client/flag_defs.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "examples/peerconnection/client/main_wnd.h" |
| 24 | #include "examples/peerconnection/client/peer_connection_client.h" |
| 25 | #include "rtc_base/checks.h" |
Niels Möller | 0da1156 | 2019-05-10 12:47:25 +0200 | [diff] [blame] | 26 | #include "rtc_base/constructor_magic.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "rtc_base/ssl_adapter.h" |
Niels Möller | 0da1156 | 2019-05-10 12:47:25 +0200 | [diff] [blame] | 28 | #include "rtc_base/string_utils.h" // For ToUtf8 |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "rtc_base/win32_socket_init.h" |
Bjorn Terelius | 3e67676 | 2018-10-29 15:26:27 +0100 | [diff] [blame] | 30 | #include "system_wrappers/include/field_trial.h" |
| 31 | #include "test/field_trial.h" |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 32 | |
Niels Möller | 0da1156 | 2019-05-10 12:47:25 +0200 | [diff] [blame] | 33 | namespace { |
| 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(). |
| 39 | class WindowsCommandLineArguments { |
| 40 | public: |
| 41 | WindowsCommandLineArguments(); |
| 42 | |
| 43 | int argc() { return argv_.size(); } |
Mirko Bonadei | 0be40bf | 2019-07-16 18:40:05 +0200 | [diff] [blame] | 44 | char** argv() { return argv_.data(); } |
Niels Möller | 0da1156 | 2019-05-10 12:47:25 +0200 | [diff] [blame] | 45 | |
| 46 | private: |
| 47 | // Owned argument strings. |
| 48 | std::vector<std::string> args_; |
| 49 | // Pointers, to get layout compatible with char** argv. |
Mirko Bonadei | 0be40bf | 2019-07-16 18:40:05 +0200 | [diff] [blame] | 50 | std::vector<char*> argv_; |
Niels Möller | 0da1156 | 2019-05-10 12:47:25 +0200 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | RTC_DISALLOW_COPY_AND_ASSIGN(WindowsCommandLineArguments); |
| 54 | }; |
| 55 | |
| 56 | WindowsCommandLineArguments::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. |
Mirko Bonadei | 0be40bf | 2019-07-16 18:40:05 +0200 | [diff] [blame] | 67 | argv_.push_back(const_cast<char*>(args_.back().c_str())); |
Niels Möller | 0da1156 | 2019-05-10 12:47:25 +0200 | [diff] [blame] | 68 | } |
| 69 | LocalFree(wide_argv); |
| 70 | } |
| 71 | |
| 72 | } // namespace |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | int PASCAL wWinMain(HINSTANCE instance, |
| 74 | HINSTANCE prev_instance, |
| 75 | wchar_t* cmd_line, |
| 76 | int cmd_show) { |
Mirko Bonadei | ba5eaee | 2018-09-17 12:20:10 +0200 | [diff] [blame] | 77 | rtc::WinsockInitializer winsock_init; |
Niels Möller | ef83d15 | 2021-06-23 13:57:48 +0200 | [diff] [blame^] | 78 | rtc::PhysicalSocketServer ss; |
| 79 | rtc::AutoSocketServerThread main_thread(&ss); |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 80 | |
Niels Möller | 0da1156 | 2019-05-10 12:47:25 +0200 | [diff] [blame] | 81 | WindowsCommandLineArguments win_args; |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 82 | int argc = win_args.argc(); |
Mirko Bonadei | 0be40bf | 2019-07-16 18:40:05 +0200 | [diff] [blame] | 83 | char** argv = win_args.argv(); |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 84 | |
Mirko Bonadei | 0be40bf | 2019-07-16 18:40:05 +0200 | [diff] [blame] | 85 | absl::ParseCommandLine(argc, argv); |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 86 | |
Bjorn Terelius | 3e67676 | 2018-10-29 15:26:27 +0100 | [diff] [blame] | 87 | // InitFieldTrialsFromString stores the char*, so the char array must outlive |
| 88 | // the application. |
Mirko Bonadei | 0be40bf | 2019-07-16 18:40:05 +0200 | [diff] [blame] | 89 | const std::string forced_field_trials = |
| 90 | absl::GetFlag(FLAGS_force_fieldtrials); |
| 91 | webrtc::field_trial::InitFieldTrialsFromString(forced_field_trials.c_str()); |
Bjorn Terelius | 3e67676 | 2018-10-29 15:26:27 +0100 | [diff] [blame] | 92 | |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 93 | // Abort if the user specifies a port that is outside the allowed |
| 94 | // range [1, 65535]. |
Mirko Bonadei | 0be40bf | 2019-07-16 18:40:05 +0200 | [diff] [blame] | 95 | if ((absl::GetFlag(FLAGS_port) < 1) || (absl::GetFlag(FLAGS_port) > 65535)) { |
| 96 | printf("Error: %i is not a valid port.\n", absl::GetFlag(FLAGS_port)); |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 97 | return -1; |
| 98 | } |
| 99 | |
Mirko Bonadei | 0be40bf | 2019-07-16 18:40:05 +0200 | [diff] [blame] | 100 | const std::string server = absl::GetFlag(FLAGS_server); |
| 101 | MainWnd wnd(server.c_str(), absl::GetFlag(FLAGS_port), |
| 102 | absl::GetFlag(FLAGS_autoconnect), absl::GetFlag(FLAGS_autocall)); |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 103 | if (!wnd.Create()) { |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 104 | RTC_NOTREACHED(); |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | rtc::InitializeSSL(); |
| 109 | PeerConnectionClient client; |
| 110 | rtc::scoped_refptr<Conductor> conductor( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 111 | new rtc::RefCountedObject<Conductor>(&client, &wnd)); |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 112 | |
| 113 | // Main loop. |
| 114 | MSG msg; |
| 115 | BOOL gm; |
| 116 | while ((gm = ::GetMessage(&msg, NULL, 0, 0)) != 0 && gm != -1) { |
| 117 | if (!wnd.PreTranslateMessage(&msg)) { |
| 118 | ::TranslateMessage(&msg); |
| 119 | ::DispatchMessage(&msg); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (conductor->connection_active() || client.is_connected()) { |
| 124 | while ((conductor->connection_active() || client.is_connected()) && |
| 125 | (gm = ::GetMessage(&msg, NULL, 0, 0)) != 0 && gm != -1) { |
| 126 | if (!wnd.PreTranslateMessage(&msg)) { |
| 127 | ::TranslateMessage(&msg); |
| 128 | ::DispatchMessage(&msg); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | rtc::CleanupSSL(); |
| 134 | return 0; |
| 135 | } |