blob: 6c34683d2ea91d61051e78de09e7fdd2e9e38401 [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
11#include <gtk/gtk.h>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "examples/peerconnection/client/conductor.h"
14#include "examples/peerconnection/client/flagdefs.h"
15#include "examples/peerconnection/client/linux/main_wnd.h"
16#include "examples/peerconnection/client/peer_connection_client.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/ssladapter.h"
19#include "rtc_base/thread.h"
Bjorn Terelius3e676762018-10-29 15:26:27 +010020#include "system_wrappers/include/field_trial.h"
21#include "test/field_trial.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000023class CustomSocketServer : public rtc::PhysicalSocketServer {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024 public:
nisse7eaa4ea2017-05-08 05:25:41 -070025 explicit CustomSocketServer(GtkMainWnd* wnd)
26 : wnd_(wnd), conductor_(NULL), client_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027 virtual ~CustomSocketServer() {}
28
nisse7eaa4ea2017-05-08 05:25:41 -070029 void SetMessageQueue(rtc::MessageQueue* queue) override {
30 message_queue_ = queue;
31 }
32
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033 void set_client(PeerConnectionClient* client) { client_ = client; }
34 void set_conductor(Conductor* conductor) { conductor_ = conductor; }
35
36 // Override so that we can also pump the GTK message loop.
Patrik Höglunda4251842018-02-20 16:03:41 +010037 bool Wait(int cms, bool process_io) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038 // Pump GTK events.
jbauch70625e52015-12-09 14:18:14 -080039 // TODO(henrike): We really should move either the socket server or UI to a
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040 // different thread. Alternatively we could look at merging the two loops
41 // by implementing a dispatcher for the socket server and/or use
42 // g_main_context_set_poll_func.
Yves Gerey665174f2018-06-19 15:03:05 +020043 while (gtk_events_pending())
44 gtk_main_iteration();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46 if (!wnd_->IsWindow() && !conductor_->connection_active() &&
47 client_ != NULL && !client_->is_connected()) {
nisse7eaa4ea2017-05-08 05:25:41 -070048 message_queue_->Quit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 }
Yves Gerey665174f2018-06-19 15:03:05 +020050 return rtc::PhysicalSocketServer::Wait(0 /*cms == -1 ? 1 : cms*/,
51 process_io);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 }
53
54 protected:
nisse7eaa4ea2017-05-08 05:25:41 -070055 rtc::MessageQueue* message_queue_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 GtkMainWnd* wnd_;
57 Conductor* conductor_;
58 PeerConnectionClient* client_;
59};
60
61int main(int argc, char* argv[]) {
62 gtk_init(&argc, &argv);
Yves Gerey665174f2018-06-19 15:03:05 +020063// g_type_init API is deprecated (and does nothing) since glib 2.35.0, see:
64// https://mail.gnome.org/archives/commits-list/2012-November/msg07809.html
oprypin6fb4f562017-01-31 06:50:14 -080065#if !GLIB_CHECK_VERSION(2, 35, 0)
Yves Gerey665174f2018-06-19 15:03:05 +020066 g_type_init();
oprypin6fb4f562017-01-31 06:50:14 -080067#endif
Yves Gerey665174f2018-06-19 15:03:05 +020068// g_thread_init API is deprecated since glib 2.31.0, see release note:
69// http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
decurtis@webrtc.orgbfa3c722015-02-17 21:22:48 +000070#if !GLIB_CHECK_VERSION(2, 31, 0)
Yves Gerey665174f2018-06-19 15:03:05 +020071 g_thread_init(NULL);
decurtis@webrtc.orgbfa3c722015-02-17 21:22:48 +000072#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 if (FLAG_help) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 rtc::FlagList::Print(NULL, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 return 0;
78 }
79
Bjorn Terelius3e676762018-10-29 15:26:27 +010080 webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
81 // InitFieldTrialsFromString stores the char*, so the char array must outlive
82 // the application.
83 webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
84
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].
87 if ((FLAG_port < 1) || (FLAG_port > 65535)) {
88 printf("Error: %i is not a valid port.\n", FLAG_port);
89 return -1;
90 }
91
92 GtkMainWnd wnd(FLAG_server, FLAG_port, FLAG_autoconnect, FLAG_autocall);
93 wnd.Create();
94
nisse7eaa4ea2017-05-08 05:25:41 -070095 CustomSocketServer socket_server(&wnd);
96 rtc::AutoSocketServerThread thread(&socket_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098 rtc::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 // Must be constructed after we set the socketserver.
100 PeerConnectionClient client;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000101 rtc::scoped_refptr<Conductor> conductor(
102 new rtc::RefCountedObject<Conductor>(&client, &wnd));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 socket_server.set_client(&client);
104 socket_server.set_conductor(conductor);
105
nisse7eaa4ea2017-05-08 05:25:41 -0700106 thread.Run();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
108 // gtk_main();
109 wnd.Destroy();
110
jbauch70625e52015-12-09 14:18:14 -0800111 // TODO(henrike): Run the Gtk main loop to tear down the connection.
112 /*
113 while (gtk_events_pending()) {
114 gtk_main_iteration();
115 }
116 */
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000117 rtc::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 return 0;
119}