xref: /aosp_15_r20/external/webrtc/examples/peerconnection/client/linux/main.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 <glib.h>
12 #include <gtk/gtk.h>
13 #include <stdio.h>
14 
15 #include "absl/flags/parse.h"
16 #include "api/scoped_refptr.h"
17 #include "examples/peerconnection/client/conductor.h"
18 #include "examples/peerconnection/client/flag_defs.h"
19 #include "examples/peerconnection/client/linux/main_wnd.h"
20 #include "examples/peerconnection/client/peer_connection_client.h"
21 #include "rtc_base/physical_socket_server.h"
22 #include "rtc_base/ssl_adapter.h"
23 #include "rtc_base/thread.h"
24 #include "system_wrappers/include/field_trial.h"
25 #include "test/field_trial.h"
26 
27 class CustomSocketServer : public rtc::PhysicalSocketServer {
28  public:
CustomSocketServer(GtkMainWnd * wnd)29   explicit CustomSocketServer(GtkMainWnd* wnd)
30       : wnd_(wnd), conductor_(NULL), client_(NULL) {}
~CustomSocketServer()31   virtual ~CustomSocketServer() {}
32 
SetMessageQueue(rtc::Thread * queue)33   void SetMessageQueue(rtc::Thread* queue) override { message_queue_ = queue; }
34 
set_client(PeerConnectionClient * client)35   void set_client(PeerConnectionClient* client) { client_ = client; }
set_conductor(Conductor * conductor)36   void set_conductor(Conductor* conductor) { conductor_ = conductor; }
37 
38   // Override so that we can also pump the GTK message loop.
39   // This function never waits.
Wait(webrtc::TimeDelta max_wait_duration,bool process_io)40   bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) override {
41     // Pump GTK events.
42     // TODO(henrike): We really should move either the socket server or UI to a
43     // different thread.  Alternatively we could look at merging the two loops
44     // by implementing a dispatcher for the socket server and/or use
45     // g_main_context_set_poll_func.
46     while (gtk_events_pending())
47       gtk_main_iteration();
48 
49     if (!wnd_->IsWindow() && !conductor_->connection_active() &&
50         client_ != NULL && !client_->is_connected()) {
51       message_queue_->Quit();
52     }
53     return rtc::PhysicalSocketServer::Wait(webrtc::TimeDelta::Zero(),
54                                            process_io);
55   }
56 
57  protected:
58   rtc::Thread* message_queue_;
59   GtkMainWnd* wnd_;
60   Conductor* conductor_;
61   PeerConnectionClient* client_;
62 };
63 
main(int argc,char * argv[])64 int main(int argc, char* argv[]) {
65   gtk_init(&argc, &argv);
66 // g_type_init API is deprecated (and does nothing) since glib 2.35.0, see:
67 // https://mail.gnome.org/archives/commits-list/2012-November/msg07809.html
68 #if !GLIB_CHECK_VERSION(2, 35, 0)
69   g_type_init();
70 #endif
71 // g_thread_init API is deprecated since glib 2.31.0, see release note:
72 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.html
73 #if !GLIB_CHECK_VERSION(2, 31, 0)
74   g_thread_init(NULL);
75 #endif
76 
77   absl::ParseCommandLine(argc, argv);
78 
79   // InitFieldTrialsFromString stores the char*, so the char array must outlive
80   // the application.
81   const std::string forced_field_trials =
82       absl::GetFlag(FLAGS_force_fieldtrials);
83   webrtc::field_trial::InitFieldTrialsFromString(forced_field_trials.c_str());
84 
85   // Abort if the user specifies a port that is outside the allowed
86   // range [1, 65535].
87   if ((absl::GetFlag(FLAGS_port) < 1) || (absl::GetFlag(FLAGS_port) > 65535)) {
88     printf("Error: %i is not a valid port.\n", absl::GetFlag(FLAGS_port));
89     return -1;
90   }
91 
92   const std::string server = absl::GetFlag(FLAGS_server);
93   GtkMainWnd wnd(server.c_str(), absl::GetFlag(FLAGS_port),
94                  absl::GetFlag(FLAGS_autoconnect),
95                  absl::GetFlag(FLAGS_autocall));
96   wnd.Create();
97 
98   CustomSocketServer socket_server(&wnd);
99   rtc::AutoSocketServerThread thread(&socket_server);
100 
101   rtc::InitializeSSL();
102   // Must be constructed after we set the socketserver.
103   PeerConnectionClient client;
104   auto conductor = rtc::make_ref_counted<Conductor>(&client, &wnd);
105   socket_server.set_client(&client);
106   socket_server.set_conductor(conductor.get());
107 
108   thread.Run();
109 
110   // gtk_main();
111   wnd.Destroy();
112 
113   // TODO(henrike): Run the Gtk main loop to tear down the connection.
114   /*
115   while (gtk_events_pending()) {
116     gtk_main_iteration();
117   }
118   */
119   rtc::CleanupSSL();
120   return 0;
121 }
122