blob: c074d7c238775fdf8c4d1008a3e67138121901f6 [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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000021class CustomSocketServer : public rtc::PhysicalSocketServer {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022 public:
nisse7eaa4ea2017-05-08 05:25:41 -070023 explicit CustomSocketServer(GtkMainWnd* wnd)
24 : wnd_(wnd), conductor_(NULL), client_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025 virtual ~CustomSocketServer() {}
26
nisse7eaa4ea2017-05-08 05:25:41 -070027 void SetMessageQueue(rtc::MessageQueue* queue) override {
28 message_queue_ = queue;
29 }
30
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031 void set_client(PeerConnectionClient* client) { client_ = client; }
32 void set_conductor(Conductor* conductor) { conductor_ = conductor; }
33
34 // Override so that we can also pump the GTK message loop.
35 virtual bool Wait(int cms, bool process_io) {
36 // Pump GTK events.
jbauch70625e52015-12-09 14:18:14 -080037 // TODO(henrike): We really should move either the socket server or UI to a
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038 // different thread. Alternatively we could look at merging the two loops
39 // by implementing a dispatcher for the socket server and/or use
40 // g_main_context_set_poll_func.
41 while (gtk_events_pending())
42 gtk_main_iteration();
43
44 if (!wnd_->IsWindow() && !conductor_->connection_active() &&
45 client_ != NULL && !client_->is_connected()) {
nisse7eaa4ea2017-05-08 05:25:41 -070046 message_queue_->Quit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000048 return rtc::PhysicalSocketServer::Wait(0/*cms == -1 ? 1 : cms*/,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 process_io);
50 }
51
52 protected:
nisse7eaa4ea2017-05-08 05:25:41 -070053 rtc::MessageQueue* message_queue_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 GtkMainWnd* wnd_;
55 Conductor* conductor_;
56 PeerConnectionClient* client_;
57};
58
59int main(int argc, char* argv[]) {
60 gtk_init(&argc, &argv);
oprypin6fb4f562017-01-31 06:50:14 -080061 // g_type_init API is deprecated (and does nothing) since glib 2.35.0, see:
62 // https://mail.gnome.org/archives/commits-list/2012-November/msg07809.html
63#if !GLIB_CHECK_VERSION(2, 35, 0)
64 g_type_init();
65#endif
decurtis@webrtc.orgbfa3c722015-02-17 21:22:48 +000066 // g_thread_init API is deprecated since glib 2.31.0, see release note:
67 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
68#if !GLIB_CHECK_VERSION(2, 31, 0)
69 g_thread_init(NULL);
70#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000072 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 if (FLAG_help) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 rtc::FlagList::Print(NULL, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 return 0;
76 }
77
78 // Abort if the user specifies a port that is outside the allowed
79 // range [1, 65535].
80 if ((FLAG_port < 1) || (FLAG_port > 65535)) {
81 printf("Error: %i is not a valid port.\n", FLAG_port);
82 return -1;
83 }
84
85 GtkMainWnd wnd(FLAG_server, FLAG_port, FLAG_autoconnect, FLAG_autocall);
86 wnd.Create();
87
nisse7eaa4ea2017-05-08 05:25:41 -070088 CustomSocketServer socket_server(&wnd);
89 rtc::AutoSocketServerThread thread(&socket_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091 rtc::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 // Must be constructed after we set the socketserver.
93 PeerConnectionClient client;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094 rtc::scoped_refptr<Conductor> conductor(
95 new rtc::RefCountedObject<Conductor>(&client, &wnd));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 socket_server.set_client(&client);
97 socket_server.set_conductor(conductor);
98
nisse7eaa4ea2017-05-08 05:25:41 -070099 thread.Run();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
101 // gtk_main();
102 wnd.Destroy();
103
jbauch70625e52015-12-09 14:18:14 -0800104 // TODO(henrike): Run the Gtk main loop to tear down the connection.
105 /*
106 while (gtk_events_pending()) {
107 gtk_main_iteration();
108 }
109 */
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000110 rtc::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 return 0;
112}