henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <glib.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | #include <gtk/gtk.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stdio.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "examples/peerconnection/client/conductor.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 16 | #include "examples/peerconnection/client/flag_defs.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "examples/peerconnection/client/linux/main_wnd.h" |
| 18 | #include "examples/peerconnection/client/peer_connection_client.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "rtc_base/flags.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 20 | #include "rtc_base/message_queue.h" |
| 21 | #include "rtc_base/physical_socket_server.h" |
| 22 | #include "rtc_base/ref_counted_object.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "rtc_base/scoped_ref_ptr.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 24 | #include "rtc_base/ssl_adapter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/thread.h" |
Bjorn Terelius | 3e67676 | 2018-10-29 15:26:27 +0100 | [diff] [blame] | 26 | #include "system_wrappers/include/field_trial.h" |
| 27 | #include "test/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 28 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 29 | class CustomSocketServer : public rtc::PhysicalSocketServer { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | public: |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 31 | explicit CustomSocketServer(GtkMainWnd* wnd) |
| 32 | : wnd_(wnd), conductor_(NULL), client_(NULL) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | virtual ~CustomSocketServer() {} |
| 34 | |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 35 | void SetMessageQueue(rtc::MessageQueue* queue) override { |
| 36 | message_queue_ = queue; |
| 37 | } |
| 38 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | void set_client(PeerConnectionClient* client) { client_ = client; } |
| 40 | void set_conductor(Conductor* conductor) { conductor_ = conductor; } |
| 41 | |
| 42 | // Override so that we can also pump the GTK message loop. |
Patrik Höglund | a425184 | 2018-02-20 16:03:41 +0100 | [diff] [blame] | 43 | bool Wait(int cms, bool process_io) override { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | // Pump GTK events. |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame] | 45 | // TODO(henrike): We really should move either the socket server or UI to a |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | // different thread. Alternatively we could look at merging the two loops |
| 47 | // by implementing a dispatcher for the socket server and/or use |
| 48 | // g_main_context_set_poll_func. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | while (gtk_events_pending()) |
| 50 | gtk_main_iteration(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | |
| 52 | if (!wnd_->IsWindow() && !conductor_->connection_active() && |
| 53 | client_ != NULL && !client_->is_connected()) { |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 54 | message_queue_->Quit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 56 | return rtc::PhysicalSocketServer::Wait(0 /*cms == -1 ? 1 : cms*/, |
| 57 | process_io); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | protected: |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 61 | rtc::MessageQueue* message_queue_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | GtkMainWnd* wnd_; |
| 63 | Conductor* conductor_; |
| 64 | PeerConnectionClient* client_; |
| 65 | }; |
| 66 | |
| 67 | int main(int argc, char* argv[]) { |
| 68 | gtk_init(&argc, &argv); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 69 | // g_type_init API is deprecated (and does nothing) since glib 2.35.0, see: |
| 70 | // https://mail.gnome.org/archives/commits-list/2012-November/msg07809.html |
oprypin | 6fb4f56 | 2017-01-31 06:50:14 -0800 | [diff] [blame] | 71 | #if !GLIB_CHECK_VERSION(2, 35, 0) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 72 | g_type_init(); |
oprypin | 6fb4f56 | 2017-01-31 06:50:14 -0800 | [diff] [blame] | 73 | #endif |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 74 | // g_thread_init API is deprecated since glib 2.31.0, see release note: |
| 75 | // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html |
decurtis@webrtc.org | bfa3c72 | 2015-02-17 21:22:48 +0000 | [diff] [blame] | 76 | #if !GLIB_CHECK_VERSION(2, 31, 0) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 77 | g_thread_init(NULL); |
decurtis@webrtc.org | bfa3c72 | 2015-02-17 21:22:48 +0000 | [diff] [blame] | 78 | #endif |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 80 | rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | if (FLAG_help) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 82 | rtc::FlagList::Print(NULL, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Bjorn Terelius | 3e67676 | 2018-10-29 15:26:27 +0100 | [diff] [blame] | 86 | webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials); |
| 87 | // InitFieldTrialsFromString stores the char*, so the char array must outlive |
| 88 | // the application. |
| 89 | webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials); |
| 90 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | // Abort if the user specifies a port that is outside the allowed |
| 92 | // range [1, 65535]. |
| 93 | if ((FLAG_port < 1) || (FLAG_port > 65535)) { |
| 94 | printf("Error: %i is not a valid port.\n", FLAG_port); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | GtkMainWnd wnd(FLAG_server, FLAG_port, FLAG_autoconnect, FLAG_autocall); |
| 99 | wnd.Create(); |
| 100 | |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 101 | CustomSocketServer socket_server(&wnd); |
| 102 | rtc::AutoSocketServerThread thread(&socket_server); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 104 | rtc::InitializeSSL(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | // Must be constructed after we set the socketserver. |
| 106 | PeerConnectionClient client; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 107 | rtc::scoped_refptr<Conductor> conductor( |
| 108 | new rtc::RefCountedObject<Conductor>(&client, &wnd)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | socket_server.set_client(&client); |
| 110 | socket_server.set_conductor(conductor); |
| 111 | |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 112 | thread.Run(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
| 114 | // gtk_main(); |
| 115 | wnd.Destroy(); |
| 116 | |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame] | 117 | // TODO(henrike): Run the Gtk main loop to tear down the connection. |
| 118 | /* |
| 119 | while (gtk_events_pending()) { |
| 120 | gtk_main_iteration(); |
| 121 | } |
| 122 | */ |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 123 | rtc::CleanupSSL(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | return 0; |
| 125 | } |