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 #include <glib.h>
12 #include <gtk/gtk.h>
13 #include <stdio.h>
14
15 #include "absl/flags/parse.h"
16 #include "api/scoped_refptr.h"
17 #include "examples/peerconnection/client/conductor.h"
18 #include "examples/peerconnection/client/flag_defs.h"
19 #include "examples/peerconnection/client/linux/main_wnd.h"
20 #include "examples/peerconnection/client/peer_connection_client.h"
21 #include "rtc_base/physical_socket_server.h"
22 #include "rtc_base/ref_counted_object.h"
23 #include "rtc_base/ssl_adapter.h"
24 #include "rtc_base/thread.h"
25 #include "system_wrappers/include/field_trial.h"
26 #include "test/field_trial.h"
27
28 class CustomSocketServer : public rtc::PhysicalSocketServer {
29 public:
CustomSocketServer(GtkMainWnd * wnd)30 explicit CustomSocketServer(GtkMainWnd* wnd)
31 : wnd_(wnd), conductor_(NULL), client_(NULL) {}
~CustomSocketServer()32 virtual ~CustomSocketServer() {}
33
SetMessageQueue(rtc::Thread * queue)34 void SetMessageQueue(rtc::Thread* queue) override { message_queue_ = queue; }
35
set_client(PeerConnectionClient * client)36 void set_client(PeerConnectionClient* client) { client_ = client; }
set_conductor(Conductor * conductor)37 void set_conductor(Conductor* conductor) { conductor_ = conductor; }
38
39 // Override so that we can also pump the GTK message loop.
Wait(int cms,bool process_io)40 bool Wait(int cms, bool process_io) override {
41 // Pump GTK events.
42 // TODO(henrike): We really should move either the socket server or UI to a
43 // different thread. Alternatively we could look at merging the two loops
44 // by implementing a dispatcher for the socket server and/or use
45 // g_main_context_set_poll_func.
46 while (gtk_events_pending())
47 gtk_main_iteration();
48
49 if (!wnd_->IsWindow() && !conductor_->connection_active() &&
50 client_ != NULL && !client_->is_connected()) {
51 message_queue_->Quit();
52 }
53 return rtc::PhysicalSocketServer::Wait(0 /*cms == -1 ? 1 : cms*/,
54 process_io);
55 }
56
57 protected:
58 rtc::Thread* message_queue_;
59 GtkMainWnd* wnd_;
60 Conductor* conductor_;
61 PeerConnectionClient* client_;
62 };
63
main(int argc,char * argv[])64 int main(int argc, char* argv[]) {
65 gtk_init(&argc, &argv);
66 // g_type_init API is deprecated (and does nothing) since glib 2.35.0, see:
67 // https://mail.gnome.org/archives/commits-list/2012-November/msg07809.html
68 #if !GLIB_CHECK_VERSION(2, 35, 0)
69 g_type_init();
70 #endif
71 // g_thread_init API is deprecated since glib 2.31.0, see release note:
72 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
73 #if !GLIB_CHECK_VERSION(2, 31, 0)
74 g_thread_init(NULL);
75 #endif
76
77 absl::ParseCommandLine(argc, argv);
78
79 // InitFieldTrialsFromString stores the char*, so the char array must outlive
80 // the application.
81 const std::string forced_field_trials =
82 absl::GetFlag(FLAGS_force_fieldtrials);
83 webrtc::field_trial::InitFieldTrialsFromString(forced_field_trials.c_str());
84
85 // Abort if the user specifies a port that is outside the allowed
86 // range [1, 65535].
87 if ((absl::GetFlag(FLAGS_port) < 1) || (absl::GetFlag(FLAGS_port) > 65535)) {
88 printf("Error: %i is not a valid port.\n", absl::GetFlag(FLAGS_port));
89 return -1;
90 }
91
92 const std::string server = absl::GetFlag(FLAGS_server);
93 GtkMainWnd wnd(server.c_str(), absl::GetFlag(FLAGS_port),
94 absl::GetFlag(FLAGS_autoconnect),
95 absl::GetFlag(FLAGS_autocall));
96 wnd.Create();
97
98 CustomSocketServer socket_server(&wnd);
99 rtc::AutoSocketServerThread thread(&socket_server);
100
101 rtc::InitializeSSL();
102 // Must be constructed after we set the socketserver.
103 PeerConnectionClient client;
104 rtc::scoped_refptr<Conductor> conductor(
105 new rtc::RefCountedObject<Conductor>(&client, &wnd));
106 socket_server.set_client(&client);
107 socket_server.set_conductor(conductor);
108
109 thread.Run();
110
111 // gtk_main();
112 wnd.Destroy();
113
114 // TODO(henrike): Run the Gtk main loop to tear down the connection.
115 /*
116 while (gtk_events_pending()) {
117 gtk_main_iteration();
118 }
119 */
120 rtc::CleanupSSL();
121 return 0;
122 }
123