blob: b3978cf7b074da332d2c59e0feada0682fd33e69 [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 Bonadei92ea95e2017-09-15 06:47:31 +020015#include "examples/peerconnection/client/conductor.h"
16#include "examples/peerconnection/client/flagdefs.h"
17#include "examples/peerconnection/client/linux/main_wnd.h"
18#include "examples/peerconnection/client/peer_connection_client.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/flags.h"
20#include "rtc_base/messagequeue.h"
21#include "rtc_base/physicalsocketserver.h"
22#include "rtc_base/refcountedobject.h"
23#include "rtc_base/scoped_ref_ptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/ssladapter.h"
25#include "rtc_base/thread.h"
Bjorn Terelius3e676762018-10-29 15:26:27 +010026#include "system_wrappers/include/field_trial.h"
27#include "test/field_trial.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000029class CustomSocketServer : public rtc::PhysicalSocketServer {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030 public:
nisse7eaa4ea2017-05-08 05:25:41 -070031 explicit CustomSocketServer(GtkMainWnd* wnd)
32 : wnd_(wnd), conductor_(NULL), client_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033 virtual ~CustomSocketServer() {}
34
nisse7eaa4ea2017-05-08 05:25:41 -070035 void SetMessageQueue(rtc::MessageQueue* queue) override {
36 message_queue_ = queue;
37 }
38
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039 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öglunda4251842018-02-20 16:03:41 +010043 bool Wait(int cms, bool process_io) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044 // Pump GTK events.
jbauch70625e52015-12-09 14:18:14 -080045 // TODO(henrike): We really should move either the socket server or UI to a
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046 // 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 Gerey665174f2018-06-19 15:03:05 +020049 while (gtk_events_pending())
50 gtk_main_iteration();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
52 if (!wnd_->IsWindow() && !conductor_->connection_active() &&
53 client_ != NULL && !client_->is_connected()) {
nisse7eaa4ea2017-05-08 05:25:41 -070054 message_queue_->Quit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 }
Yves Gerey665174f2018-06-19 15:03:05 +020056 return rtc::PhysicalSocketServer::Wait(0 /*cms == -1 ? 1 : cms*/,
57 process_io);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 }
59
60 protected:
nisse7eaa4ea2017-05-08 05:25:41 -070061 rtc::MessageQueue* message_queue_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 GtkMainWnd* wnd_;
63 Conductor* conductor_;
64 PeerConnectionClient* client_;
65};
66
67int main(int argc, char* argv[]) {
68 gtk_init(&argc, &argv);
Yves Gerey665174f2018-06-19 15:03:05 +020069// 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
oprypin6fb4f562017-01-31 06:50:14 -080071#if !GLIB_CHECK_VERSION(2, 35, 0)
Yves Gerey665174f2018-06-19 15:03:05 +020072 g_type_init();
oprypin6fb4f562017-01-31 06:50:14 -080073#endif
Yves Gerey665174f2018-06-19 15:03:05 +020074// 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.orgbfa3c722015-02-17 21:22:48 +000076#if !GLIB_CHECK_VERSION(2, 31, 0)
Yves Gerey665174f2018-06-19 15:03:05 +020077 g_thread_init(NULL);
decurtis@webrtc.orgbfa3c722015-02-17 21:22:48 +000078#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 if (FLAG_help) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000082 rtc::FlagList::Print(NULL, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 return 0;
84 }
85
Bjorn Terelius3e676762018-10-29 15:26:27 +010086 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.org28e20752013-07-10 00:45:36 +000091 // 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
nisse7eaa4ea2017-05-08 05:25:41 -0700101 CustomSocketServer socket_server(&wnd);
102 rtc::AutoSocketServerThread thread(&socket_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104 rtc::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 // Must be constructed after we set the socketserver.
106 PeerConnectionClient client;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000107 rtc::scoped_refptr<Conductor> conductor(
108 new rtc::RefCountedObject<Conductor>(&client, &wnd));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 socket_server.set_client(&client);
110 socket_server.set_conductor(conductor);
111
nisse7eaa4ea2017-05-08 05:25:41 -0700112 thread.Run();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
114 // gtk_main();
115 wnd.Destroy();
116
jbauch70625e52015-12-09 14:18:14 -0800117 // 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.orgd4e598d2014-07-29 17:36:52 +0000123 rtc::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 return 0;
125}