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