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 | |
| 11 | #include <gtk/gtk.h> |
| 12 | |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 13 | #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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 18 | #include "webrtc/base/ssladapter.h" |
| 19 | #include "webrtc/base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 21 | class CustomSocketServer : public rtc::PhysicalSocketServer { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | public: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 23 | CustomSocketServer(rtc::Thread* thread, GtkMainWnd* wnd) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 24 | : 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 44 | return rtc::PhysicalSocketServer::Wait(0/*cms == -1 ? 1 : cms*/, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | process_io); |
| 46 | } |
| 47 | |
| 48 | protected: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 49 | rtc::Thread* thread_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | GtkMainWnd* wnd_; |
| 51 | Conductor* conductor_; |
| 52 | PeerConnectionClient* client_; |
| 53 | }; |
| 54 | |
| 55 | int main(int argc, char* argv[]) { |
| 56 | gtk_init(&argc, &argv); |
| 57 | g_type_init(); |
decurtis@webrtc.org | bfa3c72 | 2015-02-17 21:22:48 +0000 | [diff] [blame] | 58 | // 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 64 | rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | if (FLAG_help) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 66 | rtc::FlagList::Print(NULL, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 67 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 80 | rtc::AutoThread auto_thread; |
| 81 | rtc::Thread* thread = rtc::Thread::Current(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | CustomSocketServer socket_server(thread, &wnd); |
| 83 | thread->set_socketserver(&socket_server); |
| 84 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 85 | rtc::InitializeSSL(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | // Must be constructed after we set the socketserver. |
| 87 | PeerConnectionClient client; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 88 | rtc::scoped_refptr<Conductor> conductor( |
| 89 | new rtc::RefCountedObject<Conductor>(&client, &wnd)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 103 | rtc::CleanupSSL(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | return 0; |
| 105 | } |