You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.2 KiB

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