128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org/*
2a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  Copyright 2012 The WebRTC Project Authors. All rights reserved.
328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *
4a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  Use of this source code is governed by a BSD-style license
5a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  that can be found in the LICENSE file in the root of the source
6a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  tree. An additional intellectual property rights grant can be found
7a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  in the file PATENTS.  All contributing project authors may
8a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis *  be found in the AUTHORS file in the root of the source tree.
928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org */
1028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
1128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#include <gtk/gtk.h>
1228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
13a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis#include "webrtc/examples/peerconnection/client/conductor.h"
14a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis#include "webrtc/examples/peerconnection/client/flagdefs.h"
15a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis#include "webrtc/examples/peerconnection/client/linux/main_wnd.h"
16a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis#include "webrtc/examples/peerconnection/client/peer_connection_client.h"
1728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
18d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org#include "webrtc/base/ssladapter.h"
19d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org#include "webrtc/base/thread.h"
2028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
21d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.orgclass CustomSocketServer : public rtc::PhysicalSocketServer {
2228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org public:
23d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  CustomSocketServer(rtc::Thread* thread, GtkMainWnd* wnd)
2428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      : thread_(thread), wnd_(wnd), conductor_(NULL), client_(NULL) {}
2528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual ~CustomSocketServer() {}
2628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
2728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  void set_client(PeerConnectionClient* client) { client_ = client; }
2828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  void set_conductor(Conductor* conductor) { conductor_ = conductor; }
2928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
3028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // Override so that we can also pump the GTK message loop.
3128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual bool Wait(int cms, bool process_io) {
3228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    // Pump GTK events.
3370625e5bf3c91c57bf704d380bcc9df86575f08ajbauch    // TODO(henrike): We really should move either the socket server or UI to a
3428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    // different thread.  Alternatively we could look at merging the two loops
3528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    // by implementing a dispatcher for the socket server and/or use
3628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    // g_main_context_set_poll_func.
3728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      while (gtk_events_pending())
3828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        gtk_main_iteration();
3928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
4028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    if (!wnd_->IsWindow() && !conductor_->connection_active() &&
4128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        client_ != NULL && !client_->is_connected()) {
4228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      thread_->Quit();
4328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    }
44d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org    return rtc::PhysicalSocketServer::Wait(0/*cms == -1 ? 1 : cms*/,
4528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org                                                 process_io);
4628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
4728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
4828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org protected:
49d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  rtc::Thread* thread_;
5028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  GtkMainWnd* wnd_;
5128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  Conductor* conductor_;
5228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  PeerConnectionClient* client_;
5328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org};
5428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
5528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgint main(int argc, char* argv[]) {
5628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  gtk_init(&argc, &argv);
5728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  g_type_init();
58bfa3c7253fc29a6c64115c49457cd69cec05932bdecurtis@webrtc.org  // g_thread_init API is deprecated since glib 2.31.0, see release note:
59bfa3c7253fc29a6c64115c49457cd69cec05932bdecurtis@webrtc.org  // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
60bfa3c7253fc29a6c64115c49457cd69cec05932bdecurtis@webrtc.org#if !GLIB_CHECK_VERSION(2, 31, 0)
61bfa3c7253fc29a6c64115c49457cd69cec05932bdecurtis@webrtc.org    g_thread_init(NULL);
62bfa3c7253fc29a6c64115c49457cd69cec05932bdecurtis@webrtc.org#endif
6328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
64d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
6528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  if (FLAG_help) {
66d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org    rtc::FlagList::Print(NULL, false);
6728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    return 0;
6828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
6928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
7028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // Abort if the user specifies a port that is outside the allowed
7128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // range [1, 65535].
7228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  if ((FLAG_port < 1) || (FLAG_port > 65535)) {
7328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    printf("Error: %i is not a valid port.\n", FLAG_port);
7428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    return -1;
7528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
7628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
7728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  GtkMainWnd wnd(FLAG_server, FLAG_port, FLAG_autoconnect, FLAG_autocall);
7828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  wnd.Create();
7928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
80d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  rtc::AutoThread auto_thread;
81d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  rtc::Thread* thread = rtc::Thread::Current();
8228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  CustomSocketServer socket_server(thread, &wnd);
8328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  thread->set_socketserver(&socket_server);
8428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
85d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  rtc::InitializeSSL();
8628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // Must be constructed after we set the socketserver.
8728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  PeerConnectionClient client;
88d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  rtc::scoped_refptr<Conductor> conductor(
89d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org      new rtc::RefCountedObject<Conductor>(&client, &wnd));
9028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  socket_server.set_client(&client);
9128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  socket_server.set_conductor(conductor);
9228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
9328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  thread->Run();
9428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
9528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // gtk_main();
9628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  wnd.Destroy();
9728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
9828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  thread->set_socketserver(NULL);
9970625e5bf3c91c57bf704d380bcc9df86575f08ajbauch  // TODO(henrike): Run the Gtk main loop to tear down the connection.
10070625e5bf3c91c57bf704d380bcc9df86575f08ajbauch  /*
10170625e5bf3c91c57bf704d380bcc9df86575f08ajbauch  while (gtk_events_pending()) {
10270625e5bf3c91c57bf704d380bcc9df86575f08ajbauch    gtk_main_iteration();
10370625e5bf3c91c57bf704d380bcc9df86575f08ajbauch  }
10470625e5bf3c91c57bf704d380bcc9df86575f08ajbauch  */
105d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  rtc::CleanupSSL();
10628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return 0;
10728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
108