You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. // clang-format off
  11. // clang formating would change include order.
  12. #include <windows.h>
  13. #include <shellapi.h> // must come after windows.h
  14. // clang-format on
  15. #include <string>
  16. #include <vector>
  17. #include "absl/flags/parse.h"
  18. #include "examples/peerconnection/client/conductor.h"
  19. #include "examples/peerconnection/client/flag_defs.h"
  20. #include "examples/peerconnection/client/main_wnd.h"
  21. #include "examples/peerconnection/client/peer_connection_client.h"
  22. #include "rtc_base/checks.h"
  23. #include "rtc_base/constructor_magic.h"
  24. #include "rtc_base/ssl_adapter.h"
  25. #include "rtc_base/string_utils.h" // For ToUtf8
  26. #include "rtc_base/win32_socket_init.h"
  27. #include "rtc_base/win32_socket_server.h"
  28. #include "system_wrappers/include/field_trial.h"
  29. #include "test/field_trial.h"
  30. namespace {
  31. // A helper class to translate Windows command line arguments into UTF8,
  32. // which then allows us to just pass them to the flags system.
  33. // This encapsulates all the work of getting the command line and translating
  34. // it to an array of 8-bit strings; all you have to do is create one of these,
  35. // and then call argc() and argv().
  36. class WindowsCommandLineArguments {
  37. public:
  38. WindowsCommandLineArguments();
  39. int argc() { return argv_.size(); }
  40. char** argv() { return argv_.data(); }
  41. private:
  42. // Owned argument strings.
  43. std::vector<std::string> args_;
  44. // Pointers, to get layout compatible with char** argv.
  45. std::vector<char*> argv_;
  46. private:
  47. RTC_DISALLOW_COPY_AND_ASSIGN(WindowsCommandLineArguments);
  48. };
  49. WindowsCommandLineArguments::WindowsCommandLineArguments() {
  50. // start by getting the command line.
  51. LPCWSTR command_line = ::GetCommandLineW();
  52. // now, convert it to a list of wide char strings.
  53. int argc;
  54. LPWSTR* wide_argv = ::CommandLineToArgvW(command_line, &argc);
  55. // iterate over the returned wide strings;
  56. for (int i = 0; i < argc; ++i) {
  57. args_.push_back(rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i])));
  58. // make sure the argv array points to the string data.
  59. argv_.push_back(const_cast<char*>(args_.back().c_str()));
  60. }
  61. LocalFree(wide_argv);
  62. }
  63. } // namespace
  64. int PASCAL wWinMain(HINSTANCE instance,
  65. HINSTANCE prev_instance,
  66. wchar_t* cmd_line,
  67. int cmd_show) {
  68. rtc::WinsockInitializer winsock_init;
  69. rtc::Win32SocketServer w32_ss;
  70. rtc::Win32Thread w32_thread(&w32_ss);
  71. rtc::ThreadManager::Instance()->SetCurrentThread(&w32_thread);
  72. WindowsCommandLineArguments win_args;
  73. int argc = win_args.argc();
  74. char** argv = win_args.argv();
  75. absl::ParseCommandLine(argc, argv);
  76. // InitFieldTrialsFromString stores the char*, so the char array must outlive
  77. // the application.
  78. const std::string forced_field_trials =
  79. absl::GetFlag(FLAGS_force_fieldtrials);
  80. webrtc::field_trial::InitFieldTrialsFromString(forced_field_trials.c_str());
  81. // Abort if the user specifies a port that is outside the allowed
  82. // range [1, 65535].
  83. if ((absl::GetFlag(FLAGS_port) < 1) || (absl::GetFlag(FLAGS_port) > 65535)) {
  84. printf("Error: %i is not a valid port.\n", absl::GetFlag(FLAGS_port));
  85. return -1;
  86. }
  87. const std::string server = absl::GetFlag(FLAGS_server);
  88. MainWnd wnd(server.c_str(), absl::GetFlag(FLAGS_port),
  89. absl::GetFlag(FLAGS_autoconnect), absl::GetFlag(FLAGS_autocall));
  90. if (!wnd.Create()) {
  91. RTC_NOTREACHED();
  92. return -1;
  93. }
  94. rtc::InitializeSSL();
  95. PeerConnectionClient client;
  96. rtc::scoped_refptr<Conductor> conductor(
  97. new rtc::RefCountedObject<Conductor>(&client, &wnd));
  98. // Main loop.
  99. MSG msg;
  100. BOOL gm;
  101. while ((gm = ::GetMessage(&msg, NULL, 0, 0)) != 0 && gm != -1) {
  102. if (!wnd.PreTranslateMessage(&msg)) {
  103. ::TranslateMessage(&msg);
  104. ::DispatchMessage(&msg);
  105. }
  106. }
  107. if (conductor->connection_active() || client.is_connected()) {
  108. while ((conductor->connection_active() || client.is_connected()) &&
  109. (gm = ::GetMessage(&msg, NULL, 0, 0)) != 0 && gm != -1) {
  110. if (!wnd.PreTranslateMessage(&msg)) {
  111. ::TranslateMessage(&msg);
  112. ::DispatchMessage(&msg);
  113. }
  114. }
  115. }
  116. rtc::CleanupSSL();
  117. return 0;
  118. }