10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2012, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <gtk/gtk.h>
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/examples/peerconnection/client/conductor.h"
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/examples/peerconnection/client/flagdefs.h"
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/examples/peerconnection/client/linux/main_wnd.h"
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/examples/peerconnection/client/peer_connection_client.h"
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
352a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/ssladapter.h"
362a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/thread.h"
370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
382a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgclass CustomSocketServer : public rtc::PhysicalSocketServer {
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
402a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  CustomSocketServer(rtc::Thread* thread, GtkMainWnd* wnd)
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      : thread_(thread), wnd_(wnd), conductor_(NULL), client_(NULL) {}
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~CustomSocketServer() {}
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void set_client(PeerConnectionClient* client) { client_ = client; }
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void set_conductor(Conductor* conductor) { conductor_ = conductor; }
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Override so that we can also pump the GTK message loop.
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool Wait(int cms, bool process_io) {
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    // Pump GTK events.
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    // TODO: We really should move either the socket server or UI to a
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    // different thread.  Alternatively we could look at merging the two loops
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    // by implementing a dispatcher for the socket server and/or use
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    // g_main_context_set_poll_func.
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      while (gtk_events_pending())
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        gtk_main_iteration();
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (!wnd_->IsWindow() && !conductor_->connection_active() &&
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        client_ != NULL && !client_->is_connected()) {
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      thread_->Quit();
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
612a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    return rtc::PhysicalSocketServer::Wait(0/*cms == -1 ? 1 : cms*/,
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 process_io);
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
662a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* thread_;
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  GtkMainWnd* wnd_;
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Conductor* conductor_;
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PeerConnectionClient* client_;
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgint main(int argc, char* argv[]) {
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  gtk_init(&argc, &argv);
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  g_type_init();
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  g_thread_init(NULL);
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
772a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (FLAG_help) {
792a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    rtc::FlagList::Print(NULL, false);
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return 0;
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Abort if the user specifies a port that is outside the allowed
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // range [1, 65535].
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if ((FLAG_port < 1) || (FLAG_port > 65535)) {
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    printf("Error: %i is not a valid port.\n", FLAG_port);
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return -1;
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  GtkMainWnd wnd(FLAG_server, FLAG_port, FLAG_autoconnect, FLAG_autocall);
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wnd.Create();
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
932a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::AutoThread auto_thread;
942a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* thread = rtc::Thread::Current();
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CustomSocketServer socket_server(thread, &wnd);
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  thread->set_socketserver(&socket_server);
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
982a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::InitializeSSL();
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Must be constructed after we set the socketserver.
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PeerConnectionClient client;
1012a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<Conductor> conductor(
1022a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      new rtc::RefCountedObject<Conductor>(&client, &wnd));
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  socket_server.set_client(&client);
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  socket_server.set_conductor(conductor);
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  thread->Run();
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // gtk_main();
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wnd.Destroy();
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  thread->set_socketserver(NULL);
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TODO: Run the Gtk main loop to tear down the connection.
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  //while (gtk_events_pending()) {
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  //  gtk_main_iteration();
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  //}
1162a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::CleanupSSL();
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return 0;
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
120