blob: cf88c36fbbf430f7692d9b66d27d18575fb11f31 [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.
33 // TODO: We really should move either the socket server or UI to a
34 // 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);
57 g_type_init();
decurtis@webrtc.orgbfa3c722015-02-17 21:22:48 +000058 // g_thread_init API is deprecated since glib 2.31.0, see release note:
59 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
60#if !GLIB_CHECK_VERSION(2, 31, 0)
61 g_thread_init(NULL);
62#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000064 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 if (FLAG_help) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000066 rtc::FlagList::Print(NULL, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 return 0;
68 }
69
70 // Abort if the user specifies a port that is outside the allowed
71 // range [1, 65535].
72 if ((FLAG_port < 1) || (FLAG_port > 65535)) {
73 printf("Error: %i is not a valid port.\n", FLAG_port);
74 return -1;
75 }
76
77 GtkMainWnd wnd(FLAG_server, FLAG_port, FLAG_autoconnect, FLAG_autocall);
78 wnd.Create();
79
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::AutoThread auto_thread;
81 rtc::Thread* thread = rtc::Thread::Current();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 CustomSocketServer socket_server(thread, &wnd);
83 thread->set_socketserver(&socket_server);
84
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085 rtc::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 // Must be constructed after we set the socketserver.
87 PeerConnectionClient client;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 rtc::scoped_refptr<Conductor> conductor(
89 new rtc::RefCountedObject<Conductor>(&client, &wnd));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 socket_server.set_client(&client);
91 socket_server.set_conductor(conductor);
92
93 thread->Run();
94
95 // gtk_main();
96 wnd.Destroy();
97
98 thread->set_socketserver(NULL);
99 // TODO: Run the Gtk main loop to tear down the connection.
100 //while (gtk_events_pending()) {
101 // gtk_main_iteration();
102 //}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103 rtc::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 return 0;
105}