blob: 2982c287f12beb4ee61072820fa1e825d588b9a2 [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
Donald E Curtisa8736442015-08-05 15:48:13 -070013#include "webrtc/examples/peerconnection/client/conductor.h"
14#include "webrtc/examples/peerconnection/client/flagdefs.h"
15#include "webrtc/examples/peerconnection/client/linux/main_wnd.h"
16#include "webrtc/examples/peerconnection/client/peer_connection_client.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/ssladapter.h"
19#include "webrtc/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:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000023 CustomSocketServer(rtc::Thread* thread, GtkMainWnd* wnd)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024 : thread_(thread), wnd_(wnd), conductor_(NULL), client_(NULL) {}
25 virtual ~CustomSocketServer() {}
26
27 void set_client(PeerConnectionClient* client) { client_ = client; }
28 void set_conductor(Conductor* conductor) { conductor_ = conductor; }
29
30 // Override so that we can also pump the GTK message loop.
31 virtual bool Wait(int cms, bool process_io) {
32 // Pump GTK events.
jbauch70625e52015-12-09 14:18:14 -080033 // TODO(henrike): We really should move either the socket server or UI to a
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034 // different thread. Alternatively we could look at merging the two loops
35 // by implementing a dispatcher for the socket server and/or use
36 // g_main_context_set_poll_func.
37 while (gtk_events_pending())
38 gtk_main_iteration();
39
40 if (!wnd_->IsWindow() && !conductor_->connection_active() &&
41 client_ != NULL && !client_->is_connected()) {
42 thread_->Quit();
43 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000044 return rtc::PhysicalSocketServer::Wait(0/*cms == -1 ? 1 : cms*/,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 process_io);
46 }
47
48 protected:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000049 rtc::Thread* thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 GtkMainWnd* wnd_;
51 Conductor* conductor_;
52 PeerConnectionClient* client_;
53};
54
55int main(int argc, char* argv[]) {
56 gtk_init(&argc, &argv);
oprypin6fb4f562017-01-31 06:50:14 -080057 // g_type_init API is deprecated (and does nothing) since glib 2.35.0, see:
58 // https://mail.gnome.org/archives/commits-list/2012-November/msg07809.html
59#if !GLIB_CHECK_VERSION(2, 35, 0)
60 g_type_init();
61#endif
decurtis@webrtc.orgbfa3c722015-02-17 21:22:48 +000062 // g_thread_init API is deprecated since glib 2.31.0, see release note:
63 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
64#if !GLIB_CHECK_VERSION(2, 31, 0)
65 g_thread_init(NULL);
66#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 if (FLAG_help) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070 rtc::FlagList::Print(NULL, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 return 0;
72 }
73
74 // Abort if the user specifies a port that is outside the allowed
75 // range [1, 65535].
76 if ((FLAG_port < 1) || (FLAG_port > 65535)) {
77 printf("Error: %i is not a valid port.\n", FLAG_port);
78 return -1;
79 }
80
81 GtkMainWnd wnd(FLAG_server, FLAG_port, FLAG_autoconnect, FLAG_autocall);
82 wnd.Create();
83
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084 rtc::AutoThread auto_thread;
85 rtc::Thread* thread = rtc::Thread::Current();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 CustomSocketServer socket_server(thread, &wnd);
87 thread->set_socketserver(&socket_server);
88
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089 rtc::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 // Must be constructed after we set the socketserver.
91 PeerConnectionClient client;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092 rtc::scoped_refptr<Conductor> conductor(
93 new rtc::RefCountedObject<Conductor>(&client, &wnd));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 socket_server.set_client(&client);
95 socket_server.set_conductor(conductor);
96
97 thread->Run();
98
99 // gtk_main();
100 wnd.Destroy();
101
102 thread->set_socketserver(NULL);
jbauch70625e52015-12-09 14:18:14 -0800103 // TODO(henrike): Run the Gtk main loop to tear down the connection.
104 /*
105 while (gtk_events_pending()) {
106 gtk_main_iteration();
107 }
108 */
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000109 rtc::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 return 0;
111}