1/*
2 *  Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
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.
9 */
10
11#include <gtk/gtk.h>
12
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"
17
18#include "webrtc/base/ssladapter.h"
19#include "webrtc/base/thread.h"
20
21class CustomSocketServer : public rtc::PhysicalSocketServer {
22 public:
23  CustomSocketServer(rtc::Thread* thread, GtkMainWnd* wnd)
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(henrike): 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    }
44    return rtc::PhysicalSocketServer::Wait(0/*cms == -1 ? 1 : cms*/,
45                                                 process_io);
46  }
47
48 protected:
49  rtc::Thread* thread_;
50  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();
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
63
64  rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
65  if (FLAG_help) {
66    rtc::FlagList::Print(NULL, false);
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
80  rtc::AutoThread auto_thread;
81  rtc::Thread* thread = rtc::Thread::Current();
82  CustomSocketServer socket_server(thread, &wnd);
83  thread->set_socketserver(&socket_server);
84
85  rtc::InitializeSSL();
86  // Must be constructed after we set the socketserver.
87  PeerConnectionClient client;
88  rtc::scoped_refptr<Conductor> conductor(
89      new rtc::RefCountedObject<Conductor>(&client, &wnd));
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(henrike): Run the Gtk main loop to tear down the connection.
100  /*
101  while (gtk_events_pending()) {
102    gtk_main_iteration();
103  }
104  */
105  rtc::CleanupSSL();
106  return 0;
107}
108