main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include <chrono>
  9. #include <iostream>
  10. #include <thread>
  11. #include <rtc/rtc.hpp>
  12. using namespace std;
  13. using namespace chrono_literals;
  14. void test_connectivity(bool signal_wrong_fingerprint);
  15. void test_pem();
  16. void test_negotiated();
  17. void test_reliability();
  18. void test_turn_connectivity();
  19. void test_track();
  20. void test_capi_connectivity();
  21. void test_capi_track();
  22. void test_websocket();
  23. void test_websocketserver();
  24. void test_capi_websocketserver();
  25. size_t benchmark(chrono::milliseconds duration);
  26. void test_benchmark() {
  27. size_t goodput = benchmark(10s);
  28. if (goodput == 0)
  29. throw runtime_error("No data received");
  30. const size_t threshold = 1000; // 1 MB/s;
  31. if (goodput < threshold)
  32. throw runtime_error("Goodput is too low");
  33. }
  34. int main(int argc, char **argv) {
  35. // C++ API tests
  36. try {
  37. cout << endl << "*** Running WebRTC connectivity test..." << endl;
  38. test_connectivity(false);
  39. cout << "*** Finished WebRTC connectivity test" << endl;
  40. } catch (const exception &e) {
  41. cerr << "WebRTC connectivity test failed: " << e.what() << endl;
  42. return -1;
  43. }
  44. try {
  45. cout << endl << "*** Running WebRTC broken fingerprint test..." << endl;
  46. test_connectivity(true);
  47. cerr << "WebRTC connectivity test failed to detect broken fingerprint" << endl;
  48. return -1;
  49. } catch (const exception &) {
  50. }
  51. try {
  52. cout << endl << "*** Running pem test..." << endl;
  53. test_pem();
  54. } catch (const exception &e) {
  55. cerr << "pem test failed: " << e.what() << endl;
  56. return -1;
  57. }
  58. // TODO: Temporarily disabled as the Open Relay TURN server is unreliable
  59. /*
  60. try {
  61. cout << endl << "*** Running WebRTC TURN connectivity test..." << endl;
  62. test_turn_connectivity();
  63. cout << "*** Finished WebRTC TURN connectivity test" << endl;
  64. } catch (const exception &e) {
  65. cerr << "WebRTC TURN connectivity test failed: " << e.what() << endl;
  66. return -1;
  67. }
  68. */
  69. try {
  70. cout << endl << "*** Running WebRTC negotiated DataChannel test..." << endl;
  71. test_negotiated();
  72. cout << "*** Finished WebRTC negotiated DataChannel test" << endl;
  73. } catch (const exception &e) {
  74. cerr << "WebRTC negotiated DataChannel test failed: " << e.what() << endl;
  75. return -1;
  76. }
  77. try {
  78. cout << endl << "*** Running WebRTC reliability mode test..." << endl;
  79. test_reliability();
  80. cout << "*** Finished WebRTC reliaility mode test" << endl;
  81. } catch (const exception &e) {
  82. cerr << "WebRTC reliability test failed: " << e.what() << endl;
  83. return -1;
  84. }
  85. #if RTC_ENABLE_MEDIA
  86. try {
  87. cout << endl << "*** Running WebRTC Track test..." << endl;
  88. test_track();
  89. cout << "*** Finished WebRTC Track test" << endl;
  90. } catch (const exception &e) {
  91. cerr << "WebRTC Track test failed: " << e.what() << endl;
  92. return -1;
  93. }
  94. #endif
  95. #if RTC_ENABLE_WEBSOCKET
  96. // TODO: Temporarily disabled as the echo service is unreliable
  97. /*
  98. try {
  99. cout << endl << "*** Running WebSocket test..." << endl;
  100. test_websocket();
  101. cout << "*** Finished WebSocket test" << endl;
  102. } catch (const exception &e) {
  103. cerr << "WebSocket test failed: " << e.what() << endl;
  104. return -1;
  105. }
  106. */
  107. try {
  108. cout << endl << "*** Running WebSocketServer test..." << endl;
  109. test_websocketserver();
  110. cout << "*** Finished WebSocketServer test" << endl;
  111. } catch (const exception &e) {
  112. cerr << "WebSocketServer test failed: " << e.what() << endl;
  113. return -1;
  114. }
  115. #endif
  116. try {
  117. // Every created object must have been destroyed, otherwise the wait will block
  118. cout << endl << "*** Running cleanup..." << endl;
  119. if(rtc::Cleanup().wait_for(10s) == future_status::timeout)
  120. throw std::runtime_error("Timeout");
  121. cout << "*** Finished cleanup..." << endl;
  122. } catch (const exception &e) {
  123. cerr << "Cleanup failed: " << e.what() << endl;
  124. return -1;
  125. }
  126. // C API tests
  127. try {
  128. cout << endl << "*** Running WebRTC C API connectivity test..." << endl;
  129. test_capi_connectivity();
  130. cout << "*** Finished WebRTC C API connectivity test" << endl;
  131. } catch (const exception &e) {
  132. cerr << "WebRTC C API connectivity test failed: " << e.what() << endl;
  133. return -1;
  134. }
  135. #if RTC_ENABLE_MEDIA
  136. try {
  137. cout << endl << "*** Running WebRTC C API track test..." << endl;
  138. test_capi_track();
  139. cout << "*** Finished WebRTC C API track test" << endl;
  140. } catch (const exception &e) {
  141. cerr << "WebRTC C API track test failed: " << e.what() << endl;
  142. return -1;
  143. }
  144. #endif
  145. #if RTC_ENABLE_WEBSOCKET
  146. try {
  147. cout << endl << "*** Running WebSocketServer C API test..." << endl;
  148. test_capi_websocketserver();
  149. cout << "*** Finished WebSocketServer C API test" << endl;
  150. } catch (const exception &e) {
  151. cerr << "WebSocketServer C API test failed: " << e.what() << endl;
  152. return -1;
  153. }
  154. #endif
  155. try {
  156. cout << endl << "*** Running C API cleanup..." << endl;
  157. rtcCleanup();
  158. cout << "*** Finished C API cleanup..." << endl;
  159. } catch (const exception &e) {
  160. cerr << "C API cleanup failed: " << e.what() << endl;
  161. return -1;
  162. }
  163. /*
  164. // Benchmark
  165. try {
  166. cout << endl << "*** Running WebRTC benchmark..." << endl;
  167. test_benchmark();
  168. cout << "*** Finished WebRTC benchmark" << endl;
  169. } catch (const exception &e) {
  170. cerr << "WebRTC benchmark failed: " << e.what() << endl;
  171. std::this_thread::sleep_for(2s);
  172. return -1;
  173. }
  174. */
  175. return 0;
  176. }