main.cc (3867B)
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 <cstdio> 14 #include <memory> 15 #include <string> 16 17 #include "absl/flags/flag.h" 18 #include "absl/flags/parse.h" 19 #include "api/environment/environment.h" 20 #include "api/environment/environment_factory.h" 21 #include "api/field_trials.h" 22 #include "api/make_ref_counted.h" 23 #include "api/scoped_refptr.h" 24 #include "api/units/time_delta.h" 25 #include "examples/peerconnection/client/conductor.h" 26 #include "examples/peerconnection/client/flag_defs.h" 27 #include "examples/peerconnection/client/linux/main_wnd.h" 28 #include "examples/peerconnection/client/peer_connection_client.h" 29 #include "rtc_base/physical_socket_server.h" 30 #include "rtc_base/ssl_adapter.h" 31 #include "rtc_base/thread.h" 32 33 class CustomSocketServer : public webrtc::PhysicalSocketServer { 34 public: 35 explicit CustomSocketServer(GtkMainWnd* wnd) 36 : wnd_(wnd), conductor_(nullptr), client_(nullptr) {} 37 ~CustomSocketServer() override {} 38 39 void SetMessageQueue(webrtc::Thread* queue) override { 40 message_queue_ = queue; 41 } 42 43 void set_client(PeerConnectionClient* client) { client_ = client; } 44 void set_conductor(Conductor* conductor) { conductor_ = conductor; } 45 46 // Override so that we can also pump the GTK message loop. 47 // This function never waits. 48 bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) override { 49 // Pump GTK events. 50 // TODO(henrike): We really should move either the socket server or UI to a 51 // different thread. Alternatively we could look at merging the two loops 52 // by implementing a dispatcher for the socket server and/or use 53 // g_main_context_set_poll_func. 54 while (gtk_events_pending()) 55 gtk_main_iteration(); 56 57 if (!wnd_->IsWindow() && !conductor_->connection_active() && 58 client_ != nullptr && !client_->is_connected()) { 59 message_queue_->Quit(); 60 } 61 return webrtc::PhysicalSocketServer::Wait(webrtc::TimeDelta::Zero(), 62 process_io); 63 } 64 65 protected: 66 webrtc::Thread* message_queue_; 67 GtkMainWnd* wnd_; 68 Conductor* conductor_; 69 PeerConnectionClient* client_; 70 }; 71 72 int main(int argc, char* argv[]) { 73 gtk_init(&argc, &argv); 74 75 absl::ParseCommandLine(argc, argv); 76 77 webrtc::Environment env = 78 webrtc::CreateEnvironment(std::make_unique<webrtc::FieldTrials>( 79 absl::GetFlag(FLAGS_force_fieldtrials))); 80 81 // Abort if the user specifies a port that is outside the allowed 82 // range [1, 65535]. 83 if ((absl::GetFlag(FLAGS_port) < 1) || (absl::GetFlag(FLAGS_port) > 65535)) { 84 printf("Error: %i is not a valid port.\n", absl::GetFlag(FLAGS_port)); 85 return -1; 86 } 87 88 const std::string server = absl::GetFlag(FLAGS_server); 89 GtkMainWnd wnd(server.c_str(), absl::GetFlag(FLAGS_port), 90 absl::GetFlag(FLAGS_autoconnect), 91 absl::GetFlag(FLAGS_autocall)); 92 wnd.Create(); 93 94 CustomSocketServer socket_server(&wnd); 95 webrtc::AutoSocketServerThread thread(&socket_server); 96 97 webrtc::InitializeSSL(); 98 // Must be constructed after we set the socketserver. 99 PeerConnectionClient client; 100 auto conductor = webrtc::make_ref_counted<Conductor>(env, &client, &wnd); 101 socket_server.set_client(&client); 102 socket_server.set_conductor(conductor.get()); 103 104 thread.Run(); 105 106 // gtk_main(); 107 wnd.Destroy(); 108 109 // TODO(henrike): Run the Gtk main loop to tear down the connection. 110 /* 111 while (gtk_events_pending()) { 112 gtk_main_iteration(); 113 } 114 */ 115 webrtc::CleanupSSL(); 116 return 0; 117 }