blob: 47f4f3618e39dc892996857fa8bcbc65a69db024 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <glib.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <gtk/gtk.h>
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stdio.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020015#include "absl/flags/parse.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010016#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "examples/peerconnection/client/conductor.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "examples/peerconnection/client/flag_defs.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "examples/peerconnection/client/linux/main_wnd.h"
20#include "examples/peerconnection/client/peer_connection_client.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/physical_socket_server.h"
22#include "rtc_base/ref_counted_object.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/ssl_adapter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread.h"
Bjorn Terelius3e676762018-10-29 15:26:27 +010025#include "system_wrappers/include/field_trial.h"
26#include "test/field_trial.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000028class CustomSocketServer : public rtc::PhysicalSocketServer {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029 public:
nisse7eaa4ea2017-05-08 05:25:41 -070030 explicit CustomSocketServer(GtkMainWnd* wnd)
31 : wnd_(wnd), conductor_(NULL), client_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032 virtual ~CustomSocketServer() {}
33
Sebastian Jansson290de822020-01-09 14:20:23 +010034 void SetMessageQueue(rtc::Thread* queue) override { message_queue_ = queue; }
nisse7eaa4ea2017-05-08 05:25:41 -070035
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036 void set_client(PeerConnectionClient* client) { client_ = client; }
37 void set_conductor(Conductor* conductor) { conductor_ = conductor; }
38
39 // Override so that we can also pump the GTK message loop.
Patrik Höglunda4251842018-02-20 16:03:41 +010040 bool Wait(int cms, bool process_io) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 // Pump GTK events.
jbauch70625e52015-12-09 14:18:14 -080042 // TODO(henrike): We really should move either the socket server or UI to a
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043 // 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.
Yves Gerey665174f2018-06-19 15:03:05 +020046 while (gtk_events_pending())
47 gtk_main_iteration();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49 if (!wnd_->IsWindow() && !conductor_->connection_active() &&
50 client_ != NULL && !client_->is_connected()) {
nisse7eaa4ea2017-05-08 05:25:41 -070051 message_queue_->Quit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 }
Yves Gerey665174f2018-06-19 15:03:05 +020053 return rtc::PhysicalSocketServer::Wait(0 /*cms == -1 ? 1 : cms*/,
54 process_io);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 }
56
57 protected:
Sebastian Jansson290de822020-01-09 14:20:23 +010058 rtc::Thread* message_queue_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 GtkMainWnd* wnd_;
60 Conductor* conductor_;
61 PeerConnectionClient* client_;
62};
63
64int main(int argc, char* argv[]) {
65 gtk_init(&argc, &argv);
Yves Gerey665174f2018-06-19 15:03:05 +020066// 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
oprypin6fb4f562017-01-31 06:50:14 -080068#if !GLIB_CHECK_VERSION(2, 35, 0)
Yves Gerey665174f2018-06-19 15:03:05 +020069 g_type_init();
oprypin6fb4f562017-01-31 06:50:14 -080070#endif
Yves Gerey665174f2018-06-19 15:03:05 +020071// 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
decurtis@webrtc.orgbfa3c722015-02-17 21:22:48 +000073#if !GLIB_CHECK_VERSION(2, 31, 0)
Yves Gerey665174f2018-06-19 15:03:05 +020074 g_thread_init(NULL);
decurtis@webrtc.orgbfa3c722015-02-17 21:22:48 +000075#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020077 absl::ParseCommandLine(argc, argv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
Bjorn Terelius3e676762018-10-29 15:26:27 +010079 // InitFieldTrialsFromString stores the char*, so the char array must outlive
80 // the application.
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020081 const std::string forced_field_trials =
82 absl::GetFlag(FLAGS_force_fieldtrials);
83 webrtc::field_trial::InitFieldTrialsFromString(forced_field_trials.c_str());
Bjorn Terelius3e676762018-10-29 15:26:27 +010084
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 // Abort if the user specifies a port that is outside the allowed
86 // range [1, 65535].
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020087 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));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 return -1;
90 }
91
Mirko Bonadei0be40bf2019-07-16 18:40:05 +020092 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));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 wnd.Create();
97
nisse7eaa4ea2017-05-08 05:25:41 -070098 CustomSocketServer socket_server(&wnd);
99 rtc::AutoSocketServerThread thread(&socket_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000101 rtc::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 // Must be constructed after we set the socketserver.
103 PeerConnectionClient client;
Niels Möller027c7932022-01-25 13:56:07 +0100104 auto conductor = rtc::make_ref_counted<Conductor>(&client, &wnd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 socket_server.set_client(&client);
106 socket_server.set_conductor(conductor);
107
nisse7eaa4ea2017-05-08 05:25:41 -0700108 thread.Run();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
110 // gtk_main();
111 wnd.Destroy();
112
jbauch70625e52015-12-09 14:18:14 -0800113 // TODO(henrike): Run the Gtk main loop to tear down the connection.
114 /*
115 while (gtk_events_pending()) {
116 gtk_main_iteration();
117 }
118 */
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000119 rtc::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 return 0;
121}